using System; using UnityEngine; public class GameManager : MonoBehaviour { private static GameManager _instance; public static GameManager GetInstance() { return _instance; } private void Awake() { if (_instance != null && _instance != this) { Destroy(gameObject); return; } _instance = this; DontDestroyOnLoad(gameObject); // Persist between scenes } // Enum for the story states of the game public enum StoryState { ChildrenLoopsDemo, Prologue, MainHousePart1, ChildrenLoops, MainHousePart2, DanielLoops, MainHousePart3, Hospital, Ending, TestingScene } // Current story state public StoryState currentStoryState; // References to individual state managers public ChildrenLoopsDemoManager childrenLoopsDemoManager; public PrologueManager prologueManager; public MainHouseP1Manager mainHouseP1Manager; public ChildrenLoopManager childrenLoopManager; public MainHouseP2Manager mainHouseP2Manager; public DanielLoopManager danielLoopManager; public MainHouseP3Manager mainHouseP3Manager; public HospitalLoopsManager hospitalManager; public EndingManager endingManager; public TestingSceneManager testingSceneManager; void Start() { ValidateManagerReferences(); if (LoadManager.GetInstance().LoadingGame == false && GameplayController.GetInstance().currentGameState != GameplayController.State.GameLaunched) { if (GameplayController.GetInstance().demoMode == false && GameplayController.GetInstance().testingMode == false) { currentStoryState = StoryState.Prologue; HandleStoryState(currentStoryState); } else if(GameplayController.GetInstance().demoMode == false && GameplayController.GetInstance().testingMode == true) { currentStoryState = StoryState.TestingScene; HandleStoryState(currentStoryState); } else { currentStoryState = StoryState.ChildrenLoopsDemo; HandleStoryState(currentStoryState); } } //StartStoryState(StoryState.Prologue); } public void MemorizeTemporarilyCurrentStorylineGlobalState() { GameProgressManager.GetInstance().currentGlobalStorylineState = currentStoryState.ToString(); } public void LoadCurrentStoryState() { if (Enum.TryParse(GameProgressManager.GetInstance().currentGlobalStorylineState, out StoryState parsedState)) { currentStoryState = parsedState; } } // Handles logic based on the current story state public void HandleStoryState(StoryState state) { Debug.Log("Handling story state"); //DisableAllManagers(); switch (state) { case StoryState.ChildrenLoopsDemo: Debug.Log("Activating Children Loops Demo Manager..."); childrenLoopsDemoManager?.Activate(); MemorizeTemporarilyCurrentStorylineGlobalState(); break; case StoryState.Prologue: Debug.Log("Activating Prologue Manager..."); prologueManager?.Activate(); MemorizeTemporarilyCurrentStorylineGlobalState(); break; case StoryState.MainHousePart1: Debug.Log("Activating Main House Part 1 Manager..."); mainHouseP1Manager?.Activate(); MemorizeTemporarilyCurrentStorylineGlobalState(); break; case StoryState.ChildrenLoops: Debug.Log("Activating Children Loops Manager..."); childrenLoopManager?.Activate(); MemorizeTemporarilyCurrentStorylineGlobalState(); break; case StoryState.MainHousePart2: Debug.Log("Activating Main House Part 2 Manager..."); mainHouseP2Manager?.Activate(); MemorizeTemporarilyCurrentStorylineGlobalState(); break; case StoryState.DanielLoops: Debug.Log("Activating Daniel Loops Manager..."); danielLoopManager?.Activate(); MemorizeTemporarilyCurrentStorylineGlobalState(); break; case StoryState.MainHousePart3: Debug.Log("Activating Main House Part 3 Manager..."); mainHouseP3Manager?.Activate(); MemorizeTemporarilyCurrentStorylineGlobalState(); break; case StoryState.Hospital: Debug.Log("Activating Hospital Manager..."); hospitalManager?.Activate(); MemorizeTemporarilyCurrentStorylineGlobalState(); break; case StoryState.Ending: Debug.Log("Activating Hospital Manager..."); endingManager?.Activate(); MemorizeTemporarilyCurrentStorylineGlobalState(); break; case StoryState.TestingScene: Debug.Log("Activating Testing Scene Manager..."); testingSceneManager?.Activate(); MemorizeTemporarilyCurrentStorylineGlobalState(); break; default: Debug.LogWarning("Unknown story state!"); break; } } public void OnStorySectionComplete(StoryState completedState) { ValidateManagerReferences(); Debug.Log($"Story section {completedState} complete!"); // Ensure the completed state matches the current state if (currentStoryState != completedState) { Debug.LogWarning($"Mismatch in story state: expected {currentStoryState}, got {completedState}."); return; } // Determine the next state StoryState nextState = GetNextStoryState(completedState); if (nextState != currentStoryState) { Debug.Log($"Transitioning to next story state: {nextState}"); ProgressToNextStoryState(nextState); } else { Debug.Log("Reached the last story state or no valid transition available."); } } private StoryState GetNextStoryState(StoryState currentState) { StoryState[] allStates = (StoryState[])System.Enum.GetValues(typeof(StoryState)); int currentIndex = System.Array.IndexOf(allStates, currentState); // If it's the last state, handle accordingly (e.g., return Ending or stay at the current state) if (currentIndex < allStates.Length - 1) { return allStates[currentIndex + 1]; } return currentState; // Stay on the current state if there's no next state } public void ProgressToNextStoryState(StoryState nextState) { currentStoryState = nextState; HandleStoryState(currentStoryState); } private void DisableAllManagers() { prologueManager?.Deactivate(); mainHouseP1Manager?.Deactivate(); childrenLoopManager?.Deactivate(); mainHouseP2Manager?.Deactivate(); danielLoopManager?.Deactivate(); mainHouseP3Manager?.Deactivate(); hospitalManager?.Deactivate(); endingManager?.Deactivate(); testingSceneManager?.Deactivate(); } private void ValidateManagerReferences() { if (childrenLoopsDemoManager == null) Debug.LogError("Children Loops Demo Manager is not assigned!"); if (prologueManager == null) Debug.LogError("Prologue Manager is not assigned!"); if (mainHouseP1Manager == null) Debug.LogError("Main House Part 1 Manager is not assigned!"); if (childrenLoopManager == null) Debug.LogError("Children Loop Manager is not assigned!"); if (mainHouseP2Manager == null) Debug.LogError("Main House Part 2 Manager is not assigned!"); if (danielLoopManager == null) Debug.LogError("Daniel Loop Manager is not assigned!"); if (mainHouseP3Manager == null) Debug.LogError("Main House Part 3 Manager is not assigned!"); if (hospitalManager == null) Debug.LogError("Hospital Manager is not assigned!"); if (endingManager == null) Debug.LogError("Ending Manager is not assigned!"); if (testingSceneManager == null) Debug.LogError("Testing Scene Manager is not assigned!"); } public void StartStoryState(StoryState state) { Debug.Log($"Starting story state: {state}"); currentStoryState = state; HandleStoryState(state); } }