Game Development Reference
In-Depth Information
Listing 7-1. Line 07
. The base
Enemy
class defines a
Health
variable, common
to all enemies. Using this, derived classes may keep track of Enemy health.
Listing 7-2. Line 08
. The
Enemy_Drone
class implements a
Damage
event, which
will be called every time the Player strikes the Enemy with a weapon: either a
punch with fists or a gun.
Listing 7-2. Lines 11-23
. Here, the Enemy's health is reduced by the weapon
damage amount, specified by the function argument
Damage
. Further, if Enemy
health is reduced to 0 or below, the
Enemy
object is destroyed using the
DestroyImmediate
function, and the NotificationsManager is updated to remove
all redundant listeners.
Let's give this code a test run to confirm it works as intended. Run the game in Editor, selecting the
Drone
object in the scene. As you attack the Enemy with your weapon, observe his
Health
variable
in the Object Inspector. For each successful strike you make, the Enemy's health will be reduced by
the damage appropriate for your weapon. The gun weapon deals the most damage. Congratulations!
We've now established an important connection between objects; specifically, the Player, Weapons,
Enemies, and the NotificationsManager (see Figure
7-9
).
Figure 7-9.
Damaging enemies with weapons!
Improving Damage Dealing: Feedback
Right now the Player deals damage to the Enemy, but we (as developers) only have the live-preview
of variable values in the Object Inspector to confirm that this behavior is truly working as intended
at runtime. There's currently no visual or graphical indication
for the gamer
that damage has been
sustained by the Enemy. Some might not regard this as a very big deal—after all, it works, and
isn't that enough? However, this dismissal might be premature. Many academic studies have been