Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(gestaltv7-eventsystem): Migration Event and @ReceiveEvent to gestalt's #16

Merged
merged 4 commits into from
Dec 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,30 +1,17 @@
/*
* Copyright 2017 MovingBlocks
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Copyright 2021 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0
package org.terasology.anatomy.AnatomyCirculation;

import org.terasology.anatomy.AnatomyCirculation.component.InjuredCirculatoryComponent;
import org.terasology.anatomy.AnatomyCirculation.event.BloodLevelChangedEvent;
import org.terasology.engine.entitySystem.entity.EntityRef;
import org.terasology.engine.entitySystem.event.ReceiveEvent;
import org.terasology.engine.entitySystem.prefab.Prefab;
import org.terasology.engine.entitySystem.prefab.PrefabManager;
import org.terasology.engine.entitySystem.systems.BaseComponentSystem;
import org.terasology.engine.entitySystem.systems.RegisterSystem;
import org.terasology.engine.logic.health.DestroyEvent;
import org.terasology.engine.registry.In;
import org.terasology.gestalt.entitysystem.event.ReceiveEvent;

@RegisterSystem
public class CirculatoryEffectsSystem extends BaseComponentSystem {
Expand All @@ -33,7 +20,8 @@ public class CirculatoryEffectsSystem extends BaseComponentSystem {
private PrefabManager prefabManager;

@ReceiveEvent
public void onBloodLevelChanged(BloodLevelChangedEvent event, EntityRef entityRef, InjuredCirculatoryComponent injuredCirculatoryComponent) {
public void onBloodLevelChanged(BloodLevelChangedEvent event, EntityRef entityRef,
InjuredCirculatoryComponent injuredCirculatoryComponent) {
if (injuredCirculatoryComponent.bloodLevel <= 0) {
Prefab bloodLossDamage = prefabManager.getPrefab("Anatomy:bloodLoss");
entityRef.send(new DestroyEvent(EntityRef.NULL, EntityRef.NULL, bloodLossDamage));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,5 @@
/*
* Copyright 2017 MovingBlocks
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Copyright 2021 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0
package org.terasology.anatomy.AnatomyCirculation;

import org.terasology.anatomy.AnatomyCirculation.component.InjuredCirculatoryComponent;
Expand All @@ -25,39 +12,36 @@
import org.terasology.engine.core.Time;
import org.terasology.engine.entitySystem.entity.EntityManager;
import org.terasology.engine.entitySystem.entity.EntityRef;
import org.terasology.engine.entitySystem.event.ReceiveEvent;
import org.terasology.engine.entitySystem.systems.BaseComponentSystem;
import org.terasology.engine.entitySystem.systems.RegisterMode;
import org.terasology.engine.entitySystem.systems.RegisterSystem;
import org.terasology.engine.logic.delay.DelayManager;
import org.terasology.engine.logic.delay.DelayedActionTriggeredEvent;
import org.terasology.engine.registry.In;
import org.terasology.gestalt.entitysystem.event.ReceiveEvent;
import org.terasology.math.TeraMath;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RegisterSystem(value = RegisterMode.AUTHORITY)
@RegisterSystem(RegisterMode.AUTHORITY)
public class CirculatoryHealthSystem extends BaseComponentSystem {

private static final String CIRCULATORY_REGEN_PREFIX = "Circulatory:Regen:";
private static final String CIRCULATORY_BLOOD_REGEN_PREFIX = "Circulatory:BloodRegen:";
private static final String CIRCULATORY_CHARACTERISTIC = "blood";
private final Map<Integer, Float> severityBleedingRateMap = new HashMap<>();
private final float bluntDamageMultiplier = 0.5f;
private final float pierceDamageMultiplier = 1.5f;

@In
private Time time;

@In
private EntityManager entityManager;

@In
private DelayManager delayManager;

private Map<Integer, Float> severityBleedingRateMap = new HashMap<>();

private float bluntDamageMultiplier = 0.5f;
private float pierceDamageMultiplier = 1.5f;

private String CIRCULATORY_REGEN_PREFIX = "Circulatory:Regen:";
private String CIRCULATORY_BLOOD_REGEN_PREFIX = "Circulatory:BloodRegen:";
private String CIRCULATORY_CHARACTERISTIC = "blood";

@Override
public void initialise() {
severityBleedingRateMap.put(1, -0.5f);
Expand All @@ -66,23 +50,30 @@ public void initialise() {
}

@ReceiveEvent
public void onBloodLevelRegen(DelayedActionTriggeredEvent event, EntityRef entityRef, InjuredCirculatoryComponent injuredCirculatoryComponent) {
public void onBloodLevelRegen(DelayedActionTriggeredEvent event, EntityRef entityRef,
InjuredCirculatoryComponent injuredCirculatoryComponent) {
if (event.getActionId().startsWith(CIRCULATORY_BLOOD_REGEN_PREFIX)) {
if (injuredCirculatoryComponent.bloodLevel >= 0 && injuredCirculatoryComponent.bloodLevel <= injuredCirculatoryComponent.maxBloodLevel && injuredCirculatoryComponent.bloodRegenRate != 0) {
if (injuredCirculatoryComponent.bloodLevel >= 0
&& injuredCirculatoryComponent.bloodLevel <= injuredCirculatoryComponent.maxBloodLevel
&& injuredCirculatoryComponent.bloodRegenRate != 0) {
int healAmount = 0;
healAmount += injuredCirculatoryComponent.bloodRegenRate / TeraMath.fastAbs(injuredCirculatoryComponent.bloodRegenRate);
injuredCirculatoryComponent.nextRegenTick = time.getGameTimeInMs() + (long) (1000 / TeraMath.fastAbs(injuredCirculatoryComponent.bloodRegenRate));
injuredCirculatoryComponent.nextRegenTick =
time.getGameTimeInMs() + (long) (1000 / TeraMath.fastAbs(injuredCirculatoryComponent.bloodRegenRate));
injuredCirculatoryComponent.bloodLevel += healAmount;
injuredCirculatoryComponent.bloodLevel = TeraMath.clamp(injuredCirculatoryComponent.bloodLevel, 0, injuredCirculatoryComponent.maxBloodLevel);
injuredCirculatoryComponent.bloodLevel = TeraMath.clamp(injuredCirculatoryComponent.bloodLevel, 0,
injuredCirculatoryComponent.maxBloodLevel);
entityRef.saveComponent(injuredCirculatoryComponent);
entityRef.send(new BloodLevelChangedEvent());
}
delayManager.addDelayedAction(entityRef, CIRCULATORY_BLOOD_REGEN_PREFIX, (long) (1000 / TeraMath.fastAbs(injuredCirculatoryComponent.bloodRegenRate)));
delayManager.addDelayedAction(entityRef, CIRCULATORY_BLOOD_REGEN_PREFIX,
(long) (1000 / TeraMath.fastAbs(injuredCirculatoryComponent.bloodRegenRate)));
}
}

@ReceiveEvent
public void onPartHealthRegen(DelayedActionTriggeredEvent event, EntityRef entityRef, InjuredCirculatoryComponent injuredCirculatoryComponent) {
public void onPartHealthRegen(DelayedActionTriggeredEvent event, EntityRef entityRef,
InjuredCirculatoryComponent injuredCirculatoryComponent) {
if (event.getActionId().startsWith(CIRCULATORY_REGEN_PREFIX)) {
String partID = event.getActionId().substring(CIRCULATORY_REGEN_PREFIX.length());
PartHealthDetails partDetails = injuredCirculatoryComponent.partHealths.get(partID);
Expand All @@ -99,7 +90,8 @@ public void onPartHealthRegen(DelayedActionTriggeredEvent event, EntityRef entit
}

@ReceiveEvent
public void onBleedingRateChanged(PartCirculatoryEffectChangedEvent event, EntityRef entityRef, InjuredCirculatoryComponent injuredCirculatoryComponent) {
public void onBleedingRateChanged(PartCirculatoryEffectChangedEvent event, EntityRef entityRef,
InjuredCirculatoryComponent injuredCirculatoryComponent) {
float bloodRegenRate = injuredCirculatoryComponent.baseBloodRegenRate;
for (Map.Entry<String, List<String>> severityPartsEntry : injuredCirculatoryComponent.parts.entrySet()) {
bloodRegenRate += severityPartsEntry.getValue().size() * severityBleedingRateMap.get(Integer.parseInt(severityPartsEntry.getKey()));
Expand All @@ -121,8 +113,9 @@ public void onCirculatoryDamage(AnatomyPartImpactedEvent event, EntityRef entity
partHealthDetails = new PartHealthDetails();
injuredCirculatoryComponent.partHealths.put(event.getTargetPart().id, partHealthDetails);
// Part has been injured for the first time, so add delayed part health regen event and blood level regen event.
delayManager.addDelayedAction(entityRef, CIRCULATORY_REGEN_PREFIX + event.getTargetPart().id, (long) (1000 / partHealthDetails.regenRate));
delayManager.addDelayedAction(entityRef, CIRCULATORY_BLOOD_REGEN_PREFIX, (long) 1000);
delayManager.addDelayedAction(entityRef, CIRCULATORY_REGEN_PREFIX + event.getTargetPart().id,
(long) (1000 / partHealthDetails.regenRate));
delayManager.addDelayedAction(entityRef, CIRCULATORY_BLOOD_REGEN_PREFIX, 1000);
}
int damageAmount = event.getAmount();
if (event.getDamageType().getName().equals("Equipment:pierceDamage")) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,5 @@
/*
* Copyright 2017 MovingBlocks
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Copyright 2021 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0
package org.terasology.anatomy.AnatomyCirculation;

import com.google.common.collect.Lists;
Expand All @@ -23,25 +10,26 @@
import org.terasology.anatomy.component.PartHealthDetails;
import org.terasology.anatomy.event.AnatomyStatusGatheringEvent;
import org.terasology.engine.entitySystem.entity.EntityRef;
import org.terasology.engine.entitySystem.event.ReceiveEvent;
import org.terasology.engine.entitySystem.systems.BaseComponentSystem;
import org.terasology.engine.entitySystem.systems.RegisterSystem;
import org.terasology.engine.logic.console.commandSystem.annotations.Command;
import org.terasology.engine.logic.console.commandSystem.annotations.Sender;
import org.terasology.engine.logic.players.event.OnPlayerRespawnedEvent;
import org.terasology.engine.network.ClientComponent;
import org.terasology.gestalt.entitysystem.event.ReceiveEvent;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RegisterSystem
public class CirculatorySystem extends BaseComponentSystem {
private Map<Integer, String> severityNameMap = new HashMap<>();

private float MINOR_BLEEDING_THRESHOLD = 0.8f;
private float BLEEDING_THRESHOLD = 0.5f;
private float SEVERE_BLEEDING_THRESHOLD = 0.2f;
private static final float MINOR_BLEEDING_THRESHOLD = 0.8f;
private static final float BLEEDING_THRESHOLD = 0.5f;
private static final float SEVERE_BLEEDING_THRESHOLD = 0.2f;

private final Map<Integer, String> severityNameMap = new HashMap<>();

@Override
public void initialise() {
Expand All @@ -51,7 +39,8 @@ public void initialise() {
}

@ReceiveEvent
public void onPartCirculatoryHealthChanged(PartCirculatoryHealthChangedEvent event, EntityRef entityRef, AnatomyComponent anatomyComponent, InjuredCirculatoryComponent injuredCirculatoryComponent) {
public void onPartCirculatoryHealthChanged(PartCirculatoryHealthChangedEvent event, EntityRef entityRef,
AnatomyComponent anatomyComponent, InjuredCirculatoryComponent injuredCirculatoryComponent) {
int severity = getEffectSeverity(event.partId, injuredCirculatoryComponent);
if (severity == 0) {
removeEffect(entityRef, event.partId);
Expand Down Expand Up @@ -121,7 +110,8 @@ private int getEffectSeverity(String partId, InjuredCirculatoryComponent injured
}

@ReceiveEvent
public void onPlayerRespawn(OnPlayerRespawnedEvent event, EntityRef entityRef, InjuredCirculatoryComponent injuredCirculatoryComponent) {
public void onPlayerRespawn(OnPlayerRespawnedEvent event, EntityRef entityRef,
InjuredCirculatoryComponent injuredCirculatoryComponent) {
entityRef.removeComponent(InjuredCirculatoryComponent.class);
}

Expand All @@ -132,10 +122,16 @@ public String showCirculatoryHealths(@Sender EntityRef client) {
String result = "";
if (injuredCirculatoryComponent != null) {
result += "Blood level : ";
result += injuredCirculatoryComponent.bloodLevel + "/" + injuredCirculatoryComponent.maxBloodLevel + " Blood regen rate: " + injuredCirculatoryComponent.bloodRegenRate + "\n";
result += injuredCirculatoryComponent.bloodLevel + "/" +
injuredCirculatoryComponent.maxBloodLevel +
" Blood regen rate: " +
injuredCirculatoryComponent.bloodRegenRate + "\n";
result += "Circulatory system healths :\n";
for (Map.Entry<String, PartHealthDetails> partHealthDetailsEntry : injuredCirculatoryComponent.partHealths.entrySet()) {
result += partHealthDetailsEntry.getKey() + " :" + partHealthDetailsEntry.getValue().health + "/" + partHealthDetailsEntry.getValue().maxHealth + "\n";
result +=
partHealthDetailsEntry.getKey()
+ " :" + partHealthDetailsEntry.getValue().health
+ "/" + partHealthDetailsEntry.getValue().maxHealth + "\n";
}
} else {
result += "Circulatory system healthy.\n";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,8 @@
/*
* Copyright 2017 MovingBlocks
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Copyright 2021 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0
package org.terasology.anatomy.AnatomyCirculation.event;

import org.terasology.engine.entitySystem.event.Event;
import org.terasology.gestalt.entitysystem.event.Event;

public class BloodLevelChangedEvent implements Event {
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,8 @@
/*
* Copyright 2017 MovingBlocks
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Copyright 2021 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0
package org.terasology.anatomy.AnatomyCirculation.event;

import org.terasology.engine.entitySystem.event.Event;
import org.terasology.gestalt.entitysystem.event.Event;

public class PartCirculatoryEffectChangedEvent implements Event {
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,8 @@
/*
* Copyright 2017 MovingBlocks
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Copyright 2021 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0
package org.terasology.anatomy.AnatomyCirculation.event;

import org.terasology.engine.entitySystem.event.Event;
import org.terasology.gestalt.entitysystem.event.Event;

public class PartCirculatoryHealthChangedEvent implements Event {
public String partId;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,16 @@
/*
* Copyright 2017 MovingBlocks
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Copyright 2021 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0
package org.terasology.anatomy;

import org.terasology.anatomy.event.AnatomyScreenButtonEvent;
import org.terasology.engine.entitySystem.entity.EntityRef;
import org.terasology.engine.entitySystem.event.ReceiveEvent;
import org.terasology.engine.entitySystem.systems.BaseComponentSystem;
import org.terasology.engine.entitySystem.systems.RegisterMode;
import org.terasology.engine.entitySystem.systems.RegisterSystem;
import org.terasology.engine.network.ClientComponent;
import org.terasology.engine.registry.In;
import org.terasology.engine.rendering.nui.NUIManager;
import org.terasology.gestalt.entitysystem.event.ReceiveEvent;
import org.terasology.input.ButtonState;

/**
Expand Down
Loading