-
Notifications
You must be signed in to change notification settings - Fork 0
/
SlimeBehaviour.cs
141 lines (108 loc) · 3.03 KB
/
SlimeBehaviour.cs
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
using UnityEngine.UI;
public class SlimeBehaviour : MonoBehaviour {
private Animator anim;
private NavMeshAgent navAgent;
public float currentHealth;
public float maxHealth = 2f;
public GameObject Hero;
private bool isDead;
Vector3 originalPosition;
public GameObject healthBar;
public GameObject healthBarPrefab;
public Transform healthBarTransform;
public AudioSource DamagedSFX;
public AudioSource DeathSFX;
public AudioSource MoveSFX;
public AudioSource AttackSFX;
public CharacterMovement cm;
void Start () {
anim = GetComponent<Animator> ();
navAgent = GetComponent<NavMeshAgent> ();
currentHealth = maxHealth;
healthBar = Instantiate (healthBarPrefab, healthBarTransform.transform) as GameObject;
healthBar.transform.GetComponentInChildren<Slider> ().value = (currentHealth / maxHealth);
originalPosition = transform.position;
}
void FixedUpdate () {
if (!isDead) {
healthBar.transform.position = new Vector3 (gameObject.transform.position.x, gameObject.transform.position.y+4f, gameObject.transform.position.z);
healthBar.transform.rotation = Quaternion.LookRotation (Camera.main.transform.forward, Camera.main.transform.up);
transform.LookAt (Hero.transform);
}
if (CharacterMovement.isDead) {
CancelInvoke ();
}
}
public void TakeDamage (){
if (!isDead) {
anim.SetTrigger ("Take_Damage");
currentHealth--;
healthBar.GetComponentInChildren<Slider> ().value = (currentHealth / maxHealth);
DamagedSFX.Play ();
//Debug.Log ("Slime health: " + currentHealth);
if (currentHealth <= 0) {
Die ();
}
}
}
void ChaseHero(){
navAgent.SetDestination (Hero.transform.position);
anim.SetTrigger ("Moving");
navAgent.speed = 1.0f;
if (!MoveSFX.isPlaying) {
MoveSFX.PlayDelayed (0.25f);
}
if (navAgent.remainingDistance <= navAgent.stoppingDistance) {
if (MoveSFX.isPlaying) {
MoveSFX.Stop ();
}
if (!IsInvoking () && !CharacterMovement.isDead) {
InvokeRepeating ("Attack", 1f, 2f);
}
} else {
CancelInvoke ();
}
}
void Attack(){
if (!isDead) {
anim.SetTrigger ("Basic_Attack");
AttackSFX.Play ();
cm.TakeDamage (1);
}
}
void Die (){
anim.SetTrigger ("Dead");
isDead = true;
//Debug.Log ("Slime is dead. isDead = "+ isDead);
CancelInvoke ("Attack");
gameObject.GetComponent<Collider> ().enabled = false;
DeathSFX.Play ();
Invoke ("RemoveBody", 5f);
}
void RemoveBody(){
//Debug.Log ("This method was run");
Destroy (gameObject);
healthBar.SetActive (false);
}
void OnTriggerStay (Collider col){
if (col.name == "Hero" && !isDead) {
//Debug.Log ("Slime: Enemy in sight!");
ChaseHero ();
}
}
void OnTriggerExit (Collider col){
if (col.name == "Hero" && !isDead) {
//Debug.Log ("Slime: Well, it's time to go home");
StopChaseHero ();
}
}
//Slime returns to its original position
void StopChaseHero(){
navAgent.SetDestination (originalPosition);
navAgent.speed = 0.8f;
}
}