diff --git a/Assets/_DDD/_Scripts/GameState/RestaurantManagementSo.cs b/Assets/_DDD/_Scripts/GameState/RestaurantManagementSo.cs index 571d5422d..2a1bf989c 100644 --- a/Assets/_DDD/_Scripts/GameState/RestaurantManagementSo.cs +++ b/Assets/_DDD/_Scripts/GameState/RestaurantManagementSo.cs @@ -20,20 +20,28 @@ public class RestaurantManagementSo : GameFlowTask public Color FoodTasteOutlineColor = Color.magenta; public Color DrinkTasteOutlineColor = Color.magenta; - [Title("오늘의 메뉴")] + [Title("오늘의 레스토랑 상태")] public int MaxFoodCount = 8; public int MaxDrinkCount = 6; + public int MaxCookwareCount = 6; - private Dictionary _foodRecipeIds = new(); - private Dictionary _drinkRecipeIds = new(); + [Title("실시간 데이터")] + [ReadOnly, ShowInInspector] private Dictionary _todayFoodRecipeIds = new(); + [ReadOnly, ShowInInspector] private Dictionary _todayDrinkRecipeIds = new(); + [ReadOnly, ShowInInspector] private List _todayWorkerIds = new(); + [ReadOnly, ShowInInspector] private List _todayCookwareIds = new(); - public IReadOnlyDictionary FoodRecipeIds => _foodRecipeIds; - public IReadOnlyDictionary DrinkRecipeIds => _drinkRecipeIds; + public IReadOnlyDictionary TodayFoodRecipeIds => _todayFoodRecipeIds; + public IReadOnlyDictionary TodayDrinkRecipeIds => _todayDrinkRecipeIds; + public IReadOnlyList TodayWorkerIds => _todayWorkerIds; + public IReadOnlyList TodayCookwareIds => _todayCookwareIds; public override Task OnReadyNewFlow(GameFlowState newFlowState) { - _foodRecipeIds.Clear(); - _drinkRecipeIds.Clear(); + _todayFoodRecipeIds.Clear(); + _todayDrinkRecipeIds.Clear(); + _todayWorkerIds.Clear(); + _todayCookwareIds.Clear(); return Task.CompletedTask; } @@ -50,24 +58,24 @@ public bool TryAddTodayMenu(ItemSlotUi itemSlotUi) if (recipeData.RecipeType == RecipeType.FoodRecipe) { - if (_foodRecipeIds.Count >= MaxFoodCount || _foodRecipeIds.ContainsKey(recipeId)) return false; + if (_todayFoodRecipeIds.Count >= MaxFoodCount || _todayFoodRecipeIds.ContainsKey(recipeId)) return false; var foodData = DataManager.Instance.GetDataSo().GetDataById(recipeData.RecipeResult); var craftableCount = foodData.GetCraftableCount(); foodData.ConsumeAllCraftableIngredients(); - _foodRecipeIds[recipeId] = craftableCount; + _todayFoodRecipeIds[recipeId] = craftableCount; added = true; } else if (recipeData.RecipeType == RecipeType.DrinkRecipe) { - if (_drinkRecipeIds.Count >= MaxDrinkCount || _drinkRecipeIds.ContainsKey(recipeId)) return false; + if (_todayDrinkRecipeIds.Count >= MaxDrinkCount || _todayDrinkRecipeIds.ContainsKey(recipeId)) return false; var drinkData = DataManager.Instance.GetDataSo().GetDataById(recipeData.RecipeResult); var craftableCount = drinkData.GetCraftableCount(); drinkData.ConsumeAllCraftableIngredients(); - _drinkRecipeIds[recipeId] = craftableCount; + _todayDrinkRecipeIds[recipeId] = craftableCount; added = true; } @@ -91,9 +99,9 @@ public bool TryRemoveTodayMenu(ItemSlotUi itemSlotUi) if (recipeData.RecipeType == RecipeType.FoodRecipe) { - if (_foodRecipeIds.TryGetValue(recipeId, out refundCount)) + if (_todayFoodRecipeIds.TryGetValue(recipeId, out refundCount)) { - removed = _foodRecipeIds.Remove(recipeId); + removed = _todayFoodRecipeIds.Remove(recipeId); evt.RecipeType = RecipeType.FoodRecipe; if (removed) @@ -105,9 +113,9 @@ public bool TryRemoveTodayMenu(ItemSlotUi itemSlotUi) } else if (recipeData.RecipeType == RecipeType.DrinkRecipe) { - if (_drinkRecipeIds.TryGetValue(recipeId, out refundCount)) + if (_todayDrinkRecipeIds.TryGetValue(recipeId, out refundCount)) { - removed = _drinkRecipeIds.Remove(recipeId); + removed = _todayDrinkRecipeIds.Remove(recipeId); evt.RecipeType = RecipeType.DrinkRecipe; if (removed) @@ -124,6 +132,33 @@ public bool TryRemoveTodayMenu(ItemSlotUi itemSlotUi) return true; } - public bool IsContainTodayMenu(string recipeId)=> _foodRecipeIds.ContainsKey(recipeId) || _drinkRecipeIds.ContainsKey(recipeId); + public bool TryAddTodayCookware(ItemSlotUi itemSlotUi) + { + var itemId = itemSlotUi.Model.Id; + + if (itemSlotUi.Model.Count <= 0 || DataManager.Instance.GetDataSo().TryGetDataById(itemId, out CookwareData cookwareData) == false) return false; + + if (_todayCookwareIds.Count >= MaxCookwareCount || _todayCookwareIds.Contains(itemId)) return false; + + _todayCookwareIds.Add(itemId); + EventBus.Broadcast(RestaurantEvents.TodayMenuAddedEvent); + + return true; + } + + public bool TryRemoveTodayCookware(ItemSlotUi itemSlotUi) + { + var itemId = itemSlotUi.Model.Id; + + if (DataManager.Instance.GetDataSo().TryGetDataById(itemId, out CookwareData cookwareData) == false) return false; + + if (_todayCookwareIds.Remove(itemId) == false) return false; + + EventBus.Broadcast( RestaurantEvents.TodayMenuRemovedEvent); + + return true; + } + + public bool IsContainTodayMenu(string recipeId)=> _todayFoodRecipeIds.ContainsKey(recipeId) || _todayDrinkRecipeIds.ContainsKey(recipeId); } } \ No newline at end of file diff --git a/Assets/_DDD/_Scripts/GameUi/RestaurantManagementUi/InventoryUi/InventorySlotUiStrategy.cs b/Assets/_DDD/_Scripts/GameUi/RestaurantManagementUi/InventoryUi/InventorySlotUiStrategy.cs index 31e81f536..41872e1d0 100644 --- a/Assets/_DDD/_Scripts/GameUi/RestaurantManagementUi/InventoryUi/InventorySlotUiStrategy.cs +++ b/Assets/_DDD/_Scripts/GameUi/RestaurantManagementUi/InventoryUi/InventorySlotUiStrategy.cs @@ -7,15 +7,12 @@ public class InventorySlotUiStrategy : IItemSlotUiStrategy { public string AnimatorControllerKey => "InventorySlotUi"; - public Task Setup(ItemSlotUi ui, ItemViewModel model) + public void Setup(ItemSlotUi ui, ItemViewModel model) { ui.SetIcon(model.ItemSprite); - ui.SetCount(model.Count); - ui.ShowCount(); + ui.ShowCountText(); ui.HideMark(); ui.SetButtonInteractable(true); - - return Task.CompletedTask; } public async Task GetAnimatorController() @@ -28,7 +25,7 @@ public void OnInventoryChanged(ItemSlotUi ui) if (ui.Model == null) return; ui.Model.UpdateCount(); - ui.SetCount(ui.Model.Count); + ui.ShowCountText(); } public bool CanCrafting(ItemSlotUi ui) diff --git a/Assets/_DDD/_Scripts/GameUi/RestaurantManagementUi/InventoryUi/InventoryView.cs b/Assets/_DDD/_Scripts/GameUi/RestaurantManagementUi/InventoryUi/InventoryView.cs index 7eb71742a..b20ed9504 100644 --- a/Assets/_DDD/_Scripts/GameUi/RestaurantManagementUi/InventoryUi/InventoryView.cs +++ b/Assets/_DDD/_Scripts/GameUi/RestaurantManagementUi/InventoryUi/InventoryView.cs @@ -50,8 +50,18 @@ public async Task Initialize() await slot.Initialize(model, new InventorySlotUiStrategy()); itemSlotUi.name = ItemSlotUiName + model.Id; - var interactor = itemSlotUi.GetComponent(); - interactor.Initialize(TodayMenuEventType.Add); + var interactor = itemSlotUi.GetComponent(); + if (model.ItemType == ItemType.Recipe) + { + await interactor.Initialize(TodayMenuEventType.Add, new TodayMenuInteractorStrategy()); + } + else + { + if (DataManager.Instance.GetDataSo().TryGetDataById(model.Id, out var cookwareData)) + { + await interactor.Initialize(TodayMenuEventType.Add, new TodayCookwareInteractorStrategy()); + } + } _slotLookup[model.Id] = slot; } diff --git a/Assets/_DDD/_Scripts/GameUi/RestaurantManagementUi/ItemDetailView.cs b/Assets/_DDD/_Scripts/GameUi/RestaurantManagementUi/ItemDetailView.cs index dd4dfabff..5b96ae205 100644 --- a/Assets/_DDD/_Scripts/GameUi/RestaurantManagementUi/ItemDetailView.cs +++ b/Assets/_DDD/_Scripts/GameUi/RestaurantManagementUi/ItemDetailView.cs @@ -3,7 +3,6 @@ using System.Threading.Tasks; using TMPro; using UnityEngine; -using UnityEngine.AddressableAssets; using UnityEngine.Localization.Components; using UnityEngine.UI; @@ -72,7 +71,7 @@ public async void Show(ItemViewModel model) { viewItemKey = _currentItemViewModel.GetRecipeResultKey; } - else if (_currentItemViewModel.ItemType == ItemType.Ingredient) + else { viewItemKey = _currentItemViewModel.Id; } diff --git a/Assets/_DDD/_Scripts/GameUi/RestaurantManagementUi/ItemUi/IInteractableUi.cs b/Assets/_DDD/_Scripts/GameUi/RestaurantManagementUi/ItemUi/IInteractableUi.cs index 9e1d80f65..73c87726d 100644 --- a/Assets/_DDD/_Scripts/GameUi/RestaurantManagementUi/ItemUi/IInteractableUi.cs +++ b/Assets/_DDD/_Scripts/GameUi/RestaurantManagementUi/ItemUi/IInteractableUi.cs @@ -1,7 +1,9 @@ +using System.Threading.Tasks; + namespace DDD { public interface IInteractableUi { - void OnInteract(); + Task OnInteract(); } } \ No newline at end of file diff --git a/Assets/_DDD/_Scripts/GameUi/RestaurantManagementUi/ItemUi/IItemSlotInteractorStrategy.cs b/Assets/_DDD/_Scripts/GameUi/RestaurantManagementUi/ItemUi/IItemSlotInteractorStrategy.cs new file mode 100644 index 000000000..c01242d4c --- /dev/null +++ b/Assets/_DDD/_Scripts/GameUi/RestaurantManagementUi/ItemUi/IItemSlotInteractorStrategy.cs @@ -0,0 +1,8 @@ +namespace DDD +{ + public interface IItemSlotInteractorStrategy + { + void OnAdded(ItemSlotUi itemSlotUi, RestaurantManagementSo restaurantManagementSo); + void OnRemoved(ItemSlotUi itemSlotUi, RestaurantManagementSo restaurantManagementSo); + } +} \ No newline at end of file diff --git a/Assets/_DDD/_Scripts/GameUi/RestaurantManagementUi/ItemUi/IItemSlotUiStrategy.cs b/Assets/_DDD/_Scripts/GameUi/RestaurantManagementUi/ItemUi/IItemSlotUiStrategy.cs index ac0ed5bf0..ba3b99566 100644 --- a/Assets/_DDD/_Scripts/GameUi/RestaurantManagementUi/ItemUi/IItemSlotUiStrategy.cs +++ b/Assets/_DDD/_Scripts/GameUi/RestaurantManagementUi/ItemUi/IItemSlotUiStrategy.cs @@ -6,7 +6,7 @@ namespace DDD public interface IItemSlotUiStrategy { string AnimatorControllerKey { get; } - Task Setup(ItemSlotUi ui, ItemViewModel model); + void Setup(ItemSlotUi ui, ItemViewModel model); Task GetAnimatorController(); } } \ No newline at end of file diff --git a/Assets/_DDD/_Scripts/GameUi/RestaurantManagementUi/TodayMenuUi/TodayMenuInteractor.cs b/Assets/_DDD/_Scripts/GameUi/RestaurantManagementUi/ItemUi/ItemSlotInteractor.cs similarity index 53% rename from Assets/_DDD/_Scripts/GameUi/RestaurantManagementUi/TodayMenuUi/TodayMenuInteractor.cs rename to Assets/_DDD/_Scripts/GameUi/RestaurantManagementUi/ItemUi/ItemSlotInteractor.cs index 553dd721a..c913b6f3c 100644 --- a/Assets/_DDD/_Scripts/GameUi/RestaurantManagementUi/TodayMenuUi/TodayMenuInteractor.cs +++ b/Assets/_DDD/_Scripts/GameUi/RestaurantManagementUi/ItemUi/ItemSlotInteractor.cs @@ -12,66 +12,45 @@ public enum TodayMenuEventType Remove } - public class TodayMenuInteractor : MonoBehaviour, IInteractableUi + public class ItemSlotInteractor : MonoBehaviour, IInteractableUi { private ItemSlotUi _itemSlotUi; private RestaurantManagementSo _restaurantManagementSo; private TaskCompletionSource _isInitialized = new(); private TodayMenuEventType _todayMenuEventType = TodayMenuEventType.None; + + public IItemSlotInteractorStrategy Strategy { get; private set; } private void Awake() { _itemSlotUi = GetComponent(); } - public async void Initialize(TodayMenuEventType todayMenuEventType) + public async Task Initialize(TodayMenuEventType todayMenuEventType, IItemSlotInteractorStrategy strategy) { _todayMenuEventType = todayMenuEventType; + Strategy = strategy; _restaurantManagementSo = await AssetManager.LoadAsset(DataConstants.RestaurantManagementSo); _isInitialized.SetResult(true); } - public async void OnInteract() + public async Task OnInteract() { await _isInitialized.Task; switch (_todayMenuEventType) { case TodayMenuEventType.Add: - OnAdded(); + Strategy.OnAdded(_itemSlotUi, _restaurantManagementSo); break; case TodayMenuEventType.Remove: - OnRemoved(); + Strategy.OnRemoved(_itemSlotUi, _restaurantManagementSo); break; case TodayMenuEventType.None: default: throw new ArgumentOutOfRangeException(); } } - - private void OnAdded() - { - if (_itemSlotUi.Strategy is not InventorySlotUiStrategy inventorySlotUiStrategy) return; - - if (inventorySlotUiStrategy.CanCrafting(_itemSlotUi)) - { - _restaurantManagementSo.TryAddTodayMenu(_itemSlotUi); - } - else - { - var evt = GameEvents.RequestShowGlobalMessageEvent; - // TODO : 테스트용 메세지 추후 삭제 및 변경 - evt.NewMessageKey = "today_menu_added_error_message_001"; - evt.FadeDuration = 0.5f; - evt.ShowDuration = 1f; - EventBus.Broadcast(evt); - } - } - - private void OnRemoved() - { - _restaurantManagementSo.TryRemoveTodayMenu(_itemSlotUi); - } } } \ No newline at end of file diff --git a/Assets/_DDD/_Scripts/GameUi/RestaurantManagementUi/ItemUi/ItemSlotUi.cs b/Assets/_DDD/_Scripts/GameUi/RestaurantManagementUi/ItemUi/ItemSlotUi.cs index c00278980..5932d68b3 100644 --- a/Assets/_DDD/_Scripts/GameUi/RestaurantManagementUi/ItemUi/ItemSlotUi.cs +++ b/Assets/_DDD/_Scripts/GameUi/RestaurantManagementUi/ItemUi/ItemSlotUi.cs @@ -1,8 +1,6 @@ -using System; using System.Threading.Tasks; using TMPro; using UnityEngine; -using UnityEngine.AddressableAssets; using UnityEngine.EventSystems; using UnityEngine.UI; @@ -17,7 +15,7 @@ public class ItemSlotUi : MonoBehaviour, ISelectHandler [SerializeField] private Image _markImage; [SerializeField] private Animator _animator; - public ItemViewModel Model { get; private set; } + [field: SerializeField] public ItemViewModel Model { get; private set; } public IItemSlotUiStrategy Strategy { get; private set; } public async Task Initialize(ItemViewModel model, IItemSlotUiStrategy strategy) @@ -27,17 +25,18 @@ public async Task Initialize(ItemViewModel model, IItemSlotUiStrategy strategy) var controller = await strategy.GetAnimatorController(); _animator.runtimeAnimatorController = controller; - await Strategy.Setup(this, model); + Strategy.Setup(this, model); } public void SetIcon(Sprite sprite) => _icon.sprite = sprite; - public void SetCount(int count) + public void ShowCountText() { + int count = Model.Count; _countText.text = count.ToString(); _countText.color = count > 0 ? Color.white : Color.red; + _countText.gameObject.SetActive(true); } - public void ShowCount() => _countText.gameObject.SetActive(true); - public void HideCount() => _countText.gameObject.SetActive(false); + public void HideCountText() => _countText.gameObject.SetActive(false); public void ShowMark(Sprite sprite) { _markImage.sprite = sprite; diff --git a/Assets/_DDD/_Scripts/GameUi/RestaurantManagementUi/ItemUi/ItemViewModel.cs b/Assets/_DDD/_Scripts/GameUi/RestaurantManagementUi/ItemUi/ItemViewModel.cs index 35201c208..797234782 100644 --- a/Assets/_DDD/_Scripts/GameUi/RestaurantManagementUi/ItemUi/ItemViewModel.cs +++ b/Assets/_DDD/_Scripts/GameUi/RestaurantManagementUi/ItemUi/ItemViewModel.cs @@ -1,13 +1,29 @@ +using System; using System.Collections.Generic; using UnityEngine; namespace DDD { + [Serializable] public class ItemViewModel { - public string Id; - public ItemType ItemType; - public int Count; + [field: SerializeField] public string Id { get; private set; } + [field: SerializeField] public ItemType ItemType { get; private set; } + [field: SerializeField] public int Count { get; private set; } + + public ItemViewModel(string id, ItemType itemType, int count) + { + Id = id; + ItemType = itemType; + Count = count; + } + + public ItemViewModel(string id, ItemType itemType) + { + Id = id; + ItemType = itemType; + Count = 0; + } public RecipeType RecipeType => ItemType == ItemType.Recipe ? DataManager.Instance.GetDataSo().GetDataById(Id).RecipeType : RecipeType.None; public Sprite ItemSprite @@ -70,6 +86,8 @@ public List GetTasteDatas } } + public void SetCount(int count) => Count = count; + public void UpdateCount() { if (ItemType == ItemType.Recipe) @@ -90,7 +108,7 @@ public void UpdateCount() Count = craftableCount; } - else if (ItemType == ItemType.Ingredient) + else { Count = InventoryManager.Instance.GetItemCount(Id); } diff --git a/Assets/_DDD/_Scripts/GameUi/RestaurantManagementUi/ItemUi/ItemViewModelFactory.cs b/Assets/_DDD/_Scripts/GameUi/RestaurantManagementUi/ItemUi/ItemViewModelFactory.cs index 9291e13a6..4fca1b3d4 100644 --- a/Assets/_DDD/_Scripts/GameUi/RestaurantManagementUi/ItemUi/ItemViewModelFactory.cs +++ b/Assets/_DDD/_Scripts/GameUi/RestaurantManagementUi/ItemUi/ItemViewModelFactory.cs @@ -1,5 +1,4 @@ using System.Collections.Generic; -using System.Linq; namespace DDD { @@ -8,28 +7,18 @@ public static class ItemViewModelFactory public static List CreateRestaurantManagementInventoryItem() { var result = new List(); - var recipeDataMap = DataManager.Instance.GetDataSo().GetDataList().ToDictionary(r => r.Id, r => r); - var foodDataMap = DataManager.Instance.GetDataSo().GetDataList().ToDictionary(f => f.Id, f => f); - var drinkDataMap = DataManager.Instance.GetDataSo().GetDataList().ToDictionary(d => d.Id, d => d); - var ingredientDataMap = DataManager.Instance.GetDataSo().GetDataList().ToDictionary(i => i.Id, i => i); - foreach (var kvp in InventoryManager.Instance.InventoryItems) { var id = kvp.Key; var item = InventoryManager.Instance.GetItemDataByIdOrNull(id); if (item == null) continue; - var model = new ItemViewModel + var modelCount = item.ItemType switch { - Id = item.Id, - ItemType = item.ItemType, - Count = item.ItemType switch - { - ItemType.Recipe => CalculateCraftableCount(item.Id), - ItemType.Ingredient => InventoryManager.Instance.GetItemCount(id), - _ => 0 - } + ItemType.Recipe => CalculateCraftableCount(item.Id), + _ => InventoryManager.Instance.GetItemCount(id) }; + var model = new ItemViewModel(item.Id, item.ItemType, modelCount); result.Add(model); } @@ -55,20 +44,15 @@ private static int CalculateCraftableCount(string recipeId) }; } - public static ItemViewModel CreateByRecipeId(string recipeId) + public static ItemViewModel CreateByItemId(string itemId) { - var recipeSo = DataManager.Instance.GetDataSo(); - if (!recipeSo.TryGetDataById(recipeId, out var recipe)) return null; - - var item = InventoryManager.Instance.GetItemDataByIdOrNull(recipeId); - if (item == null) return null; - - var model = new ItemViewModel - { - Id = recipeId, - ItemType = item.ItemType, - }; + var itemSo = DataManager.Instance.GetDataSo(); + if (!itemSo.TryGetDataById(itemId, out var itemData)) return null; + + if (InventoryManager.Instance.GetItemDataByIdOrNull(itemId) == null) return null; + var model = new ItemViewModel(itemId, itemData.ItemType); + return model; } } diff --git a/Assets/_DDD/_Scripts/GameUi/RestaurantManagementUi/RestaurantManagementUi.cs b/Assets/_DDD/_Scripts/GameUi/RestaurantManagementUi/RestaurantManagementUi.cs index 17b53c848..aef8589e5 100644 --- a/Assets/_DDD/_Scripts/GameUi/RestaurantManagementUi/RestaurantManagementUi.cs +++ b/Assets/_DDD/_Scripts/GameUi/RestaurantManagementUi/RestaurantManagementUi.cs @@ -2,7 +2,6 @@ using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.InputSystem; -using UnityEngine.Serialization; namespace DDD { @@ -19,7 +18,12 @@ protected override GameObject GetInitialSelected() var inventoryViewInitialSelectedObject = _inventoryView.GetInitialSelected(); if (inventoryViewInitialSelectedObject) return inventoryViewInitialSelectedObject; - return _menuCategoryTabs.GetFirstInteractableButton; + if (_menuCategoryTabs.GetFirstInteractableButton.activeInHierarchy) + { + return _menuCategoryTabs.GetFirstInteractableButton; + } + + return _cookwareCategoryTabs.GetFirstInteractableButton; } public async override void Open() diff --git a/Assets/_DDD/_Scripts/GameUi/RestaurantManagementUi/TabUi/TabButtonUi.cs b/Assets/_DDD/_Scripts/GameUi/RestaurantManagementUi/TabUi/TabButtonUi.cs index 948a3a258..68b52e035 100644 --- a/Assets/_DDD/_Scripts/GameUi/RestaurantManagementUi/TabUi/TabButtonUi.cs +++ b/Assets/_DDD/_Scripts/GameUi/RestaurantManagementUi/TabUi/TabButtonUi.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Threading.Tasks; using TMPro; using UnityEngine; using UnityEngine.Serialization; @@ -85,9 +86,10 @@ private void SetActiveContents(bool isActive) content.SetActive(isActive); } } - public void OnInteract() + public Task OnInteract() { _onSelected?.Invoke(TabType); + return Task.CompletedTask; } } } \ No newline at end of file diff --git a/Assets/_DDD/_Scripts/GameUi/RestaurantManagementUi/TodayMenuUi/TodayMenuInteractorStrategy.cs b/Assets/_DDD/_Scripts/GameUi/RestaurantManagementUi/TodayMenuUi/TodayMenuInteractorStrategy.cs new file mode 100644 index 000000000..5e7f32d60 --- /dev/null +++ b/Assets/_DDD/_Scripts/GameUi/RestaurantManagementUi/TodayMenuUi/TodayMenuInteractorStrategy.cs @@ -0,0 +1,31 @@ +namespace DDD +{ + public class TodayMenuInteractorStrategy : IItemSlotInteractorStrategy + { + public void OnAdded(ItemSlotUi itemSlotUi, RestaurantManagementSo restaurantManagementSo) + { + if (itemSlotUi.Strategy is not InventorySlotUiStrategy inventorySlotUiStrategy) return; + + if (inventorySlotUiStrategy.CanCrafting(itemSlotUi)) + { + restaurantManagementSo.TryAddTodayMenu(itemSlotUi); + } + else + { + var evt = GameEvents.RequestShowGlobalMessageEvent; + // TODO : 테스트용 메세지 추후 삭제 및 변경 + evt.NewMessageKey = "today_menu_added_error_message_001"; + evt.FadeDuration = 0.5f; + evt.ShowDuration = 1f; + EventBus.Broadcast(evt); + } + } + + public void OnRemoved(ItemSlotUi itemSlotUi, RestaurantManagementSo restaurantManagementSo) + { + if (itemSlotUi.Strategy is InventorySlotUiStrategy) return; + + restaurantManagementSo.TryRemoveTodayMenu(itemSlotUi); + } + } +} \ No newline at end of file diff --git a/Assets/_DDD/_Scripts/GameUi/RestaurantManagementUi/TodayMenuUi/TodayMenuSlotUiStrategy.cs b/Assets/_DDD/_Scripts/GameUi/RestaurantManagementUi/TodayMenuUi/TodayMenuSlotUiStrategy.cs index 3d71ac4af..b95fb9b9e 100644 --- a/Assets/_DDD/_Scripts/GameUi/RestaurantManagementUi/TodayMenuUi/TodayMenuSlotUiStrategy.cs +++ b/Assets/_DDD/_Scripts/GameUi/RestaurantManagementUi/TodayMenuUi/TodayMenuSlotUiStrategy.cs @@ -1,12 +1,10 @@ using System.Threading.Tasks; using UnityEngine; -using UnityEngine.AddressableAssets; namespace DDD { public class TodayMenuSlotUiStrategy : IItemSlotUiStrategy { - private RestaurantManagementSo _restaurantManagementSo; private readonly RecipeType _recipeType; public string AnimatorControllerKey => "TodayMenuSlotUi"; @@ -16,13 +14,8 @@ public TodayMenuSlotUiStrategy(RecipeType recipeType) _recipeType = recipeType; } - public async Task Setup(ItemSlotUi ui, ItemViewModel model) + public void Setup(ItemSlotUi ui, ItemViewModel model) { - if (!_restaurantManagementSo) - { - _restaurantManagementSo = await AssetManager.LoadAsset(DataConstants.RestaurantManagementSo); - } - if (model == null) { string emptySpriteKey = null; @@ -36,14 +29,14 @@ public async Task Setup(ItemSlotUi ui, ItemViewModel model) } ui.SetIcon(DataManager.Instance.GetSprite(emptySpriteKey)); - ui.HideCount(); + ui.HideCountText(); ui.HideMark(); ui.SetButtonInteractable(false); return; } ui.SetIcon(model.ItemSprite); - ui.HideCount(); + ui.HideCountText(); ui.ShowMark(DataManager.Instance.GetSprite(SpriteConstants.CheckNoSpriteKey)); // TODO : 추후에 장비와 매칭 ui.SetButtonInteractable(true); } diff --git a/Assets/_DDD/_Scripts/GameUi/RestaurantManagementUi/TodayMenuUi/TodayMenuView.cs b/Assets/_DDD/_Scripts/GameUi/RestaurantManagementUi/TodayMenuUi/TodayMenuView.cs index c67ded6d4..e26edd42b 100644 --- a/Assets/_DDD/_Scripts/GameUi/RestaurantManagementUi/TodayMenuUi/TodayMenuView.cs +++ b/Assets/_DDD/_Scripts/GameUi/RestaurantManagementUi/TodayMenuUi/TodayMenuView.cs @@ -1,7 +1,6 @@ using System.Collections.Generic; using System.Threading.Tasks; using UnityEngine; -using UnityEngine.AddressableAssets; namespace DDD { @@ -36,15 +35,15 @@ private async Task Initialize() Destroy(child.gameObject); } - int maxFoodCount = _restaurantManagementSo.MaxFoodCount; + int maxFoodCount = _restaurantManagementSo!.MaxFoodCount; _foodSlots = new List(maxFoodCount); for (int i = 0; i < _restaurantManagementSo.MaxFoodCount; i++) { var go = Instantiate(_restaurantManagementSo.ItemSlotUiPrefab, _todayFoodContent); var slot = go.GetComponent(); await slot.Initialize(null, new TodayMenuSlotUiStrategy(RecipeType.FoodRecipe)); - var todayMenuInteractor = go.GetComponent(); - todayMenuInteractor.Initialize(TodayMenuEventType.Remove); + var itemSlotInteractor = go.GetComponent(); + await itemSlotInteractor.Initialize(TodayMenuEventType.Remove, new TodayMenuInteractorStrategy()); _foodSlots.Add(slot); } @@ -61,8 +60,8 @@ private async Task Initialize() var go = Instantiate(_restaurantManagementSo.ItemSlotUiPrefab, _todayDrinkContent); var slot = go.GetComponent(); await slot.Initialize(null, new TodayMenuSlotUiStrategy(RecipeType.DrinkRecipe)); - var todayMenuInteractor = go.GetComponent(); - todayMenuInteractor.Initialize(TodayMenuEventType.Remove); + var itemSlotInteractor = go.GetComponent(); + await itemSlotInteractor.Initialize(TodayMenuEventType.Remove, new TodayMenuInteractorStrategy()); _drinkSlots.Add(slot); } @@ -86,41 +85,37 @@ public void Invoke(TodayMenuRemovedEvent evt) private void UpdateView() { int foodIndex = 0; - foreach (var kvp in _restaurantManagementSo.FoodRecipeIds) + foreach (var foodRecipeIdCountPair in _restaurantManagementSo.TodayFoodRecipeIds) { if (foodIndex >= _foodSlots.Count) break; - - string recipeId = kvp.Key; - - var model = ItemViewModelFactory.CreateByRecipeId(recipeId); + + var model = ItemViewModelFactory.CreateByItemId(foodRecipeIdCountPair.Key); var foodSlot = _foodSlots[foodIndex]; _ = foodSlot.Initialize(model, new TodayMenuSlotUiStrategy(RecipeType.FoodRecipe)); - foodSlot.SetCount(kvp.Value); + foodSlot.Model.SetCount(foodRecipeIdCountPair.Value); foodIndex++; } for (int i = foodIndex; i < _foodSlots.Count; i++) { - _ = _foodSlots[i].Initialize(null, new TodayMenuSlotUiStrategy(RecipeType.FoodRecipe)); + _ = _foodSlots[i].Initialize(null, new TodayMenuSlotUiStrategy(RecipeType.FoodRecipe)); } int drinkIndex = 0; - foreach (var kvp in _restaurantManagementSo.DrinkRecipeIds) + foreach (var drinkRecipeIdCountPair in _restaurantManagementSo.TodayDrinkRecipeIds) { if (drinkIndex >= _drinkSlots.Count) break; - - string recipeId = kvp.Key; - - var model = ItemViewModelFactory.CreateByRecipeId(recipeId); + + var model = ItemViewModelFactory.CreateByItemId(drinkRecipeIdCountPair.Key); var drinkSlot = _drinkSlots[drinkIndex]; _ = drinkSlot.Initialize(model, new TodayMenuSlotUiStrategy(RecipeType.DrinkRecipe)); - drinkSlot.SetCount(kvp.Value); + drinkSlot.Model.SetCount(drinkRecipeIdCountPair.Value); drinkIndex++; } for (int i = drinkIndex; i < _drinkSlots.Count; i++) { - _ = _drinkSlots[i].Initialize(null, new TodayMenuSlotUiStrategy(RecipeType.DrinkRecipe)); + _ = _drinkSlots[i].Initialize(null, new TodayMenuSlotUiStrategy(RecipeType.DrinkRecipe)); } } } diff --git a/Assets/_DDD/_Scripts/GameUi/RestaurantManagementUi/TodayRestaurantStateUi/TodayCookwareInteractorStrategy.cs b/Assets/_DDD/_Scripts/GameUi/RestaurantManagementUi/TodayRestaurantStateUi/TodayCookwareInteractorStrategy.cs new file mode 100644 index 000000000..45ad3c423 --- /dev/null +++ b/Assets/_DDD/_Scripts/GameUi/RestaurantManagementUi/TodayRestaurantStateUi/TodayCookwareInteractorStrategy.cs @@ -0,0 +1,31 @@ +namespace DDD +{ + public class TodayCookwareInteractorStrategy : IItemSlotInteractorStrategy + { + public void OnAdded(ItemSlotUi itemSlotUi, RestaurantManagementSo restaurantManagementSo) + { + if (itemSlotUi.Strategy is not InventorySlotUiStrategy inventorySlotUiStrategy) return; + + if (inventorySlotUiStrategy.CanCrafting(itemSlotUi)) + { + restaurantManagementSo.TryAddTodayCookware(itemSlotUi); + } + else + { + var evt = GameEvents.RequestShowGlobalMessageEvent; + // TODO : 테스트용 메세지 추후 삭제 및 변경 + evt.NewMessageKey = "today_menu_added_error_message_001"; + evt.FadeDuration = 0.5f; + evt.ShowDuration = 1f; + EventBus.Broadcast(evt); + } + } + + public void OnRemoved(ItemSlotUi itemSlotUi, RestaurantManagementSo restaurantManagementSo) + { + if (itemSlotUi.Strategy is InventorySlotUiStrategy) return; + + restaurantManagementSo.TryRemoveTodayCookware(itemSlotUi); + } + } +} \ No newline at end of file diff --git a/Assets/_DDD/_Scripts/GameUi/RestaurantManagementUi/TodayRestaurantStateUi/TodayCookwareSlotUiStrategy.cs b/Assets/_DDD/_Scripts/GameUi/RestaurantManagementUi/TodayRestaurantStateUi/TodayCookwareSlotUiStrategy.cs new file mode 100644 index 000000000..445f90a50 --- /dev/null +++ b/Assets/_DDD/_Scripts/GameUi/RestaurantManagementUi/TodayRestaurantStateUi/TodayCookwareSlotUiStrategy.cs @@ -0,0 +1,34 @@ +using System.Threading.Tasks; +using UnityEngine; + +namespace DDD +{ + public class TodayCookwareSlotUiStrategy : IItemSlotUiStrategy + { + private RestaurantManagementSo _restaurantManagementSo; + + public string AnimatorControllerKey => "TodayMenuSlotUi"; + + public void Setup(ItemSlotUi ui, ItemViewModel model) + { + if (model == null) + { + ui.SetIcon(DataManager.Instance.GetSprite(SpriteConstants.EmptyFoodSpriteKey)); + ui.HideCountText(); + ui.HideMark(); + ui.SetButtonInteractable(false); + return; + } + + ui.SetIcon(model.ItemSprite); + ui.HideCountText(); + ui.ShowMark(DataManager.Instance.GetSprite(SpriteConstants.CheckNoSpriteKey)); // TODO : 추후에 장비와 매칭 + ui.SetButtonInteractable(true); + } + + public async Task GetAnimatorController() + { + return await AssetManager.LoadAsset(AnimatorControllerKey); + } + } +} \ No newline at end of file diff --git a/Assets/_DDD/_Scripts/GameUi/RestaurantManagementUi/TodayRestaurantStateUi/TodayRestaurantStateView.cs b/Assets/_DDD/_Scripts/GameUi/RestaurantManagementUi/TodayRestaurantStateUi/TodayRestaurantStateView.cs new file mode 100644 index 000000000..2de616ab6 --- /dev/null +++ b/Assets/_DDD/_Scripts/GameUi/RestaurantManagementUi/TodayRestaurantStateUi/TodayRestaurantStateView.cs @@ -0,0 +1,121 @@ +using System.Collections.Generic; +using System.Threading.Tasks; +using UnityEngine; + +namespace DDD +{ + public class TodayRestaurantStateView : MonoBehaviour, IEventHandler, IEventHandler + { + [SerializeField] private Transform _todayWorkerContent; + [SerializeField] private Transform _todayCookwareContent; + + private List _workerSlots; + private List _cookwareSlots; + + private RestaurantManagementSo _restaurantManagementSo; + + private void Start() + { + _ = Initialize(); + } + + private void OnDestroy() + { + EventBus.Unregister(this); + EventBus.Unregister(this); + } + + private async Task Initialize() + { + _restaurantManagementSo = await AssetManager.LoadAsset(DataConstants.RestaurantManagementSo); + Debug.Assert(_restaurantManagementSo != null, "_restaurantManagementSo != null"); + + foreach (Transform child in _todayWorkerContent) + { + Destroy(child.gameObject); + } + + int maxCookwareCount = _restaurantManagementSo!.MaxCookwareCount; + _workerSlots = new List(maxCookwareCount); + for (int i = 0; i < _restaurantManagementSo.MaxCookwareCount; i++) + { + var go = Instantiate(_restaurantManagementSo.ItemSlotUiPrefab, _todayWorkerContent); + var slot = go.GetComponent(); + await slot.Initialize(null, new TodayWorkerSlotUiStrategy()); + var itemSlotInteractor = go.GetComponent(); + await itemSlotInteractor.Initialize(TodayMenuEventType.Remove, new TodayCookwareInteractorStrategy()); + + _workerSlots.Add(slot); + } + + foreach (Transform child in _todayCookwareContent) + { + Destroy(child.gameObject); + } + + _cookwareSlots = new List(maxCookwareCount); + for (int i = 0; i < _restaurantManagementSo.MaxCookwareCount; i++) + { + var go = Instantiate(_restaurantManagementSo.ItemSlotUiPrefab, _todayCookwareContent); + var slot = go.GetComponent(); + await slot.Initialize(null, new TodayCookwareSlotUiStrategy()); + var itemSlotInteractor = go.GetComponent(); + await itemSlotInteractor.Initialize(TodayMenuEventType.Remove, new TodayCookwareInteractorStrategy()); + + _cookwareSlots.Add(slot); + } + + UpdateView(); + + EventBus.Register(this); + EventBus.Register(this); + } + + public void Invoke(TodayMenuAddedEvent evt) + { + UpdateView(); + } + + public void Invoke(TodayMenuRemovedEvent evt) + { + UpdateView(); + } + + private void UpdateView() + { + int workerIndex = 0; + foreach (var workerKey in _restaurantManagementSo.TodayWorkerIds) + { + if (workerIndex >= _workerSlots.Count) break; + + var model = ItemViewModelFactory.CreateByItemId(workerKey); + var newWorkerSlot = _workerSlots[workerIndex]; + _ = newWorkerSlot.Initialize(model, new TodayWorkerSlotUiStrategy()); + newWorkerSlot.Model.SetCount(1); + workerIndex++; + } + + for (int i = workerIndex; i < _workerSlots.Count; i++) + { + _ = _workerSlots[i].Initialize(null, new TodayWorkerSlotUiStrategy()); + } + + int cookwareIndex = 0; + foreach (var cookwareKey in _restaurantManagementSo.TodayCookwareIds) + { + if (cookwareIndex >= _cookwareSlots.Count) break; + + var model = ItemViewModelFactory.CreateByItemId(cookwareKey); + var newCookwareSlot = _cookwareSlots[cookwareIndex]; + _ = newCookwareSlot.Initialize(model, new TodayCookwareSlotUiStrategy()); + newCookwareSlot.Model.SetCount(1); + cookwareIndex++; + } + + for (int i = cookwareIndex; i < _cookwareSlots.Count; i++) + { + _ = _cookwareSlots[i].Initialize(null, new TodayCookwareSlotUiStrategy()); + } + } + } +} \ No newline at end of file diff --git a/Assets/_DDD/_Scripts/GameUi/RestaurantManagementUi/TodayRestaurantStateUi/TodayWorkerSlotUiStrategy.cs b/Assets/_DDD/_Scripts/GameUi/RestaurantManagementUi/TodayRestaurantStateUi/TodayWorkerSlotUiStrategy.cs new file mode 100644 index 000000000..62ed0e67b --- /dev/null +++ b/Assets/_DDD/_Scripts/GameUi/RestaurantManagementUi/TodayRestaurantStateUi/TodayWorkerSlotUiStrategy.cs @@ -0,0 +1,34 @@ +using System.Threading.Tasks; +using UnityEngine; + +namespace DDD +{ + public class TodayWorkerSlotUiStrategy : IItemSlotUiStrategy + { + private RestaurantManagementSo _restaurantManagementSo; + + public string AnimatorControllerKey => "TodayMenuSlotUi"; + + public void Setup(ItemSlotUi ui, ItemViewModel model) + { + if (model == null) + { + ui.SetIcon(DataManager.Instance.GetSprite(SpriteConstants.EmptyFoodSpriteKey)); // TODO : 점원 빈칸 이미지로 변경 + ui.HideCountText(); + ui.HideMark(); + ui.SetButtonInteractable(false); + return; + } + + ui.SetIcon(model.ItemSprite); + ui.HideCountText(); + ui.ShowMark(DataManager.Instance.GetSprite(SpriteConstants.CheckNoSpriteKey)); // TODO : 추후에 장비와 매칭 + ui.SetButtonInteractable(true); + } + + public async Task GetAnimatorController() + { + return await AssetManager.LoadAsset(AnimatorControllerKey); + } + } +} \ No newline at end of file