Game Development Reference
In-Depth Information
Note
More information on the ultimate API ancestor class, Object, can be found online in the Unity
Lines 14, 17, 20, and 24.
These public class variables define
Damage
,
Range
,
Ammo
, and
RecoveryDelay
properties. Every weapon deals
damage
to an Enemy
within its
range
, and can be used only so long as there is sufficient
ammo
remaining. Once used, however, there is a short
recovery/delay
time (measured
in seconds) during which the Player cannot fire again. He must instead wait for
the recovery period to expire before a second attack may be made. This is to
simulate real-world recovery times when using weapons.
Line 27.
This is a Boolean determining whether the weapon has been collected by
the Player. For all weapons except fists/punch, this value should begin as
false
.
Line 30.
This Boolean specifies whether a collected weapon is currently active
and being used by the Player right now. Consequently, only one weapon may
have this flag set to true at any one time.
Line 33.
CanFire
is a Boolean describing whether the collected and equipped
weapon can be fired right now. If this is
false
, then it's because the weapon
RecoveryDelay
has not yet expired.
So how would we inherit two new weapons from this abstract base class? Simply by creating two
new script files, one for each new weapon, and specifying the
Weapon
class as the
ancestor
, instead of
MonoBehaviour
. In doing this, both classes inherit all
Weapon
behavior and functionality: that is,
Weapon
public properties also become public properties for the derived classes (see Listings 6-2 and 6-3 for
Weapon_Punch.cs
and
Weapon_Gun.cs
, respectively, configured for inheritance and ready for further
refinement and coding).
Listing 6-2. Weapon_Punch.cs: Punch Weapon Derived from Weapon Base Class
01 //------------------------------------------------
02 using UnityEngine;
03 using System.Collections;
04 //------------------------------------------------
05 public class Weapon_Punch : Weapon
06 {
07 }
Listing 6-3. Weapon_Gun.cs: Gun Weapon Derived from Weapon Base Class
01 //------------------------------------------------
02 using UnityEngine;
03 using System.Collections;
04 //------------------------------------------------
05 public class Weapon_Gun : Weapon
06 {
07 }