예제 #1
0
        public void Heal(float amount, AttributeChangeCause cause = AttributeChangeCause.FORCED, Damageable healer = null)
        {
            if (GetAttribute(Attributes.HEALTH).GetPercent() == 1.0f)
            {
                return;
            }

            //  Invoke a heal event
            HealthRegainEvent e = new HealthRegainEvent {
                cause = cause, entity = this, healer = healer, amount = amount
            };

            OnHealthRegainEvent?.Invoke(this, e);
            if (e.cancel == true)
            {
                return;
            }                               //  return if the event has been cancelled by any subscriber

            //  Update health
            GetAttribute(Attributes.HEALTH).Modify(e.amount);

            //  Send indicator
            UIMaster.SendFloatingIndicator(this.transform.position + this.transform.rotation * center, e.amount.ToString("#.0"), Color.green);
        }
예제 #2
0
        public void Damage(float damage, Vector3 hitPoint, AttributeChangeCause cause = AttributeChangeCause.FORCED, Damageable attacker = null, DamageType type = DamageType.NONE)
        {
            //  Invoke a damage event
            DamageEvent e = new DamageEvent {
                cause = cause, victim = this, attacker = attacker, type = type, damage = damage
            };

            OnDamageEvent?.Invoke(this, e);
            if (e.cancel == true)
            {
                return;
            }                               //  return if the event has been cancelled by any subscriber

            bool hadImmunity   = false;
            bool hadWeakness   = false;
            bool hadResistance = false;

            //  Check for immunity
            for (int i = 0; i < immunities.Length; i++)
            {
                if (e.type == immunities[i])
                {
                    e.damage    = 0;
                    hadImmunity = true;
                    break;
                }
            }

            //  Modify any damage by any weaknesses or resistances
            if (e.damage > 0 && e.type != DamageType.NONE)
            {
                for (int i = 0; i < weaknesses.Length; i++)
                {
                    if (e.type == weaknesses[i])
                    {
                        e.damage   *= 2;
                        hadWeakness = true;
                        break;
                    }
                }

                for (int i = 0; i < resistances.Length; i++)
                {
                    if (e.type == resistances[i])
                    {
                        e.damage     /= 2;
                        hadResistance = true;
                        break;
                    }
                }
            }

            //  If the damage is enough to kill, invoke a death event
            if (GetAttributeValue(Attributes.HEALTH) - e.damage <= 0)
            {
                DeathEvent e2 = new DeathEvent {
                    cause = cause, victim = this, attacker = attacker
                };
                OnDeathEvent?.Invoke(this, e2);
                if (e2.cancel == true)
                {
                    return;
                }                                //  return if the event has been cancelled by any subscriber
            }

            //  Update health
            GetAttribute(Attributes.HEALTH).Modify(-e.damage);

            //  Send indicator
            Color indicatorColor = Color.gray * Color.gray;

            if (hadImmunity)
            {
                indicatorColor = Color.red;
            }
            if (hadWeakness)
            {
                indicatorColor = Color.white;
            }
            if (hadResistance)
            {
                indicatorColor = Color.yellow;
            }

            if (hitPoint == null)
            {
                UIMaster.SendFloatingIndicator(this.transform.position + this.transform.rotation * center, e.damage.ToString("#.0"), indicatorColor);
            }
            else
            {
                UIMaster.SendFloatingIndicator(hitPoint, e.damage.ToString("0.0"), indicatorColor);
            }
        }