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

127 lines
3.4 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.AddressableAssets;
namespace DDD
{
public enum GameFlowState
{
None = 0,
ReadyForRestaurant = 1,
RunRestaurant = 2,
SettlementRestaurant = 3,
}
public class GameFlowManager : Singleton<GameFlowManager>, IManager
{
public GameFlowDataSo GameFlowDataSo;
2025-07-10 05:48:44 +00:00
public GameFlowAssetsSo GameFlowAssetsSo;
public GameFlowSceneMappingSo GameFlowSceneMappingSo;
public void Init()
{
GameFlowDataSo.CurrentGameState = GameFlowState.None;
}
public async void PostInit()
{
try
{
if (IsGameStarted() == false)
{
await ChangeFlow(GameFlowState.ReadyForRestaurant);
}
}
catch (Exception e)
{
Debug.LogWarning(e);
}
}
private bool IsGameStarted() => GameFlowDataSo.CurrentGameState != GameFlowState.None;
public async Task ChangeFlow(GameFlowState newFlowState)
{
if (!CanChangeFlow(newFlowState))
2025-07-09 09:45:11 +00:00
{
Debug.LogError("Can't change flow");
return;
2025-07-09 09:45:11 +00:00
}
EndCurrentFlow();
await ReadyNewFlow(newFlowState);
}
private bool CanChangeFlow(GameFlowState newFlowState)
{
return true;
2025-07-09 09:45:11 +00:00
}
private void EndCurrentFlow()
2025-07-09 09:45:11 +00:00
{
}
private async Task ReadyNewFlow(GameFlowState newFlowState)
2025-07-09 09:45:11 +00:00
{
GameFlowDataSo.CurrentGameState = newFlowState;
if (GameFlowAssetsSo.FlowItems.TryGetValue(newFlowState, out var stringKeys))
2025-07-09 09:45:11 +00:00
{
foreach (var key in stringKeys)
{
await AssetManager.LoadAsset<UnityEngine.Object>(key);
}
}
if (GameFlowAssetsSo.FlowAssets.TryGetValue(newFlowState, out var assetRefs))
2025-07-09 09:45:11 +00:00
{
foreach (var assetRef in assetRefs)
{
var obj = await AssetManager.LoadAsset<UnityEngine.Object>(assetRef);
2025-07-14 06:38:22 +00:00
if (obj is GameFlowReadyHandler handler)
{
2025-07-14 06:38:22 +00:00
await handler.OnReadyNewFlow(newFlowState);
}
}
2025-07-09 09:45:11 +00:00
}
OpenFlowScene(newFlowState);
2025-07-09 09:45:11 +00:00
StartFlow();
}
private async void OpenFlowScene(GameFlowState newFlowState)
2025-07-09 09:45:11 +00:00
{
try
2025-07-09 09:45:11 +00:00
{
if (GetFlowScene(newFlowState, out var sceneToLoad))
{
await SceneManager.Instance.ActivateScene(sceneToLoad);
}
else
{
Debug.Assert(false, "Scene not found!");
}
2025-07-09 09:45:11 +00:00
}
catch (Exception e)
2025-07-09 09:45:11 +00:00
{
Debug.LogError(e.Message);
2025-07-09 09:45:11 +00:00
}
}
private bool GetFlowScene(GameFlowState flowState, out SceneType sceneType)
2025-07-09 09:45:11 +00:00
{
return GameFlowSceneMappingSo.FlowToSceneMapping.TryGetValue(flowState, out sceneType);
2025-07-09 09:45:11 +00:00
}
private void StartFlow()
2025-07-09 09:45:11 +00:00
{
2025-07-09 09:45:11 +00:00
}
}
}