using UnityEngine; namespace Watermelon { [System.Serializable] public class Particle { // The name of the particle, used for registration and identification purposes. [SerializeField] string particleName; // The prefab that contains the particle effect (e.g., GameObject with ParticleSystem). [SerializeField] GameObject particlePrefab; /// /// The name of the particle (used for registration and identification) /// public string ParticleName => particleName; /// /// The prefab that contains the particle effect /// public GameObject ParticlePrefab => particlePrefab; /// /// Boolean flag indicating if the particle has a special behavior attached (like a custom script). /// This property is automatically determined when the particle is initialized. /// public bool SpecialBehaviour { get; private set; } /// /// Pool for managing instances of this particle (for object pooling) /// public Pool ParticlePool { get; private set; } [System.NonSerialized] private bool isInitialized; public Particle(string particleName, GameObject particlePrefab) { this.particleName = particleName; this.particlePrefab = particlePrefab; // Validation: check if name isn't empty if (string.IsNullOrEmpty(particleName)) Debug.LogError("[Particles]: Particle name can't be empty!"); // Validation: check if prefab is exists if (particlePrefab == null) Debug.LogError($"[Particles]: Prefab isn't linked for {particleName} particle"); } /// /// Initializes the particle, setting up object pooling and checking for special behaviors. /// public void Init() { // Validation: check if name isn't empty if (string.IsNullOrEmpty(particleName)) { Debug.LogError("[Particles]: Particle name can't be empty!"); return; } // Validation: check if prefab is exists if (particlePrefab == null) { Debug.LogError($"[Particles]: Prefab isn't linked for {particleName} particle"); return; } // If the particle is already initialized, exit early to avoid redundant setup. if (isInitialized) return; // Mark the particle as initialized to prevent repeated setup. isInitialized = true; // Create a pool for this particle prefab to efficiently reuse instances. ParticlePool = new Pool(particlePrefab, $"Particle_{ParticleName}"); // Check if the particle prefab contains a custom behavior script (ParticleBehaviour). SpecialBehaviour = particlePrefab.GetComponent(); // Check if prefab contains ParticleSystem component if (particlePrefab.GetComponent() == null) { Debug.LogError($"[Particles]: Particle ({particleName}) prefab doesn't contain a ParticleSystem component!", particlePrefab); } } public void Destroy() { isInitialized = false; PoolManager.DestroyPool(ParticlePool); ParticlePool = null; } /// /// Plays the particle effect through the ParticlesController, optionally applying a delay. /// public ParticleCase Play(float delay = 0) { return ParticlesController.PlayParticle(this, delay); } } }