Enemy AI - UQdeco2800/2022-studio-3 GitHub Wiki
Introduction
This page is creating for EnemyAI. EnemyAI sets the movement and attack mode of the enemy, for example, the enemy can attack other units remotely within a certain distance
- updown and leftright tasks
- change the entity speed
- Ranged Attack Task
Usage
1. Creating updown and leftright tasks, These tasks can move the entity horizontally or up and down.
public void update() {
float position_x = this.owner.getEntity().getPosition().x;
if (isMove) {
if(position_x <= p_x + x/2) {
this.owner.getEntity().getComponent(PhysicsComponent.class).getBody().setLinearVelocity(new Vector2(1,0));
} else {
isMove = false;
}
} else {
if(position_x >= p_x - x/2) {
this.owner.getEntity().getComponent(PhysicsComponent.class).getBody().setLinearVelocity(new Vector2(-1,0));
} else {
isMove = true;
}
}
}
PhysicsMovementComponent.java
file.
2. Change the movement speed of the entity by adding a method to the public PhysicsMovementComponent(Vector2 speed) {
maxSpeed = speed;
}
3. Creating rangedAttack task, this task can control the attack interval, distance, and target of the entity.
This function controls the attack interval.
public void update() {
if (timeSource.getTime() >= endtime) {
this.owner.getEntity().getEvents().trigger("attack");
endtime = timeSource.getTime() + (int)(waitTime * 1000);
}
}
This function controls the distance of the entity.
private float DistanceToTarget() {
return owner.getEntity().getPosition().dst(target.getPosition());
}