aboutsummaryrefslogtreecommitdiff
path: root/Assets/Scripts/DamageVolumeSystem/HealthData.cs
blob: 8da1e5fd11c0286a8e74b4916a33071a6e008429 (plain) (blame)
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
using System;
using UnityEngine;
using UnityEngine.Events;

namespace DamageVolumeSystem
{
    [Icon("Assets/Scripts/DamageVolumeSystem/Icon/HealthData.png")]
    public class HealthData : MonoBehaviour
    {
        public int defaultHealth = 5000;
        public int maxHealth = 5000;

        public UnityEvent<Damage> onAid = new();
        public UnityEvent<Damage> onHurt = new();
        public UnityEvent onDead = new();
    
        private int _health;

        public int Health
        {
            get => _health;
            set
            {
                var offset = value - _health;
                var offsetAbs = Mathf.Abs(offset);
                switch (offset)
                {
                    case > 0:
                        Aid(new Damage
                        {
                            Group = 12,
                            Force = offsetAbs,
                            Direction = Vector3.zero
                        });
                        return;
                    case < 0:
                        Hurt(new Damage
                        {
                            Group = 12,
                            Force = offsetAbs,
                            Direction = Vector3.zero
                        });
                        break;
                }
            }
        }

        private void Start()
        {
            _health = defaultHealth;
        }

        private void Reset()
        {
            var newHealth = Mathf.Clamp(defaultHealth, 0, maxHealth);
            SetHealth(newHealth);
        }

        public void Aid(Damage damage)
        {
            AddHealth(damage.Force);
            onAid.Invoke(damage);
        }

        public void Hurt(Damage damage)
        {
            ReduceHealth(damage.Force);
            onHurt.Invoke(damage);

            if (_health <= 0)
                onDead.Invoke();
        }
        
        public static HealthData operator +(HealthData a, Damage b)
        {
            a.Aid(b);
            return a;
        }
        
        public static HealthData operator -(HealthData a, Damage b)
        {
            a.Hurt(b);
            return a;
        }

        private void SetHealth(int newHealth)
            => _health = Mathf.Clamp(newHealth, 0, maxHealth);
        
        private void AddHealth(int newHealth)
            => _health = Mathf.Clamp(_health + newHealth, 0, maxHealth);
        
        private void ReduceHealth(int newHealth)
            => _health = Mathf.Clamp(_health - newHealth, 0, maxHealth);
    }
}