ProjectDDD/Assets/_DDD/_Scripts/GameState/RestaurantManagementSo.cs

129 lines
4.7 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
{
// TODO : 체크리스트 기능
// TODO : 조리도구 등록, 해제 기능
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(ItemSlotUi itemSlotUi)
{
string recipeId = itemSlotUi.Model.Id;
if (itemSlotUi.Model.ItemType != ItemType.Recipe) return false;
if (!DataManager.Instance.GetDataSo<RecipeDataSo>().TryGetDataById(recipeId, out RecipeData recipeData)) return false;
bool added = false;
if (recipeData.RecipeType == RecipeType.FoodRecipe)
{
if (_foodRecipeIds.Count >= MaxFoodCount || _foodRecipeIds.ContainsKey(recipeId)) return false;
var foodData = DataManager.Instance.GetDataSo<FoodDataSo>().GetDataById(recipeData.RecipeResult);
var craftableCount = foodData.GetCraftableCount();
foodData.ConsumeAllCraftableIngredients();
_foodRecipeIds[recipeId] = craftableCount;
added = true;
}
else if (recipeData.RecipeType == RecipeType.DrinkRecipe)
{
if (_drinkRecipeIds.Count >= MaxDrinkCount || _drinkRecipeIds.ContainsKey(recipeId)) return false;
var drinkData = DataManager.Instance.GetDataSo<DrinkDataSo>().GetDataById(recipeData.RecipeResult);
var craftableCount = drinkData.GetCraftableCount();
drinkData.ConsumeAllCraftableIngredients();
_drinkRecipeIds[recipeId] = craftableCount;
added = true;
}
if (added)
{
EventBus.Broadcast(RestaurantEvents.TodayMenuAddedEvent);
}
return added;
}
public bool TryRemoveTodayMenu(ItemSlotUi itemSlotUi)
{
string recipeId = itemSlotUi.Model.Id;
var evt = RestaurantEvents.TodayMenuRemovedEvent;
if (!DataManager.Instance.GetDataSo<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.GetDataSo<FoodDataSo>().GetDataById(recipeData.RecipeResult);
foodData.RefundIngredients(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.GetDataSo<DrinkDataSo>().GetDataById(recipeData.RecipeResult);
drinkData.RefundIngredients(refundCount);
}
}
}
if (!removed) return false;
EventBus.Broadcast(evt);
return true;
}
public bool IsContainTodayMenu(string recipeId)=> _foodRecipeIds.ContainsKey(recipeId) || _drinkRecipeIds.ContainsKey(recipeId);
}
}