Advertisement
Guest User

ParticleTrigger

a guest
Jan 25th, 2020
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.08 KB | None | 0 0
  1. ///-----------------------------------------------------------------
  2. ///   Author : Sebastien RAYMONDAUD                    
  3. ///   Date   : 28/11/2019 10:38
  4. ///-----------------------------------------------------------------
  5.  
  6. using Convoy.Interfaces;
  7. using Fr.RaymondaudSebastien.PoolingObject;
  8. using System.Collections;
  9. using System.Collections.Generic;
  10. using UnityEngine;
  11.  
  12. namespace Convoy.Juiciness
  13. {
  14.     public class Particle
  15.     {
  16.         private float elapsedTime = 0;
  17.         public float duration = 0;
  18.         public PoolParticle particle = null;
  19.         public bool onPlay = false;
  20.         public Transform parent = null;
  21.         public System.Action<Particle, bool> callBackRemove = null;
  22.         private bool destroy = false;
  23.  
  24.         public Particle(string type, Transform parent, bool destroy, Vector3 position, System.Action<Particle, bool> callBackRemove)
  25.         {
  26.             this.parent = parent;
  27.             this.callBackRemove = callBackRemove;
  28.             this.destroy = destroy;
  29.  
  30.             particle = (PoolParticle)PoolManager.GetFromPool(type);
  31.             particle.transform.parent = null;
  32.             particle.transform.position = position;
  33.             particle.transform.localScale = Vector3.one;
  34.             particle.particle.Play();
  35.  
  36.             duration = particle.particle.main.duration;
  37.             elapsedTime = 0;
  38.             onPlay = true;
  39.         }
  40.  
  41.         public void ManualUpdate()
  42.         {
  43.             if (!onPlay)
  44.                 return;
  45.  
  46.             elapsedTime += Time.deltaTime;
  47.             if (elapsedTime >= duration)
  48.             {
  49.                 particle.transform.SetParent(parent, false);
  50.                 particle.transform.localPosition = Vector3.zero;
  51.                 onPlay = false;
  52.                 elapsedTime = 0;
  53.  
  54.                 particle.Dispose();
  55.                 callBackRemove?.Invoke(this, destroy);
  56.             }
  57.         }
  58.     }
  59.  
  60.     public class ParticleTrigger : MonoBehaviour
  61.     {
  62.         #region PROPERTIES
  63.         [RequireInterface(typeof(IParticleEmitter))] [SerializeField] protected Object m_particleEmitter = null;
  64.         protected IParticleEmitter _particleEmitter = null;
  65.         [SerializeField] private PoolParticle m_poolParticle = null;
  66.         private List<Particle> particuleList = new List<Particle>();
  67.        
  68.         #endregion
  69.  
  70.         virtual protected void Start()
  71.         {
  72.             _particleEmitter = m_particleEmitter as IParticleEmitter;
  73.             _particleEmitter.OnPlayParticle += Emitter_PlayParticle;
  74.             _particleEmitter.OnPlayParticleWithCallback += Emitter_PlayParticleWithCallback;
  75.         }
  76.  
  77.         virtual protected void Emitter_PlayParticle(bool destroy, Vector3 position)
  78.         {
  79.             particuleList.Add(new Particle(m_poolParticle.TypePool, transform, destroy, position, RemoveParticleInList));
  80.         }
  81.  
  82.         virtual protected void Emitter_PlayParticleWithCallback(bool destroy, Vector3 position, System.Action callback)
  83.         {
  84.             particuleList.Add(new Particle(m_poolParticle.TypePool, transform, destroy, position,
  85.                                             (Particle p, bool d) => { callback?.Invoke();
  86.                                                                             RemoveParticleInList(p, d);
  87.                                                                           }));
  88.         }
  89.  
  90.         protected void RemoveParticleInList(Particle p, bool destroy)
  91.         {
  92.             particuleList.Remove(p);
  93.             //if (destroy)
  94.                 //Destroy(this);
  95.         }
  96.  
  97.         virtual protected void Update()
  98.         {
  99.             for (int i = particuleList.Count - 1; i >= 0; i--)
  100.             {
  101.                 particuleList[i].ManualUpdate();
  102.             }
  103.         }
  104.  
  105.         virtual protected void OnDestroy()
  106.         {
  107.             if (_particleEmitter != null)
  108.             {
  109.                 _particleEmitter.OnPlayParticle -= Emitter_PlayParticle;
  110.                 _particleEmitter.OnPlayParticleWithCallback -= Emitter_PlayParticleWithCallback;
  111.             }
  112.         }
  113.     }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement