ProjectDDD/Assets/_DDD/_Scripts/GameFlow/GameFlowManager.cs

108 lines
2.7 KiB
C#

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using UnityEngine;
namespace DDD
{
[Flags]
public enum GameFlowState : uint
{
None = 0u,
ReadyForRestaurant = 1u,
RunRestaurant = 1u << 1,
SettlementRestaurant = 1u << 2,
All = 0xFFFFFFFFu
}
public class GameFlowManager : Singleton<GameFlowManager>, IManager
{
public GameFlowDataSo GameFlowDataSo;
public GameFlowSceneMappingSo GameFlowSceneMappingSo;
public List<IGameFlowHandler> FlowHandlers = new List<IGameFlowHandler>();
public void PreInit()
{
GameFlowDataSo.CurrentGameState = GameFlowState.None;
}
public Task Init()
{
return Task.CompletedTask; ;
}
public async void PostInit()
{
if (IsGameStarted() == false)
{
await ChangeFlow(GameFlowState.ReadyForRestaurant);
}
}
private bool IsGameStarted() => GameFlowDataSo.CurrentGameState != GameFlowState.None;
public async Task ChangeFlow(GameFlowState newFlowState)
{
if (CanChangeFlow(newFlowState) == false)
{
Debug.LogError("Can't change flow");
return;
}
await EndCurrentFlow();
await ReadyNewFlow(newFlowState);
}
private bool CanChangeFlow(GameFlowState newFlowState)
{
return true;
}
private async Task EndCurrentFlow()
{
var endCurrentFlowState = GameFlowDataSo.CurrentGameState;
foreach (var handler in FlowHandlers)
{
await handler.OnExitCurrentFlow(endCurrentFlowState);
}
}
private async Task ReadyNewFlow(GameFlowState newFlowState)
{
GameFlowDataSo.CurrentGameState = newFlowState;
await OpenFlowScene(newFlowState);
foreach (var handler in FlowHandlers)
{
await handler.OnReadyNewFlow(newFlowState);
}
StartFlow();
}
private async Task OpenFlowScene(GameFlowState newFlowState)
{
if (GetFlowScene(newFlowState))
{
await SceneManager.Instance.ActivateScene(newFlowState);
}
else
{
Debug.Assert(false, "Scene not found!");
}
}
private bool GetFlowScene(GameFlowState flowState)
{
return GameFlowSceneMappingSo.FlowToSceneMapping.ContainsKey(flowState);
}
private void StartFlow()
{
}
}
}