Files
M-Gameton-06/Gameton-06/Assets/Gameton/Scripts/UI/ShopItemUI.cs
Mingu Kim 8cae00f132 룰렛에 mvp 패턴 추가중
- 룰렛 결과에 따른 재화 획득 기능 추가
2025-06-19 21:29:50 +09:00

74 lines
2.4 KiB
C#

using System.Collections;
using System.Collections.Generic;
using TMPro;
using UniRx;
using UnityEngine;
using UnityEngine.UI;
namespace TON
{
// View
public class ShopItemUI : MonoBehaviour
{
[SerializeField] private Image image;
[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>();
}
}
// Presenter
public class ShopItemPresenter
{
public ReactiveProperty<int> Price { get; set; }
public ReactiveProperty<string> PotionType { get; set; }
public ReactiveProperty<int> Quantity { get; set; }
public ReactiveCommand BuyCommand { get; set; }
private ShopItemModel _model;
public ShopItemPresenter(ShopPresenter shopPresenter, ShopItemModel model)
{
_model = model;
Price = new ReactiveProperty<int>(_model.Price);
PotionType = new ReactiveProperty<string>(_model.PotionType);
Quantity = new ReactiveProperty<int>(_model.Quantity);
BuyCommand = new ReactiveCommand();
BuyCommand.Subscribe(_ => shopPresenter.BuyPotion(this));
}
}
// Model
public class ShopItemModel
{
public int Price;
public string PotionType;
public int Quantity;
public ShopItemModel(int price, string postionType, int quantity)
{
Price = price;
PotionType = postionType;
Quantity = quantity;
}
}
}