95 lines
2.5 KiB
C#
95 lines
2.5 KiB
C#
using System;
|
|
using SepCore.Event;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
|
|
namespace SepCore.UI
|
|
{
|
|
public class ShopGoodsItem : MonoBehaviour
|
|
{
|
|
[SerializeField] private IconArea _iconArea;
|
|
|
|
[SerializeField] private TMP_Text _titleText;
|
|
|
|
[SerializeField] private TMP_Text _typeText;
|
|
|
|
[SerializeField] private TMP_Text _descriptionText;
|
|
|
|
[SerializeField] private TMP_Text _costText;
|
|
[SerializeField] private CommonButton _purchaseButton = null;
|
|
[SerializeField] private TMP_Text _purchaseLabel = null;
|
|
[SerializeField] private CanvasGroup _canvasGroup = null;
|
|
|
|
private GoodsItemContext _data;
|
|
|
|
private int _index = -1;
|
|
|
|
private bool _listenersBound;
|
|
|
|
public void Init(GoodsItemContext data, int index)
|
|
{
|
|
EnsureListeners();
|
|
|
|
_index = index;
|
|
_data = data;
|
|
_canvasGroup.alpha = 1f;
|
|
_purchaseLabel.text = "购买";
|
|
|
|
_iconArea.OnInit(data.Icon, data.Rarity);
|
|
_titleText.text = data.Name;
|
|
_typeText.text = data.TypeName;
|
|
_descriptionText.text = data.Description;
|
|
_costText.text = $"{data.Price} <sprite name=\"coin\" index=0>";
|
|
}
|
|
|
|
public void OnReset()
|
|
{
|
|
_index = -1;
|
|
_data = null;
|
|
}
|
|
|
|
public void MarkPurchased()
|
|
{
|
|
OnReset();
|
|
_canvasGroup.alpha = 0.5f;
|
|
_purchaseButton.interactable = false;
|
|
_purchaseLabel.text = "已购买";
|
|
}
|
|
|
|
public void RefreshAvailability(int playerCoin, bool weaponCapacityFull)
|
|
{
|
|
if (_index < 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
bool weaponFull = _data.ItemType == ItemType.Weapon && weaponCapacityFull;
|
|
bool affordable = playerCoin >= _data.Price;
|
|
_purchaseButton.interactable = affordable && !weaponFull;
|
|
_purchaseLabel.text = weaponFull ? "武器已满" : affordable ? "购买" : "金币不足";
|
|
}
|
|
|
|
private void EnsureListeners()
|
|
{
|
|
if (_listenersBound)
|
|
{
|
|
return;
|
|
}
|
|
|
|
_listenersBound = true;
|
|
|
|
_purchaseButton.onClick.AddListener(OnPurchaseClick);
|
|
}
|
|
|
|
private void OnPurchaseClick()
|
|
{
|
|
if (_index < 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
GameEntry.Event.Fire(this, ShopPurchaseEventArgs.Create(_index));
|
|
}
|
|
}
|
|
}
|