using System; using System.Collections.Generic; using System.Threading.Tasks; using UnityEngine; using UnityEngine.AddressableAssets; namespace DDD { public class GameController : Singleton, IManager, IGameFlowHandler { [SerializeField] private AssetReference _gameData; public GameData GetGameData() => GameData.Instance; public GameState GetGameState() => GameState.Instance; private List _gameFlowControllers = new(); private static readonly List GameFlowControllerTypes = new(); public void PreInit() { CreateGameState(); RegisterFlowHandler(); } public async Task Init() { await LoadData(); await GetGameData().LoadData(); await InitializeAllFlowControllers(); } public void PostInit() { } private void CreateGameState() { GameState.CreateScriptSingleton(); } private void RegisterFlowHandler() { GameFlowManager.Instance.FlowHandlers.Add(this); } private async Task InitializeAllFlowControllers() { // Create controllers and initialize them foreach (var gameFlowControllerType in GameFlowControllerTypes) { // create new controllers from gameFlowControllerType var newController = ScriptableObject.CreateInstance(gameFlowControllerType); var newFlowController = newController as FlowController; _gameFlowControllers.Add(newFlowController); await newFlowController.InitializeController(); } foreach (var gameFlowController in _gameFlowControllers) { await gameFlowController.InitializeState(); } } private async Task LoadData() { await Task.CompletedTask; } public async Task OnReadyNewFlow(GameFlowState newFlowState) { List tasks = new List(); // Default initialization foreach (var gameFlowController in _gameFlowControllers) { tasks.Add(gameFlowController.OnReadyNewFlow(newFlowState)); } await Task.WhenAll(tasks); } public async Task OnExitCurrentFlow(GameFlowState exitingFlowState) { List tasks = new List(); foreach (var gameFlowController in _gameFlowControllers) { tasks.Add(gameFlowController.OnExitCurrentFlow(exitingFlowState)); } await Task.WhenAll(tasks); } } }