LingeringFire.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace HQFPSWeapons
  5. {
  6. public class LingeringFire : MonoBehaviour
  7. {
  8. [SerializeField]
  9. private LightEffect m_LightEffect = null;
  10. [SerializeField]
  11. [Clamp(0f, 1000f)]
  12. private float m_DamagePerSecond = 20f;
  13. [SerializeField]
  14. [Range(0f, 10f)]
  15. private float m_LingeringTime = 5f;
  16. [SerializeField]
  17. private float m_StopDuration = 1f;
  18. [SerializeField]
  19. private LayerMask m_LayerMask = new LayerMask();
  20. private ParticleSystem m_FireParticles;
  21. private AudioSource m_AudioS;
  22. private bool m_CanDamage = true;
  23. private List<LivingEntity> m_AffectedEntities = new List<LivingEntity>();
  24. private float m_DamageMultiplier = 1f;
  25. public void StartFire()
  26. {
  27. m_AudioS = GetComponent<AudioSource>();
  28. m_FireParticles = GetComponent<ParticleSystem>();
  29. m_FireParticles.Stop();
  30. var main = m_FireParticles.main;
  31. main.duration = m_LingeringTime;
  32. m_FireParticles.Play();
  33. m_LightEffect.Play(true);
  34. PositionFire();
  35. StartCoroutine(C_FireLingering());
  36. }
  37. private void OnTriggerEnter(Collider other)
  38. {
  39. if (other.GetComponent<LivingEntity>() != null)
  40. m_AffectedEntities.Add(other.GetComponent<LivingEntity>());
  41. }
  42. private void OnTriggerExit(Collider other)
  43. {
  44. if (other.GetComponent<LivingEntity>() != null)
  45. m_AffectedEntities.Remove(other.GetComponent<LivingEntity>());
  46. }
  47. private void OnTriggerStay(Collider other)
  48. {
  49. Fire();
  50. }
  51. private void PositionFire()
  52. {
  53. RaycastHit hit;
  54. if (Physics.Raycast(transform.position, Vector3.down, out hit, 10f, m_LayerMask))
  55. {
  56. transform.position = hit.point;
  57. }
  58. }
  59. private void Fire()
  60. {
  61. if (!m_CanDamage)
  62. return;
  63. foreach (var entity in m_AffectedEntities)
  64. {
  65. if (entity != null)
  66. entity.ChangeHealth.Try(new HealthEventData(-m_DamagePerSecond * m_DamageMultiplier * Time.deltaTime));
  67. }
  68. }
  69. IEnumerator C_FireLingering()
  70. {
  71. yield return new WaitForSeconds(m_LingeringTime);
  72. m_FireParticles.Stop();
  73. m_LightEffect.Stop(true);
  74. float stopDuration = Time.time + m_StopDuration;
  75. WaitForEndOfFrame wait = new WaitForEndOfFrame();
  76. while (stopDuration > Time.time)
  77. {
  78. m_AudioS.volume -= Time.deltaTime * ( 1 / m_StopDuration);
  79. m_DamageMultiplier -= Time.deltaTime * (1 / m_StopDuration);
  80. yield return wait;
  81. }
  82. m_CanDamage = false;
  83. }
  84. }
  85. }