Game Development Reference
In-Depth Information
In the
checkInvaderCollisions()
method, we check whether any of the invaders has collided
with the ship. That's a pretty simple affair, since all we need to do is loop through all invaders
and check for overlap between each invader's bounding sphere and the ship's bounding sphere.
According to our game mechanics definition, the game ends if the ship collides with an invader.
This is why we set the ship's lives to 1 before we call the
Ship.kill()
method. After that call, the
ship's
lives
member is set to 0, which we'll use in another method to check for the game-over
state.
private void
checkShotCollisions() {
int
len = shots.size();
for
(
int
i = 0; i < len; i++) {
Shot shot = shots.get(i);
boolean
shotRemoved =
false
;
int
len2 = shields.size();
for
(
int
j = 0; j < len2; j++) {
Shield shield = shields.get(j);
if
(OverlapTester.
overlapSpheres
(shield.bounds, shot.bounds)) {
shields.remove(j);
shots.remove(i);
i--;
len--;
shotRemoved =
true
;
break
;
}
}
if
(shotRemoved)
continue
;
if
(shot.velocity.z < 0) {
len2 = invaders.size();
for
(
int
j = 0; j < len2; j++) {
Invader invader = invaders.get(j);
if
(OverlapTester.
overlapSpheres
(invader.bounds,
shot.bounds)
&& invader.state == Invader.
INVADER_ALIVE
) {
invader.kill();
listener.explosion();
score += 10;
shots.remove(i);
i--;
len--;
break
;
}
}
}
else
{
if
(OverlapTester.
overlapSpheres
(shot.bounds, ship.bounds)
&& ship.state == Ship.
SHIP_ALIVE
) {
ship.kill();
listener.explosion();