Пример #1
0
 public void OnDeath(HealthEvent health)
 {
     if (spawnOnDeath)
     {
         Spawn("OnDeath", health);
     }
 }
Пример #2
0
 public void OnDamaged(HealthEvent health)
 {
     if (spawnOnDamaged)
     {
         Spawn("OnDamaged", health);
     }
 }
Пример #3
0
 public void OnRestored(HealthEvent health)
 {
     if (spawnOnRestored)
     {
         Spawn("OnRestored", health);
     }
 }
Пример #4
0
 /// <summary>Adds health.</summary>
 public void Heal(HealthEvent healthEvent)
 {
     if (!DisableHealthChange)
     {
         healthEvent.Amount = Mathf.Abs(healthEvent.Amount);
         ChangeHealth(healthEvent);
     }
 }
Пример #5
0
 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);
     }
 }
Пример #6
0
 void OnDeath(HealthEvent health)
 {
     amount = health.Amount;
 }
Пример #7
0
 void OnDamaged(HealthEvent health)
 {
     amount = health.Amount;
 }
Пример #8
0
 void OnHealed(HealthEvent health)
 {
     amount = health.Amount;
 }
Пример #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);
        }