Files
Shopping_UnityVR/Assets/02_Scripts/Player/PlayerController.cs
2026-05-11 17:27:04 +09:00

100 lines
3.1 KiB
C#

using System.Collections.Generic;
using UnityEngine;
using VRShopping.Interact;
using VRShopping.Items;
using VRShopping.Player;
using VRShopping.Shopping;
using VRShopping.UI;
public class PlayerController : MonoBehaviour,ISceneInitializable
{
private Animator _anim;
//나중에 stateMachine으로 변경
private bool _controlPositive = true;
[SerializeField] private ShoppingOrderView _shoppingOrderView;
[SerializeField] private PlayerWallet playerWallet;
private void Awake()
{
_anim = GetComponent<Animator>();
}
private void Start()
{
}
public void OnSceneLoaded()
{
InputManager.Instance.XRLeftControllerPrimaryButton_Event += this.ToggleShoppingOrderList;
}
public async void ToggleShoppingOrderList()
{
if(!_controlPositive) return;
Debug.Log("ToggleShoppingOrderList");
_controlPositive = false;
await _shoppingOrderView.ToggleShoppingOrderList();
_controlPositive = true;
}
public void Checkout(CheckoutMachine checkoutMachine)
{
List<CheckoutProductionRow> checkoutList = checkoutMachine.GetCheckoutList();
if(playerWallet.PayMoney(checkoutMachine.CheckoutSum))
{
Debug.Log("결제완료");
if (ShoppingOrderMissionClearCheck(checkoutList, out var missing))
{
Debug.Log("게임 클리어");
GameManager.Instance.GameSceneUI.ShowClearPanel();
}
else
{
var parts = new List<string>(missing.Count);
foreach (var (group, shortage) in missing) parts.Add($"{group.ToKorean()} x{shortage}");
GameManager.Instance.GameSceneUI.ShowMissingPanel(parts);
}
}
else
{
Debug.Log("예산 부족으로 결제 불가");
}
}
//계산한 품목이 장보기 목록을 모두 충족하는지 검사 (더 많은 건 허용)
//missing: 부족한 ProductGroup과 부족 수량 목록
private bool ShoppingOrderMissionClearCheck(List<CheckoutProductionRow> checkoutList, out List<(ProductGroup group, int shortage)> missing)
{
missing = new List<(ProductGroup, int)>();
ShoppingOrderList orderList = _shoppingOrderView != null ? _shoppingOrderView.OrderList : null;
if (orderList == null) return true;
//구매한 품목을 ProductGroup 기준으로 집계
var boughtByGroup = new Dictionary<ProductGroup, int>();
foreach (var row in checkoutList)
{
if (row.Item == null) continue;
boughtByGroup.TryGetValue(row.Item.ProductGroup, out int count);
boughtByGroup[row.Item.ProductGroup] = count + row.Quantity;
}
//목록의 각 항목을 전부 확인해서 부족분 수집
foreach (var entry in orderList.Entries)
{
boughtByGroup.TryGetValue(entry.ProductGroup, out int count);
int shortage = entry.RequiredQuantity - count;
if (shortage > 0) missing.Add((entry.ProductGroup, shortage));
}
return missing.Count == 0;
}
}