Explosive.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using System.Collections;
  2. using System.Security;
  3. using UnityEngine;
  4. using UnityEngine.Events;
  5. namespace HQFPSWeapons
  6. {
  7. public class Explosive : Projectile
  8. {
  9. [SerializeField]
  10. public bool m_DetonateOnImpact = false;
  11. [ShowIf("m_DetonateOnImpact", true)]
  12. [SerializeField]
  13. [Range(0f, 15f)]
  14. private float m_DetonationDelay = 1.5f;
  15. [SerializeField]
  16. private GameObject m_ObjectToDisable = null;
  17. [SerializeField]
  18. private Explosion m_Explosion = null;
  19. [SerializeField]
  20. private LingeringFire m_LingeringFire = null;
  21. [Space]
  22. [SerializeField]
  23. private UnityEvent m_OnExplosiveLaunched = null;
  24. private LivingEntity m_Detonator;
  25. private bool m_IsDetonating;
  26. public override void Launch(LivingEntity launcher)
  27. {
  28. if(m_IsDetonating)
  29. return;
  30. m_IsDetonating = true;
  31. m_OnExplosiveLaunched.Invoke();
  32. if(!m_DetonateOnImpact)
  33. StartCoroutine(C_DetonateWithDelay(launcher));
  34. else
  35. m_Detonator = launcher;
  36. }
  37. private void OnCollisionEnter(Collision collision)
  38. {
  39. if (m_DetonateOnImpact && m_IsDetonating)
  40. StartCoroutine(C_DetonateWithDelay(m_Detonator));
  41. }
  42. private IEnumerator C_DetonateWithDelay(LivingEntity launcher)
  43. {
  44. m_DetonateOnImpact = false;
  45. if (m_ObjectToDisable != null)
  46. m_ObjectToDisable.SetActive(false);
  47. yield return new WaitForSeconds(m_DetonationDelay);
  48. if (m_Explosion != null)
  49. {
  50. m_Explosion.transform.SetParent(null, true);
  51. m_Explosion.gameObject.SetActive(true);
  52. m_Explosion.Explode(launcher);
  53. }
  54. if (m_LingeringFire != null)
  55. {
  56. m_LingeringFire.transform.SetParent(null, true);
  57. m_LingeringFire.transform.rotation = Quaternion.identity;
  58. m_LingeringFire.gameObject.SetActive(true);
  59. m_LingeringFire.StartFire();
  60. }
  61. Destroy(gameObject);
  62. }
  63. }
  64. }