상점 포션 구매 UniRx(MVP 패턴)를 활용하여 수정중

This commit is contained in:
Mingu Kim
2025-06-02 00:28:39 +09:00
parent 8a54d47b56
commit 66ee4d6d2e
6 changed files with 156 additions and 47 deletions

View File

@@ -22663,12 +22663,12 @@ GameObject:
m_Component:
- component: {fileID: 2640998817256912009}
m_Layer: 5
m_Name: PositionPopUpLayout
m_Name: PotionPopUpLayout
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 0
m_IsActive: 1
--- !u!224 &2640998817256912009
RectTransform:
m_ObjectHideFlags: 0

View File

@@ -0,0 +1,61 @@
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
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;
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 87c3d53b1193f3c4bb806d0427e769ca
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,70 @@
using System.Collections;
using System.Collections.Generic;
using UniRx;
using UnityEngine;
namespace TON
{
public class ShopPresenter
{
private int hpPotionPrice1 = 200; // HP 포션 가격
private int hpPotionPrice5 = 1000; // HP 포션 가격
private int hpPotionPrice20 = 3600; // HP 포션 가격
private int mpPotionPrice1 = 400; // MP 포션 가격
private int mpPotionPrice5 = 2000; // MP 포션 가격
private int mpPotionPrice20 = 7600; // MP 포션 가격
private PlayerDataManager playerDataManager;
private List<ShopItemModel> shopItemModels = new();
public ReactiveCollection<ShopItemPresenter> ShopItems { get; } = new();
public ShopPresenter()
{
// 싱글톤으로 PlayerDataManager 접근
playerDataManager = PlayerDataManager.Singleton;
if (playerDataManager == null)
{
Debug.LogError("PlayerDataManager가 초기화되지 않았습니다.");
}
shopItemModels.Add(new ShopItemModel(hpPotionPrice1, "hp", 1));
shopItemModels.Add(new ShopItemModel(hpPotionPrice5, "hp", 5));
shopItemModels.Add(new ShopItemModel(hpPotionPrice20, "hp", 20));
shopItemModels.Add(new ShopItemModel(mpPotionPrice1, "mp", 1));
shopItemModels.Add(new ShopItemModel(mpPotionPrice5, "mp", 5));
shopItemModels.Add(new ShopItemModel(mpPotionPrice20, "mp", 20));
foreach (var item in shopItemModels)
{
ShopItems.Add(new ShopItemPresenter(this, item));
}
}
// 포션 구매 메서드
private void BuyPotion(int price, string potionType, int quantity)
{
if (playerDataManager.goldAmount >= price)
{
playerDataManager.UseGold(price, (isSuccess) =>
{
if (isSuccess)
{
if (potionType == "hp")
{
playerDataManager.AddPotion("HP", quantity);
}
else if (potionType == "mp")
{
playerDataManager.AddPotion("MP", quantity);
}
UIManager.Singleton.UpdateCashData();
}
});
}
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 8729e0724cdac06409677bf2e8bec238
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -6,56 +6,12 @@ namespace TON
{
[SerializeField] private GameObject HeartPopUp;
[SerializeField] private GameObject PositionPopUp;
[SerializeField] private int hpPotionPrice1 = 200; // HP 포션 가격
[SerializeField] private int hpPotionPrice5 = 1000; // HP 포션 가격
[SerializeField] private int hpPotionPrice20 = 3600; // HP 포션 가격
[SerializeField] private int mpPotionPrice1 = 400; // MP 포션 가격
[SerializeField] private int mpPotionPrice5 = 2000; // MP 포션 가격
[SerializeField] private int mpPotionPrice20 = 7600; // MP 포션 가격
private PlayerDataManager playerDataManager;
void Start()
{
// 싱글톤으로 PlayerDataManager 접근
playerDataManager = PlayerDataManager.Singleton;
if (playerDataManager == null)
{
Debug.LogError("PlayerDataManager가 초기화되지 않았습니다.");
}
}
public void OnClickLobbyButton()
{
Main.Singleton.ChangeScene(SceneType.Lobby);
}
// 포션 구매 메서드
private void BuyPotion(int price, string potionType, int quantity)
{
if (playerDataManager.goldAmount >= price)
{
playerDataManager.UseGold(price, (isSuccess) =>
{
if (isSuccess)
{
if (potionType == "hp")
{
playerDataManager.AddPotion("HP", quantity);
}
else if (potionType == "mp")
{
playerDataManager.AddPotion("MP", quantity);
}
UIManager.Singleton.UpdateCashData();
}
});
}
}
// HP 포션 구매 버튼 클릭 시 호출
public void OnClickBuyHpPotion1Button()
{