-
Notifications
You must be signed in to change notification settings - Fork 7
Attack Patterns
The AttackPatternComponent
is responsible for managing the attack patterns of entities within the game. It allows entities to initiate attacks with a specified frequency and provides methods for starting and scheduling attacks.
It is recommended to use AI tasks for movement and only add attacking behaviour in this class.
- Set attack frequency (seconds) in constructor
-
startAttack
- start attack loop if not started already, triggered when entity detected in InteractionDetector -
attack
- Attacks (no action by default) and schedules next attack.
The code below outlines how to make Oxygen Eater shoot projectiles at player. The OxygenEaterAttackPattern
inherits from AttackPatternComponent
.
In attack function, get nearest entity to oxygen eater and shoot at that direction, call super.attack()
to schedule next attack.
@Override
protected void attack() {
Entity nearestEntity = interactionDetector.getNearest(interactionDetector.getEntitiesInRange());
if (nearestEntity == null) { // No entity detected, clear attack loop
currentAttackEvent = null;
return;
}
Vector2 nearestEntityPosition = nearestEntity.getCenterPosition();
String attackDirection =
entity.getCenterPosition().sub(nearestEntityPosition).x < 0 ? DirectionUtils.RIGHT : DirectionUtils.LEFT;
// Trigger events for direction change and attack animation
entity.getEvents().trigger("directionChange", attackDirection);
entity.getEvents().trigger("attackStart");
// Shoot projectile with delay after entity's attack animation
entity.getEvents().scheduleEvent(0.2f, "shoot", nearestEntityPosition);
super.attack();
}
The diagram below outlines how this component is used in the game. This structure allows for each hostile entity to have different attack patterns, including melee attacks, and shooting projectiles in customisable ways.