123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653 |
- /// #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
- {
- /// <summary>
- /// UI逻辑处理类
- /// </summary>
- /// <typeparam name=""></typeparam>
- public partial class DragonEggTreasurePanelCtrl : UIController<DragonEggTreasurePanelVM>
- {
- 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<DragonEggFlyIconView> _flyingList;
- private List<GButton> _dragonBtnList;
- private List<KindItem> _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);
- }
- /// <summary>
- /// 打开宝箱等级弹窗界面
- /// </summary>
- /// <param name="context"></param>
- 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);
- }
- /// <summary>
- /// 点击背包按钮
- /// </summary>
- /// <param name="context"></param>
- private void OnClickBagBtn(EventContext context)
- {
- DragonEggService.Instance.SendToOpenTemporaryEquipBag();
- }
- /// <summary>
- /// 积分进度条满值可领取对应的宝箱奖励
- /// </summary>
- /// <param name="context"></param>
- 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}";
- }
- /// <summary>
- /// 加载龙的spine
- /// </summary>
- /// <param name="spineName"></param>
- private void LoadDragonSpine(string spineName)
- {
- VM.DragonSpine.LoadSpine(spineName, "box_idle", true);
- }
- /// <summary>
- /// 是否显示龙的spine模型
- /// </summary>
- /// <param name="bShow"></param>
- private void ShowDragonSpine(bool bShow)
- {
- VM.DragonSpine.visible = bShow;
- VM.DragonIcon.visible = !bShow;
- }
- /// <summary>
- /// 龙蛋积分变化
- /// </summary>
- /// <param name="addScore"></param>
- /// <param name="bTweenValue"></param>
- 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<GButton>() { 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);
- }
- /// <summary>
- /// 判断龙裔宝卵数量是否
- /// </summary>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// 龙蛋品质宝箱数量
- /// </summary>
- /// <param name="index"></param>
- /// <param name="num"></param>
- 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<DragonEggFlyIconView>();
- 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);
- }
- });
- }
- /// <summary>
- /// 物品全部飞入背包后的回调函数
- /// </summary>
- /// <param name="flyView"></param>
- /// <param name="bScore"></param>
- 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<KindItem>();
- 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;
- }
- /// <summary>
- /// 打开恭喜获得界面是否需要显示一键处理按钮
- /// </summary>
- /// <returns></returns>
- private bool IsShowOneKeyBtn(List<KindItem> 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;
- }
- /// <summary>
- /// 打开龙蛋宝藏
- /// </summary>
- private void OpenDragonTreasure()
- {
- int itemId = GetTreasureChestId();
- var boxNum = ItemData.Instance.GetItemNum(itemId);
- _useCount = boxNum < _costDragonEggNum ? (int)boxNum : _costDragonEggNum;
- DragonEggService.Instance.SendToOpenDragonTreasure(itemId, _useCount);
- }
- /// <summary>
- /// 开龙蛋宝箱的掉落动画
- /// </summary>
- 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);
- }
- }
- }
- /// <summary>
- /// 获取道具tableId
- /// </summary>
- /// <param name="itemData"></param>
- /// <returns></returns>
- 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;
- }
- }
- /// <summary>
- /// 宝箱按钮领取状态
- /// </summary>
- /// <param name="bGray"></param>
- private void OnchangeAwardState(bool bAward)
- {
- VM.AwardState.selectedIndex = bAward ? 1 : 0;
- }
- private void OnSelectDragon(int index)
- {
- VM.MenusBtnCtrl.selectedIndex = index;
- ShowSelectDragonUI();
- }
- /// <summary>
- /// 获取当前选择的龙蛋道具id
- /// </summary>
- /// <returns></returns>
- private int GetTreasureChestId()
- {
- return DragonEggData.Instance.GetTreasureChestId(VM.MenusBtnCtrl.selectedIndex);
- }
- /// <summary>
- /// 当前选中的龙蛋
- /// </summary>
- 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);
- }
- /// <summary>
- /// 龙蛋按钮菜单栏变化
- /// </summary>
- /// <param name="context"></param>
- private void OnMenusBtnCtrlChange(EventContext context)
- {
- ShowSelectDragonUI();
- }
- private void ShowDragonName(int selectedIndex)
- {
- VM.PreviousBtn.visible = selectedIndex != 0;
- VM.NextBtn.visible = selectedIndex != 4;
- }
- /// <summary>
- /// 领取龙蛋积分宝箱后龙蛋进度条积分变化
- /// </summary>
- 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);
- }
- }
- /// <summary>
- /// 宝藏积分UI
- /// </summary>
- /// <param name="treasureInfo"></param>
- 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;
- }
- }
- /// <summary>
- /// 龙蛋开箱动画
- /// </summary>
- 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);
- });
- }
- }
- /// <summary>
- /// 请求宝箱消息返回,刷新UI信息
- /// </summary>
- /// <param name="eventId"></param>
- /// <param name="args"></param>
- private void RefreshTreasureUI(int eventId, object args)
- {
- ShowUI();
- }
- /// <summary>
- /// 成功开启龙蛋
- /// </summary>
- /// <param name="eventId"></param>
- /// <param name="args"></param>
- private void OpenDragonTreasureSucess(int eventId, object args)
- {
- if (DragonEggData.Instance.DropItems?.Count > 0)
- {
- PlayDropAni();
- }
- ShowDragonBoxNum(ItemData.Instance.GetItemNum(GetTreasureChestId()));
- }
- /// <summary>
- /// 宝箱等级变化
- /// </summary>
- /// <param name="eventId"></param>
- /// <param name="args"></param>
- private void RefreshTreasureChestUI(int eventId, object args)
- {
- ShowDragonLv();
- }
- /// <summary>
- /// 道具变化
- /// </summary>
- /// <param name="eventId"></param>
- /// <param name="args"></param>
- private void UpdataItemData(int eventId, object args)
- {
- var itemDataList = args as List<ActItem>;
- 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;
- }
- }
- /// <summary>
- /// 打开装备背包
- /// </summary>
- /// <param name="eventId"></param>
- /// <param name="args"></param>
- private void OpenEquipBag(int eventId, object args)
- {
- Context.UI.OpenAsync(UIKeys.TemporaryEquipBagPanel);
- }
- }
- }
|