Game Development Reference
In-Depth Information
Finally, line 73 validates whether the nearest hit object was an Enemy by using its tag (enemy
implementation is considered in the next chapter). Notice from line 79 that damage is applied to the
enemy using a SendMessage function, creating a degree of independence between the weapon and
enemy classes, because the weapon class needs knowledge of the enemy data type or interface
specifics. We just send the enemy object a SendMessage command.
Now go ahead and take this component for a test ride (see Figure 6-17 for the component settings
I've used). You should not have a functional punch weapon. Although this chapter has covered only
the punch weapon so far, the gun weapon can be created using almost the same approach. The
crucial difference between the punch and the gun rests in having finite ammo, the weapon range,
and the damage inflicted.
Figure 6-17. BLAM! The punch weapon in action!
Weapon Changing and Polymorphism
As soon as the Player collects more than one weapon, he'll want the ability to change or cycle
between them on demand. Doing this requires some coding changes both to the weapon
classes and to the PlayerController class, and we'll see an Object Orientation feature, known
as polymorphism , at work. The PlayerController will need to maintain a list of collected weapons
and make only one of them active for the Player at any one time. The weapons too will need to
be notified when they have become the active weapon and when deactivated. Before proceeding
further, however, this section assumes you have both a fists/punch and a gun weapon configured,
with their sprites set up to render in an orthographic camera (see Figure 6-18 for the GameObject
setup that I'm using). Remember, you can always load up the Start or End projects included in the
book files to follow along (found in Chapter06/Start and Chapter06/End ).
 
Search WWH ::




Custom Search