Advertisement
Guest User

Enemy

a guest
Apr 8th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.31 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4.  
  5. //Enemy inherits from MovingObject, our base class for objects that can move, Player also inherits from this.
  6. public class Enemy : MovingObject
  7. {
  8.     public int playerDamage;                            //The amount of food points to subtract from the player when attacking.
  9.     public int enemyHealth = 3;                         //The amount of health the enemies have
  10.  
  11.     private Animator animator;                          //Variable of type Animator to store a reference to the enemy's Animator component.
  12.     private Transform target;                           //Transform to attempt to move toward each turn.
  13.     private bool skipMove;                              //Boolean to determine whether or not enemy should skip a turn or move this turn.
  14.    
  15.     public AudioClip enemyAttack1;
  16.     public AudioClip enemyAttack2;
  17.  
  18.     //Start overrides the virtual Start function of the base class.
  19.     protected override void Start ()
  20.     {
  21.         //Register this enemy with our instance of GameManager by adding it to a list of Enemy objects.
  22.         //This allows the GameManager to issue movement commands.
  23.         GameManager.instance.AddEnemyToList (this);
  24.  
  25.         //Get and store a reference to the attached Animator component.
  26.         animator = GetComponent<Animator> ();
  27.  
  28.         //Find the Player GameObject using it's tag and store a reference to its transform component.
  29.         target = GameObject.FindGameObjectWithTag("Player").transform;
  30.  
  31.         //Call the start function of our base class MovingObject.
  32.         base.Start ();
  33.     }
  34.  
  35.  
  36.     //Override the AttemptMove function of MovingObject to include functionality needed for Enemy to skip turns.
  37.     //See comments in MovingObject for more on how base AttemptMove function works.
  38.     protected override void AttemptMove <T> (int xDir, int yDir)
  39.     {
  40.         //Check if skipMove is true, if so set it to false and skip this turn.
  41.         if(skipMove)
  42.         {
  43.             skipMove = false;
  44.             return;
  45.  
  46.         }
  47.  
  48.         //Call the AttemptMove function from MovingObject.
  49.         base.AttemptMove <T> (xDir, yDir);
  50.  
  51.         //Now that Enemy has moved, set skipMove to true to skip next move.
  52.         skipMove = true;
  53.     }
  54.  
  55.  
  56.     //MoveEnemy is called by the GameManger each turn to tell each Enemy to try to move towards the player.
  57.     public void MoveEnemy ()
  58.     {
  59.         //Declare variables for X and Y axis move directions, these range from -1 to 1.
  60.         //These values allow us to choose between the cardinal directions: up, down, left and right.
  61.         int xDir = 0;
  62.         int yDir = 0;
  63.  
  64.         //If the difference in positions is approximately zero (Epsilon) do the following:
  65.         if(Mathf.Abs (target.position.x - transform.position.x) < float.Epsilon)
  66.  
  67.             //If the y coordinate of the target's (player) position is greater than the y coordinate of this enemy's position set y direction 1 (to move up). If not, set it to -1 (to move down).
  68.             yDir = target.position.y > transform.position.y ? 1 : -1;
  69.  
  70.         //If the difference in positions is not approximately zero (Epsilon) do the following:
  71.         else
  72.             //Check if target x position is greater than enemy's x position, if so set x direction to 1 (move right), if not set to -1 (move left).
  73.             xDir = target.position.x > transform.position.x ? 1 : -1;
  74.  
  75.         //Call the AttemptMove function and pass in the generic parameter Player, because Enemy is moving and expecting to potentially encounter a Player
  76.         AttemptMove <Player> (xDir, yDir);
  77.     }
  78.  
  79.  
  80.     //OnCantMove is called if Enemy attempts to move into a space occupied by a Player, it overrides the OnCantMove function of MovingObject
  81.     //and takes a generic parameter T which we use to pass in the component we expect to encounter, in this case Player
  82.     protected override void OnCantMove <T> (T component)
  83.     {
  84.         //Declare hitPlayer and set it to equal the encountered component.
  85.         Player hitPlayer = component as Player;
  86.  
  87.         //Call the LoseFood function of hitPlayer passing it playerDamage, the amount of foodpoints to be subtracted.
  88.         hitPlayer.LoseFood (playerDamage);
  89.  
  90.         //Set the attack trigger of animator to trigger Enemy attack animation.
  91.         animator.SetTrigger ("enemyAttack");
  92.  
  93.     SoundManager.instance.RandomizeSfx (enemyAttack1, enemyAttack2);
  94.  
  95.     }
  96.     public void LoseHealth (int loss)
  97.     {
  98.         //Subtract lost food points from the players total.
  99.         enemyHealth -= loss;
  100.  
  101.         CheckForDeath();
  102.     }
  103.     private void CheckForDeath()
  104.     {
  105.         if (enemyHealth <=0)
  106.         {
  107.             gameObject.SetActive (false);
  108.         }
  109.     }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement