-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEnemy.cs
More file actions
67 lines (56 loc) · 1.68 KB
/
Enemy.cs
File metadata and controls
67 lines (56 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
public class Enemy : MonoBehaviour
{
public int health;
public int damageToPlayer;
public int moneyOnDeath;
public float moveSpeed;
// Path
private Transform[] path;
private int curPathWaypoint;
public GameObject healthBarPrefab;
public static event UnityAction OnDestroyed;
void Start ()
{
path = GameManager.instance.enemyPath.waypoints;
// create the health bar
Canvas canvas = FindObjectOfType<Canvas>();
GameObject healthBar = Instantiate(healthBarPrefab, canvas.transform);
healthBar.GetComponent<EnemyHealthBar>().Initialize(this);
}
void Update ()
{
MoveAlongPath();
}
// called every frame to move the enemy towards the end of the path
void MoveAlongPath ()
{
if(curPathWaypoint < path.Length)
{
transform.position = Vector3.MoveTowards(transform.position, path[curPathWaypoint].position, moveSpeed * Time.deltaTime);
if(transform.position == path[curPathWaypoint].position)
curPathWaypoint++;
}
// if we're at the end of the path
else
{
GameManager.instance.TakeDamage(damageToPlayer);
OnDestroyed.Invoke();
Destroy(gameObject);
}
}
// called when a tower deals damage to the enemy
public void TakeDamage (int amount)
{
health -= amount;
if(health <= 0)
{
GameManager.instance.AddMoney(moneyOnDeath);
OnDestroyed.Invoke();
Destroy(gameObject);
}
}
}