| | 1 | | using System.Collections; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using UnityEngine; |
| | 4 | | using System.Linq; |
| | 5 | |
|
| | 6 | | public class ParticleManager : MonoBehaviour { |
| 0 | 7 | | public static ParticleManager Instance { get; private set; } |
| | 8 | |
|
| | 9 | | [SerializeField] |
| | 10 | | private Queue<GameObject> _missileTrailPool; |
| | 11 | | [SerializeField] |
| | 12 | | private Queue<GameObject> _missileExplosionPool; |
| | 13 | |
|
| 0 | 14 | | private List<TrailRenderer> _agentTrailRenderers = new List<TrailRenderer>(); |
| | 15 | |
|
| | 16 | | [SerializeField, Tooltip("The material to use for commandeered interceptor trail renderers")] |
| | 17 | | public Material interceptorTrailMatFaded; |
| | 18 | | [SerializeField, Tooltip("The material to use for commandeered threat trail renderers")] |
| | 19 | | public Material threatTrailMatFaded; |
| | 20 | |
|
| 0 | 21 | | private void Awake() { |
| 0 | 22 | | if (Instance == null) { |
| 0 | 23 | | Instance = this; |
| 0 | 24 | | DontDestroyOnLoad(gameObject); |
| 0 | 25 | | } else { |
| 0 | 26 | | Destroy(gameObject); |
| 0 | 27 | | } |
| 0 | 28 | | } |
| | 29 | |
|
| 0 | 30 | | private void Start() { |
| 0 | 31 | | _missileTrailPool = new Queue<GameObject>(); |
| 0 | 32 | | _missileExplosionPool = new Queue<GameObject>(); |
| | 33 | |
|
| 0 | 34 | | if (SimManager.Instance.simulatorConfig.enableMissileTrailEffect) { |
| 0 | 35 | | InitializeMissileTrailParticlePool(); |
| 0 | 36 | | } |
| 0 | 37 | | if (SimManager.Instance.simulatorConfig.enableExplosionEffect) { |
| 0 | 38 | | InitializeMissileExplosionParticlePool(); |
| 0 | 39 | | } |
| | 40 | |
|
| | 41 | | // Subscribe to events! |
| 0 | 42 | | SimManager.Instance.OnNewInterceptor += RegisterNewInterceptor; |
| 0 | 43 | | SimManager.Instance.OnNewThreat += RegisterNewThreat; |
| | 44 | |
|
| 0 | 45 | | SimManager.Instance.OnSimulationEnded += RegisterSimulationEnded; |
| 0 | 46 | | } |
| | 47 | |
|
| 0 | 48 | | private void InitializeMissileTrailParticlePool() { |
| | 49 | | // Grab from Resources/Prefabs/Effects |
| 0 | 50 | | GameObject missileTrailPrefab = |
| | 51 | | Resources.Load<GameObject>("Prefabs/Effects/InterceptorSmokeEffect"); |
| | 52 | |
|
| | 53 | | // Pre-instantiate 10 missile trail particles |
| 0 | 54 | | for (int i = 0; i < 10; i++) { |
| 0 | 55 | | InstantiateMissileTrail(missileTrailPrefab); |
| 0 | 56 | | } |
| | 57 | | // Instantiate over an interval |
| 0 | 58 | | StartCoroutine(InstantiateMissileTrailsOverTime(missileTrailPrefab, 200, 0.05f)); |
| 0 | 59 | | } |
| | 60 | |
|
| 0 | 61 | | private void InitializeMissileExplosionParticlePool() { |
| 0 | 62 | | GameObject missileExplosionPrefab = |
| | 63 | | Resources.Load<GameObject>("Prefabs/Effects/InterceptExplosionEffect"); |
| | 64 | | // Pre-instantiate 10 missile trail particles |
| 0 | 65 | | for (int i = 0; i < 10; i++) { |
| 0 | 66 | | InstantiateMissileExplosion(missileExplosionPrefab); |
| 0 | 67 | | } |
| 0 | 68 | | StartCoroutine(InstantiateMissileExplosionsOverTime(missileExplosionPrefab, 200, 0.05f)); |
| 0 | 69 | | } |
| | 70 | |
|
| 0 | 71 | | private void RegisterSimulationEnded() { |
| 0 | 72 | | foreach (var trailRenderer in _agentTrailRenderers) { |
| 0 | 73 | | Destroy(trailRenderer.gameObject); |
| 0 | 74 | | } |
| 0 | 75 | | _agentTrailRenderers.Clear(); |
| 0 | 76 | | } |
| | 77 | |
|
| 0 | 78 | | private void RegisterNewInterceptor(Interceptor interceptor) { |
| 0 | 79 | | interceptor.OnInterceptHit += RegisterInterceptorHit; |
| 0 | 80 | | interceptor.OnInterceptMiss += RegisterInterceptorMiss; |
| 0 | 81 | | } |
| | 82 | |
|
| 0 | 83 | | private void RegisterNewThreat(Threat threat) { |
| 0 | 84 | | threat.OnThreatHit += RegisterThreatHit; |
| 0 | 85 | | threat.OnThreatMiss += RegisterThreatMiss; |
| 0 | 86 | | } |
| | 87 | |
|
| 0 | 88 | | private void RegisterInterceptorHit(Interceptor interceptor, Threat threat) { |
| 0 | 89 | | if (SimManager.Instance.simulatorConfig.enableExplosionEffect) { |
| 0 | 90 | | PlayMissileExplosion(interceptor.transform.position); |
| 0 | 91 | | } |
| 0 | 92 | | if (SimManager.Instance.simulatorConfig.persistentFlightTrails) { |
| 0 | 93 | | CommandeerAgentTrailRenderer(interceptor); |
| 0 | 94 | | CommandeerAgentTrailRenderer(threat); |
| 0 | 95 | | } |
| 0 | 96 | | } |
| | 97 | |
|
| 0 | 98 | | private void RegisterInterceptorMiss(Interceptor interceptor, Threat threat) { |
| 0 | 99 | | if (SimManager.Instance.simulatorConfig.persistentFlightTrails) { |
| 0 | 100 | | CommandeerAgentTrailRenderer(interceptor); |
| 0 | 101 | | } |
| 0 | 102 | | } |
| | 103 | |
|
| 0 | 104 | | private void RegisterThreatHit(Threat threat) { |
| 0 | 105 | | if (SimManager.Instance.simulatorConfig.persistentFlightTrails) { |
| 0 | 106 | | CommandeerAgentTrailRenderer(threat); |
| 0 | 107 | | } |
| 0 | 108 | | } |
| | 109 | |
|
| 0 | 110 | | private void RegisterThreatMiss(Threat threat) { |
| 0 | 111 | | if (SimManager.Instance.simulatorConfig.persistentFlightTrails) { |
| 0 | 112 | | CommandeerAgentTrailRenderer(threat); |
| 0 | 113 | | } |
| 0 | 114 | | } |
| | 115 | |
|
| 0 | 116 | | private void CommandeerAgentTrailRenderer(Agent agent) { |
| | 117 | | // Take the TrailRenderer component off of the agent, onto us, and store it |
| | 118 | | // (so we can destroy it later on simulation end) |
| 0 | 119 | | TrailRenderer trailRenderer = agent.GetComponentInChildren<TrailRenderer>(); |
| 0 | 120 | | if (trailRenderer != null) { |
| 0 | 121 | | trailRenderer.transform.parent = transform; |
| 0 | 122 | | _agentTrailRenderers.Add(trailRenderer); |
| 0 | 123 | | trailRenderer.material = (agent is Threat) ? threatTrailMatFaded : interceptorTrailMatFaded; |
| 0 | 124 | | } else { |
| 0 | 125 | | Debug.LogWarning("Agent has no TrailRenderer component"); |
| 0 | 126 | | } |
| 0 | 127 | | } |
| | 128 | |
|
| | 129 | | /// <summary> |
| | 130 | | /// Returns a missile explosion particle prefab from the pool and plays it at the specified |
| | 131 | | /// location. If the pool is empty, it returns null. |
| | 132 | | /// </summary> |
| 0 | 133 | | public GameObject PlayMissileExplosion(Vector3 position) { |
| 0 | 134 | | if (_missileExplosionPool.Count > 0) { |
| 0 | 135 | | GameObject explosion = _missileExplosionPool.Dequeue(); |
| 0 | 136 | | explosion.transform.position = position; |
| | 137 | |
|
| 0 | 138 | | ParticleSystem particleSystem = explosion.GetComponent<ParticleSystem>(); |
| 0 | 139 | | if (particleSystem != null) { |
| 0 | 140 | | particleSystem.Clear(); |
| 0 | 141 | | particleSystem.Play(); |
| 0 | 142 | | StartCoroutine(ReturnExplosionAfterDelay(explosion, particleSystem.main.duration)); |
| 0 | 143 | | } else { |
| 0 | 144 | | Debug.LogError("Missile explosion particle has no ParticleSystem component"); |
| 0 | 145 | | _missileExplosionPool.Enqueue(explosion); |
| 0 | 146 | | return null; |
| | 147 | | } |
| | 148 | |
|
| 0 | 149 | | return explosion; |
| | 150 | | } |
| 0 | 151 | | return null; |
| 0 | 152 | | } |
| | 153 | |
|
| 0 | 154 | | private IEnumerator ReturnExplosionAfterDelay(GameObject explosion, float delay) { |
| 0 | 155 | | yield return new WaitForSeconds(delay); |
| 0 | 156 | | ReturnMissileExplosionParticle(explosion); |
| 0 | 157 | | } |
| | 158 | |
|
| 0 | 159 | | private void ReturnMissileExplosionParticle(GameObject explosion) { |
| 0 | 160 | | if (explosion == null) { |
| 0 | 161 | | Debug.LogError("Attempted to return a null missile explosion particle"); |
| 0 | 162 | | return; |
| | 163 | | } |
| | 164 | |
|
| 0 | 165 | | explosion.transform.parent = transform; |
| 0 | 166 | | explosion.transform.localPosition = Vector3.zero; |
| 0 | 167 | | ParticleSystem particleSystem = explosion.GetComponent<ParticleSystem>(); |
| 0 | 168 | | if (particleSystem != null) { |
| 0 | 169 | | particleSystem.Stop(); |
| 0 | 170 | | particleSystem.Clear(); |
| 0 | 171 | | } else { |
| 0 | 172 | | Debug.LogError("Attempted to return a missile explosion particle with no particle system"); |
| 0 | 173 | | } |
| | 174 | |
|
| 0 | 175 | | _missileExplosionPool.Enqueue(explosion); |
| 0 | 176 | | } |
| | 177 | |
|
| 0 | 178 | | private GameObject InstantiateMissileTrail(GameObject prefab) { |
| 0 | 179 | | GameObject trail = Instantiate(prefab, transform); |
| 0 | 180 | | trail.GetComponent<ParticleSystem>().Stop(); |
| 0 | 181 | | _missileTrailPool.Enqueue(trail); |
| 0 | 182 | | return trail; |
| 0 | 183 | | } |
| | 184 | |
|
| 0 | 185 | | private GameObject InstantiateMissileExplosion(GameObject prefab) { |
| 0 | 186 | | GameObject explosion = Instantiate(prefab, transform); |
| 0 | 187 | | explosion.GetComponent<ParticleSystem>().Stop(); |
| 0 | 188 | | _missileExplosionPool.Enqueue(explosion); |
| 0 | 189 | | return explosion; |
| 0 | 190 | | } |
| | 191 | |
|
| | 192 | | private IEnumerator InstantiateMissileTrailsOverTime(GameObject prefab, int count, |
| 0 | 193 | | float duration) { |
| 0 | 194 | | float interval = duration / count; |
| 0 | 195 | | for (int i = 0; i < count; i++) { |
| 0 | 196 | | InstantiateMissileTrail(prefab); |
| 0 | 197 | | yield return new WaitForSeconds(interval); |
| 0 | 198 | | } |
| 0 | 199 | | } |
| | 200 | |
|
| | 201 | | private IEnumerator InstantiateMissileExplosionsOverTime(GameObject prefab, int count, |
| 0 | 202 | | float duration) { |
| 0 | 203 | | float interval = duration / count; |
| 0 | 204 | | for (int i = 0; i < count; i++) { |
| 0 | 205 | | InstantiateMissileExplosion(prefab); |
| 0 | 206 | | yield return new WaitForSeconds(interval); |
| 0 | 207 | | } |
| 0 | 208 | | } |
| | 209 | |
|
| | 210 | | /// <summary> |
| | 211 | | /// Returns a missile trail particle prefab from the pool. |
| | 212 | | /// If the pool is empty, it returns null |
| | 213 | | /// </summary> |
| | 214 | | /// <returns></returns> |
| 0 | 215 | | public GameObject RequestMissileTrailParticle() { |
| 0 | 216 | | if (_missileTrailPool.Count > 0 && |
| 0 | 217 | | SimManager.Instance.simulatorConfig.enableMissileTrailEffect) { |
| 0 | 218 | | GameObject trail = _missileTrailPool.Dequeue(); |
| | 219 | |
|
| 0 | 220 | | return trail; |
| | 221 | | } |
| 0 | 222 | | return null; |
| 0 | 223 | | } |
| | 224 | |
|
| 0 | 225 | | public void ReturnMissileTrailParticle(GameObject trail) { |
| 0 | 226 | | if (trail == null) { |
| 0 | 227 | | Debug.LogError("Attempted to return a null missile trail particle"); |
| 0 | 228 | | return; |
| | 229 | | } |
| | 230 | |
|
| 0 | 231 | | trail.transform.parent = transform; |
| 0 | 232 | | trail.transform.localPosition = Vector3.zero; |
| 0 | 233 | | ParticleSystem particleSystem = trail.GetComponent<ParticleSystem>(); |
| 0 | 234 | | if (particleSystem != null) { |
| 0 | 235 | | particleSystem.Stop(); |
| 0 | 236 | | particleSystem.Clear(); |
| 0 | 237 | | var emission = particleSystem.emission; |
| 0 | 238 | | emission.enabled = true; |
| 0 | 239 | | } else { |
| 0 | 240 | | Debug.LogError("Attempted to return a missile trail particle with no particle system"); |
| 0 | 241 | | } |
| | 242 | |
|
| 0 | 243 | | _missileTrailPool.Enqueue(trail); |
| 0 | 244 | | } |
| | 245 | | } |