/// <summary> /// Take damage from an attacker /// </summary> /// <param name="damageValue">Approximate damage value to apply to enemy health</param> /// <param name="timeDelta">Time since last damage calculation</param> public virtual void TakeDamage(float damageValue, float timeDelta = 1f) { // Ignore attacks if already dead or invincible if (isDead || invincible) { return; } Anim.SetTrigger("TakeDamage"); if (takeDamageVocalSFX != null) { if (Mathf.Approximately(timeDelta, 1f) || !VocalAudio.isPlaying) { StartCoroutine(AudioHelper.PlayAudioOverlap(VocalAudio, takeDamageVocalSFX)); } } // Calculate any changes based on stats and modifiers here first float hitValue = (damageValue - ComputeDefenseModifier()) * timeDelta; health.Subtract(hitValue < 0 ? 0 : hitValue); if (Mathf.Approximately(health.CurrentValue, 0f)) { Die(); } }
public void Subtract_ShouldSetCurrentValueProperly() { float val = instance.maxValue / 2; instance.Init(); instance.Subtract(val); Assert.AreEqual(instance.maxValue - val, instance.CurrentValue); // Reached minimum value instance.Subtract(val); Assert.AreEqual(instance.minValue, instance.CurrentValue); // Stays at minimum value instance.Subtract(val); Assert.AreEqual(instance.minValue, instance.CurrentValue); }