-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHealthHandler.cs
More file actions
288 lines (249 loc) · 7.24 KB
/
HealthHandler.cs
File metadata and controls
288 lines (249 loc) · 7.24 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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
using System;
using System.Collections;
using System.Runtime.CompilerServices;
using UnityEngine;
using UnityEngine.AI;
using UnityEngine.UI;
/// <summary>
/// Modular Health System with optional knockback options.
/// </summary>
public class HealthHandler : MonoBehaviour
{
[Header("Knockback-Switches")]
[SerializeField] private bool charKnockback;
[SerializeField] private bool regularKnockback;
[SerializeField] private bool bounceOffEnemy;
[Header("Health-Objects")]
public Slider guiHealth;
public Material thisMaterial;
public ParticleSystem deathExplosion;
public AudioSource audioSource;
public AudioClip hurtSound;
[Header("Health-Controls")]
public float healthAmt;
public int optionalCooldownFrames;
[SerializeField] private string lossTag;
[SerializeField] private string gainTag;
private Collider userCollider;
private bool isInvincible;
private bool isDead;
private bool deathStarted;
private bool isPooled;
private CharacterMovement mainChar;
private NavMeshAgent enemyMesh;
/// <summary>
/// Checks for various bool states and healthAmt
/// </summary>
private void Awake()
{
if (bounceOffEnemy)
{
enemyMesh = GetComponent<NavMeshAgent>();
}
if (charKnockback)
{
mainChar = GetComponent<CharacterMovement>();
}
isDead = false;
isInvincible = false;
userCollider = GetComponent<Collider>();
if (healthAmt < 0)
{
isInvincible = true;
}
}
/// <summary>
/// This is to allow outside scripts to cause 1 damage
/// </summary>
public void TakeDamage()
{
Debug.LogWarning("Hurt");
float DAMAGE_AMMOUNT = 1.0F;
healthAmt -= DAMAGE_AMMOUNT;
}
/// <summary>
/// This is to allow outside scripts to cause an ammount of damage
/// </summary>
public void TakeDamage(float ammount)
{
Debug.LogWarning("Hurt");
healthAmt -= ammount;
}
/// <summary>
/// This will set the object health to zero killing it.
/// </summary>
public void InstaKill()
{
Debug.LogWarning("Hurt");
isDead = true;
}
/// <summary>
///
/// </summary>
/// <param name="other"></param>
public void CheckDistance(Transform other)
{
float dist = Vector3.Distance(transform.position, other.transform.position);
if (dist < 0.5f)
{
enemyMesh.enabled = false; // ONLY disable when actually hit
}
else
{
enemyMesh.enabled = true;
}
}
/// <summary>
/// Allows for control when a collider interacts
/// </summary>
/// <param name="other"></param>
///
private void OnTriggerStay(Collider other)
{
if (isInvincible) return;
if (!string.IsNullOrEmpty(lossTag) && other.CompareTag(lossTag))
{
Debug.LogWarning("Is Loss");
if (healthAmt > 1.0f)
{
healthAmt -= 1.0f;
if (charKnockback)
{
StartCoroutine(mainChar.TemporarilyDisableMovement(0.1f));
KnockbackCharacter(other.transform);
}
if (regularKnockback)
{
Knockback(other.transform);
}
StartCoroutine(HurtCooldown());
}
else
{
isDead = true;
}
}
if (!string.IsNullOrEmpty(gainTag) && other.CompareTag(gainTag))
{
healthAmt += 1.0f;
}
}
/// <summary>
/// Knockback for object 'source'
/// </summary>
/// <param name="source"></param>
private void Knockback(Transform source)
{
Vector3 direction = (transform.position - source.position).normalized;
StartCoroutine(Knockback(direction, 0.1f, 2f));
}
/// <summary>
/// Knockback for object 'source' on everything but the y axis.
/// </summary>
/// <param name="source"></param>
private void KnockbackCharacter(Transform source)
{
Vector3 direction = (transform.position - source.transform.position).normalized;
direction.y = 0f;
direction.Normalize();
StartCoroutine(Knockback(direction, 0.3f, 10f));
}
/// <summary>
/// Error Checking to fail silenetly if the userCollider is Null.
/// </summary>
void Start()
{
userCollider = GetComponent<Collider>();
if (userCollider == null)
{
Debug.LogError("Collider not attached. This script requires one.");
}
}
/// <summary>
/// Observes guiHealth constantly and isDead bool
/// </summary>
void Update()
{
if (guiHealth != null)
{
guiHealth.value = healthAmt;
}
if (isDead && !deathStarted)
{
deathStarted = true;
StartCoroutine(DeathAnimation());
}
}
/// <summary>
/// This allows us to utilze deterministic knockback data.
/// </summary>
/// <param name="dir"></param>
/// <param name="duration"></param>
/// <param name="strength"></param>
/// <returns></returns>
private IEnumerator Knockback(Vector3 dir, float duration, float strength)
{
if (bounceOffEnemy && enemyMesh != null)
enemyMesh.enabled = false;
float timer = 0f;
while (timer < duration)
{
transform.position += dir * strength * Time.deltaTime;
timer += Time.deltaTime;
yield return null;
}
if (bounceOffEnemy && enemyMesh != null)
enemyMesh.enabled = true;
}
/// <summary>
/// Make the object un-interactable, play both the hurtSound and deathExplosion.
/// </summary>
/// <returns></returns>
IEnumerator DeathAnimation()
{
if (enemyMesh != null)
{
enemyMesh.enabled = false;
}
if (hurtSound != null)
{
Debug.LogWarning("Play Sound");
audioSource.PlayOneShot(hurtSound);
}
if (deathExplosion != null)
{
deathExplosion.Play();
yield return new WaitForSeconds(deathExplosion.main.duration);
}
if (isPooled)
{
ObjectPoolManager.ReturnObjectToPool(gameObject);
}
else
{
Destroy(this.gameObject);
}
}
/// <summary>
/// This gives the object invincibiliy frames after getting hit.
/// </summary>
/// <returns></returns>
private IEnumerator HurtCooldown()
{
Debug.LogWarning("CoolingDown");
Color baseColor = thisMaterial.color;
Color baseGreen = baseColor;
baseGreen.g = 1f;
isInvincible = true;
for (int i = 0; i < optionalCooldownFrames; i++)
{
thisMaterial.color = baseGreen;
yield return new WaitForSeconds(0.15f);
thisMaterial.color = baseColor;
yield return new WaitForSeconds(0.15f);
Debug.LogWarning("Cooling Down");
}
thisMaterial.color = baseColor;
isInvincible = false;
}
}