Game Development Reference
In-Depth Information
Adding new functions for enemy tank AI
Two new functions will be added next that attempt to make the enemy tanks appear somewhat
intelligent. We have not added every bit of conceivable AI to the enemy tanks, just enough to
make them a small challenge. We call this “good-enough AI.” Once you see how it works, it can
be modified and extended to make the enemy tanks even smarter.
Chasing the player
We want the enemy tanks to chase the player tank if they are in the same region. We will
accomplish this individually, for each enemy tank, by calculating the number of tiles an enemy
tank is from the player tank. The difference in tiles left or right will be called the
horizontalDiff
.
The difference in tiles up and down will be called the
verticalDiff
. See Figure 7-6 for a
situational example.
Figure 7-6.
Chase example
We will create a function called
chaseObject
that can be reused in other games that employ a tile-
based structure. This function takes two parameters (
TileByTileBlitSptite
instances). The first
object will be the
prey
object and the second will be the
predator
. The
predator
will chase the
prey
if they are in the same region. If they are in the same region, we attempt to create a list of
possible moves for the predator. The list of possible moves will be prioritized using the direction
the
predator
must move to find the
prey
and the distance between the two objects. We check the