ProjectDDD/Assets/_DDD/_Scripts/GameController/GameController.cs
2025-08-19 13:51:42 +09:00

92 lines
2.7 KiB
C#

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.AddressableAssets;
namespace DDD
{
public class GameController : Singleton<GameController>, IManager, IGameFlowHandler
{
[SerializeField] private AssetReference _gameData;
public GameData GetGameData() => GameData.Instance;
public GameState GetGameState() => GameState.Instance;
private List<FlowController> _gameFlowControllers = new();
private static readonly List<Type> 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<Task> tasks = new List<Task>();
// Default initialization
foreach (var gameFlowController in _gameFlowControllers)
{
tasks.Add(gameFlowController.OnReadyNewFlow(newFlowState));
}
await Task.WhenAll(tasks);
}
public async Task OnExitCurrentFlow(GameFlowState exitingFlowState)
{
List<Task> tasks = new List<Task>();
foreach (var gameFlowController in _gameFlowControllers)
{
tasks.Add(gameFlowController.OnExitCurrentFlow(exitingFlowState));
}
await Task.WhenAll(tasks);
}
}
}