CapersProject/Assets/02.Scripts/Ui/Tycoon/CookUi.cs
Nam Tae Gun d7010a279b 타이쿤 추가 업데이트 내용
+ 화면 밖에서 손님이 요구하는 중일 때, Indicator를 통해서 Ui 표시
+ Open, Closed Ui 추가 및 기능 연결
+ 테이블 찾는 로직 변경 (전부 랜덤) - 기존에는 항상 같은 순서로 자리를 채움
+ 통계용 데이터 CustomerVisitInfo 추가 (추후에 통계Ui 생길 때 연결)
+ 대화 조건 변경
+ 일부 가구들 상호작용 조건 변경

+ Outline shader Render Face(Front -> Both 변경 - Front면 x축 뒤집는 경우 안나옴)
+ GraphicMaterialOverride를 사용하는 경우, 에디터에서 전체화면 등 특정 상황에서 material이 사라지는 버그 수정
+ InteractionFuniture Open, Closed 공통 기능으로 병합
2024-07-22 09:42:29 +09:00

305 lines
11 KiB
C#

using System;
using System.Collections.Generic;
using BlueWater.Items;
using Sirenix.OdinInspector;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
namespace BlueWater.Uis
{
public class CookUi : MonoBehaviour
{
[Title("컴포넌트")]
[SerializeField, Required]
private Image _selectedFoodImage;
[SerializeField, Required]
private TMP_Text _selectedFoodName;
[SerializeField, Required]
private TMP_Text _selectedFoodTasteText;
[SerializeField, Required]
private TMP_Dropdown _finishedFoodSortDropdown;
[SerializeField, Required]
private Button _cookButton;
[SerializeField, Required]
private TMP_Text _cookText;
[Title("프리팹 생성 위치")]
[SerializeField, Required]
private Transform _finishedFoodSlotLocation;
[SerializeField, Required]
private Transform _ingredientFoodSlotLocation;
[Title("프리팹")]
[SerializeField, Required]
private TycoonItemSlotUi _finishedFoodSlotUi;
[SerializeField, Required]
private IngredientItemSlotUi _ingredientItemSlotUi;
private List<TycoonItemSlotUi> _finishedFoodSlotUis;
private List<IngredientItemSlotUi> _highIngredientSlotUis;
private List<IngredientItemSlotUi> _normalIngredientSlotUis;
private DataManager _dataManager;
private ItemManager _itemManager;
private DailyFoodMenuUi _dailyFoodMenuUi;
private FoodData _selectedFoodData;
private bool _isEnoughIngredient;
private void Awake()
{
Initialize();
InventorySynchronization();
SelectFinishedFood(null);
}
private void Start()
{
_dataManager.OnChangeFoodRecipe += AddFoodRecipe;
_dataManager.Inventory.OnChangeItemSlot += OnInventoryChange;
}
private void OnDestroy()
{
_dataManager.OnChangeFoodRecipe -= AddFoodRecipe;
_dataManager.Inventory.OnChangeItemSlot -= OnInventoryChange;
}
private void Initialize()
{
_dataManager = DataManager.Instance;
_itemManager = ItemManager.Instance;
_dailyFoodMenuUi = TycoonUiManager.Instance.TycoonManagementUi.CookMenuUi.DailyFoodMenuUi;
_finishedFoodSlotUis = new List<TycoonItemSlotUi>();
foreach (Transform element in _finishedFoodSlotLocation)
{
Destroy(element.gameObject);
}
_highIngredientSlotUis = new List<IngredientItemSlotUi>(5);
_normalIngredientSlotUis = new List<IngredientItemSlotUi>(5);
foreach (Transform element in _ingredientFoodSlotLocation)
{
Destroy(element.gameObject);
}
for (var i = 0; i < 10; i++)
{
var newItemSlot = Instantiate(_ingredientItemSlotUi, _ingredientFoodSlotLocation).GetComponent<IngredientItemSlotUi>();
if (i < 5)
{
_highIngredientSlotUis.Add(newItemSlot);
_highIngredientSlotUis[i].SetIsLocked(true);
}
else
{
_normalIngredientSlotUis.Add(newItemSlot);
}
newItemSlot.SetItemSlot(null);
}
CheckCookable();
}
private void InventorySynchronization()
{
foreach (var element in _dataManager.FoodRecipes)
{
var newItemSlot = Instantiate(_finishedFoodSlotUi, _finishedFoodSlotLocation).GetComponent<TycoonItemSlotUi>();
var foodData = _itemManager.GetFoodDataByIdx(element);
newItemSlot.SetFoodData(foodData);
_finishedFoodSlotUis.Add(newItemSlot);
newItemSlot.AddButtonClickListener(() => SelectFinishedFood(foodData));
}
}
private void AddFoodRecipe(FoodData foodData)
{
var newItemSlot = Instantiate(_finishedFoodSlotUi, _finishedFoodSlotLocation).GetComponent<TycoonItemSlotUi>();
newItemSlot.SetFoodData(foodData);
_finishedFoodSlotUis.Add(newItemSlot);
newItemSlot.AddButtonClickListener(() => SelectFinishedFood(foodData));
}
private void OnInventoryChange(ItemSlot itemSlot, bool isAdded)
{
UpdateIngredientSlots();
}
private void UpdateIngredientSlots()
{
if (_selectedFoodData == null) return;
SetIngredient(_selectedFoodData);
}
private void SelectFinishedFood(FoodData foodData)
{
if (foodData == null)
{
_selectedFoodImage.enabled = false;
_selectedFoodImage.sprite = null;
_selectedFoodName.text = null;
_selectedFoodTasteText.text = null;
SetIngredient(null);
}
else
{
_selectedFoodData = foodData;
_selectedFoodImage.sprite = _itemManager.GetItemDataByIdx(_selectedFoodData.Idx).Sprite;
_selectedFoodName.text = _selectedFoodData.Name;
_selectedFoodTasteText.text = _selectedFoodData.TasteToString();
_selectedFoodImage.enabled = true;
SetIngredient(_selectedFoodData);
}
}
private void SetIngredient(FoodData foodData)
{
if (foodData == null)
{
for (var i = 0; i < 5; i++)
{
_normalIngredientSlotUis[i].SetItemSlot(null);
}
CheckCookable();
return;
}
var ingredients = foodData.GetValidIngredients();
var ingredientCount = ingredients.Count;
for (var i = 0; i < 5; i++)
{
if (i < ingredientCount)
{
_normalIngredientSlotUis[i].SetItemSlot(new ItemSlot(ingredients[i].Idx, ingredients[i].Quantity));
}
else
{
_normalIngredientSlotUis[i].SetItemSlot(null);
}
}
CheckCookable();
}
public void CheckCookable()
{
if (_selectedFoodData == null || _selectedFoodData.Idx == 0)
{
_cookButton.interactable = false;
_cookText.color = _cookButton.colors.disabledColor;
return;
}
if (!_dailyFoodMenuUi.IsEmptyDailyFoodMenu(_selectedFoodData))
{
_cookButton.interactable = false;
_cookText.color = _cookButton.colors.disabledColor;
return;
}
var ingredientCount = _selectedFoodData.GetValidIngredients().Count;
for (var i = 0; i < ingredientCount; i++)
{
if (_normalIngredientSlotUis[i].IsEnoughIngredient) continue;
_cookButton.interactable = false;
_cookText.color = _cookButton.colors.disabledColor;
return;
}
_cookButton.interactable = true;
_cookText.color = Color.black;
}
public void Cook()
{
foreach (var element in _selectedFoodData.GetValidIngredients())
{
var itemSlot = _dataManager.Inventory.GetItemByIdx(element.Idx);
_dataManager.Inventory.RemoveItem(itemSlot, element.Quantity);
}
_dailyFoodMenuUi.AddDailyFoodMenu(_selectedFoodData);
CheckCookable();
}
public void SortButton()
{
if (_finishedFoodSortDropdown.value == 0) return;
SortItemSlotUi((FoodSortingType)_finishedFoodSortDropdown.value);
_finishedFoodSortDropdown.value = 0;
}
private void SortItemSlotUi(FoodSortingType sortingType)
{
switch (sortingType)
{
case FoodSortingType.None:
return;
case FoodSortingType.PriceUp:
_finishedFoodSlotUis.Sort((x, y) =>
_itemManager.GetItemDataByIdx(x.FoodData.Idx).Price.
CompareTo(_itemManager.GetItemDataByIdx(y.FoodData.Idx).Price));
break;
case FoodSortingType.PriceDown:
_finishedFoodSlotUis.Sort((x, y) =>
_itemManager.GetItemDataByIdx(y.FoodData.Idx).Price.
CompareTo(_itemManager.GetItemDataByIdx(x.FoodData.Idx).Price));
break;
case FoodSortingType.CookwareTypeUp:
_finishedFoodSlotUis.Sort((x, y) =>
x.FoodData.Type.
CompareTo(y.FoodData.Type));
break;
case FoodSortingType.CookwareTypeDown:
_finishedFoodSlotUis.Sort((x, y) =>
y.FoodData.Type.
CompareTo(x.FoodData.Type));
break;
case FoodSortingType.TasteUp:
_finishedFoodSlotUis.Sort((x, y) =>
x.FoodData.Taste.
CompareTo(y.FoodData.Taste));
break;
case FoodSortingType.TasteDown:
_finishedFoodSlotUis.Sort((x, y) =>
y.FoodData.Taste.
CompareTo(x.FoodData.Taste));
break;
case FoodSortingType.NameUp:
_finishedFoodSlotUis.Sort((x, y) =>
string.Compare(x.FoodData.Name, y.FoodData.Name, StringComparison.Ordinal));
break;
case FoodSortingType.NameDown:
_finishedFoodSlotUis.Sort((x, y) =>
string.Compare(y.FoodData.Name, x.FoodData.Name, StringComparison.Ordinal));
break;
default:
throw new ArgumentOutOfRangeException();
}
for (var i = 0; i < _finishedFoodSlotUis.Count; i++)
{
_finishedFoodSlotUis[i].transform.SetSiblingIndex(i);
}
}
public void TycoonClosed()
{
SelectFinishedFood(null);
}
}
}