ProjectDDD/Assets/_DDD/_Scripts/GameState/RestaurantManagementSo.cs
2025-07-28 20:33:04 +09:00

112 lines
4.0 KiB
C#

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Sirenix.OdinInspector;
using UnityEngine;
namespace DDD
{
[CreateAssetMenu(fileName = "RestaurantManagementSo", menuName = "GameState/RestaurantManagementSo")]
public class RestaurantManagementSo : GameFlowTask
{
public ItemSlotUi ItemSlotUiPrefab;
[Title("선택된 메뉴 상세 내용")]
public TasteHashTagSlotUi TasteHashTagSlotUiPrefab;
public Color FoodTasteOutlineColor = Color.magenta;
public Color DrinkTasteOutlineColor = Color.magenta;
[Title("오늘의 메뉴")]
public int MaxFoodCount = 8;
public int MaxDrinkCount = 6;
private Dictionary<string, int> _foodRecipeIds = new();
private Dictionary<string, int> _drinkRecipeIds = new();
public IReadOnlyDictionary<string, int> FoodRecipeIds => _foodRecipeIds;
public IReadOnlyDictionary<string, int> DrinkRecipeIds => _drinkRecipeIds;
public override Task OnReadyNewFlow(GameFlowState newFlowState)
{
_foodRecipeIds.Clear();
_drinkRecipeIds.Clear();
return Task.CompletedTask;
}
public bool TryAddTodayMenu(IInventorySlotUi itemSlotUi)
{
string recipeId = itemSlotUi.Model.Id;
if (itemSlotUi.Model.ItemType != ItemType.Recipe) return false;
if (!DataManager.Instance.RecipeDataSo.TryGetDataById(recipeId, out RecipeData recipeData))
return false;
if (recipeData.RecipeType == RecipeType.FoodRecipe)
{
if (_foodRecipeIds.Count >= MaxFoodCount || _foodRecipeIds.ContainsKey(recipeId))
return false;
_foodRecipeIds[recipeId] = 1;
}
else if (recipeData.RecipeType == RecipeType.DrinkRecipe)
{
if (_drinkRecipeIds.Count >= MaxDrinkCount || _drinkRecipeIds.ContainsKey(recipeId))
return false;
_drinkRecipeIds[recipeId] = 1;
}
EventBus.Broadcast(RestaurantEvents.TodayMenuAddedEvent);
return true;
}
public bool TryRemoveTodayMenu(IInventorySlotUi itemSlotUi)
{
string recipeId = itemSlotUi.Model.Id;
var evt = RestaurantEvents.TodayMenuRemovedEvent;
if (!DataManager.Instance.RecipeDataSo.TryGetDataById(recipeId, out RecipeData recipeData)) return false;
bool removed = false;
int refundCount = 0;
if (recipeData.RecipeType == RecipeType.FoodRecipe)
{
if (_foodRecipeIds.TryGetValue(recipeId, out refundCount))
{
removed = _foodRecipeIds.Remove(recipeId);
evt.RecipeType = RecipeType.FoodRecipe;
if (removed)
{
var foodData = DataManager.Instance.FoodDataSo.GetDataById(recipeData.RecipeResult);
CraftingHelper.RefundIngredients(foodData.GetIngredients(), refundCount);
}
}
}
else if (recipeData.RecipeType == RecipeType.DrinkRecipe)
{
if (_drinkRecipeIds.TryGetValue(recipeId, out refundCount))
{
removed = _drinkRecipeIds.Remove(recipeId);
evt.RecipeType = RecipeType.DrinkRecipe;
if (removed)
{
var drinkData = DataManager.Instance.DrinkDataSo.GetDataById(recipeData.RecipeResult);
CraftingHelper.RefundIngredients(drinkData.GetIngredients(), refundCount);
}
}
}
if (!removed) return false;
EventBus.Broadcast(evt);
return true;
}
public bool IsContainTodayMenu(string recipeId)=> _foodRecipeIds.ContainsKey(recipeId) || _drinkRecipeIds.ContainsKey(recipeId);
}
}