shaoxiadiablo/Assets/AGame/Scripts/UI/BoxShopPanel/BoxShopPanel.PropBoxWidget.cs

421 lines
9.8 KiB
C#
Raw Normal View History

2025-05-18 01:04:31 +08:00
// ***********************************************************************
// Assembly : Game
// Author : Kimch
// Created : 2020-12-07
// Description :
// Last Modified By :
// Last Modified On :
// ***********************************************************************
// <copyright file= "ShopBoxPanel.BoxWidget" company="Kunpo"></copyright>
// <summary></summary>
// ***********************************************************************
using TMPro;
using UnityEngine;
using UnityEngine.UI;
namespace G.UI
{
partial class BoxShopPanel
{
/// <summary>
///
/// </summary>
public class PropBoxWidget : KUIWidget
{
#region Field
#pragma warning disable CS0649 // 从未对字段赋值,字段将一直保持其默认值 null
[KUIFlag]
TextMeshProUGUI _tmpName;
[KUIFlag]
KUIImage _imgIcon;
[KUIFlag]
Button _btnTips;
[KUIFlag]
TextMeshProUGUI _tmpGet;
[KUIFlag]
Button _btnBuy;
[KUIFlag]
Image _imgCostIcon;
[KUIFlag]
TextMeshProUGUI _tmpCostCount;
[KUIFlag]
TextMeshProUGUI _tmpPointer;
[KUIFlag]
TextMeshProUGUI _tmpTimer;
[KUIFlag]
KUIImage _imgAd;
[KUIFlag]
TextMeshProUGUI _tmpFree;
[KUIFlag]
KUIImage _imgPrivilege;
[KUIFlag]
Image _imgAdTicket;
[KUIFlag]
TextMeshProUGUI _tmpAdTicketCount;
//10连
[KUIFlag]
Button _btnBuy10;
[KUIFlag]
TextMeshProUGUI _tmpPointer10;
[KUIFlag]
Image _imgCostIcon10;
[KUIFlag]
TextMeshProUGUI _tmpCostCount10;
#pragma warning restore CS0649 // 从未对字段赋值,字段将一直保持其默认值 null
#endregion
#region Method
public override void Refresh()
{
if (this.data is BoxShopProxy.PropBoxInfo propBoxInfo)
{
if (_tmpName)
_tmpName.text = propBoxInfo.name;
//10连
if (propBoxInfo.multiCount > 0)
{
var multiPrice = propBoxInfo.multiPrice;
if (multiPrice.id > 0)
ShowMultiPrice(propBoxInfo.multiCount, multiPrice.id, multiPrice.count, MoneyProxy.Instance.GetMoney(multiPrice.id));
}
//先显示免费
if (propBoxInfo.freeInterval > 0)
{
var freeRemainSeconds = propBoxInfo.freeRemainSeconds;
if (propBoxInfo.freeRemainSeconds <= 0)
{
ShowFree();
return;
}
else
{
_tmpPointer.text = $"免费领取:{F.Utils.Time.ToTimeString2(freeRemainSeconds)}" ;
}
}
if (propBoxInfo.keyPrice.id > 0)
{
int keyCount = MoneyProxy.Instance.GetMoney(propBoxInfo.keyPrice.id);
if (keyCount > 0)
{
ShowKey(propBoxInfo.keyPrice.id, 1, keyCount);
return;
}
}
//广告类型
if (propBoxInfo.adTimeId > 0)
{
var vector = TimeProxy.Instance.GetTimeInfo2(propBoxInfo.adTimeId);
ShowAd(vector.remain, vector.max);
return;
}
var unitPrice = propBoxInfo.unitPrice;
if (unitPrice.id > 0)
ShowPrice(unitPrice.id, unitPrice.count, 0);
}
}
void ShowAd(int cur, int max)
{
if (_imgAd)
_imgAd.gameObject.SetActive(true);
if (_tmpFree)
{
_tmpFree.gameObject.SetActive(true);
_tmpFree.text = "免费";
}
if (_imgPrivilege)
{
_imgPrivilege.gameObject.SetActive(true);
var card = RmbShopProxy.Instance.IsActivitionCard();
var adTickets = MoneyProxy.Instance.GetMoney(Item.Id.kAdTicket);
if (card == 3)
{
_imgAd.ShowSprite(1);
_imgPrivilege.ShowSprite(1);
_imgAdTicket.gameObject.SetActive(false);
_tmpAdTicketCount.gameObject.SetActive(false);
}
else if (card == 4)
{
_imgAd.ShowSprite(1);
_imgPrivilege.ShowSprite(0);
_imgAdTicket.gameObject.SetActive(false);
_tmpAdTicketCount.gameObject.SetActive(false);
}
else if (adTickets > 0)
{
_imgAd.ShowSprite(1);
_imgAdTicket.gameObject.SetActive(true);
_tmpAdTicketCount.gameObject.SetActive(true);
_tmpAdTicketCount.text = "x" + adTickets.ToString();
_imgPrivilege.gameObject.SetActive(false);
}
else
{
_imgAd.ShowSprite(0);
_imgPrivilege.gameObject.SetActive(false);
_imgAdTicket.gameObject.SetActive(false);
_tmpAdTicketCount.gameObject.SetActive(false);
}
}
if (_tmpTimer)
{
_tmpTimer.text = $"今日剩余次数{cur}/{max}";
}
else
{
_tmpPointer.text = $"今日剩余次数{cur}/{max}";
}
_imgCostIcon.gameObject.SetActive(false);
_tmpCostCount.gameObject.SetActive(false);
}
void ShowPrice(int id, int cur, int max)
{
if (_imgAd)
_imgAd.gameObject.SetActive(false);
if (_tmpFree)
{
_tmpFree.gameObject.SetActive(false);
}
if (_imgPrivilege)
{
_imgPrivilege.gameObject.SetActive(false);
_imgAdTicket.gameObject.SetActive(false);
_tmpAdTicketCount.gameObject.SetActive(false);
}
_imgCostIcon.gameObject.SetActive(true);
var propItem = ItemProxy.Instance.GetStaticItem<ItemProp>(id);
if (propItem != null)
IconProxy.Instance.SetSprite(_imgCostIcon, propItem.icon);
_tmpCostCount.gameObject.SetActive(true);
_tmpCostCount.text = $"{cur}";
_tmpPointer.text = "";
if (_tmpTimer)
{
_tmpTimer.text = "";
}
}
void ShowMultiPrice(int multi, int id, int cur, int max)
{
var propItem = ItemProxy.Instance.GetStaticItem<ItemProp>(id);
if (propItem != null)
IconProxy.Instance.SetSprite(_imgCostIcon10, propItem.icon);
_tmpCostCount10.gameObject.SetActive(true);
_tmpCostCount10.text = $"{cur}";
_tmpPointer10.text = $"开启{multi}次";
}
void ShowKey(int id, int cur, int max)
{
if (_imgAd)
_imgAd.gameObject.SetActive(false);
if (_tmpFree)
{
_tmpFree.gameObject.SetActive(false);
}
if (_imgPrivilege)
{
_imgPrivilege.gameObject.SetActive(false);
_imgAdTicket.gameObject.SetActive(false);
_tmpAdTicketCount.gameObject.SetActive(false);
}
_imgCostIcon.gameObject.SetActive(true);
var propItem = ItemProxy.Instance.GetStaticItem<ItemProp>(id);
if (propItem != null)
IconProxy.Instance.SetSprite(_imgCostIcon, propItem.icon);
_tmpCostCount.gameObject.SetActive(true);
_tmpCostCount.text = $"{cur}/{max}";
}
private void ShowFree()
{
if (_imgAd)
_imgAd.gameObject.SetActive(false);
if (_tmpFree)
{
_tmpFree.gameObject.SetActive(true);
_tmpFree.text = "免费";
}
if (_imgPrivilege)
{
_imgPrivilege.gameObject.SetActive(false);
_imgAdTicket.gameObject.SetActive(false);
_tmpAdTicketCount.gameObject.SetActive(false);
}
_imgCostIcon.gameObject.SetActive(false);
_tmpCostCount.gameObject.SetActive(false);
_tmpPointer.text = "开启一次";
if (_tmpTimer)
{
_tmpTimer.text = "开启一次";
_tmpPointer.text = "";
}
}
private void SetTime(int seconds)
{
var canOpen = seconds <= 0;
_tmpGet.text = canOpen ? "领取" : F.Utils.Time.ToTimeString2(seconds);
_imgIcon.ShowSprite(canOpen ? 0 : 1);
}
private int _lastOpenSeconds;
private int _lastFreeSeconds;
public void UpdateState()
{
if (this.data is BoxShopProxy.PropBoxInfo propBoxInfo)
{
if (propBoxInfo.openInterval > 0)
{
var openRemainSeconds = propBoxInfo.openRemainSeconds;
if (openRemainSeconds > 0)
{
_tmpFree.text = F.Utils.Time.ToTimeString2(openRemainSeconds);
}
else
{
_tmpFree.text = "免费";
if (_lastOpenSeconds > 0)
{
Refresh();
}
}
_lastOpenSeconds = openRemainSeconds;
return;
}
if (propBoxInfo.freeInterval > 0)
{
var freeRemainSeconds = propBoxInfo.freeRemainSeconds;
if (freeRemainSeconds > 0)
{
_tmpPointer.text = $"免费领取:{F.Utils.Time.ToTimeString2(freeRemainSeconds)}";
}
else
{
_tmpPointer.text = "开启一次";
if (_tmpTimer)
{
_tmpTimer.text = "开启一次";
_tmpPointer.text = "";
}
if (_lastFreeSeconds > 0)
{
Refresh();
}
}
_lastFreeSeconds = freeRemainSeconds;
return;
}
}
}
/// <summary>
///
/// </summary>
void OnGetBtnClick()
{
if (this.data is BoxShopProxy.PropBoxInfo propBoxInfo)
{
BoxShopProxy.Instance.OpenPropBox(propBoxInfo.id,
(error, message) =>
{
if (error == 0)
{
Refresh();
}
else
{
ToastBox.ShowText(message);
}
});
}
SoundProxy.PlayFxAsync(GlobalDefine.BUTTON_CLICK_SOUND);
}
/// <summary>
///
/// </summary>
void OnGet10BtnClick()
{
if (this.data is BoxShopProxy.PropBoxInfo propBoxInfo)
{
BoxShopProxy.Instance.OpenPropBox10(propBoxInfo.id,
(error, message) =>
{
if (error == 0)
{
Refresh();
}
else
{
ToastBox.ShowText(message);
}
});
}
SoundProxy.PlayFxAsync(GlobalDefine.BUTTON_CLICK_SOUND);
}
void OnTipsBtnClick()
{
if (this.data is BoxShopProxy.PropBoxInfo propBoxInfo)
{
MessageBox.ShowMessage("概率说明", propBoxInfo.item.tips);
}
SoundProxy.PlayFxAsync(GlobalDefine.BUTTON_CLICK_SOUND);
}
#endregion
#region Unity
/// <summary>
///
/// </summary>
void Awake()
{
SetViewData();
_btnBuy.onClick.AddListener(this.OnGetBtnClick);
if (_btnBuy10)
_btnBuy10.onClick.AddListener(this.OnGet10BtnClick);
if (_btnTips)
_btnTips.onClick.AddListener(OnTipsBtnClick);
}
float _lastUpdateTime;
private void Update()
{
if (Time.time - _lastUpdateTime >= 1f)
{
_lastUpdateTime = Time.time;
UpdateState();
}
}
#endregion
}
}
}