Game Development Reference
In-Depth Information
The script handles the number of lives in a similar way to that
PlayerHealth
uses to handle
the health: we have an initial value that is enforced to be at least 1 at the beginning, and the
internal value can be later altered through
GiveLife()
and
TakeLife()
functions. Notice that
TakeLife()
can send two messages when called: if the player still has more lives it sends
OnLifeLost
message. However, if the last life has just been lost,
OnAllLivesLost
message is
sent. We need now to a mechanism to call
TakeLife()
when the player's health reaches zero or
less. In other words,
OnPlayerDeath
message needs to be forwarded as
TakeLife
message.
This mechanism is fairly simple and can be achieved through
PlayerDeathReporter
script
shown in Listing 78.
Listing 78:
A script to report player's death event to lives manager in order to take a life from player
As you see, the script is fairly simple and self explanatory. Remember that we have created
LivesManager
script to handle the event of player's death by reducing a life. However, we
still need to handle the case when all lives are lost. Up to now, nothing really happens when