public void OnDeath(HealthEvent health)
 {
     if (spawnOnDeath)
     {
         Spawn("OnDeath", health);
     }
 }
 public void OnDamaged(HealthEvent health)
 {
     if (spawnOnDamaged)
     {
         Spawn("OnDamaged", health);
     }
 }
 public void OnRestored(HealthEvent health)
 {
     if (spawnOnRestored)
     {
         Spawn("OnRestored", health);
     }
 }
Example #4
0
 /// <summary>Adds health.</summary>
 public void Heal(HealthEvent healthEvent)
 {
     if (!DisableHealthChange)
     {
         healthEvent.Amount = Mathf.Abs(healthEvent.Amount);
         ChangeHealth(healthEvent);
     }
 }
 void Spawn(string method, HealthEvent health)
 {
     if (prefab)
     {
         GameObject go = Instantiate(prefab, spawnPosition ? spawnPosition.position : transform.position, transform.rotation) as GameObject;
         go.SendMessage(method, health, SendMessageOptions.DontRequireReceiver);
     }
 }
 void OnDeath(HealthEvent health)
 {
     amount = health.Amount;
 }
 void OnDamaged(HealthEvent health)
 {
     amount = health.Amount;
 }
 void OnHealed(HealthEvent health)
 {
     amount = health.Amount;
 }
Example #9
0
        /// <summary>Directly modify the health value and send events.</summary>
        public float ChangeHealth(HealthEvent healthEvent)
        {
            if (!DisableHealthChange)
            {
                float newHealth    = Mathf.Clamp(Value + healthEvent.Amount, 0, MaxValue);
                float healthChange = newHealth - Value;

                if (healthChange > 0)
                {
                    if (Incurable)
                    {
                        return(Value);
                    }
                }
                else if (healthChange < 0)
                {
                    if (Invincible)
                    {
                        return(Value);
                    }
                }
                else
                {
                    return(Value);
                }

                float valueBeforeChange = Value;

                Value = newHealth;

                HealthEvent received = new HealthEvent(healthEvent.EventObject, healthChange);
                HealthEvent caused   = new HealthEvent(gameObject, healthChange);

                if (healthChange > 0 && valueBeforeChange <= 0)  //Revived
                {
                    if (OnRevived != null)
                    {
                        OnRevived.Invoke(received);
                    }

                    if (healthEvent.EventObject)
                    {
                        Health causedEvent = healthEvent.EventObject.GetComponentInParent <Health>();

                        if (causedEvent && causedEvent.OnCausedRevival != null)
                        {
                            causedEvent.OnCausedRevival.Invoke(caused);
                        }
                    }
                }
                else if (healthChange < 0 && valueBeforeChange >= MaxValue)  //First Damaged
                {
                    if (OnFirstDamaged != null)
                    {
                        OnFirstDamaged.Invoke(received);
                    }

                    if (healthEvent.EventObject)
                    {
                        Health causedEvent = healthEvent.EventObject.GetComponentInParent <Health>();

                        if (causedEvent && causedEvent.OnCausedFirstDamage != null)
                        {
                            causedEvent.OnCausedFirstDamage.Invoke(caused);
                        }
                    }
                }

                if (healthChange > 0)  //Heal
                {
                    bool healthIsFull = Value >= MaxValue;

                    if (OnHeal != null)
                    {
                        OnHeal.Invoke(received);
                    }

                    if (healthEvent.EventObject)
                    {
                        Health causedEvent = healthEvent.EventObject.GetComponentInParent <Health>();

                        if (causedEvent && causedEvent.OnCausedHeal != null)
                        {
                            causedEvent.OnCausedHeal.Invoke(caused);
                        }
                    }
                }
                else if (healthChange < 0)  //Damaged
                {
                    if (OnDamaged != null)
                    {
                        OnDamaged.Invoke(received);
                    }

                    if (healthEvent.EventObject)
                    {
                        Health causedEvent = healthEvent.EventObject.GetComponentInParent <Health>();

                        if (causedEvent && causedEvent.OnCausedDamage != null)
                        {
                            causedEvent.OnCausedDamage.Invoke(caused);
                        }
                    }
                }

                if (Value <= 0)
                {
                    if (healthChange < 0)  //Death
                    {
                        if (OnDeath != null)
                        {
                            OnDeath.Invoke(received);
                        }

                        if (healthEvent.EventObject)
                        {
                            Health causedEvent = healthEvent.EventObject.GetComponentInParent <Health>();

                            if (causedEvent && causedEvent.OnCausedDeath != null)
                            {
                                causedEvent.OnCausedDeath.Invoke(caused);
                            }
                        }
                    }
                }
                else if (Value >= MaxValue)  //Restored
                {
                    if (healthChange > 0)
                    {
                        if (OnFullyRestored != null)
                        {
                            OnFullyRestored.Invoke(received);
                        }

                        if (healthEvent.EventObject)
                        {
                            Health causedEvent = healthEvent.EventObject.GetComponentInParent <Health>();

                            if (causedEvent && causedEvent.OnCausedFullRestoration != null)
                            {
                                causedEvent.OnCausedFullRestoration.Invoke(caused);
                            }
                        }
                    }
                }
            }

            return(Value);
        }