diff options
Diffstat (limited to 'td/Assets/Scripts/Enemy.cs')
-rw-r--r-- | td/Assets/Scripts/Enemy.cs | 51 |
1 files changed, 26 insertions, 25 deletions
diff --git a/td/Assets/Scripts/Enemy.cs b/td/Assets/Scripts/Enemy.cs index 7273370..4d15b64 100644 --- a/td/Assets/Scripts/Enemy.cs +++ b/td/Assets/Scripts/Enemy.cs @@ -1,38 +1,39 @@ -using System.Collections; -using System.Collections.Generic; +using System.Collections.Generic; using UnityEngine; public class Enemy : MonoBehaviour { + /* This is a general class that contains an enemy, + * Currently it follows the pathway, and dies when reacing the end */ - public float speed; - public float initialHp; - public Transform pathWay; + [Header("Attributes")] + public float Speed; // Speed multiplier + public int InitialHp; // HealthPoints + public int Damage; + public List<Vector3> Waypoints; // Pathway waypoints, should be set by the spawner + + [Header("Scripting vars")] + public Player Player; // Reference to the player object, should be set when instantiating - List<Vector3> waypoints = new List<Vector3>(); - Vector3 waypointPos; - int waypointNum = -1; // Using minus one so that first addition returns 0, first element in array - - void Start () { - foreach (Transform child in pathWay) { - waypoints.Add (child.position); - } - } + private Vector3 _waypointPos; // Current waypoint position + private int _waypointNum = -1; // Using minus one so that first addition returns 0, first element in array void Update () { - updateWaypoint (); + if (Player.GameIsPaused()) { return; } // This ensures that the game stays paused + + if ( (transform.position == _waypointPos && _waypointNum + 1 < Waypoints.Count) || _waypointNum == -1) { + _waypointNum++; + _waypointPos = new Vector3 (Waypoints [_waypointNum].x, 0.483f, Waypoints [_waypointNum].z); + } - float transformStep = speed * Time.deltaTime; - transform.position = Vector3.MoveTowards (transform.position, waypointPos, transformStep); + float transformStep = Speed * Time.deltaTime; + transform.position = Vector3.MoveTowards (transform.position, _waypointPos, transformStep); - if (waypointNum == waypoints.Count - 1) { + // Selfdestruct if object reached the end + if (_waypointNum + 1 >= Waypoints.Count) { + WaveSpawner.EnemiesAlive--; + Player.DecreaseHealth (Damage); Destroy (gameObject); - } - } - - void updateWaypoint() { - if ( (transform.position == waypointPos && waypointNum < waypoints.Count - 1) || waypointNum == -1) { - waypointNum++; - waypointPos = new Vector3 (waypoints [waypointNum].x, 0.604f, waypoints [waypointNum].z); + return; } } |