iTz_WyTe

Unit

Mar 18th, 2017
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.74 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. public class Unit : MonoBehaviour {
  6.  
  7.  
  8.     public Transform target;
  9.     public float speed = 2;
  10.     public int essence = 100;
  11.     public string weakness;
  12.     Vector3[] path;
  13.     int targetIndex;
  14.     Grid grid;
  15.  
  16.     void Awake() {
  17.         target = GameObject.FindGameObjectWithTag ("Target").transform;
  18.     }
  19.  
  20.     void Start() {
  21.         UpdatePath ();
  22.     }
  23.  
  24.     public void TakeDamage (int amount) {
  25.         essence -= amount;
  26.         PlayerStats.playerEssence += amount;
  27.         if (essence <= 0) {
  28.             Die ();
  29.         }
  30.     }
  31.  
  32.     void Die (){
  33.         Destroy (gameObject);
  34.         return;
  35.     }
  36.  
  37.     public void OnPathFound(Vector3[] newPath, bool pathSuccessful) {
  38.         if (pathSuccessful) {
  39.             Debug.Log ("Path Found!");
  40.             path = newPath;
  41.             targetIndex = 0;
  42.             StopCoroutine ("FollowPath");
  43.             StartCoroutine ("FollowPath");
  44.         } else {
  45.             return;
  46.         }
  47.     }
  48.  
  49.     IEnumerator FollowPath() {
  50.         targetIndex = 0;
  51.         Vector3 currentWaypoint = path[0];
  52.         while (true) {
  53.             if (transform.position == currentWaypoint) {
  54.                 targetIndex ++;
  55.                 if (targetIndex == path.Length) {
  56.                     Destroy (gameObject);
  57.                     PlayerStats.playerLives -= 1;
  58.                     Debug.Log ("You lost a life!");
  59.                     yield break;
  60.                 }
  61.                 currentWaypoint = path[targetIndex];
  62.             }
  63.  
  64.             transform.position = Vector3.MoveTowards(transform.position,currentWaypoint,speed * Time.deltaTime);
  65.             yield return null;
  66.  
  67.         }
  68.     }
  69.  
  70.     public void UpdatePath () {
  71.         if (gameObject == null)
  72.             return;
  73.             PathRequestManager.RequestPath (transform.position, target.position, OnPathFound);
  74.     }
  75.  
  76.     public void OnDrawGizmos() {
  77.         if (path != null) {
  78.             for (int i = targetIndex; i < path.Length; i ++) {
  79.                 Gizmos.color = Color.black;
  80.                 Gizmos.DrawCube(path[i], Vector3.one);
  81.             }
  82.         }
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment