ProjectDDD/Assets/_DDD/_Scripts/GameFlow/GameFlowManager.cs
2025-07-09 18:45:11 +09:00

160 lines
4.4 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.SceneManagement;
namespace DDD
{
public enum GameFlowState
{
None = 0,
ReadyForRestaurant = 1,
RunRestaurant = 2,
SettlementRestaurant = 3,
}
public class GameFlowData : ScriptableObject
{
public GameFlowState CurrentGameState;
}
[CreateAssetMenu(fileName = "GameFlowAssets", menuName = "GameFlow/GameFlowAssets")]
public class GameFlowAssets : ScriptableObject
{
public Dictionary<GameFlowState, List<string>> FlowItems = new();
public Dictionary<GameFlowState, List<AssetReference>> FlowAssets = new();
}
[CreateAssetMenu(fileName = "GameFlowSceneMapping", menuName = "GameFlow/GameFlowSceneMapping")]
public class GameFlowSceneMapping : ScriptableObject
{
public Dictionary<GameFlowState, Scene> FlowToSceneMapping = new();
}
public class GameFlowManager : Singleton<GameFlowManager>, IManager
{
private GameFlowData _gameFlowData = null;
public GameFlowAssets GameFlowAssets = null;
public GameFlowSceneMapping GameFlowSceneMapping;
public void Init()
{
_gameFlowData = new GameFlowData();
}
public void PostInit()
{
SceneManager.Instance.OnSceneChanged += OnFlowSceneOpened;
if (IsGameStarted() == false)
{
ChangeFlow(GameFlowState.ReadyForRestaurant);
}
}
public bool IsGameStarted() => _gameFlowData.CurrentGameState != GameFlowState.None;
protected override void Awake()
{
base.Awake();
}
private void Start()
{
}
private bool CanChangeFlow(GameFlowState newFlowState)
{
return true;
}
public void ChangeFlow(GameFlowState newFlowState)
{
StartCoroutine(ChangeFlowCoroutine(newFlowState));
}
private IEnumerator ChangeFlowCoroutine(GameFlowState newFlowState)
{
if (CanChangeFlow(newFlowState) == false)
{
Debug.LogError("Can't change flow");
yield break;
}
EndCurrentFlow();
}
public void EndCurrentFlow()
{
}
public IEnumerator ReadyNewFlow(GameFlowState newFlowState)
{
OpenFlowScene(newFlowState);
// Ready Assets
if (GameFlowAssets.FlowItems.ContainsKey(newFlowState))
{
List<string> Items = GameFlowAssets.FlowItems[newFlowState];
// Addressables.LoadAssetsAsync(Items, null);
// TODO : 여러 에셋 로드하고, 콜백 받을때까지 기다리기
// Wait
}
if (GameFlowAssets.FlowAssets.ContainsKey(newFlowState))
{
//List<AssetReference> Assets = GameFlowAssets.FlowItems[newFlowState];
// Addressables.LoadAssetsAsync(Assets, )
// TODO : 여러 에셋 로드하고, 콜백 받을때까지 기다리기
// Wait
}
// Ready Scene
GetFlowScene(newFlowState, out var flowScene);
yield return new WaitUntil(() => _currentScene == flowScene );
StartFlow();
}
public void OpenFlowScene(GameFlowState newFlowState)
{
if (GetFlowScene(newFlowState, out var sceneToLoad))
{
SceneManager.Instance.RequestSceneLoad(sceneToLoad);
}
else
{
Debug.Assert(false, "Scene not found!");
}
}
private Scene _currentScene;
public void OnFlowSceneOpened(Scene newScene)
{
_currentScene = newScene;
}
public bool GetFlowScene(GameFlowState flowState, out Scene scene)
{
if (GameFlowSceneMapping.FlowToSceneMapping.TryGetValue(flowState, out scene))
{
return true;
}
return false;
}
public void StartFlow()
{
// Broadcast new flow started
}
}
}