Game Development Reference
In-Depth Information
3. If its location is close enough to the
moveTarget
vector, it should pick a new
one nearby, as shown in the following code:
else if(moveTarget == null ||
this.spatial.getWorldTranslation().distance(moveTarget)
< 1f){
float x = (FastMath.nextRandomFloat() - 0.5f) * 2f;
moveTarget = new Vector3f(x, 0, (1f -
FastMath.abs(x)) - 0.5f).multLocal(5f);
moveTarget.addLocal(this.spatial.getWorldTranslation());
}
4. Otherwise, it should keep moving towards the target, as shown in the following
code:
else {
Vector3f direction =
moveTarget.subtract(this.spatial.getWorldTranslation()).normalizeLocal();
aiControl.move(direction, true);
}
5. Finally, in the
stateExit
method, we should make it stop moving using the
following code:
aiControl.move(Vector3f.ZERO, false);
That's one state out of three; let's look at the
AttackState
. We can implement this by
performing the following steps:
1. The
AttackState
keeps track of values related to firing. It needs to have a
float for
fireDistance
, which is how far the AI can fire; an integer called
clip
, which is how many rounds it has in the current clip; another integer called
ammo
, which defines how many rounds it has in total; and finally, a float called
fireCooldown
, which defines the time between each shot the AI fires.
2. In the
stateEnter
method, we give the AI some ammunition. This is mostly
for testing purposes, as shown in the following code: