public void Update() { float daltaTime = Time.deltaTime; ApplyEffect[] appledEffects = new ApplyEffect[_appledEffects.Values.Count]; _appledEffects.Values.CopyTo(appledEffects, 0); foreach (ApplyEffect applyEffect in appledEffects) { applyEffect.During -= daltaTime; if (applyEffect.During <= 0) { applyEffect.Effect.Unapply(this); _appledEffects.Remove(applyEffect.Effect.GetType()); } } foreach (ApplyEffect applyEffect in _stackEffect.ToArray()) { applyEffect.During -= daltaTime; if (applyEffect.During <= 0) { applyEffect.Effect.Unapply(this); _stackEffect.Remove(applyEffect); } } }
public void DealDamage(Damage damage) { //apply basic damage HitPoint -= damage.HitDamage; foreach (var effect in damage.Effects) { if (!effect.IsStack) { ApplyEffect applyEffect = new ApplyEffect(); applyEffect.During = damage.During; applyEffect.Effect = effect; applyEffect.EffectValue = effect.GetEffectValue(this); applyEffect.BeforeApplyEffectValue = effect.GetBeforeApplyEffectValue(this); _stackEffect.Add(applyEffect); effect.Apply(this); } else { if (_appledEffects.ContainsKey(effect.GetType())) { ApplyEffect applyEffect = (ApplyEffect)_appledEffects[effect.GetType()]; var oldEffectValue = applyEffect.EffectValue; var newEffectValue = effect.GetEffectValue(applyEffect.BeforeApplyEffectValue); if (newEffectValue > oldEffectValue) { applyEffect.Effect.Unapply(this); effect.Apply(this); applyEffect.EffectValue = newEffectValue; applyEffect.During = damage.During; } else { applyEffect.During = damage.During; } } else { ApplyEffect applyEffect = new ApplyEffect(); applyEffect.During = damage.During; applyEffect.Effect = effect; applyEffect.EffectValue = effect.GetEffectValue(this); applyEffect.BeforeApplyEffectValue = effect.GetBeforeApplyEffectValue(this); _appledEffects.Add(effect.GetType(), applyEffect); effect.Apply(this); } } } if (HitPoint <= 0) { SendMessage("Death"); _deathable.Death(); } Debug.Log(string.Format("{0} take damage {1}", this.name, damage.HitDamage)); }