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

Update to depend on Health module #15

Merged
merged 1 commit into from
Aug 12, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
41 changes: 20 additions & 21 deletions src/main/java/org/terasology/hunger/HungerAuthoritySystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import org.terasology.engine.Time;
import org.terasology.entitySystem.entity.EntityManager;
import org.terasology.entitySystem.entity.EntityRef;
import org.terasology.entitySystem.entity.lifecycleEvents.BeforeDeactivateComponent;
import org.terasology.entitySystem.event.EventPriority;
import org.terasology.entitySystem.event.ReceiveEvent;
import org.terasology.entitySystem.prefab.Prefab;
Expand All @@ -33,23 +32,21 @@
import org.terasology.hunger.event.FoodConsumedEvent;
import org.terasology.logic.characters.AliveCharacterComponent;
import org.terasology.logic.common.ActivateEvent;
import org.terasology.logic.console.commandSystem.annotations.Command;
import org.terasology.logic.console.commandSystem.annotations.CommandParam;
import org.terasology.logic.console.commandSystem.annotations.Sender;
import org.terasology.logic.delay.DelayManager;
import org.terasology.logic.delay.PeriodicActionTriggeredEvent;
import org.terasology.logic.health.BeforeHealEvent;
import org.terasology.logic.health.DoDamageEvent;
import org.terasology.logic.health.event.ActivateRegenEvent;
import org.terasology.logic.health.event.DeactivateRegenEvent;
import org.terasology.logic.health.event.DoDamageEvent;
import org.terasology.logic.inventory.InventoryManager;
import org.terasology.logic.inventory.InventoryUtils;
import org.terasology.logic.inventory.ItemComponent;
import org.terasology.logic.permission.PermissionManager;
import org.terasology.logic.players.event.OnPlayerRespawnedEvent;
import org.terasology.logic.players.event.OnPlayerSpawnedEvent;
import org.terasology.network.ClientComponent;
import org.terasology.registry.In;
import org.terasology.world.WorldComponent;

import static org.terasology.logic.health.RegenAuthoritySystem.BASE_REGEN;

/**
* The authority system monitoring player hunger levels, related events and commands.
*/
Expand Down Expand Up @@ -92,19 +89,18 @@ public class HungerAuthoritySystem extends BaseComponentSystem {
/**
* The interval (in milliseconds) at which healthDecreaseAmount (above) is applied to the component.
*/
public int healthDecreaseInterval = 30000;
public int healthDecreaseInterval = 3000;

public static final String HUNGER_DAMAGE_ACTION_ID = "Hunger Damage";
private boolean destroyDrink = false;

public void postBegin(){
public void postBegin() {
boolean processedOnce = false;
for(EntityRef entity : entityManager.getEntitiesWith(WorldComponent.class)) {
for (EntityRef entity : entityManager.getEntitiesWith(WorldComponent.class)) {
if (!processedOnce) {
delayManager.addPeriodicAction(entity, HUNGER_DAMAGE_ACTION_ID, 0, healthDecreaseInterval);
processedOnce = true;
}
else{
} else {
logger.warn("More than one entity with WorldComponent found");
}
}
Expand All @@ -114,16 +110,20 @@ public void postBegin(){
* Deals a unit of hunger damage to the character.
*/
@ReceiveEvent
public void onPeriodicActionTriggered(PeriodicActionTriggeredEvent event, EntityRef entity_unused) {
public void onPeriodicActionTriggered(PeriodicActionTriggeredEvent event, EntityRef entityUnused) {
if (event.getActionId().equals(HUNGER_DAMAGE_ACTION_ID)) {
for (EntityRef entity : entityManager.getEntitiesWith(HungerComponent.class, AliveCharacterComponent.class)) {
HungerComponent hunger = entity.getComponent(HungerComponent.class);
hunger.lastCalculatedFood = Math.max(0,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not necessary, as the HungerWindow.java uses method based on time calculation.
I don't know which is better in terms of performance.

hunger.lastCalculatedFood - (healthDecreaseInterval * hunger.foodDecayPerSecond) / 1000);
hunger.lastCalculationTime = time.getGameTimeInMs();
entity.saveComponent(hunger);

// Check to see if health should be decreased
if (HungerUtils.getHungerForEntity(entity) < hunger.healthLossThreshold) {
Prefab starvationDamagePrefab = prefabManager.getPrefab("hunger:starvationDamage");
entity.send(new DoDamageEvent(hunger.healthDecreaseAmount, starvationDamagePrefab));
entity.saveComponent(hunger);
Prefab starvationDamagePrefab = prefabManager.getPrefab("hunger:starvationDamage");
entity.send(new DoDamageEvent(hunger.healthDecreaseAmount, starvationDamagePrefab));
entity.saveComponent(hunger);
}
}
}
Expand All @@ -137,11 +137,10 @@ public void onPeriodicActionTriggered(PeriodicActionTriggeredEvent event, Entity
* @param hunger The HungerComponent object, containing settings for Hunger.
*/
@ReceiveEvent
public void onHealthRegen(BeforeHealEvent event, EntityRef entity,
public void onHealthRegen(ActivateRegenEvent event, EntityRef entity,
HungerComponent hunger) {
if (event.getInstigator() == entity
&& HungerUtils.getHungerForEntity(entity) < hunger.healthStopRegenThreshold) {
event.consume();
if (HungerUtils.getHungerForEntity(entity) < hunger.healthStopRegenThreshold && event.id.equals(BASE_REGEN)) {
entity.send(new DeactivateRegenEvent());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public class HungerComponent implements Component {
* the entity to lose health.
*/
@Replicate
public float healthLossThreshold = 1;
public float healthLossThreshold = 15;

/**
* The entity will stop regenerating health if their food capacity is < this threshold.
Expand Down