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, IManager { public GameFlowDataSo GameFlowDataSo; public GameFlowAssetsSo GameFlowAssetsSo; public GameFlowSceneMappingSo GameFlowSceneMappingSo; public void Init() { GameFlowDataSo.CurrentGameState = GameFlowState.None; } public async void PostInit() { try { if (IsGameStarted() == false) { await Task.Delay(3000); 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)) { Debug.LogError("Can't change flow"); return; } EndCurrentFlow(); await ReadyNewFlow(newFlowState); } private bool CanChangeFlow(GameFlowState newFlowState) { return true; } private void EndCurrentFlow() { } private async Task ReadyNewFlow(GameFlowState newFlowState) { GameFlowDataSo.CurrentGameState = newFlowState; if (GameFlowAssetsSo.FlowItems.TryGetValue(newFlowState, out var stringKeys)) { foreach (var key in stringKeys) { await AssetManager.LoadAsset(key); } } if (GameFlowAssetsSo.FlowAssets.TryGetValue(newFlowState, out var assetRefs)) { foreach (var assetRef in assetRefs) { var obj = await AssetManager.LoadAsset(assetRef); if (obj is GameObject prefab) { Instantiate(prefab); } else { Debug.LogWarning($"[ReadyNewFlow] Not a GameObject: {assetRef.RuntimeKey}"); } } } OpenFlowScene(newFlowState); StartFlow(); } private async void OpenFlowScene(GameFlowState newFlowState) { try { if (GetFlowScene(newFlowState, out var sceneToLoad)) { await SceneManager.Instance.ActivateScene(sceneToLoad); } else { Debug.Assert(false, "Scene not found!"); } } catch (Exception e) { Debug.LogError(e.Message); } } private bool GetFlowScene(GameFlowState flowState, out SceneType sceneType) { return GameFlowSceneMappingSo.FlowToSceneMapping.TryGetValue(flowState, out sceneType); } private void StartFlow() { } } }