SFX_SimpleProjectileWeapon.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using System.Collections;
  2. using UnityEngine;
  3. using UnityEngine.Serialization;
  4. // ReSharper disable once CheckNamespace
  5. namespace QFX.SFX
  6. {
  7. public sealed class SFX_SimpleProjectileWeapon : SFX_ControlledObject
  8. {
  9. public GameObject LaunchParticleSystem;
  10. public Transform LaunchTransform;
  11. public SFX_LightAnimator LightAnimator;
  12. public GameObject Projectile;
  13. public float FireRate = 0.5f;
  14. private bool _isFireAllowed = true;
  15. private ParticleSystem _launchPs;
  16. public override void Setup()
  17. {
  18. base.Setup();
  19. var launchGo = Instantiate(LaunchParticleSystem, transform, true);
  20. launchGo.transform.rotation = LaunchTransform.rotation;
  21. _launchPs = launchGo.GetComponent<ParticleSystem>();
  22. }
  23. public override void Run()
  24. {
  25. base.Run();
  26. LightAnimator.Run();
  27. _launchPs.transform.position = LaunchTransform.position;
  28. }
  29. public override void Stop()
  30. {
  31. base.Stop();
  32. LightAnimator.Stop();
  33. }
  34. private void Update()
  35. {
  36. if (!IsRunning)
  37. return;
  38. if (_isFireAllowed)
  39. StartCoroutine("Fire");
  40. }
  41. private IEnumerator Fire()
  42. {
  43. _isFireAllowed = false;
  44. _launchPs.Play(true);
  45. Vector3 position;
  46. Quaternion rotation;
  47. if (LaunchTransform != null)
  48. {
  49. position = LaunchTransform.position;
  50. rotation = LaunchTransform.rotation;
  51. }
  52. else
  53. {
  54. position = transform.position;
  55. rotation = transform.rotation;
  56. }
  57. var go = Instantiate(Projectile, position, rotation);
  58. var emitterKeeper = go.GetComponent<SFX_IEmitterKeeper>();
  59. if (emitterKeeper != null)
  60. emitterKeeper.EmitterTransform = transform;
  61. LightAnimator.Run();
  62. yield return new WaitForSeconds(FireRate);
  63. _isFireAllowed = true;
  64. }
  65. }
  66. }