상점 UI 포션 MVP 패턴 추가

This commit is contained in:
Mingu Kim
2025-06-08 23:07:23 +09:00
parent 66ee4d6d2e
commit b125bc748a
4 changed files with 150 additions and 47 deletions

View File

@@ -14,10 +14,24 @@ namespace TON
[SerializeField] private TextMeshProUGUI title;
[SerializeField] private TextMeshProUGUI txtPrice;
[SerializeField] private Button buyButton;
public void Bind(ShopItemPresenter presenter)
{
presenter.Price.Subscribe(text => txtPrice.text = text.ToString()).AddTo(this);
presenter.PotionType.CombineLatest(presenter.Quantity, (type, quantity)=> $"{type} 포션 {quantity}개")
.Subscribe(text => title.text = text)
.AddTo(this);
presenter.BuyCommand.BindTo(buyButton).AddTo(this);
}
[ContextMenu("Bind")]
public void BindReference()
{
image = transform.Find("ItemLayout/ItemImage").GetComponent<Image>();
title = transform.Find("ItemLayout/Text (TMP)").GetComponent<TextMeshProUGUI>();
txtPrice = transform.Find("ItemLayout/Button/Text (TMP)").GetComponent<TextMeshProUGUI>();
buyButton = transform.Find("ItemLayout/Button").GetComponent<Button>();
}
}

View File

@@ -44,21 +44,21 @@ namespace TON
}
// 포션 구매 메서드
private void BuyPotion(int price, string potionType, int quantity)
public void BuyPotion(ShopItemPresenter shopItem)
{
if (playerDataManager.goldAmount >= price)
if (playerDataManager.goldAmount >= shopItem.Price.Value)
{
playerDataManager.UseGold(price, (isSuccess) =>
playerDataManager.UseGold(shopItem.Price.Value, (isSuccess) =>
{
if (isSuccess)
{
if (potionType == "hp")
if (shopItem.PotionType.Value == "hp")
{
playerDataManager.AddPotion("HP", quantity);
playerDataManager.AddPotion("HP", shopItem.Quantity.Value);
}
else if (potionType == "mp")
else if (shopItem.PotionType.Value == "mp")
{
playerDataManager.AddPotion("MP", quantity);
playerDataManager.AddPotion("MP", shopItem.Quantity.Value);
}
UIManager.Singleton.UpdateCashData();

View File

@@ -1,47 +1,33 @@
using System.Collections.Generic;
using UnityEngine;
namespace TON
{
// MVP 패턴 : Model <> Presenter <> View
// View
public class ShopUI : UIBase
{
[SerializeField] private GameObject HeartPopUp;
[SerializeField] private GameObject PositionPopUp;
[SerializeField] private List<ShopItemUI> ShopItems;
private ShopPresenter _presenter;
private void Start()
{
_presenter = new ShopPresenter();
for (int i = 0; i < ShopItems.Count; i++)
{
ShopItems[i].Bind(_presenter.ShopItems[i]);
}
}
public void OnClickLobbyButton()
{
Main.Singleton.ChangeScene(SceneType.Lobby);
}
// HP 포션 구매 버튼 클릭 시 호출
public void OnClickBuyHpPotion1Button()
{
BuyPotion(hpPotionPrice1, "hp", 1);
}
public void OnClickBuyHpPotion5Button()
{
BuyPotion(hpPotionPrice5, "hp", 5);
}
public void OnClickBuyHpPotion20Button()
{
BuyPotion(hpPotionPrice20, "hp", 20);
}
// MP 포션 구매 버튼 클릭 시 호출
public void OnClickBuyMpPotion1Button()
{
BuyPotion(mpPotionPrice1, "mp", 1);
}
public void OnClickBuyMpPotion5Button()
{
BuyPotion(mpPotionPrice5, "mp", 5);
}
public void OnClickBuyMpPotion20Button()
{
BuyPotion(mpPotionPrice20, "mp", 20);
}
}
}