Game Development Reference
In-Depth Information
if
(invader.state == Invader.
INVADER_DEAD
&&
invader.stateTime > Invader.
INVADER_EXPLOSION_TIME
) {
invaders.remove(i);
i--;
len--;
}
}
}
The
updateInvaders()
method has a couple of responsibilities. It loops through all invaders and
calls their
update()
method. Once an
Invader
instance is updated, we check whether it is alive.
If it is, we give it a chance to fire a shot by generating a random number. If that number is below
0.001, it fires a shot. This means that each invader has a 0.1% chance of firing a shot each
frame. If that happens, we instantiate a new shot, set its velocity so that it moves in the direction
of the positive z axis, and inform the listener of that event. If the invader is dead and has finished
exploding, we simply remove it from the current list of invaders.
private void
updateShots(
float
deltaTime) {
int
len = shots.size();
for
(
int
i = 0; i < len; i++) {
Shot shot = shots.get(i);
shot.update(deltaTime);
if
(shot.position.z <
WORLD_MIN_Z
||
shot.position.z > 0) {
shots.remove(i);
i--;
len--;
}
}
}
The
updateShots()
method is simple, as well. We loop through all shots, update them, and
check whether each one has left the playing field, in which case we remove it from the shots list.
private void
checkInvaderCollisions() {
if
(ship.state == Ship.
SHIP_EXPLODING
)
return
;
int
len = invaders.size();
for
(
int
i = 0; i < len; i++) {
Invader invader = invaders.get(i);
if
(OverlapTester.
overlapSpheres
(ship.bounds, invader.bounds)) {
ship.lives = 1;
ship.kill();
return
;
}
}
}