게임 플로우 변경 (PreInit 추가)

This commit is contained in:
NTG_Lenovo 2025-07-21 13:11:38 +09:00
parent ffd534cb13
commit f0e24f4c43
18 changed files with 116 additions and 88 deletions

View File

@ -16,7 +16,7 @@ MonoBehaviour:
m_GUID: 30e7f67fe9aaa7849a34c9b6e2bc53ae m_GUID: 30e7f67fe9aaa7849a34c9b6e2bc53ae
m_SerializeEntries: m_SerializeEntries:
- m_GUID: 0912c3de85fd61848a41575faa2794ed - m_GUID: 0912c3de85fd61848a41575faa2794ed
m_Address: CreateEnvironment m_Address: CreateEnvironmentSo
m_ReadOnly: 0 m_ReadOnly: 0
m_SerializedLabels: [] m_SerializedLabels: []
FlaggedDuringContentUpdateRestriction: 0 FlaggedDuringContentUpdateRestriction: 0
@ -56,7 +56,7 @@ MonoBehaviour:
- Atlas - Atlas
FlaggedDuringContentUpdateRestriction: 0 FlaggedDuringContentUpdateRestriction: 0
- m_GUID: 47e757b9a170ab649af14c4d7b80ac41 - m_GUID: 47e757b9a170ab649af14c4d7b80ac41
m_Address: CreateRestaurantPlayer m_Address: CreateRestaurantPlayerSo
m_ReadOnly: 0 m_ReadOnly: 0
m_SerializedLabels: [] m_SerializedLabels: []
FlaggedDuringContentUpdateRestriction: 0 FlaggedDuringContentUpdateRestriction: 0

View File

@ -10,5 +10,5 @@ MonoBehaviour:
m_Enabled: 1 m_Enabled: 1
m_EditorHideFlags: 0 m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 419e829d5eec9544e94be59817cdb69c, type: 3} m_Script: {fileID: 11500000, guid: 419e829d5eec9544e94be59817cdb69c, type: 3}
m_Name: CreateEnvironment m_Name: CreateEnvironmentSo
m_EditorClassIdentifier: m_EditorClassIdentifier:

View File

@ -10,6 +10,6 @@ MonoBehaviour:
m_Enabled: 1 m_Enabled: 1
m_EditorHideFlags: 0 m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 246a7785bd15ac84b9f240005b987f1f, type: 3} m_Script: {fileID: 11500000, guid: 246a7785bd15ac84b9f240005b987f1f, type: 3}
m_Name: CreateRestaurantPlayer m_Name: CreateRestaurantPlayerSo
m_EditorClassIdentifier: m_EditorClassIdentifier:
_spawnPosition: {x: 0, y: 0, z: 15} _spawnPosition: {x: 0, y: 0, z: 15}

View File

@ -15,21 +15,19 @@ namespace DDD
{ {
public class AssetManager : Singleton<AssetManager>, IManager public class AssetManager : Singleton<AssetManager>, IManager
{ {
public void Init() public void PreInit()
{ {
} }
public async void PostInit() public async Task Init()
{ {
try await Addressables.InitializeAsync().Task;
{ }
await Addressables.InitializeAsync().Task;
} public void PostInit()
catch (Exception e) {
{
Debug.Assert(false, $"Addressables initialization failed\n{e}");
}
} }
public static async Task<T> LoadAsset<T>(string key) where T : UnityEngine.Object public static async Task<T> LoadAsset<T>(string key) where T : UnityEngine.Object

View File

@ -1,4 +1,5 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading.Tasks;
using Sirenix.OdinInspector; using Sirenix.OdinInspector;
using Unity.Cinemachine; using Unity.Cinemachine;
@ -16,12 +17,17 @@ public class CameraManager : Singleton<CameraManager>, IManager
private Dictionary<CameraType, CameraGameObject> _cameraGameObjects = new(); private Dictionary<CameraType, CameraGameObject> _cameraGameObjects = new();
private CinemachineBrain _cinemachineBrain; private CinemachineBrain _cinemachineBrain;
public void Init() public void PreInit()
{ {
_cinemachineBrain = GetComponent<CinemachineBrain>(); _cinemachineBrain = GetComponent<CinemachineBrain>();
} }
public Task Init()
{
return Task.CompletedTask;;
}
public void PostInit() public void PostInit()
{ {

View File

@ -15,48 +15,46 @@ public class DataManager : Singleton<DataManager>, IManager
private Dictionary<string, Sprite> _spriteAtlas; private Dictionary<string, Sprite> _spriteAtlas;
public bool IsInitialized { get; private set; } public bool IsInitialized { get; private set; }
public void Init() public void PreInit()
{ {
} }
public async void PostInit() public async Task Init()
{ {
try ItemDataSo = await AssetManager.LoadAsset<ItemDataSo>(DataConstants.ItemDataSo);
FoodDataSo = await AssetManager.LoadAsset<FoodDataSo>(DataConstants.FoodDataSo);
EnvironmentDataSo = await AssetManager.LoadAsset<EnvironmentDataSo>(DataConstants.EnvironmentDataSo);
List<SpriteAtlas> spriteAtlases = await AssetManager.LoadAssetsByLabel<SpriteAtlas>(DataConstants.AtlasLabel);
_spriteAtlas = new Dictionary<string, Sprite>(spriteAtlases.Count);
foreach (var atlas in spriteAtlases)
{ {
ItemDataSo = await AssetManager.LoadAsset<ItemDataSo>(DataConstants.ItemDataSo); if (atlas == null) continue;
FoodDataSo = await AssetManager.LoadAsset<FoodDataSo>(DataConstants.FoodDataSo);
EnvironmentDataSo = await AssetManager.LoadAsset<EnvironmentDataSo>(DataConstants.EnvironmentDataSo); var count = atlas.spriteCount;
if (count == 0) continue;
var sprites = new Sprite[count];
atlas.GetSprites(sprites);
List<SpriteAtlas> spriteAtlases = await AssetManager.LoadAssetsByLabel<SpriteAtlas>(DataConstants.AtlasLabel); foreach (var sprite in sprites)
_spriteAtlas = new Dictionary<string, Sprite>(spriteAtlases.Count);
foreach (var atlas in spriteAtlases)
{ {
if (atlas == null) continue; if (sprite == null) continue;
var count = atlas.spriteCount;
if (count == 0) continue;
var sprites = new Sprite[count];
atlas.GetSprites(sprites);
foreach (var sprite in sprites) var key = sprite.name.Replace(CommonConstants.Clone, string.Empty).Trim();
{ _spriteAtlas.TryAdd(key, sprite);
if (sprite == null) continue;
var key = sprite.name.Replace(CommonConstants.Clone, string.Empty).Trim();
_spriteAtlas.TryAdd(key, sprite);
}
} }
}
IsInitialized = true; IsInitialized = true;
} }
catch (Exception e)
{ public void PostInit()
Debug.LogError($"So bind failed\n{e.Message}"); {
}
} }
public async Task WaitUntilInitialized() public async Task WaitUntilInitialized()

View File

@ -2,7 +2,6 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
using UnityEngine; using UnityEngine;
using UnityEngine.AddressableAssets;
namespace DDD namespace DDD
{ {
@ -19,24 +18,22 @@ public class GameFlowManager : Singleton<GameFlowManager>, IManager
public GameFlowDataSo GameFlowDataSo; public GameFlowDataSo GameFlowDataSo;
public GameFlowSceneMappingSo GameFlowSceneMappingSo; public GameFlowSceneMappingSo GameFlowSceneMappingSo;
public List<IGameFlowHandler> FlowHandlers = new List<IGameFlowHandler>(); public List<IGameFlowHandler> FlowHandlers = new List<IGameFlowHandler>();
public void Init() public void PreInit()
{ {
GameFlowDataSo.CurrentGameState = GameFlowState.None; GameFlowDataSo.CurrentGameState = GameFlowState.None;
} }
public Task Init()
{
return Task.CompletedTask;;
}
public void PostInit() public void PostInit()
{ {
try if (IsGameStarted() == false)
{ {
if (IsGameStarted() == false) ChangeFlow(GameFlowState.ReadyForRestaurant);
{
ChangeFlow(GameFlowState.ReadyForRestaurant);
}
}
catch (Exception e)
{
Debug.LogWarning(e);
} }
} }
@ -65,10 +62,15 @@ private void EndCurrentFlow()
} }
private void ReadyNewFlow(GameFlowState newFlowState) private async void ReadyNewFlow(GameFlowState newFlowState)
{ {
GameFlowDataSo.CurrentGameState = newFlowState; GameFlowDataSo.CurrentGameState = newFlowState;
foreach (var handler in FlowHandlers)
{
await handler.OnReadyNewFlow(newFlowState);
}
OpenFlowScene(newFlowState); OpenFlowScene(newFlowState);
StartFlow(); StartFlow();

View File

@ -13,12 +13,17 @@ public class FadeManager : Singleton<FadeManager>, IManager
private float _fadeInDuration = 1f; private float _fadeInDuration = 1f;
private CanvasGroup _canvasGroup; private CanvasGroup _canvasGroup;
public void Init() public void PreInit()
{ {
_canvasGroup = GetComponent<CanvasGroup>(); _canvasGroup = GetComponent<CanvasGroup>();
} }
public Task Init()
{
return Task.CompletedTask;;
}
public void PostInit() public void PostInit()
{ {
_canvasGroup.alpha = 0f; _canvasGroup.alpha = 0f;

View File

@ -10,7 +10,7 @@ public class GameManager : Singleton<GameManager>
private List<Singleton> _managerInstances; private List<Singleton> _managerInstances;
protected void Start() protected async void Start()
{ {
base.OnAwake(); base.OnAwake();
@ -24,7 +24,7 @@ protected void Start()
// 매니저 초기화 // 매니저 초기화
_managerInstances = new List<Singleton>(_managerDefinitionSo.ManagerClasses.Count); _managerInstances = new List<Singleton>(_managerDefinitionSo.ManagerClasses.Count);
foreach (var managerObject in _managerDefinitionSo.ManagerClasses) foreach (var managerObject in _managerDefinitionSo.ManagerClasses)
{ {
var managerInstance = Instantiate(managerObject); var managerInstance = Instantiate(managerObject);
@ -33,7 +33,15 @@ protected void Start()
_managerInstances.Add(managerInstance); _managerInstances.Add(managerInstance);
if (managerInstance is IManager manager) if (managerInstance is IManager manager)
{ {
manager.Init(); manager.PreInit();
}
}
foreach (var managerInstance in _managerInstances)
{
if (managerInstance is IManager manager)
{
await manager.Init();
} }
} }
foreach (var managerInstance in _managerInstances) foreach (var managerInstance in _managerInstances)

View File

@ -1,8 +1,11 @@
using System.Threading.Tasks;
namespace DDD namespace DDD
{ {
public interface IManager public interface IManager
{ {
void Init(); void PreInit();
Task Init();
void PostInit(); void PostInit();
} }
} }

View File

@ -23,14 +23,14 @@ public class SceneManager : Singleton<SceneManager>, IManager
private SceneInstance _currentSceneInstance; private SceneInstance _currentSceneInstance;
public Action<SceneInstance> OnSceneChanged; public Action<SceneInstance> OnSceneChanged;
public void Init() public void PreInit()
{ {
Array sceneTypeArray = Enum.GetValues(typeof(SceneType)); Array sceneTypeArray = Enum.GetValues(typeof(SceneType));
_loadedScenes = new Dictionary<SceneType, SceneInstance>(sceneTypeArray.Length); _loadedScenes = new Dictionary<SceneType, SceneInstance>(sceneTypeArray.Length);
} }
public async void PostInit() public async Task Init()
{ {
try try
{ {
@ -46,6 +46,11 @@ public async void PostInit()
Debug.LogWarning($"Scene preload failed\n{e}"); Debug.LogWarning($"Scene preload failed\n{e}");
} }
} }
public void PostInit()
{
}
public SceneInstance GetSceneInstance(SceneType sceneType) => _loadedScenes[sceneType]; public SceneInstance GetSceneInstance(SceneType sceneType) => _loadedScenes[sceneType];

View File

@ -3,8 +3,8 @@
namespace DDD namespace DDD
{ {
[CreateAssetMenu(fileName = "CreateRestaurantPlayer", menuName = "GameFlow/CreateRestaurantPlayer")] [CreateAssetMenu(fileName = "CreateRestaurantPlayerSo", menuName = "GameFlow/CreateRestaurantPlayerSo")]
public class CreateRestaurantPlayer : GameFlowTask public class CreateRestaurantPlayerSo : GameFlowTask
{ {
[SerializeField] [SerializeField]
private Vector3 _spawnPosition; private Vector3 _spawnPosition;

View File

@ -7,15 +7,21 @@ public class RestaurantController : Singleton<RestaurantController>, IManager, I
{ {
private RestaurantEnvironmentState _restaurantEnvironmentState; private RestaurantEnvironmentState _restaurantEnvironmentState;
public RestaurantEnvironmentState RestaurantEnvironmentState => _restaurantEnvironmentState; public RestaurantEnvironmentState RestaurantEnvironmentState => _restaurantEnvironmentState;
// TODO : GameManager에 등록되게 So에 추가해주세요. private const string CreateRestaurantPlayerSo = "CreateRestaurantPlayerSo";
private const string CreateEnvironmentSo = "CreateEnvironmentSo";
public void Init()
public void PreInit()
{ {
LoadOrCreateRestaurantState(); LoadOrCreateRestaurantState();
RegisterFlowHandler(); RegisterFlowHandler();
} }
public Task Init()
{
return Task.CompletedTask;;
}
public void PostInit() public void PostInit()
{ {
GenerateDummyEnvironmentProps();// XXX : DUMMY! REMOVE THIS GenerateDummyEnvironmentProps();// XXX : DUMMY! REMOVE THIS
@ -29,7 +35,7 @@ private void RegisterFlowHandler()
private void LoadOrCreateRestaurantState() private void LoadOrCreateRestaurantState()
{ {
// TODO : Load states from saved files. if none, create them. // TODO : Load states from saved files. if none, create them.
_restaurantEnvironmentState = new RestaurantEnvironmentState(); _restaurantEnvironmentState = ScriptableObject.CreateInstance<RestaurantEnvironmentState>();
} }
private void GenerateDummyEnvironmentProps() private void GenerateDummyEnvironmentProps()
@ -58,19 +64,17 @@ private void GenerateDummyEnvironmentProps()
} }
public Task OnReadyNewFlow(GameFlowState newFlowState) public async Task OnReadyNewFlow(GameFlowState newFlowState)
{ {
if (newFlowState == GameFlowState.ReadyForRestaurant) if (newFlowState == GameFlowState.ReadyForRestaurant)
{ {
CreateRestaurantPlayer createRestaurantPlayerJob = new CreateRestaurantPlayer(); CreateRestaurantPlayerSo createRestaurantPlayerSoJob = await AssetManager.LoadAsset<CreateRestaurantPlayerSo>(CreateRestaurantPlayerSo);
CreateEnvironment createEnvironmentJob = new CreateEnvironment(); CreateEnvironmentSo createEnvironmentSoJob = await AssetManager.LoadAsset<CreateEnvironmentSo>(CreateEnvironmentSo);
var playerHandle = createRestaurantPlayerJob.OnReadyNewFlow(newFlowState); var playerHandle = createRestaurantPlayerSoJob.OnReadyNewFlow(newFlowState);
var propHandle = createEnvironmentJob.OnReadyNewFlow(newFlowState); var propHandle = createEnvironmentSoJob.OnReadyNewFlow(newFlowState);
// Combine handles and return it // Combine handles and return it
return Task.WhenAll(playerHandle, propHandle); await Task.WhenAll(playerHandle, propHandle);
} }
return Task.CompletedTask;
} }
} }
} }

View File

@ -3,8 +3,8 @@
namespace DDD namespace DDD
{ {
[CreateAssetMenu(fileName = "CreateEnvironment", menuName = "GameFlow/CreateEnvironment")] [CreateAssetMenu(fileName = "CreateEnvironmentSo", menuName = "GameFlow/CreateEnvironmentSo")]
public class CreateEnvironment : GameFlowTask public class CreateEnvironmentSo : GameFlowTask
{ {
public override async Task OnReadyNewFlow(GameFlowState newFlowState) public override async Task OnReadyNewFlow(GameFlowState newFlowState)
{ {
@ -13,8 +13,7 @@ public override async Task OnReadyNewFlow(GameFlowState newFlowState)
var props = RestaurantController.Instance.RestaurantEnvironmentState.RestaurantEnvironmentProps; var props = RestaurantController.Instance.RestaurantEnvironmentState.RestaurantEnvironmentProps;
foreach (var prop in props) foreach (var prop in props)
{ {
var restaurantEnvironment = var restaurantEnvironment = Instantiate(baseRestaurantEnvironmentPrefab).GetComponent<RestaurantEnvironment>();
Instantiate(baseRestaurantEnvironmentPrefab).GetComponent<RestaurantEnvironment>();
restaurantEnvironment.Initialize(prop); restaurantEnvironment.Initialize(prop);
} }
} }