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

fix: fleeing entities must have a CharacterMovement component #85

Merged
merged 2 commits into from
Dec 2, 2021
Merged
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
Expand Up @@ -13,9 +13,8 @@
import org.terasology.engine.entitySystem.systems.RegisterMode;
import org.terasology.engine.entitySystem.systems.RegisterSystem;
import org.terasology.engine.logic.characters.CharacterMovementComponent;
import org.terasology.module.health.events.OnDamagedEvent;
import org.terasology.engine.registry.In;

import org.terasology.module.health.events.OnDamagedEvent;

/*
* Listens for events relevant to this module's animals and responds as necessary
Expand All @@ -26,20 +25,31 @@ public class BehaviorsEventSystem extends BaseComponentSystem {
@In
private Time time;

@ReceiveEvent(components = FleeOnHitComponent.class)
public void onDamage(OnDamagedEvent event, EntityRef entity) {
/**
* Make an entity with the a {@link FleeOnHitComponent} flee from the instigator when being damaged.
*
* @param event the {@link OnDamageEvent} notifying about the entity having taken damage
* @param entity the entity being damaged
* @param fleeOnHitComponent only entities with this component flee if they take damage
* @param characterMovementComponent a fleeing entity needs a character movement component to allow it to actually flee from the
* attacker
*/
@ReceiveEvent
public void onDamage(OnDamagedEvent event, EntityRef entity,
FleeOnHitComponent fleeOnHitComponent,
CharacterMovementComponent characterMovementComponent) {

// Make entity flee
FleeingComponent fleeingComponent = new FleeingComponent();
fleeingComponent.instigator = event.getInstigator();
fleeingComponent.minDistance = entity.getComponent(FleeOnHitComponent.class).minDistance;
entity.addOrSaveComponent(fleeingComponent);
// Make entity flee (update existing FleeingComponent or add fresh component if not present)
entity.upsertComponent(FleeingComponent.class, maybeFleeingComponent -> {
FleeingComponent fleeingComponent = maybeFleeingComponent.orElse(new FleeingComponent());
fleeingComponent.instigator = event.getInstigator();
fleeingComponent.minDistance = fleeOnHitComponent.minDistance;
return fleeingComponent;
});

// Increase speed by multiplier factor
CharacterMovementComponent characterMovementComponent = entity.getComponent(CharacterMovementComponent.class);
characterMovementComponent.speedMultiplier = entity.getComponent(FleeOnHitComponent.class).speedMultiplier;
entity.addOrSaveComponent(characterMovementComponent);

characterMovementComponent.speedMultiplier = fleeOnHitComponent.speedMultiplier;
entity.saveComponent(characterMovementComponent);
}

/**
Expand Down