/// #pkgName FGUI包名
/// #panelName UIPanel名字
/// #UIName = $"{#pkgName}{#panelName}" UIKey名字
/// 该脚本由模板创建
/// created by cb 2024
using FairyGUI;
using FL.Data;
using System;
using UnityEngine;
using XGame.Database;
using XGame.Framework.Time;
using XGame.Framework.UI;
namespace FL.FGUI
{
///
/// UI逻辑处理类
///
///
public partial class TreasureChestReduceTimePanelCtrl : UIController
{
private int _useCount; // 当前要使用的加速卡数量
private int _maxCount; // 拥有的加速卡的数量
private int _needNum; // 完成升级时间需要的加速卡数量
private long _needTime; // 完成升级需要的时间(豪秒)
private int _reduceTime; // 加速卡减少时间(秒)
private long _openUIServerTime; // 打开此界面时所处的当前的时间戳(用于加速类道具打开此界面停留时间比较久时)
private ITimer _upgradeTimer; // 宝箱升级时间倒计时定时器
private const int StayTime = 30000; // 停留时间超过30秒
protected override void OnEnable(object intent)
{
AddUIListenres();
InitUI((int)intent);
_openUIServerTime = Context.Time.GetNowTime();
}
protected override void OnDisable()
{
RemoveUIListenres();
ClearUpgradeTimer();
}
#region UI事件
private void AddUIListenres()
{
VM.AddBtn.onClick.Add(OnClickAddBtn);
VM.SubBtn.onClick.Add(OnClickSubBtn);
VM.UseBtn.onClick.Add(OnClickUseBtn);
}
private void RemoveUIListenres()
{
VM.AddBtn.onClick.Remove(OnClickAddBtn);
VM.SubBtn.onClick.Remove(OnClickSubBtn);
VM.UseBtn.onClick.Remove(OnClickUseBtn);
}
private void OnClickAddBtn(EventContext context)
{
_useCount += 1;
if (_useCount > _needNum) _useCount = _needNum;
ShowUseCount();
}
private void OnClickSubBtn(EventContext context)
{
_useCount -= 1;
if (_useCount < 0) _useCount = 0;
ShowUseCount();
}
private void OnClickUseBtn(EventContext context)
{
var curServerTime = Context.Time.GetNowTime(); // 当前服务器时间戳
if (curServerTime < DragonEggData.Instance.UpLvTime)
{
if (curServerTime - _openUIServerTime > StayTime)
{
ShowUseNum();
}
DragonEggService.Instance.SendToAcceleration(1,_useCount);
}
Context.ClosePanel();
}
#endregion
private void InitUI(int itemId)
{
_maxCount = ItemData.Instance.GetItemNum(itemId); // 当前拥有的道具数量
var itemInfo = ItemTableRepo.Get(itemId);
if (itemInfo != null)
{
VM.ItemIcon.icon = itemInfo.Icon;
_reduceTime = itemInfo.Effect[1];
}
ShowUI();
}
private void ShowUI()
{
ShowUseNum();
ShowUseCount();
ClearUpgradeTimer();
if (_upgradeTimer == null)
{
_upgradeTimer = Context.Time.AddLooperTimer(500, (int dt) =>
{
ShowTimeUI();
});
}
}
private void ClearUpgradeTimer()
{
if (_upgradeTimer != null)
{
_upgradeTimer.Cancel();
_upgradeTimer = null;
}
}
private void ShowUseNum()
{
_needTime = DragonEggData.Instance.UpLvTime - Context.Time.GetNowTime();
_needNum = (int)(_needTime + (_reduceTime - 1) * 1000) / (_reduceTime * 1000);
if (_needNum > _maxCount) _needNum = _maxCount;
_useCount = _needNum;
}
private void ShowBtnState()
{
VM.AddBtn.enabled = _useCount < _needNum;
VM.SubBtn.enabled = _useCount > 0;
VM.UseBtn.enabled = _useCount > 0;
}
private void ShowUseCount()
{
VM.NumLabel.text = $"{_useCount}/{_needNum}";
if (_needNum > 0)ShowTimeUI();
ShowBtnState();
}
private void ShowTimeUI()
{
_needTime = DragonEggData.Instance.UpLvTime - Context.Time.GetNowTime();
if (_needTime < 0)
{
ClearUpgradeTimer();
_useCount = 0;
_needNum = 0;
ShowUseCount();
VM.DescLabel.text = string.Format(StringDefine.treasureChestUpTime, "00:00:00", "00:00:00");
}
else
{
TimeSpan timeData = TimeSpan.FromMilliseconds(_needTime);
TimeSpan reduceTime = TimeSpan.FromSeconds(_useCount * _reduceTime);
VM.DescLabel.text = string.Format(StringDefine.treasureChestUpTime, timeData.ToString(@"hh\:mm\:ss"), reduceTime.ToString("hh\\:mm\\:ss"));
}
}
}
}