Game Development Reference
In-Depth Information
} else {
for (Saucer* saucer in saucers){
if ([saucer overlapsWith: bullet]){
[saucer decrementHealth:[bullet damage]];
Shield* shield = [Shield shieldProtecting:saucer From:bullet];
[self addActor:shield];
[self removeActor:bullet];
break;
}
}
}
}
for (Powerup* powerup in powerups){
if ([powerup overlapsWith:viper]){
[self playAudio:AUDIO_GOT_POWERUP];
[powerup doHitOn:viper in:self];
}
}
doCollisionDetection
. While iterating over all of
Bullets
, we start by considering the interactions between
Bullets
and the
Vipers
. We do this
source
property on the
Bullet
. If the
source
was a
Saucer
, then the
Bullet
was
Viper
and cannot hurt a
Saucer
. If the
Bullet
overlaps with the
Viper
, we decrement
Viper's
health, add a
Shield
, and remove the
Bullet
from the game.
If the
Bullet
was not shot by a
Saucer
, it must have come from the
Viper
. So we check to see if
the Bullet overlaps with any of the
Saucers
. If it does, we decrement the health of the
Saucer
, add a
Shield
to the
Saucer
, and remove the
Bullet
.
The last relationship we have to consider in
doCollisionDetection
is that between
Powerups
and the
Viper
. We iterate over all of the
Powerups
in the game and check if they overlap. If they do, we call
doHitOn:in:
on the
Powerup
to apply the bonus. The implementation of
doHit:in:
will be described
when we consider the
Powerup
class in detail.
We have now looked at all of the interactions between the different Actors. Before looking at
each of the different Actor classes, let's take a look how we update the score and the health bar
on the screen.
Updating the HUD
For every step of the game, the score or the health meter might change. Collectively, these two
components are called the heads-up display (HUD). Looking back to Listing 12-7, we see that these
two components are connected to the
BeltCommanderController
though an
IBOutlet
. We see that
the
scoreLabel
is a simple
UILabel
, but the class
HealthBarView
is unfamiliar. We will discuss the
class
HealthBarView
in a moment; first, let's look at the code that updates these components in the
task
doUpdateHUD
, as shown in Listing 12-16.