using System; using UnityEngine; using UnityEngine.Events; namespace Core.Health { /// /// A UnityEvent that passes through the HealthChangeInfo /// [Serializable] public class HealthChangeEvent : UnityEvent { } /// /// A UnityEvent that passes through the HitInfo /// [Serializable] public class HitEvent : UnityEvent { } /// /// Damageable listener. /// 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; /// /// Lazy loads the DamageableBehaviour /// protected virtual void Awake() { LazyLoad(); } /// /// Subscribes to events /// 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; } /// /// Unsubscribes from events on disable /// 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; } /// /// Raises the death UnityEvent. /// protected virtual void OnDeath(HealthChangeInfo info) { died.Invoke(); } /// /// Raises the max health UnityEvent. /// protected virtual void OnReachedMaxHealth() { reachedMaxHealth.Invoke(); } /// /// Raises the heal UnityEvent. /// /// Info. protected virtual void OnHealed(HealthChangeInfo info) { healed.Invoke(info); } /// /// Raises the damage UnityEvent. /// /// Info. protected virtual void OnDamaged(HealthChangeInfo info) { damaged.Invoke(info); } /// /// Raises the healthChanged UnityEvent. /// /// Info. protected virtual void OnHealthChanged(HealthChangeInfo info) { healthChanged.Invoke(info); } /// /// Raises the hit UnityEvent. /// /// Info. protected virtual void OnHit(HitInfo info) { hit.Invoke(info); } /// /// Looks for the damageableBehaviour if it is not already assigned /// It may be assigned in editor or from a previous LazyLoad() call /// protected void LazyLoad() { if (damageableBehaviour != null) { return; } damageableBehaviour = GetComponent(); } } }