Game Development Reference
In-Depth Information
135 //Deactivate equipped
136 IsEquipped = false;
137
138 foreach(SpriteRenderer SR in WeaponSprites)
139 SR.enabled = false;
140 }
141 //------------------------------------------------
142 }
Line 36. This weapon class registers for a WeaponChange event. When this event
occurs, the NotificationsManager will invoke the WeaponChange function (line
126), which typically will hide the weapon sprites for the active weapon, when it
becomes deactivated as the Player changes to a different weapon.
Line 104. The EquipWeapon function is called by the PlayerController class
when the weapon becomes activated for the Player—that is, when it becomes
the currently selected weapon. Notice, this function also raises a WeaponChange
event at line 119.
Let's also take a look over the Weapon_Gun class, as shown in Listing 6-9. Notice that its
implementation is very similar to the punch weapon in Listing 6-8. It simply features extra code to
handle limited and expired ammo. In fact, the Weapon_Gun class is so similar to Weapon_Punch , that
one might even be tempted to insert an additional level of class inheritance, creating a new base
class between Weapon and its derivatives Weapon_Gun and Weapon_Punch . This class would define
more behavior common to Weapon_Punch and Weapon_Gun , but without “infecting” the original Weapon
class. Decisions about class inheritance and where functionality belongs is critical to designing a
solid class framework for your games.
Listing 6-9. Weapon_Gun.cs: Completed Gun Weapon
001 //------------------------------------------------
002 using UnityEngine;
003 using System.Collections;
004 //------------------------------------------------
005 public class Weapon_Gun : Weapon
006 {
007 //------------------------------------------------
008 //Default Sprite to show for weapon when active and not attacking
009 public SpriteRenderer DefaultSprite = null;
010
011 //Sound to play on attack
012 public AudioClip WeaponAudio = null;
013
014 //Audio Source for sound playback
015 private AudioSource SFX = null;
016
017 //Reference to all child sprite renderers for this weapon
018 private SpriteRenderer[] WeaponSprites = null;
019
 
Search WWH ::




Custom Search