using System;
using System.Collections.Generic;
using Core.Utilities;
using UnityEngine;
namespace TowerDefense.Effects
{
///
/// Simple effect support script to reset trails and particles on enable, and also
/// stops and starts reused emitters (to prevent them emitting when moving after being repooled)
///
public class PoolableEffect : Poolable
{
protected List m_Systems;
protected List m_Trails;
bool m_EffectsEnabled;
// TEST CODE TO DEBUG:
protected static int ID_START = 0;
protected int mGuid = 0;
///
/// Stop emitting all particles
///
public void StopAll()
{
foreach (var particleSystem in m_Systems)
{
particleSystem.Stop();
}
}
///
/// Turn off all known systems
///
public void TurnOffAllSystems()
{
if (!m_EffectsEnabled)
{
return;
}
// Reset all systems and trails
foreach (var particleSystem in m_Systems)
{
particleSystem.Clear();
var emission = particleSystem.emission;
emission.enabled = false;
}
foreach (var trailRenderer in m_Trails)
{
int tid = mGuid;
try
{
trailRenderer.Clear();
trailRenderer.enabled = false;
}catch( SystemException e)
{
Debug.Log("SystemExcept:" + e.ToString());
}
}
m_EffectsEnabled = false;
}
///
/// Turn on all known systems
///
public void TurnOnAllSystems()
{
if (m_EffectsEnabled)
{
return;
}
// Re-enable all systems and trails
foreach (var particleSystem in m_Systems)
{
particleSystem.Clear();
var emission = particleSystem.emission;
emission.enabled = true;
}
foreach (var trailRenderer in m_Trails)
{
trailRenderer.Clear();
trailRenderer.enabled = true;
}
m_EffectsEnabled = true;
}
protected override void Repool()
{
base.Repool();
TurnOffAllSystems();
}
protected virtual void Awake()
{
m_EffectsEnabled = true;
mGuid = ID_START++;
// Cache systems and trails, but only active and emitting ones
m_Systems = new List();
m_Trails = new List();
foreach (var system in GetComponentsInChildren())
{
if (system.emission.enabled && system.gameObject.activeSelf)
{
m_Systems.Add(system);
}
}
foreach (var trail in GetComponentsInChildren())
{
if (trail.enabled && trail.gameObject.activeSelf)
{
m_Trails.Add(trail);
}
}
TurnOffAllSystems();
}
}
}