84 lines
2.5 KiB
C#
84 lines
2.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Threading.Tasks;
|
|
using UnityEngine;
|
|
|
|
namespace DDD
|
|
{
|
|
public class GameController : Singleton<GameController>, IManager, IGameFlowHandler
|
|
{
|
|
private static readonly List<Type> GameFlowControllerTypes = new();
|
|
|
|
public GameDataSo GetGameData() => GameDataSo.instance;
|
|
public GameStateSo GetGameState() => GameStateSo.instance;
|
|
|
|
private List<FlowController> _gameFlowControllers = new();
|
|
|
|
public void PreInit()
|
|
{
|
|
RegisterFlowHandler();
|
|
}
|
|
|
|
public async Task Init()
|
|
{
|
|
await LoadData();
|
|
await GameDataSo.instance.LoadData();
|
|
await InitializeAllFlowControllers();
|
|
}
|
|
|
|
public void PostInit()
|
|
{
|
|
}
|
|
|
|
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 restaurantFlowControllerType
|
|
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 Task LoadData()
|
|
{
|
|
return 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);
|
|
}
|
|
}
|
|
} |