Game Development Reference
In-Depth Information
How it works...
The
CoverPoint
class we created adds the behavior to any
Spatial
instances to act as
a cover. In a game, you would most likely not see the
CoverPoint
spatial, but it's good
for debug and editing purposes. The concept can be expanded to cover other types of in-
terest points for AI, as well as modified to handle volumes, rather than points using the spa-
tial's geometry.
Once the
SeekCoverState
is enabled, it will try to find a suitable cover point that's rel-
ative to the target's position (at that time). It does this using the dot product between
cov-
erDirection
and the direction to the target. If this is positive, it means the target is in
front of the cover, and it picks this as
targetCover
.
When the AI reaches this, it sets
targetCover
to
null
. This means that when
con-
trolUpdate
is called the next time, it will exit the state and enable
AttackState
in-
stead. In a real game, the AI would most likely use some kind of navigation or pathfinding
to get there. You can get an introduction to navigation in the next recipe. There is also the
Pathfinding: Our own A* pathfinder
recipe that covers implementing pathfinding later in
the chapter.
With the current implementation of the AI, the result might be a bit erratic, since it doesn't
remember the target's position. It might very well be that it doesn't see the target once it
reaches the cover and instantly switches to
PatrolState
.