using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Events; public class ActivatorManager : MonoBehaviour { public static ActivatorManager Instance; [Tooltip("Events to run per step; index is the step number.")] public List StepEvents; public List TimeLineEvents; [Min(0)] public int stepIndex = 0; int loadedStepIndex; private void Awake() => Instance = this; /// Execute exactly one step by index (no bounds errors). public void ExecuteStep(int index) { //if (StepEvents == null || StepEvents.Count == 0) return; index = Mathf.Clamp(index, 0, TimeLineEvents.Count - 1); //StepEvents[index]?.Invoke(); TimeLineEvents[index]?.Invoke(); //StartCoroutine(executeStepRoutine(index)); } /// Advance to next step and execute its event. public void AdvanceActivate() { stepIndex++; //int next = Mathf.Clamp(stepIndex + 1, 0, Mathf.Max(0, (StepEvents?.Count ?? 1) - 1)); //stepIndex = next; //ExecuteStep(stepIndex); } /// /// Rebuild state after load: execute steps 0..target in order. /// Call this from ES3 load after setting stepIndex. /// public void ReplayTo(int target) { StartCoroutine(ReplayToCoroutine(target)); ////if (StepEvents == null || StepEvents.Count == 0) return; //// target = Mathf.Clamp(target, 0, StepEvents.Count - 1); //loadedStepIndex = stepIndex; //if (stepIndex < 0) return; //target = stepIndex; //for (int i = 0; i <= target; i++) ExecuteStep(i); //// stepIndex = target; //stepIndex = loadedStepIndex; } IEnumerator ReplayToCoroutine(int target) { loadedStepIndex = stepIndex; if (stepIndex > 0) target = stepIndex; for (int i = 0; i <= target; i++) { yield return new WaitForSeconds(0.03f); ExecuteStep(i); } // stepIndex = target; stepIndex = loadedStepIndex; ES3SingleStore.LoadObjectives(); yield return null; } }