PlayerDeath.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. using System.Collections;
  2. using UnityEngine;
  3. using Random = UnityEngine.Random;
  4. namespace HQFPSWeapons
  5. {
  6. public class PlayerDeath : PlayerComponent
  7. {
  8. [SerializeField]
  9. private GameObject m_Camera = null;
  10. [BHeader("Audio")]
  11. [SerializeField]
  12. private AudioSource m_AudioSource = null;
  13. [SerializeField]
  14. private SoundPlayer m_DeathAudio = null;
  15. [BHeader("Stuff To Disable On Death")]
  16. [SerializeField]
  17. private GameObject[] m_ObjectsToDisable = null;
  18. [SerializeField]
  19. private Behaviour[] m_BehavioursToDisable = null;
  20. [SerializeField]
  21. private Collider[] m_CollidersToDisable = null;
  22. [BHeader("Player Head Hitbox")]
  23. [SerializeField]
  24. private Collider m_HeadCollider = null;
  25. [SerializeField]
  26. private Rigidbody m_HeadRigidbody = null;
  27. [BHeader("Respawn")]
  28. [SerializeField]
  29. private float m_RespawnDuration = 5f;
  30. [SerializeField]
  31. private bool m_RestartSceneOnRespawn = false;
  32. private Transform m_CameraStartParent;
  33. private Quaternion m_CameraStartRotation;
  34. private Vector3 m_CameraStartPosition;
  35. //Hitbox
  36. private Vector3 m_HeadHitboxStartPosition;
  37. private Quaternion m_HeadHitboxStartRotation;
  38. public void OnLoad()
  39. {
  40. Player.Health.AddChangeListener(OnChanged_Health);
  41. }
  42. private void Awake()
  43. {
  44. Player.Health.AddChangeListener(OnChanged_Health);
  45. //Camera set up
  46. m_CameraStartRotation = m_Camera.transform.localRotation;
  47. m_CameraStartPosition = m_Camera.transform.localPosition;
  48. m_CameraStartParent = m_Camera.transform.parent;
  49. //Hitbox set up
  50. m_HeadHitboxStartPosition = m_HeadCollider.transform.localPosition;
  51. m_HeadHitboxStartRotation = m_HeadCollider.transform.localRotation;
  52. }
  53. private void OnChanged_Health(float health)
  54. {
  55. if(health == 0f)
  56. {
  57. On_Death();
  58. m_Camera.transform.parent = m_HeadCollider.transform;
  59. }
  60. }
  61. private void On_Death()
  62. {
  63. m_DeathAudio.Play(ItemSelection.Method.Random, m_AudioSource);
  64. //Disable
  65. foreach (var obj in m_ObjectsToDisable)
  66. {
  67. if (obj != null)
  68. obj.SetActive(false);
  69. else
  70. Debug.LogWarning("Check out PlayerDeath for missing references, an object reference was found null!", this);
  71. }
  72. foreach (var behaviour in m_BehavioursToDisable)
  73. {
  74. if (behaviour != null)
  75. behaviour.enabled = false;
  76. else
  77. Debug.LogWarning("Check out PlayerDeath for missing references, a behaviour reference was found null!", this);
  78. }
  79. foreach (var collider in m_CollidersToDisable)
  80. {
  81. if(collider != null)
  82. collider.enabled = false;
  83. else
  84. Debug.LogWarning("Check out PlayerDeath for missing references, a collider reference was found null!", this);
  85. }
  86. m_HeadCollider.transform.localPosition = m_HeadHitboxStartPosition;
  87. m_HeadCollider.transform.localRotation = m_HeadHitboxStartRotation;
  88. m_HeadCollider.isTrigger = false;
  89. m_HeadRigidbody.isKinematic = false;
  90. m_HeadRigidbody.AddForce(Player.Velocity.Get() * 0.5f, ForceMode.Force);
  91. m_HeadRigidbody.AddRelativeTorque(new Vector3(Random.value - 0.5f, Random.value - 0.5f, Random.value - 0.5f) * 35, ForceMode.Force);
  92. PostProcessingManager.Instance.DoDeathAnim();
  93. Player.Death.Send();
  94. StartCoroutine(C_Respawn());
  95. }
  96. private IEnumerator C_Respawn()
  97. {
  98. yield return new WaitForSeconds(m_RespawnDuration);
  99. if (m_RestartSceneOnRespawn)
  100. GameManager.Instance.StartGame();
  101. else
  102. {
  103. GameManager.Instance.SetPlayerPosition();
  104. m_Camera.transform.parent = m_CameraStartParent;
  105. m_Camera.transform.localRotation = m_CameraStartRotation;
  106. m_Camera.transform.localPosition = m_CameraStartPosition;
  107. m_HeadCollider.isTrigger = true;
  108. m_HeadRigidbody.isKinematic = true;
  109. PostProcessingManager.Instance.RestoreDefaultProfile();
  110. foreach (var obj in m_ObjectsToDisable)
  111. obj.SetActive(true);
  112. foreach (var behaviour in m_BehavioursToDisable)
  113. behaviour.enabled = true;
  114. foreach (var collider in m_CollidersToDisable)
  115. collider.enabled = true;
  116. Player.MoveInput.Set(Vector2.zero);
  117. Player.Health.Set(100f);
  118. Player.Stamina.Set(100f);
  119. Player.Respawn.Send();
  120. }
  121. }
  122. }
  123. }