/// #pkgName FGUI包名 /// #panelName UIPanel名字 /// #UIName = $"{#pkgName}{#panelName}" UIKey名字 /// 该脚本由模板创建 /// created by cb 2024 using FairyGUI; using FL.Data; using FL.Data.Items; using FL.Network; using Spine.Unity; using System.Collections.Generic; using UnityEngine; using XGame.Database; using XGame.Framework.FGUI; using XGame.Framework.Time; using XGame.Framework.UI; namespace FL.FGUI { /// /// UI逻辑处理类 /// /// public partial class DragonEggTreasurePanelCtrl : UIController { private bool _bFlyingAni; // 是否处于开龙宝藏飞行动画状态 private int _useCount; // 使用的品质宝箱数量 private int _maxTreasureSocre; // 当前宝藏进度的最大积分值 private string _treasureChestName; // 当前宝藏宝箱名称 private ITimer _openBoxTimer; private Vector3 _startPos; private Vector3 _endPos; private Vector3 _scoreEndPos; private HashSet _flyingList; private List _dragonBtnList; private List _dropItemList; private const int _costDragonEggNum = 10; // 每次消耗的龙蛋数量 protected override void OnEnable(object intent) { AddUIListenres(); AddEventListener(); Init(); } protected override void OnDisable() { RemoveUIListenres(); RemoveEventListener(); ClearOpenTimer(); if (_flyingList != null) { foreach (var flying in _flyingList) { Context.Asset.Recycle(flying); } _flyingList.Clear(); _flyingList = null; } if (_dropItemList != null) _dropItemList.Clear(); if (_dragonBtnList != null) { _dragonBtnList.Clear(); _dragonBtnList = null; } VM.DragonSpine.url = string.Empty; } #region UI事件 private void AddUIListenres() { VM.NextBtn.onClick.Add(OnClickNextBtn); VM.PreviousBtn.onClick.Add(OnClickPreviousBtn); VM.BagBtn.onClick.Add(OnClickBagBtn); VM.BoxAwardBtn.onClick.Add(OnClickBoxAwardBtn); VM.DragonSpineBtn.onClick.Add(OnclickDragonSpineBtn); VM.LevelBtn.onClick.Add(OnClickLevelBtn); VM.MenusBtnCtrl.onChanged.Add(OnMenusBtnCtrlChange); } private void RemoveUIListenres() { VM.NextBtn.onClick.Remove(OnClickNextBtn); VM.PreviousBtn.onClick.Remove(OnClickPreviousBtn); VM.BagBtn.onClick.Remove(OnClickBagBtn); VM.BoxAwardBtn.onClick.Remove(OnClickBoxAwardBtn); VM.DragonSpineBtn.onClick.Remove(OnclickDragonSpineBtn); VM.LevelBtn.onClick.Remove(OnClickLevelBtn); VM.MenusBtnCtrl.onChanged.Remove(OnMenusBtnCtrlChange); } /// /// 打开宝箱等级弹窗界面 /// /// private void OnClickLevelBtn(EventContext context) { Context.UI.OpenAsync(UIKeys.TreasureChestUpgradePanel); } private void OnClickNextBtn(EventContext context) { OnSelectDragon(VM.MenusBtnCtrl.selectedIndex + 1); } private void OnClickPreviousBtn(EventContext context) { OnSelectDragon(VM.MenusBtnCtrl.selectedIndex - 1); } /// /// 点击背包按钮 /// /// private void OnClickBagBtn(EventContext context) { DragonEggService.Instance.SendToOpenTemporaryEquipBag(); } /// /// 积分进度条满值可领取对应的宝箱奖励 /// /// private void OnClickBoxAwardBtn(EventContext context) { DragonEggService.Instance.SendToOpenTreasureBoxAward(); } private void OnclickDragonSpineBtn(EventContext context) { if (_bFlyingAni) { //XGame.Log.Info("处于开宝箱状态"); return; } if (IsEmptyBox()) return; _bFlyingAni = true; OpenDragonEggBoxAni(); OpenDragonTreasure(); } #endregion private void AddEventListener() { EventSingle.Instance.AddListener(EventDefine.RefreshTreasureProgressUI, RefreshTreasureProgressUI); EventSingle.Instance.AddListener(EventDefine.RefreshTreasureUI, RefreshTreasureUI); EventSingle.Instance.AddListener(EventDefine.OpenDragonTreasureSucess, OpenDragonTreasureSucess); EventSingle.Instance.AddListener(EventDefine.OpenEquipBag, OpenEquipBag); EventSingle.Instance.AddListener(EventDefine.RefreshTreasureChestUI, RefreshTreasureChestUI); EventSingle.Instance.AddListener(EventDefine.UpdataItemData, UpdataItemData); } private void RemoveEventListener() { EventSingle.Instance.RemoveListener(EventDefine.RefreshTreasureProgressUI, RefreshTreasureProgressUI); EventSingle.Instance.RemoveListener(EventDefine.RefreshTreasureUI, RefreshTreasureUI); EventSingle.Instance.RemoveListener(EventDefine.OpenDragonTreasureSucess, OpenDragonTreasureSucess); EventSingle.Instance.RemoveListener(EventDefine.OpenEquipBag, OpenEquipBag); EventSingle.Instance.RemoveListener(EventDefine.RefreshTreasureChestUI, RefreshTreasureChestUI); EventSingle.Instance.RemoveListener(EventDefine.UpdataItemData, UpdataItemData); } private void Init() { _bFlyingAni = false; var startPos = VM.DragonSpineBtn.displayObject.TransformPoint(new Vector2(VM.DragonSpineBtn.width / 3, VM.DragonSpineBtn.height / 2), VM.DragonSpineBtn.displayObject); _startPos = new Vector3(startPos.x, startPos.y, 0); var endPos = VM.BagBtn.displayObject.TransformPoint(new Vector3(VM.BagBtn.width/10, VM.BagBtn.height/8), VM.DragonSpine.displayObject); _endPos = new Vector3(endPos.x, endPos.y, 0); ShowUI(); DragonEggService.Instance.SendToTreasureInfo(); } private void ShowUI() { InitDrangonBoxUI(); ShowDragonTreasureUI(null); ShowDragonLv(); } private void ShowDragonLv() { VM.LevelBtn.title = $"LV.{DragonEggData.Instance.Level}"; } /// /// 加载龙的spine /// /// private void LoadDragonSpine(string spineName) { VM.DragonSpine.LoadSpine(spineName, "box_idle", true); } /// /// 是否显示龙的spine模型 /// /// private void ShowDragonSpine(bool bShow) { VM.DragonSpine.visible = bShow; VM.DragonIcon.visible = !bShow; } /// /// 龙蛋积分变化 /// /// /// private void ShowDragonEggScoreUI(bool bTweenValue) { VM.EggPointBar.max = _maxTreasureSocre; if (bTweenValue) VM.EggPointBar.TweenValue(DragonEggData.Instance.DragonTreasureSocre, 0.5f); else VM.EggPointBar.value = DragonEggData.Instance.DragonTreasureSocre; OnchangeAwardState(DragonEggData.Instance.DragonTreasureSocre >= _maxTreasureSocre); int subSocre = _maxTreasureSocre > DragonEggData.Instance.DragonTreasureSocre ? _maxTreasureSocre - DragonEggData.Instance.DragonTreasureSocre : 0; VM.ScoreAwardLabel.text = string.Format(StringDefine.dragonTreasureScore, subSocre, _treasureChestName); } private void InitDrangonBoxUI() { if (_dragonBtnList == null) _dragonBtnList = new List() { VM.DragonBtn1, VM.DragonBtn2, VM.DragonBtn3, VM.DragonBtn4, VM.DragonBtn5 }; int index = -1; for (int i = 0; i < 5; i++) { long num = ItemData.Instance.GetItemNum(DragonEggData.Instance.GetTreasureChestId(i)); if (num > 0 && index == -1) { index = i; ShowDragonBoxNum(num); } } OnSelectDragon(index == -1 ? 0 : index); } /// /// 判断龙裔宝卵数量是否 /// /// private bool IsEmptyBox(bool bShowTip = true) { var boxNum = ItemData.Instance.GetItemNum(GetTreasureChestId()); if (boxNum == 0) { if (bShowTip) { Context.ShowTips(StringDefine.dragonEggsEmpty); } return true; } return false; } /// /// 龙蛋品质宝箱数量 /// /// /// private void ShowDragonBoxNum(long num) { VM.NumLabel.text = num.ToString(); } private void ShowFlyIconView(int itemId, bool bScore) { var flyParam = new FlyingIconParam() { iconName = ItemTableRepo.Get(itemId).Icon, startPos = _startPos, bScore = bScore, endPos = _endPos, }; if (_flyingList == null) _flyingList = new HashSet(); var loadAsync = Context.Asset.LoadFguiNested(UINestedKeys.DragonEggFlyIcon, VM.DragonSpineBtn); loadAsync.On(_ => { var flyView = loadAsync.Result as DragonEggFlyIconView; if (flyView != null) { flyParam.callback = (bScore) => { FlyingCallback(flyView, bScore); }; flyView.Ctrl.ShowUI(flyParam); _flyingList.Add(flyView); } }); } /// /// 物品全部飞入背包后的回调函数 /// /// /// private void FlyingCallback(DragonEggFlyIconView flyView, bool bScore) { _flyingList.Remove(flyView); Context.Asset.Recycle(flyView); if (_flyingList.Count == 0) OnchangBagState(false); if (bScore) { ShowDragonEggScoreUI(true); _bFlyingAni = false; if (_dropItemList == null) _dropItemList = new List(); else _dropItemList.Clear(); _dropItemList.AddRange(DragonEggData.Instance.DropItems); if (_dropItemList.Count > 1) { OnSortByEquipPower(); } var obtainData = new ObtainItemsParam() { dropItems = _dropItemList, bShowBtn = IsShowOneKeyBtn(DragonEggData.Instance.DropItems), }; var loadAsync = Context.UI.OpenAsync(UIKeys.ItemGetItemPanel, obtainData); loadAsync.On(_=> { DragonEggData.Instance.DropItems.Clear(); }); }; } private void OnSortByEquipPower() { _dropItemList.Sort((aItem, bItem)=> { int aEquipFlag = aItem.count == 0 ? 1 : 0; int bEquipFlag = bItem.count == 0 ? 1 : 0; if (aEquipFlag == bEquipFlag) { if (aEquipFlag == 1) { var aEquipData = EquipData.Instance.GetTemporaryEquipItem(aItem.id); var bEquipData = EquipData.Instance.GetTemporaryEquipItem(bItem.id); var aHighPower = GetCompareValue(aEquipData); var bHighPower = GetCompareValue(bEquipData); if (aHighPower == bHighPower) { if (aEquipData.FightingPower == bEquipData.FightingPower) { return aEquipData.Id.CompareTo(bEquipData.Id); // 升序 } return bEquipData.FightingPower.CompareTo(aEquipData.FightingPower); //降序 } else return bHighPower.CompareTo(aHighPower); } return aItem.count.CompareTo(bItem.count); } return bEquipFlag.CompareTo(aEquipFlag);//降序 }); } private int GetCompareValue(EquipItem equipData) { int compareFlag = 0; if (equipData?.Table?.Part > 0) { long compareFightingPower = equipData?.FightingPower ?? 0; var equipItem = EquipData.Instance.GetWearEquipData(equipData.Table.Part); long wearFightingPower = equipItem?.FightingPower ?? 0; if (compareFightingPower > wearFightingPower) compareFlag = 1; } return compareFlag; } /// /// 打开恭喜获得界面是否需要显示一键处理按钮 /// /// private bool IsShowOneKeyBtn(List dropItems) { bool bShowBtn = false; if (dropItems.Count > 0) { for (int i = 0; i < dropItems.Count; i++) { if (dropItems[i].count == 0) { return true; } } } return bShowBtn; } private void OnchangBagState(bool bOpen) { VM.BagBtn.GetController("State").selectedIndex = bOpen ? 1 : 0; } /// /// 打开龙蛋宝藏 /// private void OpenDragonTreasure() { int itemId = GetTreasureChestId(); var boxNum = ItemData.Instance.GetItemNum(itemId); _useCount = boxNum < _costDragonEggNum ? (int)boxNum : _costDragonEggNum; DragonEggService.Instance.SendToOpenDragonTreasure(itemId, _useCount); } /// /// 开龙蛋宝箱的掉落动画 /// private void PlayDropAni() { OnchangBagState(true); int count = DragonEggData.Instance.DropItems.Count - 1; for (int i = 0; i < DragonEggData.Instance.DropItems.Count; i++) { var itemData = DragonEggData.Instance.DropItems[i]; if (itemData != null) { ShowFlyIconView(GetItemId(itemData), i == count); } } } /// /// 获取道具tableId /// /// /// private int GetItemId(KindItem itemData) { if (itemData?.count == 0) { var equipData = EquipData.Instance.GetTemporaryEquipItem(itemData.id); return equipData == null ? 0 : equipData.TableId; } else { // 非装备的物品道具 return itemData.id; } } /// /// 宝箱按钮领取状态 /// /// private void OnchangeAwardState(bool bAward) { VM.AwardState.selectedIndex = bAward ? 1 : 0; } private void OnSelectDragon(int index) { VM.MenusBtnCtrl.selectedIndex = index; ShowSelectDragonUI(); } /// /// 获取当前选择的龙蛋道具id /// /// private int GetTreasureChestId() { return DragonEggData.Instance.GetTreasureChestId(VM.MenusBtnCtrl.selectedIndex); } /// /// 当前选中的龙蛋 /// private void ShowSelectDragonUI() { int selectedIndex = VM.MenusBtnCtrl.selectedIndex; ShowDragonName(selectedIndex); int itemId = GetTreasureChestId(); var itemInfo = ItemTableRepo.Get(itemId); if (itemInfo != null) { bool bShowDesc = itemInfo?.Desc.Length > 0; VM.ProbalityLabel.visible = bShowDesc; VM.DescBg.visible = bShowDesc; if (bShowDesc) VM.ProbalityLabel.text = itemInfo.Desc; } ShowDragonBoxNum(ItemData.Instance.GetItemNum(itemId)); // 获取龙的spine LoadDragonSpine("box_skeletondata"); ShowDragonSpine(false); } /// /// 龙蛋按钮菜单栏变化 /// /// private void OnMenusBtnCtrlChange(EventContext context) { ShowSelectDragonUI(); } private void ShowDragonName(int selectedIndex) { VM.PreviousBtn.visible = selectedIndex != 0; VM.NextBtn.visible = selectedIndex != 4; } /// /// 领取龙蛋积分宝箱后龙蛋进度条积分变化 /// private void RefreshTreasureProgressUI(int eventId, object args) { var rewardItems = DragonEggData.Instance.DragonTreasureRewardItems; if (rewardItems?.Count > 0) { var obtainData = new ObtainItemsParam() { dropItems = rewardItems, bShowBtn = IsShowOneKeyBtn(rewardItems), }; var loadAsync = Context.UI.OpenAsync(UIKeys.ItemGetItemPanel, obtainData); loadAsync.On(_ => { DragonEggData.Instance.DragonTreasureRewardItems.Clear(); }); } var treasureInfo = treasureBonusTableRepo.Get(DragonEggData.Instance.DragonTreasureId); if (treasureInfo != null) { ShowDragonTreasureUI(treasureInfo); } } /// /// 宝藏积分UI /// /// private void ShowDragonTreasureUI(treasureBonusTable treasureInfo) { if (treasureInfo == null) { treasureInfo = treasureBonusTableRepo.Get(DragonEggData.Instance.DragonTreasureId); } if (treasureInfo == null) return; _maxTreasureSocre = treasureInfo.Progress; var itemInfo = ItemTableRepo.Get(treasureInfo.Reward); if (itemInfo != null) { _treasureChestName = itemInfo.Name; //_treasureRewardId = itemInfo.Id; } ShowDragonEggScoreUI(false); int index = DragonEggData.Instance.GetTreasureChestIndex(treasureInfo.Reward); if (index > 0) { VM.BoxAwardBtn.GetController("c1").selectedIndex = index - 1; } } /// /// 龙蛋开箱动画 /// private void OpenDragonEggBoxAni() { ShowDragonSpine(true); if (VM.DragonSpine.Play("box_open", false)) { ClearOpenTimer(); _openBoxTimer = Context.Time.AddDelayTimer(VM.DragonSpine.spineAnimation.GetDurationMS(), () => { VM.DragonSpine.Play("box_idle", true); ShowDragonSpine(false); }); } } /// /// 请求宝箱消息返回,刷新UI信息 /// /// /// private void RefreshTreasureUI(int eventId, object args) { ShowUI(); } /// /// 成功开启龙蛋 /// /// /// private void OpenDragonTreasureSucess(int eventId, object args) { if (DragonEggData.Instance.DropItems?.Count > 0) { PlayDropAni(); } ShowDragonBoxNum(ItemData.Instance.GetItemNum(GetTreasureChestId())); } /// /// 宝箱等级变化 /// /// /// private void RefreshTreasureChestUI(int eventId, object args) { ShowDragonLv(); } /// /// 道具变化 /// /// /// private void UpdataItemData(int eventId, object args) { var itemDataList = args as List; if (itemDataList?.Count > 0) { int itemId = GetTreasureChestId(); for (int i = 0; i < itemDataList.Count; i++) { if (itemDataList[i].count > 0 && itemDataList[i].id == itemId) { ShowDragonBoxNum(ItemData.Instance.GetItemNum(itemId)); return; } } } } private void ClearOpenTimer() { if (_openBoxTimer != null) { _openBoxTimer.Cancel(); _openBoxTimer = null; } } /// /// 打开装备背包 /// /// /// private void OpenEquipBag(int eventId, object args) { Context.UI.OpenAsync(UIKeys.TemporaryEquipBagPanel); } } }