EntityDeathHandler.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. using System.Collections;
  2. using UnityEngine;
  3. namespace HQFPSWeapons
  4. {
  5. public class EntityDeathHandler : LivingEntityComponent
  6. {
  7. [Header("Audio")]
  8. [SerializeField]
  9. private AudioSource m_AudioSource = null;
  10. [SerializeField]
  11. private SoundPlayer m_DeathAudio = null;
  12. [Header("Stuff To Disable On Death")]
  13. [SerializeField]
  14. private GameObject[] m_ObjectsToDisable = null;
  15. [SerializeField]
  16. private Behaviour[] m_BehavioursToDisable = null;
  17. [SerializeField]
  18. private Collider[] m_CollidersToDisable = null;
  19. [Header("Death Animation")]
  20. [SerializeField]
  21. [Tooltip("On death, you can either have a ragdoll, or an animation to play.")]
  22. private bool m_EnableDeathAnim = false;
  23. [SerializeField]
  24. private Animator m_Animator = null;
  25. [Header("Destroy Timer")]
  26. [SerializeField]
  27. [Clamp(0f, 1000f)]
  28. [Tooltip("")]
  29. private float m_DestroyTimer = 0f;
  30. private void Awake()
  31. {
  32. Entity.Health.AddChangeListener(OnChanged_Health);
  33. }
  34. private void OnChanged_Health(float health)
  35. {
  36. if(health == 0f)
  37. On_Death();
  38. }
  39. private void On_Death()
  40. {
  41. m_DeathAudio.Play(ItemSelection.Method.Random, m_AudioSource);
  42. if(m_EnableDeathAnim && m_Animator)
  43. m_Animator.SetTrigger("Die");
  44. foreach(var obj in m_ObjectsToDisable)
  45. obj.SetActive(false);
  46. foreach(var behaviour in m_BehavioursToDisable)
  47. {
  48. var animator = behaviour as Animator;
  49. if(animator != null)
  50. Destroy(animator);
  51. else
  52. behaviour.enabled = false;
  53. }
  54. foreach(var collider in m_CollidersToDisable)
  55. collider.enabled = false;
  56. Destroy(gameObject, m_DestroyTimer);
  57. Entity.Death.Send();
  58. }
  59. }
  60. }