using System.Collections.Generic; using UnityEngine; using System; using System.Collections; using NaughtyAttributes; using UnityEngine.Events; public class StepOrchestrator : MonoBehaviour { // Const strings private const string LOG_START_INIT = "Started Loading initializing the steps Link System"; [Header("Disclaimer")] [InfoBox("It needs to be a parent of StepsList objects. Or manually add the lists. Not both.", EInfoBoxType.Normal)] [HorizontalLine(color: EColor.Blue)] [Header("Callbacks")] [Tooltip("Event called when the system is initialized")] [SerializeField] private UnityEvent OnSystemInitialized; /// /// If true, the lists will be loaded from the children of the game object that have the StepsList script attached /// [Tooltip("If true, the lists will be loaded from the children of the game object that have the StepsList script attached")] [SerializeField] private bool loadsListsFromChildren = true; /// /// List of all the StepsList that are part of the game. Order is respected. /// [HideIf("loadsListsFromChildren")] [Tooltip("List of all the StepsList that are part of the game. Order is respected.")] [SerializeField] private List stepsLists = new(); /// /// Flag to check if the step link finished initializing all lists /// private bool isInitialized = false; /// /// Flag to check if the steps link system is running the lists /// private bool isRunning = false; void Start() { Debug.Log(LOG_START_INIT); StartCoroutine(InitStepLinkSystem()); } /// /// Initialize the step link system, getting all the children lists and initialize them, too /// /// private IEnumerator InitStepLinkSystem() { if (!isInitialized) { if (loadsListsFromChildren) { stepsLists.Clear(); foreach (Transform child in transform) { if (child.GetComponent() != null) { Debug.Log("Found StepsList: " + child.name); stepsLists.Add(child.GetComponent()); } } } else { foreach (StepsListScript list in stepsLists) { if (list.GetComponent() == null) { stepsLists.Remove(list); Debug.LogWarning("The list: " + list.name + " has not the StepsList attached. And has been removed."); } } } if (stepsLists.Count == 0) { Debug.LogWarning("No Valid StepsList found"); } else { foreach (StepsListScript list in stepsLists) { yield return StartCoroutine(list.InitListSteps()); } } isInitialized = true; } else { Debug.LogWarning("This step link system is already initialized"); } } /// /// Run all the lists in order /// /// public void RunAllLists() { if (!isInitialized) { throw new Exception("The step link system is not initialized"); } else if (isRunning) { throw new Exception("The step link system is already running a list"); } else if (isInitialized && !isRunning) { StartCoroutine(RunAll()); } } /// /// Run a list by index /// /// /// public void RunListByIndex(int index) { if (!isInitialized) { throw new Exception("The step link system is not initialized"); } else if (isRunning) { throw new Exception("The step link system is already running a list"); } else if (isInitialized && !isRunning) { if (index < stepsLists.Count && index >= 0) { StartCoroutine(stepsLists[index].StartSteps()); } else { throw new Exception("Invalid index of list, list has " + stepsLists.Count + " elements"); } } } /// /// Run a list by name /// /// /// public void RunListByName(String name) { if (!isInitialized) { throw new Exception("The step link system is not initialized"); } else if (isRunning) { throw new Exception("The step link system is already running a list"); } else if (isInitialized && !isRunning) { foreach (StepsListScript list in stepsLists) { if (list.name == name) { StartCoroutine(list.StartSteps()); return; } } throw new Exception("No list found with the name: " + name); } } /// /// Run all the lists in order /// private IEnumerator RunAll() { if (isRunning) { throw new Exception("The step link system is already running a list"); } else { isRunning = true; for (int index = 0; index < stepsLists.Count; index++) { yield return StartCoroutine(stepsLists[index].StartSteps()); } isRunning = false; yield return null; } } }