HTML and CSS Reference
On each iteration, the player.currentTile is incremented by 1 . This will change the
tile that is rendered to be the next tile in the playerTiles array. When destinationX and
destinationY are equal to the x and y values for the player, the movement and animation
stop, and the game state is changed to the GAME_STATE_EVALUATE_PLAYER_MOVE state.
Now that the player has been moved to the next tile, the player.row and player.col
attributes are set to player.nextRow and player.nextCol , respectively.
Next, if the player is on a goal tile, the player.win attribute will be set to true . If the
player is on a wall tile, the player.hit will be set to true .
We then loop though all of the enemy objects and see whether any occupy the same tile
as the player. If they do, both the player and the enemy hit attributes are set to true .
Next, we move the game to the GAME_STATE_ENEMY_MOVE state.
This state uses the homegrown chase AI—discussed in “Simple Homegrown AI Over-
view” on page 515 —to choose a direction in which to move each enemy tank. It does
this by looping through all the tanks and applying the logic to them individually.
This function first uses a little tile-based math to determine where the player is in re-
lation to an enemy tank. It then creates an array of directions to test based on these
calculations. It stores these as string values in a variable called directionsToTest .
Next, it uses the chanceRandomMovement value ( 25% ) to determine whether it will use the
list of directions it just compiled, or whether it will throw them out and simply choose
a random direction to move in.
In either case, it must check all of the available directions (either in the list of
directionsToMove or in all four directions for random movement) to see which is the
first that will not move the tank off the side of the screen.
Once it has the direction to move in, it sets the destinationX and destinationY values
of the enemy tank using the same tile size * x and tile size * y trick used for the
Finally, it sets the game state to GAME_STATE_ANIMATE_ENEMY .
Like GAME_STATE_ANIMATE_PLAYER , this state moves and animates the tank to its new
location represented by its destinationX and destinationY values. It must do this for
each of the enemy tanks, so it uses the enemyMoveCompleteCount variable to keep count
of how many of the enemy tanks have finished their moves.