EntityVitals.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using UnityEngine;
  2. namespace HQFPSWeapons
  3. {
  4. /// <summary>
  5. ///
  6. /// </summary>
  7. public class EntityVitals : GenericVitals
  8. {
  9. [BHeader("Fall Damage")]
  10. [SerializeField]
  11. [Range(1f, 15f)]
  12. [Tooltip("At which landing speed, the entity will start taking damage.")]
  13. private float m_MinFallSpeed = 4f;
  14. [SerializeField]
  15. [Range(10f, 50f)]
  16. [Tooltip("At which landing speed, the entity will die, if it has no defense.")]
  17. private float m_MaxFallSpeed = 15f;
  18. [BHeader("Audio")]
  19. [SerializeField]
  20. [Tooltip("The sounds that will be played when this entity receives damage.")]
  21. private SoundPlayer m_HurtAudio = null;
  22. [SerializeField]
  23. private float m_TimeBetweenScreams = 1f;
  24. [SerializeField]
  25. private SoundPlayer m_FallDamageAudio = null;
  26. [BHeader("Animation")]
  27. [SerializeField]
  28. private Animator m_Animator = null;
  29. [SerializeField]
  30. private float m_GetHitMax = 30f;
  31. private float m_NextTimeCanScream;
  32. private void Awake()
  33. {
  34. Entity.ChangeHealth.SetTryer(Try_ChangeHealth);
  35. Entity.FallImpact.AddListener(On_FallImpact);
  36. Entity.Health.AddChangeListener(OnChanged_Health);
  37. }
  38. private void OnChanged_Health(float health)
  39. {
  40. float delta = health - Entity.Health.GetPreviousValue();
  41. if(delta < 0f)
  42. {
  43. if (m_Animator != null)
  44. {
  45. m_Animator.SetFloat ("Get Hit Amount", Mathf.Abs (delta / m_GetHitMax));
  46. m_Animator.SetTrigger ("Get Hit");
  47. }
  48. if(Time.time > m_NextTimeCanScream)
  49. {
  50. m_HurtAudio.Play(ItemSelection.Method.RandomExcludeLast, m_AudioSource);
  51. m_NextTimeCanScream = Time.time + m_TimeBetweenScreams;
  52. }
  53. }
  54. }
  55. private void On_FallImpact(float impactSpeed)
  56. {
  57. if(impactSpeed >= m_MinFallSpeed)
  58. {
  59. Entity.ChangeHealth.Try(new HealthEventData(-100f * (impactSpeed / m_MaxFallSpeed)));
  60. m_FallDamageAudio.Play(ItemSelection.Method.Random, m_AudioSource);
  61. }
  62. }
  63. }
  64. }