ProjectDDD/Assets/_DDD/_Scripts/GameFlow/GameFlowManager.cs
2025-07-10 14:48:44 +09:00

166 lines
4.6 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using Sirenix.OdinInspector;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.SceneManagement;
using UnityEngine.Serialization;
namespace DDD
{
public enum GameFlowState
{
None = 0,
ReadyForRestaurant = 1,
RunRestaurant = 2,
SettlementRestaurant = 3,
}
public class GameFlowDataSo : ScriptableObject
{
public GameFlowState CurrentGameState;
}
[CreateAssetMenu(fileName = "GameFlowAssetsSo", menuName = "GameFlow/GameFlowAssetsSo")]
public class GameFlowAssetsSo : ScriptableObject
{
[ShowInInspector]
public Dictionary<GameFlowState, List<string>> FlowItems = new();
[ShowInInspector]
public Dictionary<GameFlowState, List<AssetReference>> FlowAssets = new();
}
[CreateAssetMenu(fileName = "GameFlowSceneMappingSo", menuName = "GameFlow/GameFlowSceneMappingSo")]
public class GameFlowSceneMappingSo : ScriptableObject
{
[ShowInInspector]
public Dictionary<GameFlowState, Scene> FlowToSceneMapping = new();
}
public class GameFlowManager : Singleton<GameFlowManager>, IManager
{
private GameFlowDataSo _gameFlowDataSo;
public GameFlowAssetsSo GameFlowAssetsSo;
public GameFlowSceneMappingSo GameFlowSceneMappingSo;
public void Init()
{
_gameFlowDataSo = ScriptableObject.CreateInstance<GameFlowDataSo>();
}
public void PostInit()
{
SceneManager.Instance.OnSceneChanged += OnFlowSceneOpened;
if (IsGameStarted() == false)
{
ChangeFlow(GameFlowState.ReadyForRestaurant);
}
}
public bool IsGameStarted() => _gameFlowDataSo.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 (GameFlowAssetsSo.FlowItems.ContainsKey(newFlowState))
{
List<string> Items = GameFlowAssetsSo.FlowItems[newFlowState];
// Addressables.LoadAssetsAsync(Items, null);
// TODO : 여러 에셋 로드하고, 콜백 받을때까지 기다리기
// Wait
}
if (GameFlowAssetsSo.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 (GameFlowSceneMappingSo.FlowToSceneMapping.TryGetValue(flowState, out scene))
{
return true;
}
return false;
}
public void StartFlow()
{
// Broadcast new flow started
}
}
}