From cb90c9d2882f64b999d903510296344ef662a07f Mon Sep 17 00:00:00 2001 From: NTG Date: Sun, 17 Aug 2025 21:49:06 +0900 Subject: [PATCH] =?UTF-8?q?GameController=20(RestaurantController=20?= =?UTF-8?q?=EB=B3=B4=EB=8B=A4=20=EC=83=81=EC=9C=84)=20=EC=BB=A8=ED=8A=B8?= =?UTF-8?q?=EB=A1=A4=EB=9F=AC=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../_Scripts/GameController/GameController.cs | 84 +++++++++++++++++++ Assets/_DDD/_Scripts/GameData/GameDataSo.cs | 47 +++++++++++ .../GameData/GameLocalizationDataSo.cs | 11 +++ .../_Scripts/GameState/GameLevelStateSo.cs | 42 ++++++++++ 4 files changed, 184 insertions(+) create mode 100644 Assets/_DDD/_Scripts/GameController/GameController.cs create mode 100644 Assets/_DDD/_Scripts/GameData/GameDataSo.cs create mode 100644 Assets/_DDD/_Scripts/GameData/GameLocalizationDataSo.cs create mode 100644 Assets/_DDD/_Scripts/GameState/GameLevelStateSo.cs diff --git a/Assets/_DDD/_Scripts/GameController/GameController.cs b/Assets/_DDD/_Scripts/GameController/GameController.cs new file mode 100644 index 000000000..ef28e2fc6 --- /dev/null +++ b/Assets/_DDD/_Scripts/GameController/GameController.cs @@ -0,0 +1,84 @@ +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using UnityEngine; + +namespace DDD +{ + public class GameController : Singleton, IManager, IGameFlowHandler + { + private static readonly List GameFlowControllerTypes = new(); + + public GameDataSo GetGameData() => GameDataSo.instance; + public GameStateSo GetGameState() => GameStateSo.instance; + + private List _gameFlowControllers = new(); + + public void PreInit() + { + RegisterFlowHandler(); + } + + public async Task Init() + { + await LoadData(); + await GameDataSo.instance.LoadData(); + await InitializeAllFlowControllers(); + } + + public void PostInit() + { + } + + private void RegisterFlowHandler() + { + GameFlowManager.Instance.FlowHandlers.Add(this); + } + + private async Task InitializeAllFlowControllers() + { + // Create controllers and initialize them + foreach (var gameFlowControllerType in GameFlowControllerTypes) + { + // create new controllers from restaurantFlowControllerType + var newController = ScriptableObject.CreateInstance(gameFlowControllerType); + var newFlowController = newController as FlowController; + _gameFlowControllers.Add(newFlowController); + await newFlowController.InitializeController(); + } + + foreach (var gameFlowController in _gameFlowControllers) + { + await gameFlowController.InitializeState(); + } + } + + private Task LoadData() + { + return Task.CompletedTask; + } + + public async Task OnReadyNewFlow(GameFlowState newFlowState) + { + List tasks = new List(); + // Default initialization + + + foreach (var gameFlowController in _gameFlowControllers) + { + tasks.Add(gameFlowController.OnReadyNewFlow(newFlowState)); + } + await Task.WhenAll(tasks); + } + + public async Task OnExitCurrentFlow(GameFlowState exitingFlowState) + { + List tasks = new List(); + foreach (var gameFlowController in _gameFlowControllers) + { + tasks.Add(gameFlowController.OnExitCurrentFlow(exitingFlowState)); + } + await Task.WhenAll(tasks); + } + } +} \ No newline at end of file diff --git a/Assets/_DDD/_Scripts/GameData/GameDataSo.cs b/Assets/_DDD/_Scripts/GameData/GameDataSo.cs new file mode 100644 index 000000000..9569c6f9c --- /dev/null +++ b/Assets/_DDD/_Scripts/GameData/GameDataSo.cs @@ -0,0 +1,47 @@ +using System.Threading.Tasks; +using UnityEditor; +using UnityEngine; +using UnityEngine.AddressableAssets; +using UnityEngine.Localization.SmartFormat.PersistentVariables; +using UnityEngine.ResourceManagement.AsyncOperations; + +namespace DDD +{ + [CreateAssetMenu(fileName = "GameDataSo", menuName = "GameData/GameDataSo", order = 0)] + public class GameDataSo : ScriptableSingleton + { + [Header("Asset References")] + [SerializeField] private AssetReference _gameLocalizationDataSo; + + public GameLocalizationDataSo GameLocalizationData { get; private set; } + + private bool _isLoaded; + + public async Task LoadData() + { + if (_isLoaded) + { + return; + } + + var smartStringHandle = _gameLocalizationDataSo.LoadAssetAsync(); + + await smartStringHandle.Task; + + GameLocalizationData = smartStringHandle.Result; + + Debug.Assert(GameLocalizationData != null, "SmartStringVariableGroup is null"); + + _isLoaded = true; + } + + private void OnDisable() + { + if (!_isLoaded) return; + + _gameLocalizationDataSo.ReleaseAsset(); + + _isLoaded = false; + } + } +} \ No newline at end of file diff --git a/Assets/_DDD/_Scripts/GameData/GameLocalizationDataSo.cs b/Assets/_DDD/_Scripts/GameData/GameLocalizationDataSo.cs new file mode 100644 index 000000000..932733072 --- /dev/null +++ b/Assets/_DDD/_Scripts/GameData/GameLocalizationDataSo.cs @@ -0,0 +1,11 @@ +using UnityEngine; +using UnityEngine.Localization.SmartFormat.PersistentVariables; + +namespace DDD +{ + [CreateAssetMenu(fileName = "GameLocalizationDataSo", menuName = "GameData/GameLocalizationDataSo")] + public class GameLocalizationDataSo : ScriptableObject + { + public VariablesGroupAsset SmartStringVariableGroup; + } +} \ No newline at end of file diff --git a/Assets/_DDD/_Scripts/GameState/GameLevelStateSo.cs b/Assets/_DDD/_Scripts/GameState/GameLevelStateSo.cs new file mode 100644 index 000000000..8c621a0dc --- /dev/null +++ b/Assets/_DDD/_Scripts/GameState/GameLevelStateSo.cs @@ -0,0 +1,42 @@ +using UnityEngine; + +namespace DDD +{ + public class GameLevelStateSo : ScriptableObject + { + public int Level = 1; + + private int _lastLevel; + + private void OnValidate() + { + if (Level != _lastLevel) + { + _lastLevel = Level; + var dirtyEvt = GameEvents.SmartVariablesDirtyEvent; + dirtyEvt.DomainFlags = SmartVariablesDomain.PlayerLevel; + EventBus.Broadcast(dirtyEvt); + } + } + + public void SetLevel(int newLevel) + { + if (Level == newLevel) return; + Level = newLevel; + _lastLevel = Level; + var dirtyEvt = GameEvents.SmartVariablesDirtyEvent; + dirtyEvt.DomainFlags = SmartVariablesDomain.PlayerLevel; + EventBus.Broadcast(dirtyEvt); + } + + public void IncreaseLevel(int delta = 1) + { + if (delta == 0) return; + Level += delta; + _lastLevel = Level; + var dirtyEvt = GameEvents.SmartVariablesDirtyEvent; + dirtyEvt.DomainFlags = SmartVariablesDomain.PlayerLevel; + EventBus.Broadcast(dirtyEvt); + } + } +} \ No newline at end of file