using UnityEngine; public class FireManager : MonoBehaviour, IListener { [SerializeField] private float fireDelay = 1f; [SerializeField][Min(1)] private int firesToWin = 10; [SerializeField] private CounterEditable maxFiresDisp = null; private FireBehaviour[] firePlaces; private int lastFireIdx = -1; private int firesExtinguished = 0; public FireBehaviour CurrentFire => lastFireIdx >= 0 ? firePlaces[lastFireIdx] : null; void Start() { firePlaces = GetComponentsInChildren<FireBehaviour>(); if (maxFiresDisp) maxFiresDisp.UpdateMaxValue(firesToWin); EventManager.Instance.AddListener(EVENT_TYPE.ON_FIRE_EXTINGUISHED, this); EventManager.Instance.AddListener(EVENT_TYPE.ON_FIRE_MAX_CHANGED, this); Invoke("StartNewFire", 0.5f); } public void OnEvent<T>(EVENT_TYPE eventType, Component Sender, T param = default) { switch (eventType) { case EVENT_TYPE.ON_FIRE_EXTINGUISHED: firesExtinguished++; if (firesExtinguished == firesToWin) { EventManager.Instance.PostNotification<Object>(EVENT_TYPE.ON_FIRE_WON, this); break; } Invoke("StartNewFire", fireDelay); break; case EVENT_TYPE.ON_FIRE_MAX_CHANGED: firesToWin = (dynamic)param; break; } } private void StartNewFire() { int newIdx = lastFireIdx; while (newIdx == lastFireIdx) { newIdx = Random.Range(0, firePlaces.Length); } lastFireIdx = newIdx; firePlaces[newIdx].StartFire(); } #if UNITY_EDITOR private void OnValidate() { if(maxFiresDisp) maxFiresDisp.UpdateMaxValue(firesToWin); } #endif }