-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlrHealthSystem.cs
More file actions
60 lines (51 loc) · 1.61 KB
/
Copy pathPlrHealthSystem.cs
File metadata and controls
60 lines (51 loc) · 1.61 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
using System;
using TMPro;
using UnityEngine;
public class PlrHealthSystem : MonoBehaviour
{
private float health;
public float maxHealth = 100f;
public TextMeshProUGUI healthTextComponent;
public TeleportSystem teleportSystemScript;
public GameObject playerRef;
public DestroyAllEnemiesScript destroyAllEnemiesScript;
//
public event EventHandler OnPlayerDeath;
//https://www.youtube.com/watch?v=OUDBGiAiOqA
private void Start()
{
health = maxHealth;
healthTextComponent.text = "Health: " + health + " / " + maxHealth;
}
public float GetHealth()
{
return health;
}
public void AddOrSubToHealth(float amount)
{
float tempCheck = health + amount;
if (tempCheck > maxHealth)
{
tempCheck = maxHealth;
}
if (tempCheck < 0)
{
tempCheck = 0;
ResetPlayer();
return;
}
health = tempCheck;
healthTextComponent.text = "Health: " + health + " / " + maxHealth;
}
private void ResetPlayer()
{
health = maxHealth;
healthTextComponent.text = "Health: " + health + " / " + maxHealth;
teleportSystemScript.TeleportPlayerToLastSpawn();
destroyAllEnemiesScript.StartCoroutine("DestroyAllEnemies");
// waits till above
OnPlayerDeath?.Invoke(this, EventArgs.Empty);
// ? for checking null ref. like if I have this invoke but nobody listens
// if I didn't have that ?, it'd throw an error for that case
}
}