Game Development Reference
In-Depth Information
For now, let's turn back to the previous chapter (Chapter 6) for creating weapons, and view Listing
6-8 (line 78). Here, you'll see that when a weapon strikes an enemy object, it uses the SendMessage
function to initiate a Damage event on the Enemy object being hit, if such an event exists. The Damage
event should handle all damage-receiving functionality. With this in mind, let's consider the starting
implementation for both the Enemy base class and the Enemy_Drone derived class, as shown in
Listings 7-1 and 7-2; comments follow.
Listing 7-1. Enemy.cs: Base Class for Enemy Objects
01 using UnityEngine;
02 using System.Collections;
03
04 public class Enemy : MonoBehaviour
05 {
06 //Current health of this enemy - inherited by descendent classes
07 public int Health = 100;
08 }
Listing 7-2. Enemy_Drone.cs: Derived Class for Drone Objects
01 using UnityEngine;
02 using System.Collections;
03
04 public class Enemy_Drone : Enemy
05 {
06 //------------------------------------------------
07 //Event called when damaged by an attack
08 public void Damage(int Damage = 0)
09 {
10 //Reduce health
11 Health -= Damage;
12
13 //Check if dead
14 if(Health <= 0)
15 {
16 //Send enemy destroyed notification
17 GameManager.Notifications.PostNotification(this, "EnemyDestroyed");
18
19 //Remove object from scene
20 DestroyImmediate(gameObject);
21
22 //Clean up old listeners
23 GameManager.Notifications.RemoveRedundancies();
24 }
25 }
26 //------------------------------------------------
27 }
 
Search WWH ::




Custom Search