Files
back_cantanBuilding/Assets/Scripts/UI/Shop/ItemShopSlot.cs
2026-05-26 16:15:54 +08:00

177 lines
6.0 KiB
C#

using asap.core;
using cfg;
using game;
using GameCore;
using System;
using System.Collections.Generic;
using TMPro;
using tysdk;
using UnityEngine;
using UnityEngine.UI;
public class ItemShopSlot : MonoBehaviour
{
#region Field&&Property
[SerializeField]
private Button btn_buy;
[SerializeField]
private RewardItemNew rewardItem;
[SerializeField]
private GameObject
go_soldOut;
[SerializeField]
private Transform
tran_tag;
[SerializeField]
TMP_Text
txt_itemName;
private PlayerShopData.ItemShopData _data;
#endregion
private void Start()
{
}
#region Function
public void SetData(PlayerShopData.ItemShopData data)
{
gameObject.SetActive(true);
_data = data;
if (_data.Record.ItemID == 1002)
{
_data.Count = (int)GContext.container.Resolve<PlayerItemData>().GetExtraCoinMag(_data.Count);
}
RefreshView();
}
public void RefreshView()
{
rewardItem.SetData(new ItemData
{
id = _data.Record.ItemID,
count = _data.Count,
});
rewardItem.transform.Find("text_num").GetComponent<TMP_Text>().text = "x" + ConvertTools.GetNumberString(_data.Count);
txt_itemName.text = LocalizationMgr.GetText(GContext.container.Resolve<Tables>().TbItem.DataMap[_data.Record.ItemID].Name_l10n_key);
for (int i = 0; i < tran_tag.childCount; i++)
{
tran_tag.GetChild(i).gameObject.SetActive(false);
}
// go_redPoint.SetActive(false);
btn_buy.transform.Find("Ani_Container/p_text").gameObject.SetActive(false);
if (_data.Record.hasBought == 0)
{
// GContext.container.Resolve<IUIService>().SetImageSprite(img_light, ( $"bg_shop_resource_item_{quality + 1}" ));
//Not Boudght
go_soldOut.SetActive(false);
btn_buy.transform.parent.gameObject.SetActive(true);
btn_buy.onClick.AddListener(OnBuy);
//Cost
var cost = _data.GetDiscountDiamondCost();
//Not Free
btn_buy.transform.Find("Ani_Container/p_text").gameObject.SetActive(true);
btn_buy.transform.Find("Ani_Container/p_text").GetComponent<TMP_Text>().text = ConvertTools.GetNumberString(cost);
//Discount
var discount = _data.Record.Discount;
var tagJudgeList = GContext.container.Resolve<Tables>().TbStoreConfig.Get(1).PriceTag;
if (!Mathf.Approximately(discount, 1f))
{
var lastDiscount = 0f;
var index = 0;
foreach (var tagjudge in tagJudgeList)
{
if (discount * 10f > lastDiscount && discount * 10f <= tagjudge)
{
break;
}
index++;
lastDiscount = tagjudge;
}
if (tran_tag.childCount <= index)
Debug.LogError($"[ItemShopSlot::RefreshView] tagJudge {index} exceeds tagsCount {tran_tag.childCount}");
var tag = tran_tag.GetChild(index);
tag.gameObject.SetActive(true);
tag.GetChild(0).GetComponent<TMP_Text>().text = ConvertTools.GetNumberString3(discount);
}
}
else
{
//Has Bought
// img_light.enabled = false;
go_soldOut.SetActive(true);
btn_buy.transform.parent.gameObject.SetActive(false);
btn_buy.onClick.RemoveAllListeners();
// cg_rewardItem.alpha = 0.6f;
}
}
private void OnBuy()
{
if (_data.Record.hasBought == 0)
{
var cost = _data.GetDiscountDiamondCost();
if (cost == 0)
{
//Free
var itemData = new ItemData { id = _data.Record.ItemID, count = _data.Count };
itemData.curCount = (ulong)GContext.container.Resolve<PlayerItemData>().GetItemCount(_data.Record.ItemID);
GContext.container.Resolve<PlayerItemData>().AddItem(itemData);
GContext.container.Resolve<PlayerShopData>().SetItemShopSlotBought(_data.ItemShopID);
GContext.Publish(new ShowData(new List<ItemData> { itemData }, RewardType.NormalOne));
GContext.Publish(new ShowData());
RefreshView();
}
else
{
var curDiamond = GContext.container.Resolve<PlayerData>().diamond;
//Not Free
if (curDiamond >= cost)
{
var itemData = new ItemData { id = _data.Record.ItemID, count = _data.Count };
itemData.curCount = (ulong)GContext.container.Resolve<PlayerItemData>().GetItemCount(_data.Record.ItemID);
GContext.container.Resolve<PlayerData>().AddDiamond(-cost);
GContext.container.Resolve<PlayerShopData>().SetItemShopSlotBought(_data.ItemShopID);
GContext.Publish(new ShowData(new List<ItemData> { itemData }, RewardType.NormalOne));
GContext.container.Resolve<PlayerItemData>().AddItem(itemData);
GContext.Publish(new ShowData());
#if AGG
using (var e = GEvent.GameEvent("resource_shop"))
{
e.AddContent("cost_diamond_count", cost)
.AddContent("change_type", 2)
.AddContent("change_num", _data.Count)
.AddContent("item_id", _data.Record.ItemID);
}
#endif
RefreshView();
}
else
{
ToastPanel.Show(LocalizationMgr.GetText("UI_ToastPanel_59"));
_ = UIManager.Instance.ShowUI(UITypes.LackOfResourceConfirmPopupPanel);
}
}
}
}
#endregion
}