SFX_PhysicsHomingMissileLauncher.cs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. // ReSharper disable once CheckNamespace
  5. namespace QFX.SFX
  6. {
  7. public class SFX_PhysicsHomingMissileLauncher : SFX_ControlledObject
  8. {
  9. public bool FollowTarget;
  10. public Transform LaunchTarget;
  11. public SFX_PhysicsHomingMissile Missile;
  12. public float MissileVelocity;
  13. public float MissileLaunchForce;
  14. public float MissileTurn;
  15. public int MissilesCount = 1;
  16. public float InstantiateMissileDelay;
  17. public float FocusOnTargetDelay;
  18. public float RandomSphereSize;
  19. private SFX_TargetMarker m_sfxTargetMarker;
  20. public override void Run()
  21. {
  22. base.Run();
  23. StartCoroutine("InstantiateProjectiles");
  24. }
  25. private void Awake()
  26. {
  27. m_sfxTargetMarker = GetComponent<SFX_TargetMarker>();
  28. }
  29. private IEnumerator InstantiateProjectiles()
  30. {
  31. int targetCounts = 0;
  32. int targetIdx = 0;
  33. var targetsGameObjects = new List<GameObject>();
  34. var targetsPositions = new List<Vector3>();
  35. if (m_sfxTargetMarker != null)
  36. {
  37. if (m_sfxTargetMarker.MarkTargetMode == SFX_TargetMarker.MarkMode.GameObject)
  38. {
  39. targetsGameObjects = m_sfxTargetMarker.MarkedGameObjects;
  40. targetCounts = targetsGameObjects.Count;
  41. }
  42. else
  43. {
  44. targetsPositions = m_sfxTargetMarker.MarkedPositions;
  45. targetCounts = targetsPositions.Count;
  46. }
  47. }
  48. for (int i = 0; i < MissilesCount; i++)
  49. {
  50. var randomPosition = LaunchTarget.position + Random.insideUnitSphere * RandomSphereSize;
  51. var homingMissile = Instantiate(Missile, transform.position, transform.rotation);
  52. homingMissile.FollowTarget = FollowTarget;
  53. if (m_sfxTargetMarker != null)
  54. {
  55. if (targetCounts > 0)
  56. {
  57. if (targetIdx >= targetCounts)
  58. targetIdx = 0;
  59. if (m_sfxTargetMarker.MarkTargetMode == SFX_TargetMarker.MarkMode.GameObject)
  60. homingMissile.TargetTransform = targetsGameObjects[targetIdx++].transform;
  61. else homingMissile.TargetPosition = targetsPositions[targetIdx++];
  62. }
  63. }
  64. homingMissile.FocusOnTargetDelay = FocusOnTargetDelay;
  65. homingMissile.LaunchPosition = randomPosition;
  66. homingMissile.Velocity = MissileVelocity;
  67. homingMissile.Turn = MissileTurn;
  68. homingMissile.LaunchForce = MissileLaunchForce;
  69. homingMissile.Run();
  70. yield return new WaitForSeconds(InstantiateMissileDelay);
  71. }
  72. }
  73. }
  74. }