Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- public class Unit : MonoBehaviour {
- public Transform target;
- public float speed = 2;
- public int essence = 100;
- public string weakness;
- Vector3[] path;
- int targetIndex;
- Grid grid;
- void Awake() {
- target = GameObject.FindGameObjectWithTag ("Target").transform;
- }
- void Start() {
- UpdatePath ();
- }
- public void TakeDamage (int amount) {
- essence -= amount;
- PlayerStats.playerEssence += amount;
- if (essence <= 0) {
- Die ();
- }
- }
- void Die (){
- Destroy (gameObject);
- return;
- }
- public void OnPathFound(Vector3[] newPath, bool pathSuccessful) {
- if (pathSuccessful) {
- Debug.Log ("Path Found!");
- path = newPath;
- targetIndex = 0;
- StopCoroutine ("FollowPath");
- StartCoroutine ("FollowPath");
- } else {
- return;
- }
- }
- IEnumerator FollowPath() {
- targetIndex = 0;
- Vector3 currentWaypoint = path[0];
- while (true) {
- if (transform.position == currentWaypoint) {
- targetIndex ++;
- if (targetIndex == path.Length) {
- Destroy (gameObject);
- PlayerStats.playerLives -= 1;
- Debug.Log ("You lost a life!");
- yield break;
- }
- currentWaypoint = path[targetIndex];
- }
- transform.position = Vector3.MoveTowards(transform.position,currentWaypoint,speed * Time.deltaTime);
- yield return null;
- }
- }
- public void UpdatePath () {
- if (gameObject == null)
- return;
- PathRequestManager.RequestPath (transform.position, target.position, OnPathFound);
- }
- public void OnDrawGizmos() {
- if (path != null) {
- for (int i = targetIndex; i < path.Length; i ++) {
- Gizmos.color = Color.black;
- Gizmos.DrawCube(path[i], Vector3.one);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment