Game Development Reference
In-Depth Information
moveFound = true;
}
}
if (move==MOVE_RIGHT && predator.inTunnel==false) {
if (checkTile(MOVE_RIGHT, predator )) {
switchMovement(MOVE_RIGHT, predator );
moveFound = true;
}
}
if (move==MOVE_LEFT && predator.inTunnel==false) {
if (checkTile(MOVE_LEFT,predator )) {
switchMovement(MOVE_LEFT, predator );
moveFound = true;
}
}
if (move==MOVE_STOP && predator.inTunnel==false) {
switchMovement(MOVE_STOP, predator );
moveFound = true;
}
movePtr++;
if (movePtr == moveDirectionsToTest.length) {
switchMovement(MOVE_STOP, predator );
moveFound = true;
}
}
}
}
These are main points in the
chaseObject
function:
1.
To begin, this function takes two parameters, both
TileByTileBlitSprites
. The first is
the
prey
, and the second is the
predator
. The
predator
will chase the
prey
if they are
in the same region.
2.
The first section of the function searches for directions to fill up the
moveDirectionsToTest
array. This array of moves will be used in the second part of
the function to find a direction to move in that is not blocked by a wall.
3.
The
horizontalDiff
and
verticalDiff
local variables calculate the horizontal and
vertical difference (in tiles) between the predator and the prey.
4.
The two sets of
if
statements compare the absolute value (to compensate for
negative values) of
verticalDiff
and
horizontalDiff
values, so we can determine in
which direction the
predator
is closer to the
prey
. All we care about is which
(
verticalDiff
or
horizontalDiff
) is a lower number.