GameController (RestaurantController 보다 상위) 컨트롤러 추가
This commit is contained in:
parent
3099961e65
commit
cb90c9d288
84
Assets/_DDD/_Scripts/GameController/GameController.cs
Normal file
84
Assets/_DDD/_Scripts/GameController/GameController.cs
Normal file
@ -0,0 +1,84 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using UnityEngine;
|
||||||
|
|
||||||
|
namespace DDD
|
||||||
|
{
|
||||||
|
public class GameController : Singleton<GameController>, IManager, IGameFlowHandler
|
||||||
|
{
|
||||||
|
private static readonly List<Type> GameFlowControllerTypes = new();
|
||||||
|
|
||||||
|
public GameDataSo GetGameData() => GameDataSo.instance;
|
||||||
|
public GameStateSo GetGameState() => GameStateSo.instance;
|
||||||
|
|
||||||
|
private List<FlowController> _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<Task> tasks = new List<Task>();
|
||||||
|
// Default initialization
|
||||||
|
|
||||||
|
|
||||||
|
foreach (var gameFlowController in _gameFlowControllers)
|
||||||
|
{
|
||||||
|
tasks.Add(gameFlowController.OnReadyNewFlow(newFlowState));
|
||||||
|
}
|
||||||
|
await Task.WhenAll(tasks);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task OnExitCurrentFlow(GameFlowState exitingFlowState)
|
||||||
|
{
|
||||||
|
List<Task> tasks = new List<Task>();
|
||||||
|
foreach (var gameFlowController in _gameFlowControllers)
|
||||||
|
{
|
||||||
|
tasks.Add(gameFlowController.OnExitCurrentFlow(exitingFlowState));
|
||||||
|
}
|
||||||
|
await Task.WhenAll(tasks);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
47
Assets/_DDD/_Scripts/GameData/GameDataSo.cs
Normal file
47
Assets/_DDD/_Scripts/GameData/GameDataSo.cs
Normal file
@ -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<GameDataSo>
|
||||||
|
{
|
||||||
|
[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<GameLocalizationDataSo>();
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
11
Assets/_DDD/_Scripts/GameData/GameLocalizationDataSo.cs
Normal file
11
Assets/_DDD/_Scripts/GameData/GameLocalizationDataSo.cs
Normal file
@ -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;
|
||||||
|
}
|
||||||
|
}
|
42
Assets/_DDD/_Scripts/GameState/GameLevelStateSo.cs
Normal file
42
Assets/_DDD/_Scripts/GameState/GameLevelStateSo.cs
Normal file
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user