/// #pkgName FGUI包名
/// #panelName UIPanel名字
/// #UIName = $"{#pkgName}{#panelName}" UIKey名字
/// 该脚本由模板创建
/// created by cb 2024
using FairyGUI;
using FL.Data;
using System.Collections.Generic;
using System;
using XGame.Database;
using XGame.Framework.Time;
using XGame.Framework.UI;
using UnityEngine;
namespace FL.FGUI
{
///
/// UI逻辑处理类
///
///
public partial class TreasureChestUpgradePanelCtrl : UIController
{
private ITimer _upLvTimer; // 宝箱升级倒计时定时器
private const int GoldId = 1; // 升级消耗金币(道具id)
private const int AccelerationId = 1000; // 加速卡道具id
private treasureTable _curTreasure;
private treasureTable _nextTreasure;
protected override void OnEnable(object intent)
{
AddUIListenres();
AddEventLister();
InitUI();
ShowUI();
}
protected override void OnDisable()
{
RemoveUIListenres();
RemoveEventLister();
ClearStepTimer();
VM.GoldResItem.Disable();
VM.AccelerationResItem.Disable();
VM.CostAccelerationResItem.Disable();
}
#region UI事件
private void AddUIListenres()
{
VM.ProbabilityBtn.onClick.Add(OnClickProbabilityBtn);
VM.IncomeBtn.onClick.Add(OnClickIncomeBtn);
VM.UpgradeBtn.onClick.Add(OnClickUpgradeBtn);
VM.AssistBtn.onClick.Add(OnClickAssistBtn);
VM.ReduceTimeBtn.onClick.Add(OnClickReduceTimeBtn);
VM.AccelerationTimeBtn.onClick.Add(OnClickAccelerationTimeBtn);
}
private void RemoveUIListenres()
{
VM.ProbabilityBtn.onClick.Remove(OnClickProbabilityBtn);
VM.IncomeBtn.onClick.Remove(OnClickIncomeBtn);
VM.UpgradeBtn.onClick.Remove(OnClickUpgradeBtn);
VM.AssistBtn.onClick.Remove(OnClickAssistBtn);
VM.ReduceTimeBtn.onClick.Remove(OnClickReduceTimeBtn);
VM.AccelerationTimeBtn.onClick.Remove(OnClickAccelerationTimeBtn);
}
private void OnClickProbabilityBtn(EventContext context)
{
ShowPageUI(0);
}
private void OnClickIncomeBtn(EventContext context)
{
ShowPageUI(1);
}
///
/// 升级按钮
///
///
private void OnClickUpgradeBtn(EventContext context)
{
DragonEggService.Instance.SendToUpgrade();
}
//协助按钮
private void OnClickAssistBtn(EventContext context)
{
EventSingle.Instance.Notify(EventDefine.ShowTips, "发送援助到公会聊天频道");
}
///
/// 免费观看广告获取减少时间
///
///
private void OnClickReduceTimeBtn(EventContext context)
{
DragonEggService.Instance.SendToAcceleration(2, 0);
}
///
/// 加速按钮
///
///
private void OnClickAccelerationTimeBtn(EventContext context)
{
Context.UI.OpenAsync(UIKeys.TreasureChestReduceTimePanel,AccelerationId);
}
#endregion
private void AddEventLister()
{
EventSingle.Instance.AddListener(EventDefine.RefreshUpgradeTime, RefreshUpgradeTime);
EventSingle.Instance.AddListener(EventDefine.ShowEquipQualityProbability, ShowEquipQualityProbability);
EventSingle.Instance.AddListener(EventDefine.RefreshWatchAd, RefreshWatchAd);
EventSingle.Instance.AddListener(EventDefine.RefreshTreasureChestUI, RefreshTreasureChestUI);
}
private void RemoveEventLister()
{
EventSingle.Instance.RemoveListener(EventDefine.RefreshUpgradeTime, RefreshUpgradeTime);
EventSingle.Instance.RemoveListener(EventDefine.ShowEquipQualityProbability, ShowEquipQualityProbability);
EventSingle.Instance.RemoveListener(EventDefine.RefreshWatchAd, RefreshWatchAd);
EventSingle.Instance.RemoveListener(EventDefine.RefreshTreasureChestUI, RefreshTreasureChestUI);
}
private void ShowUI()
{
_curTreasure = treasureTableRepo.Get(DragonEggData.Instance.Level);
_nextTreasure = treasureTableRepo.Get(DragonEggData.Instance.Level + 1);
bool bMaxLv = _nextTreasure == null;
ShowBoxLv(DragonEggData.Instance.Level, bMaxLv);
ShowExpUI(_curTreasure, bMaxLv);
ShowPageUI(VM.PageCtrl.selectedIndex);
}
private void ShowPageUI(int index)
{
VM.PageCtrl.selectedIndex = index;
if (index == 0)
{
ShowEquipProbability(_curTreasure, _nextTreasure);
}
else
{
ShowIncome(_curTreasure, _nextTreasure);
}
}
private void ShowBoxLv(int curLv, bool bMaxLv)
{
VM.MaxLvCtrl.selectedIndex = bMaxLv ? 1 : 0;
if (!bMaxLv)
{
VM.NextLvLabel.text = (curLv + 1).ToString();
}
VM.CurLvLabel.text = curLv.ToString();
}
private void ShowExpUI(treasureTable curTreasure, bool bMaxLv)
{
if (!bMaxLv && curTreasure?.Expend != null)
{
if (DragonEggData.Instance.UpLvTime > Context.Time.GetNowTime())
{
ShowUpgradeTimeUI();
return;
}
VM.BoxLvExpBar.max = curTreasure.Expend;
VM.BoxLvExpBar.value = DragonEggData.Instance.Exp;
var needExp = curTreasure.Expend > DragonEggData.Instance.Exp ? curTreasure.Expend - DragonEggData.Instance.Exp : 0;
VM.NeedExpLabel.text = string.Format(StringDefine.upgradeDragonEggBoxExp, needExp);
VM.UpgradeBtn.enabled = DragonEggData.Instance.Exp >= curTreasure.Expend;
}
}
///
/// 升级倒计时状态中
///
private void ShowUpgradeTimeUI()
{
ShowUpgradeState(1);
ClearStepTimer();
if (_upLvTimer == null)
{
_upLvTimer = Context.Time.AddLooperTimer(500, (int dt) =>
{
ShowLeftStepTime();
});
}
ShowLeftStepTime();
}
///
/// 龙蛋升阶倒计时
///
private void ShowLeftStepTime()
{
long leftTime = DragonEggData.Instance.UpLvTime - Context.Time.GetNowTime();
if (leftTime > 0)
{
TimeSpan timeData = TimeSpan.FromMilliseconds(leftTime);
VM.EndTimeLabel.text = timeData.ToString(@"hh\:mm\:ss");
}
else
{
ClearStepTimer();
ShowUpgradeState(0);
}
}
private void ShowUpgradeState(int index)
{
VM.UpgradeCtrl.selectedIndex = index;
if (index == 1)
{
ShowWatchAdUI();
}
}
private void RefreshWatchAd(int eventId, object args)
{
if (VM.UpgradeCtrl.selectedIndex == 1)
{
ShowWatchAdUI();
}
}
private void ShowWatchAdUI()
{
// 处于观看广告按钮CD状态中
VM.CdTimeCtrl.selectedIndex = DragonEggData.Instance.WatchADCount < KeyValue.boxUpBillNum ? 0 : 1;
VM.AdNumLabel.text = string.Format(StringDefine.treasureChestWatchADCount, DragonEggData.Instance.WatchADCount, KeyValue.boxUpBillNum);
}
private void ClearStepTimer()
{
if (_upLvTimer != null)
{
_upLvTimer.Cancel();
_upLvTimer = null;
}
}
///
/// 刷新宝箱等级界面UI
///
///
///
private void RefreshTreasureChestUI(int eventId, object args)
{
ShowUI();
}
///
/// 刷新升级倒计时
///
///
///
private void RefreshUpgradeTime(int eventId, object args)
{
ShowUpgradeTimeUI();
}
///
/// 收益页签查看装备品质产出几率按钮事件监听
///
///
///
private void ShowEquipQualityProbability(int eventId, object args)
{
ShowPageUI(0);
}
private void InitUI()
{
VM.GoldResItem.Ctrl.ShowUI(GoldId, ItemData.Instance.GetItemNum(GoldId));
VM.AccelerationResItem.Ctrl.ShowUI(AccelerationId, ItemData.Instance.GetItemNum(AccelerationId));
VM.CostAccelerationResItem.Ctrl.ShowUI(AccelerationId, 1);
VM.CdTimeLabel.text = string.Format(StringDefine.treasureChestADTime, Mathf.FloorToInt(KeyValue.boxUpBillTime / 60));
}
///
/// 装备概率UI
///
private void ShowEquipProbability(treasureTable curTreasure, treasureTable nextTreasure)
{
var probabilityDataList = new List();
bool bMaxLv = nextTreasure == null;
for (int i = 0; i < curTreasure.EquipmentProbability.Length; i += 2)
{
probabilityDataList.Add(new ProbabilityParam()
{
quality = curTreasure.EquipmentProbability[i],
curValue = curTreasure.EquipmentProbability[i + 1],
nextValue = bMaxLv ? 0 : nextTreasure.EquipmentProbability[i + 1],
bMaxLv = bMaxLv,
});
}
VM.ProbabilityList.BindDatas(probabilityDataList);
}
///
/// 宝箱收益UI
///
private void ShowIncome(treasureTable curTreasure, treasureTable nextTreasure)
{
var dropDataList = new List()
{
new TreasureChestIncome(){dropId=curTreasure.WoodenChestDrop[0],nextLvDropId=nextTreasure == null ? 0 : nextTreasure.WoodenChestDrop[0] },
new TreasureChestIncome(){dropId=curTreasure.BlackIronChestDrop[0],nextLvDropId=nextTreasure == null ? 0 : nextTreasure.BlackIronChestDrop[0] },
new TreasureChestIncome(){dropId=curTreasure.PlatinumChestDrop[0],nextLvDropId=nextTreasure == null ? 0 : nextTreasure.PlatinumChestDrop[0] },
new TreasureChestIncome(){dropId=curTreasure.ChemistryChestDrop[0],nextLvDropId=nextTreasure == null ? 0 : nextTreasure.ChemistryChestDrop[0] },
new TreasureChestIncome(){dropId=curTreasure.PearlsChestDrop[0],nextLvDropId=nextTreasure == null ? 0 : nextTreasure.PearlsChestDrop[0] },
};
VM.IncomeList.BindDatas(dropDataList);
}
}
}