Game Development Reference
In-Depth Information
How to do it...
Let's begin by defining a class called
CoverPoint
, extending
AbstractControl
by
performing the following steps:
1. For now we can add a
Vector3f
called
coverDirection
. With getters and
setters, that's all that's needed.
2. We create a class called
SeekCoverState
, extending our
AIState
class from
the previous recipe.
3. It needs a list of
CoverPoints
called
availableCovers
, and a
Cover-
Point
called
targetCover
.
4. In the
stateEnter
method, it should look for a suitable cover point. We can do
this with the following piece of code. It parses the list and takes the first
Cover-
Point
where the dot product of the direction and
coverDirection
is positive:
for(CoverPoint cover: availableCovers){
if(aiControl.getTarget() != null){
Vector3f directionToTarget =
cover.getSpatial().getWorldTranslation().add(aiControl.getTarget().getWorldTranslation()).normalizeLocal();
if(cover.getCoverDirection().dot(directionToTarget) >
0){
targetCover = cover;
break;
}
}
}
5. In the
controlUpdate
method, the AI should move towards
targetCover
if
it has one.
6. Once it gets close enough,
targetCover
should be set to null, indicating it
should switch to
AttackState
.
7. When this happens,
stateExit
should tell the AI to stop moving.
8. After adding the new state to the AI control class, to let it know it has the ability to
seek cover, we also need to modify other states to enable it.
9. Most suitable is
PatrolState
, where it can switch to
SeekCoverState
in-
stead of
AttackState
when it spots a target.