-
Notifications
You must be signed in to change notification settings - Fork 0
/
HpRegeneration.cs
79 lines (63 loc) · 1.93 KB
/
HpRegeneration.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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HpRegeneration : MonoBehaviour
{
[SerializeField]
private int maxHealth = 5;
[SerializeField]
private float healthRegenerationRate = 1;
[SerializeField]
private float regenerationDelay = 1;
private int currentHealth;
private float lastDamagesTime = -1;
private float regeneratedHealth;
public bool IsDead
{
get { return currentHealth == 0; }
}
private void Awake()
{
currentHealth = maxHealth;
}
void Update()
{
if ( lastDamagesTime >= 0 && Time.time - lastDamagesTime >= regenerationDelay )
{
RegenerateHealth();
}
}
public void RegenerateHealth()
{
regeneratedHealth += healthRegenerationRate * Time.deltaTime;
int flooredRegeneratedHealth = Mathf.FloorToInt( regeneratedHealth );
regeneratedHealth -= flooredRegeneratedHealth;
Heal( flooredRegeneratedHealth );
}
public void Hurt( int damages )
{
if ( damages < 0 )
throw new System.ArgumentException( "Damages must be greater or equal to 0", nameof( damages ) );
if ( IsDead )
return;
currentHealth = Mathf.Clamp( currentHealth - damages, 0, maxHealth );
regeneratedHealth = 0;
lastDamagesTime = Time.time;
if( IsDead )
{
// Die
}
}
public void Heal( int amount )
{
if ( amount < 0 )
throw new System.ArgumentException( "Healed amount must be greater or equal to 0", nameof( amount ) );
currentHealth = Mathf.Clamp( currentHealth + amount, 0, maxHealth );
if( currentHealth == maxHealth )
{
lastDamagesTime = -1;
regeneratedHealth = 0;
// Full health
}
}
}