Game Development Reference
In-Depth Information
Figure 12-11. Movement of the invaders: left, down, right, down, left, down, right, down . . .
An invader follows an extremely simplistic movement pattern. From its initial position, it first
moves to the right for some distance. Next, it moves downward (which means in the direction of
the positive z axis on the playing field), again for a specified distance. Once it is done with that, it
starts moving to the right, basically backtracking to the same x coordinate where it was before it
started moving left.
The left and right movement distances are always the same, except in the beginning.
Figure 12-11 illustrates the movement of the top-left invader. Its first left movement is shorter
than all subsequent movements to the left or right. The horizontal movement distance is half
the playing field width, 14 units in this case. For the first horizontal movement, the distance an
invader has to travel is half this, or 7 units.
What we have to do is keep track of the direction in which an invader is moving, and how far it
has already moved in that direction. If it reaches the movement distance for the given movement
state (14 units for horizontal movement, 1 unit for vertical movement), it switches to the next
movement state. All invaders will initially have their movement distance set to half the playing
field's width. Look again at Figure 12-11 to see why that works. This will make the invaders
bounce off the edges of the playing field to the left and right.
Invaders also have a constant velocity. Well, the velocity will actually increase each time we
generate a new wave of invaders if all the invaders from the current wave are dead. We can
achieve this simply by multiplying this default velocity by some constant that is set from outside,
namely the World class responsible for updating all invaders.
Finally, we have to keep track of the state of the invader, which can be alive or exploding. We'll
use the same mechanism as in the case of the ship, with a state and a state time. Listing 12-9
shows the code.
Search WWH ::




Custom Search