Game Development Reference
In-Depth Information
5.
The first set of
if
statements is for the vertical direction (to determine if the
verticalDiff
is less than the
horizontalDiff
). If
verticalDiff
is less than 0, the
prey
is above (up from) from the
predator
. If the
verticalDiff
is greater than 0, the
prey
is
below (down from) the
predator
.
If up is the shortest direction, we push
MOVE_UP
and then
MOVE_DOWN
for the up direction. If
down is the shortest direction, we push
MOVE_DOWN
then
MOVE_UP
for the down direction.
Why do we do this? This is where the “good enough AI” comes in. We want to give the
predator a series of moves to test. The goal is to have the enemy tanks find at least
one valid move (or stop) on each frame tick that they are in the center of a tile. We
know that we really want the
predator
to move up, but if a wall blocks that direction,
we want it to move down.
So far, if the
verticalDiff
is smaller than the
horizontalDiff
, we will want to move in a
vertical direction. If the
verticalDiff
is greater than 0, we want the predator to move up.
If the predator cannot move up, we will have it try to move down as the next best choice.
6.
The next task the code does is look to see what horizontal move to add to the
moveDirectionsToTest
array. The first two directions will already be
MOVE_UP
and
MOVE_DOWN
(not necessarily in that order), so to fill out the
moveDirectionsToTest
array,
we look to see if
horizontalDiff
is greater or less than 0.
MOVE_LEFT
and
MOVE_RIGHT
are added based on this test.
7.
The second set of
if
statements tests if
horizontalDiff
is less than
verticalDiff
.
This occurs when the
predator
is closer to the
prey
in the horizontal direction rather
than the vertical direction. These work exactly the same when the
verticalDiff
is less
than the
horizontalDiff
, but prioritize horizontal movement over vertical movement.
8.
If
horizontalDiff
and
verticalDiff
both equal 0 (), the
predator
is on top of the
prey
, and the
predator
doesn't move at all.
9.
If the absolute value of
horizontalDiff
equals the absolute value if
verticalDiff
, we
simply choose a direction based on a random value and take an educated guess. The
random value chooses between vertical or horizontal. Within those random values, the
code checks the value of the
verticalDiff
or
horizontalDiff
(depending on the
random value) and chooses to move toward the player as the first move is possible.
10.
MOVE_STOP
is always added to the end of the
moveDirectionsToTest
array.
11.
The final portion of the code uses a
while
loop to look for a valid move for the
predator. If no move can be found, the predator will remain stopped.
The AI is somewhat limited but is enough for a small challenge to the player. It can be extended to
not allow an enemy tank to move in the opposite direction from which it came (unless there are no
other possible moves). This would prevent some of the back and forth pacing style movement of the
tanks. We will not cover that in this chapter, but if now that you know how the movement list works,
you can add that logic in if you need it. There are some very good path finding algorithms such as
A* that can be used to find the shortest path between a predator and prey also.