ProjectDDD/Assets/_DDD/_Scripts/GameUi/New/ItemViewModel.cs
2025-07-28 20:33:04 +09:00

105 lines
3.4 KiB
C#

using System.Collections.Generic;
using UnityEngine;
namespace DDD
{
public class ItemViewModel
{
public string Id;
public ItemType ItemType;
public int Count;
public RecipeType RecipeType => ItemType == ItemType.Recipe ? DataManager.Instance.RecipeDataSo.GetDataById(Id).RecipeType : RecipeType.None;
public Sprite ItemSprite
{
get
{
if (ItemType == ItemType.Recipe)
{
DataManager.Instance.RecipeDataSo.TryGetDataById(Id, out var recipe);
return DataManager.Instance.GetSprite(recipe.RecipeResult);
}
return DataManager.Instance.GetSprite(Id);
}
}
public string GetRecipeResultKey
{
get
{
if (ItemType != ItemType.Recipe) return null;
return DataManager.Instance.RecipeDataSo.GetDataById(Id).RecipeResult;
}
}
public List<TasteData> GetTasteDatas
{
get
{
switch (RecipeType)
{
case RecipeType.FoodRecipe:
return DataManager.Instance.FoodDataSo.GetDataById(GetRecipeResultKey).GetTasteDatas();
case RecipeType.DrinkRecipe:
return DataManager.Instance.DrinkDataSo.GetDataById(GetRecipeResultKey).GetTasteDatas();
}
return null;
}
}
public void UpdateCount(int newCount)
{
if (ItemType == ItemType.Recipe)
{
int craftableCount = 0;
if (RecipeType == RecipeType.FoodRecipe)
{
craftableCount = DataManager.Instance.FoodDataSo.GetDataById(Id).GetCraftableCount();
}
else if (RecipeType == RecipeType.DrinkRecipe)
{
craftableCount = DataManager.Instance.DrinkDataSo.GetDataById(Id).GetCraftableCount();
}
Count = craftableCount;
}
else if (ItemType == ItemType.Ingredient)
{
Count = newCount;
}
}
private List<IngredientEntry> GetCurrentIngredients()
{
if (ItemType != ItemType.Recipe) return null;
var resultKey = GetRecipeResultKey;
return RecipeType switch
{
RecipeType.FoodRecipe => DataManager.Instance.FoodDataSo.GetDataById(resultKey).GetIngredients(),
RecipeType.DrinkRecipe => DataManager.Instance.DrinkDataSo.GetDataById(resultKey).GetIngredients(),
_ => null
};
}
public bool TryConsumeIngredient(int count = 1)
{
return CraftingHelper.TryConsumeIngredients(GetCurrentIngredients(), count);
}
public int ConsumeAllCraftableIngredients()
{
var ingredients = GetCurrentIngredients();
int max = CraftingHelper.GetCraftableCount(ingredients);
return CraftingHelper.ConsumeAll(ingredients, max);
}
public void RefundIngredients(int count = 1)
{
CraftingHelper.RefundIngredients(GetCurrentIngredients(), count);
}
}
}