chenxin
2020-11-28 eafda00b2799204f913a11c835bd9ca7c64dd1e7
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
using System;
using UnityEngine;
using UnityEngine.Events;
 
namespace Core.Health
{
    /// <summary>
    /// A UnityEvent that passes through the HealthChangeInfo
    /// </summary>
    [Serializable]
    public class HealthChangeEvent : UnityEvent<HealthChangeInfo>
    {
    }
 
    /// <summary>
    /// A UnityEvent that passes through the HitInfo
    /// </summary>
    [Serializable]
    public class HitEvent : UnityEvent<HitInfo>
    {
    }
 
    /// <summary>
    /// Damageable listener.
    /// </summary>
    public class DamageableListener : MonoBehaviour
    {
        // The damageable behaviour to listen to
        [Tooltip("Leave this empty if the DamageableBehaviour and DamageableListener are on the same component")]
        public DamageableBehaviour damageableBehaviour;
 
        // Events for health change (i.e. healing/damage) - to be configured in the editor
        public HealthChangeEvent damaged;
        
        public HealthChangeEvent healed;
 
        // Events for death and max health - to be configured in the editor
        public UnityEvent died;
 
        public UnityEvent reachedMaxHealth;
 
        // Event for when health is change
        public HealthChangeEvent healthChanged;
        
        // Event for hits
        [Header("The hit event is different from the damage event as it also contains hit position data")]
        public HitEvent hit;
 
        /// <summary>
        /// Lazy loads the DamageableBehaviour
        /// </summary>
        protected virtual void Awake()
        {
            LazyLoad();
        }
 
        /// <summary>
        /// Subscribes to events
        /// </summary>
        protected virtual void OnEnable()
        {
            damageableBehaviour.configuration.died += OnDeath;
            damageableBehaviour.configuration.reachedMaxHealth += OnReachedMaxHealth;
            damageableBehaviour.configuration.healed += OnHealed;
            damageableBehaviour.configuration.damaged += OnDamaged;
            damageableBehaviour.configuration.healthChanged += OnHealthChanged;
            damageableBehaviour.hit += OnHit;
        }
 
        /// <summary>
        /// Unsubscribes from events on disable
        /// </summary>
        protected virtual void OnDisable()
        {
            damageableBehaviour.configuration.died -= OnDeath;
            damageableBehaviour.configuration.reachedMaxHealth -= OnReachedMaxHealth;
            damageableBehaviour.configuration.healed -= OnHealed;
            damageableBehaviour.configuration.damaged -= OnDamaged;
            damageableBehaviour.configuration.healthChanged -= OnHealthChanged;
            damageableBehaviour.hit -= OnHit;
        }
 
        /// <summary>
        /// Raises the death UnityEvent.
        /// </summary>
        protected virtual void OnDeath(HealthChangeInfo info)
        {
            died.Invoke();
        }
 
        /// <summary>
        /// Raises the max health UnityEvent.
        /// </summary>
        protected virtual void OnReachedMaxHealth()
        {
            reachedMaxHealth.Invoke();
        }
 
        /// <summary>
        /// Raises the heal UnityEvent.
        /// </summary>
        /// <param name="info">Info.</param>
        protected virtual void OnHealed(HealthChangeInfo info)
        {
            healed.Invoke(info);
        }
 
        /// <summary>
        /// Raises the damage UnityEvent.
        /// </summary>
        /// <param name="info">Info.</param>
        protected virtual void OnDamaged(HealthChangeInfo info)
        {
            damaged.Invoke(info);
        }
        
        /// <summary>
        /// Raises the healthChanged UnityEvent.
        /// </summary>
        /// <param name="info">Info.</param>
        protected virtual void OnHealthChanged(HealthChangeInfo info)
        {
            healthChanged.Invoke(info);
        }
 
        /// <summary>
        /// Raises the hit UnityEvent.
        /// </summary>
        /// <param name="info">Info.</param>
        protected virtual void OnHit(HitInfo info)
        {
            hit.Invoke(info);
        }
 
        /// <summary>
        /// Looks for the damageableBehaviour if it is not already assigned
        /// It may be assigned in editor or from a previous LazyLoad() call
        /// </summary>
        protected void LazyLoad()
        {
            if (damageableBehaviour != null)
            {
                return;
            }
 
            damageableBehaviour = GetComponent<DamageableBehaviour>();
        }
    }
}