Game Development Reference
In-Depth Information
if
(movedDistance > World.
WORLD_MAX_X
) {
move =
MOVE_DOWN
;
movedDistance = 0;
wasLastStateLeft =
false
;
}
}
if
(move ==
MOVE
_
DOWN
) {
position.z += deltaTime *
INVADER
_
VELOCITY
* speedMultiplier;
if
(movedDistance > 1) {
if
(wasLastStateLeft)
move =
MOVE
_
RIGHT
;
else
move =
MOVE
_
LEFT
;
movedDistance = 0;
}
}
bounds.center.set(position);
}
stateTime += deltaTime;
}
The
update()
method takes the current delta time and speed multiplier to make the new waves
of invaders move faster. We perform the movement only if the invader is alive, of course.
We start off by calculating how many units the invader will travel in this update and we increase
the
movedDistance
member accordingly. If it moves to the left, we update the position directly
by subtracting the movement velocity from the x coordinate of the position multiplied by the
delta time and speed multiplier. If it has moved far enough, we tell it to start moving vertically by
setting the
move
member to
MOVE_DOWN
. Also, we set
wasLastStateLeft
to
true
, so that we know
that, after the down movement is finished, the invader has to move to the right.
We do exactly the same for handling movement to the right. The only difference is that we
subtract the movement velocity from the position's x coordinate and set
wasLastStateLeft
to
false
once the movement distance has been reached.
If the invader moves downward, we manipulate the z coordinate of the invader's position and
again check how far it has been moving in that direction. If it reached the movement distance
for downward movement, we switch the movement state to either
MOVE_LEFT
or
MOVE_RIGHT
,
depending on the last horizontal movement direction encoded in the
wasLastStateLeft
member.
Once we are done updating the invader's position, we set the position of the bounding sphere,
as we did for the ship. Finally, we update the current state time and consider the update done.
public void
kill() {
state =
INVADER
_
DEAD
;
stateTime = 0;
}
}
The
kill()
method here serves the same purpose as the
kill()
method for the
Ship
class. It
allows us to tell the invader that it should start dying. We set its state to
INVADER_DEAD
and reset