-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnergy.cs
More file actions
60 lines (50 loc) · 1.62 KB
/
Energy.cs
File metadata and controls
60 lines (50 loc) · 1.62 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
// for health, mana, etc.
using UnityEngine;
using UnityEngine.Events;
using Mirror;
namespace uMMORPG
{
public abstract class Energy : NetworkBehaviour
{
// current value
// set & get: keep between min and max
[SyncVar] int _current = 0;
public int current
{
get { return Mathf.Min(_current, max); }
set
{
bool emptyBefore = _current == 0;
_current = Mathf.Clamp(value, 0, max);
if (_current == 0 && !emptyBefore) onEmpty.Invoke();
}
}
// maximum value (may depend on buffs, items, etc.)
public abstract int max { get; }
// recovery rate (may depend on buffs, items etc.)
public abstract int recoveryRate { get; }
// don't recover while dead. all energy scripts need to check Health.
public Health health;
// spawn with full energy? important for monsters, etc.
public bool spawnFull = true;
[Header("Events")]
public UnityEvent onEmpty;
public override void OnStartServer()
{
// set full energy on start if needed
if (spawnFull) current = max;
// recovery every second
InvokeRepeating(nameof(Recover), 1, 1);
}
// get percentage
public float Percent() =>
(current != 0 && max != 0) ? (float)current / (float)max : 0;
// recover once a second
[Server]
public void Recover()
{
if (enabled && health.current > 0)
current += recoveryRate;
}
}
}