Game Development Reference
In-Depth Information
Listing 12-11. BeltCommanderController.m (applyGameLogic)
-(void)applyGameLogic{
if ([viper health] <= 0.0f){
[self playAudio:AUDIO_VIPER_EXPLOSION];
[self doEndGame];
} else {
[self doAddNewTrouble];
[self doCollisionDetection];
[self doUpdateHUD];
if ([self stepNumber]%30 == 0){
[self checkAchievements];
}
}
}
In Listing 12-11, we see the task
applyGameLogic
that is called once for every step of the game. The
first thing we do is check to see if the
Viper
's health is below 0.0. If it is, we play the explosion sound
and end the game—simple as that. If the
Viper
is still alive, we continue by calling
doAddNewTrouble
,
doCollisionDetection
, and
doUpdateHUD
. The call to
checkAchievements
is done only once every
30 steps. This is done for performance reasons—we don't really want to make these extra calls for
every step. This does have the drawback that achievements might be reached by the player but not
recorded. For a more sophisticated game, we would designate which achievements are tested when.
We just listed off a good number of tasks, and we will take each in turn, starting with the task
doEndGame
, as shown in Listing 12-12.
Listing 12-12. BeltCommanderController.m (doEndGame)
-(void)doEndGame{
[self setIsPaused:YES];
[delegate gameOver:self];
}
In Listing 12-12, we see the very simple task
doEndGame
where we simply pause the game and call
gameOver:
on the delegate. The task
gameOver:
is implemented by
RootViewController
and can be
seen in Listing 12-5. Next, let's review how
Actors
are added to the game.
Adding Actors
We know from experience that Actors are added by calling
addActor:
. However, to make a game, we
have to include some logic that dictates when and how an Actor is added. Let's continue and see
how we add new
Actors
to the game in the task
doAddNewTrouble
, as shown in Listing 12-13.