Game Development Reference
In-Depth Information
fireCooldown = 2f;
}
7. Finally, if it is still waiting for the weapon to cool down since the last shot, it
should keep waiting, as shown in the following code:
else if(fireCooldown > 0f){
fireCooldown -= tpf;
}
The third and final state for our AI is
RetreatState
. We can implement this by per-
forming the following steps:
1. Like the
PatrolState
, it should have a
moveTarget
field that it tries to
reach.
2. We also add a float called
fleeTimer
that defines for how long it will try to get
away.
3. In its
controlUpdate
method, if
fleeTimer
has not reached
0
yet, and it
still feels a threat, it will pick a location opposite from the target and move to-
wards it, as shown in the following code:
Vector3f worldTranslation =
this.spatial.getWorldTranslation();
if (fleeTimer > 0f && aiControl.getTarget() != null) {
if (moveTarget == null ||
worldTranslation.distance(moveTarget) < 1f) {
moveTarget =
worldTranslation.subtract(aiControl.getTarget().getWorldTranslation());
moveTarget.addLocal(worldTranslation);
}
fleeTimer -= tpf;
Vector3f direction =
moveTarget.subtract(worldTranslation).normalizeLocal();
aiControl.move(direction, true);
}
4. Otherwise, it's all clear, and it will switch to
PatrolState
.