WaterBarrel.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. namespace HQFPSWeapons
  4. {
  5. public class WaterBarrel : MonoBehaviour, IDamageable
  6. {
  7. [SerializeField]
  8. private AudioSource m_AudioSource = null;
  9. [SerializeField]
  10. private Rigidbody m_Rigidbody = null;
  11. [SerializeField]
  12. private float m_MassWithoutWater = 15f;
  13. [SerializeField]
  14. private ParticleSystem m_WaterFX = null;
  15. [SerializeField]
  16. private float m_WaterQuantity = 10f;
  17. [SerializeField]
  18. private float m_DefaultStartSpeed = 2f;
  19. private List<ParticleSystem> m_WaterLeaks = new List<ParticleSystem>();
  20. private float m_CurrentWaterQuantity;
  21. private float m_InitialMass;
  22. public void TakeDamage(HealthEventData damage)
  23. {
  24. if(m_CurrentWaterQuantity > 0f && damage.HitPoint != Vector3.zero && damage.HitDirection != Vector3.zero)
  25. {
  26. ParticleSystem waterLeak = (ParticleSystem)Instantiate(m_WaterFX, damage.HitPoint, Quaternion.LookRotation(damage.HitNormal == Vector3.zero ? -damage.HitDirection : damage.HitNormal));
  27. waterLeak.transform.SetParent(transform);
  28. m_WaterLeaks.Add(waterLeak);
  29. if(m_WaterLeaks.Count == 1)
  30. m_AudioSource.Play();
  31. }
  32. }
  33. public LivingEntity GetEntity()
  34. {
  35. return null;
  36. }
  37. private void Start()
  38. {
  39. m_CurrentWaterQuantity = m_WaterQuantity;
  40. m_InitialMass = m_Rigidbody.mass;
  41. }
  42. private void Update()
  43. {
  44. if(m_CurrentWaterQuantity <= 0 || m_WaterLeaks.Count == 0)
  45. return;
  46. m_CurrentWaterQuantity -= Time.deltaTime * m_WaterLeaks.Count;
  47. for(int i = 0;i < m_WaterLeaks.Count;i ++)
  48. {
  49. var currentSpeed = Mathf.Clamp(m_DefaultStartSpeed * (m_CurrentWaterQuantity / m_WaterQuantity), 0f, m_DefaultStartSpeed);
  50. var main = m_WaterLeaks[i].main;
  51. main.startSpeedMultiplier = currentSpeed;
  52. if(currentSpeed <= 0f)
  53. {
  54. m_WaterLeaks[i].Stop();
  55. m_AudioSource.Stop();
  56. }
  57. }
  58. m_Rigidbody.mass = m_MassWithoutWater + (m_InitialMass - m_MassWithoutWater) * (m_CurrentWaterQuantity / m_WaterQuantity);
  59. }
  60. }
  61. }