/// #pkgName FGUI包名
/// #panelName UIPanel名字
/// #UIName = $"{#pkgName}{#panelName}" UIKey名字
/// 该脚本由模板创建
/// created by cb 2024
using FairyGUI;
using FL.Data;
using FL.Network;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using XGame.Database;
using XGame.Framework.UI;
namespace FL.FGUI
{
///
/// 高级坐骑升级预览
///
public struct UpgradePreviewParam
{
public int mountId;
public int level;
public int skillId;
}
///
/// UI逻辑处理类
///
///
public partial class MountAdvancedNestedCtrl : UIController
{
private bool _bInitData = true;
private int _curAdvancedIndex; // 当前选择的高级坐骑索引
private int[] _costData; // 解锁|升级消耗的道具id和数量
private List _advancedMountsInfoList; // 高级坐骑信息列表数据
private Dictionary> _upgradePreviewMap; // 坐骑升级预览数据
protected override void OnEnable(object intent)
{
AddUIListenres();
AddEventListener();
}
protected override void OnDisable()
{
RemoveUIListenres();
RemoveEventListener();
}
#region UI事件
private void AddUIListenres()
{
VM.RideBtn.onClick.Add(OnClickRideBtn);
VM.UpgradePreviewBtn.onClick.Add(OnClickUpgradePreviewBtn);
VM.EnableSkillBtn.onClick.Add(OnClickEnableSkillBtn);
VM.UpgradeMountBtn.onClick.Add(OnClickUpgradeMountBtn);
VM.UnlockMountBtn.onClick.Add(OnClickUnlockMountBtn);
}
private void RemoveUIListenres()
{
VM.RideBtn.onClick.Remove(OnClickRideBtn);
VM.UpgradePreviewBtn.onClick.Remove(OnClickUpgradePreviewBtn);
VM.EnableSkillBtn.onClick.Remove(OnClickEnableSkillBtn);
VM.UpgradeMountBtn.onClick.Remove(OnClickUpgradeMountBtn);
VM.UnlockMountBtn.onClick.Remove(OnClickUnlockMountBtn);
}
private void OnClickRideBtn(EventContext context)
{
int mountId = _advancedMountsInfoList[_curAdvancedIndex].Id;
if (mountId == 0)
{
XGame.Framework.Assert.IsNotNull(mountId);
}
if (mountId == MountData.Instance.MountModelId)
{
MountService.Instance.SendToRideDown();
}
else
MountService.Instance.SendToRideMount(mountId);
}
private void OnClickUpgradePreviewBtn(EventContext context)
{
int mountId = _advancedMountsInfoList[_curAdvancedIndex].Id;
var dataList = _upgradePreviewMap.ContainsKey(mountId) ? _upgradePreviewMap[mountId] : null;
Context.UI.OpenAsync(UIKeys.MountUpgradePreviewPanel, dataList);
}
private void OnClickEnableSkillBtn(EventContext context)
{
if (VM.SkillCtrl.selectedIndex == 0)
MountService.Instance.SendToEnableAdvancedSkill(_advancedMountsInfoList[_curAdvancedIndex].Id);
else
MountService.Instance.SendToCancelAdvancedSkill();
}
private void OnClickUpgradeMountBtn(EventContext context)
{
MountService.Instance.SendToUpgradeStar(_advancedMountsInfoList[_curAdvancedIndex].Id);
}
private void OnClickUnlockMountBtn(EventContext context)
{
if (_advancedMountsInfoList[_curAdvancedIndex]?.Unlock.Length > 1)
{
int itemId = _advancedMountsInfoList[_curAdvancedIndex].Unlock[0];
int needNum = _advancedMountsInfoList[_curAdvancedIndex].Unlock[1];
if (ItemService.Instance.IsEnough(itemId, needNum, true))
{
MountService.Instance.SendToUnlockAdvancedMount(_advancedMountsInfoList[_curAdvancedIndex].Id);
}
}
}
#endregion
private void AddEventListener()
{
Context.AddListener(EventDefine.ChangeRideState, ChangeRideState);
Context.AddListener(EventDefine.UpdataItemData, OnUpdateItemNum);
Context.AddListener(EventDefine.UnlockAdvancedMountSucessful, UnlockAdvancedMountSucessful);
Context.AddListener(EventDefine.UseMountSkillSucessful, UseMountSkillSucessful);
Context.AddListener(EventDefine.UpgradeAdvancedMountSucessful, UpgradeAdvancedMountSucessful);
VM.AdvancedMountList.ItemClickEvent += ClickAdvancedMountItem;
}
private void RemoveEventListener()
{
Context.RemoveListener(EventDefine.ChangeRideState, ChangeRideState);
Context.RemoveListener(EventDefine.UpdataItemData, OnUpdateItemNum);
Context.RemoveListener(EventDefine.UnlockAdvancedMountSucessful, UnlockAdvancedMountSucessful);
Context.RemoveListener(EventDefine.UseMountSkillSucessful, UseMountSkillSucessful);
Context.RemoveListener(EventDefine.UpgradeAdvancedMountSucessful, UpgradeAdvancedMountSucessful);
VM.AdvancedMountList.ItemClickEvent -= ClickAdvancedMountItem;
}
public void ClearData()
{
_advancedMountsInfoList?.Clear();
_advancedMountsInfoList = null;
_upgradePreviewMap?.Clear();
_upgradePreviewMap = null;
_bInitData = true;
}
///
/// 高级坐骑
///
private void GetAdvanceMountData()
{
if (_advancedMountsInfoList == null) _advancedMountsInfoList = new List();
var dataList = zuoqiBaseTableRepo.GetAll();
foreach (var item in dataList)
{
if (item.Type == 2) // 高级坐骑
{
_advancedMountsInfoList.Add(item);
}
}
}
private void Init()
{
GetAdvanceMountData();
InitUpgradePreviewData();
}
private void InitUpgradePreviewData()
{
if (_upgradePreviewMap == null)
{
_upgradePreviewMap = new Dictionary>();
}
var cfgDataList = zuoqiStarTableRepo.GetAll();
UpgradePreviewParam upgradePreviewData;
foreach (var item in cfgDataList)
{
if (item.Preview > 0)
{
upgradePreviewData = new UpgradePreviewParam()
{
mountId = item.Zqid,
level = item.Level,
skillId = item.StarSkill
};
if (_upgradePreviewMap.ContainsKey(item.Zqid))
{
_upgradePreviewMap[item.Zqid].Add(upgradePreviewData);
}
else
{
_upgradePreviewMap.Add(item.Zqid, new List()
{
upgradePreviewData
});
}
}
}
}
private void ShowAdvancedNestedUI(bool bShow, bool bHideList = true)
{
if (!bShow)
{
bHideList = !bHideList;
}
VM.AdvancedMountList.visible = bHideList;
if (bShow)
{
VM.UnlockMountItem.Enable(null);
}
else
{
VM.UnlockMountItem.Disable();
}
}
///
/// 显示高级坐骑界面UI
///
public void ShowAdvancedMountUI(bool bSelectAdvancedIndex)
{
if (_bInitData)
{
Init();
_bInitData = false;
}
_advancedMountsInfoList.Sort((a, b) =>
{
int aUnlcok = MountData.Instance.IsUnLock(a.Id) ? 1 : 0;
int bUnlcok = MountData.Instance.IsUnLock(b.Id) ? 1 : 0;
if (aUnlcok == bUnlcok)
{
int aLevel = MountData.Instance.GetAdvancedMountLv(a.Id);
int bLevel = MountData.Instance.GetAdvancedMountLv(b.Id);
if (aLevel == bLevel)
{
return a.Id - b.Id;
}
return bLevel - aLevel;
}
return bUnlcok - aUnlcok;
});
if (bSelectAdvancedIndex)
{
int index = _advancedMountsInfoList.FindIndex(item => item.Id == MountData.Instance.MountModelId);
if (index != -1) _curAdvancedIndex = index;
else _curAdvancedIndex = 0;
}
ShowMountsListUI();
}
private void ShowMountsListUI()
{
VM.AdvancedMountList.BindDatas(_advancedMountsInfoList);
ClickAdvancedMountItem(_curAdvancedIndex);
}
///
/// 高级坐骑的升级预览和启用技能按钮状态
///
///
private void ShowAdvancedMountBtn(bool bShow)
{
VM.UpgradePreviewBtn.visible = bShow;
VM.EnableSkillBtn.visible = bShow;
}
///
/// 选择的高级坐骑
///
///
private void ShowSelectAdvancedMount(int selectIndex)
{
_curAdvancedIndex = selectIndex;
var advancedMountInfo = _advancedMountsInfoList[selectIndex];
if (advancedMountInfo != null)
{
VM.MountNameLabel.text = advancedMountInfo.Name;
bool bUnlock = MountData.Instance.IsUnLock(advancedMountInfo.Id);
int lv = MountData.Instance.GetAdvancedMountLv(advancedMountInfo.Id);
ShowMountSpine(advancedMountInfo.Spine);
ShowAttrUI(lv, advancedMountInfo.Id, bUnlock);
ShowRideUI(bUnlock, advancedMountInfo.Id == MountData.Instance.MountModelId);
ShowEnableSkillBtn(bUnlock, advancedMountInfo.Id == MountData.Instance.UseSkillMountId);
VM.MountStar.visible = lv > 0;
ShowAdvanceMountStarUI(lv);
}
}
///
/// 坐骑的立绘
///
///
private void ShowMountSpine(string spineName)
{
VM.MountSpine.LoadSpine(spineName, "stand", true);
}
///
/// 高级坐骑的技能id
///
///
private void ShowMountSkillUI(int skillId)
{
var skillInfo = SkillTableRepo.Get(skillId);
if (skillInfo != null)
{
VM.SkillIcon.icon = skillInfo.Icon;
VM.SkillNameLabel.text = skillInfo.Name;
VM.SkillDescLabel.text = skillInfo.Desc;
}
}
///
///
///
///
private void ShowEnableSkillBtn(bool bShow, bool bEnable = false)
{
VM.EnableSkillBtn.visible = bShow;
if (bShow)
{
VM.SkillCtrl.selectedIndex = bEnable ? 1 : 0;
}
}
///
/// 是否显示高级坐骑升级预览
///
///
private void ShowUpgradePreviewBtn(bool bShow)
{
VM.UpgradePreviewBtn.visible = bShow;
}
///
/// 高级坐骑拥有的效果
///
///
///
private void ShowAttrUI(int curLv, int mountId, bool bUnlock)
{
if (curLv == 0)
{
var mountBaseInfo = zuoqiBaseTableRepo.Get(mountId);
XGame.Framework.Assert.IsNotNull(mountBaseInfo, $"坐骑表zuoqiBase中不存在id:{mountId}");
ShowCostUI(mountBaseInfo.Unlock);
}
//10000*坐骑id+等级
var mountStarInfo = zuoqiStarTableRepo.Get(mountId * 10000 + curLv);
if (mountStarInfo == null)
{
XGame.Log.Error($"坐骑表zuoqiStar中不存在id:{mountId * 10000 + curLv}");
return;
}
if (bUnlock)
ShowCostUI(mountStarInfo.StarUpCost);
ShowMountSkillUI(mountStarInfo.StarSkill);
int attrType = mountStarInfo.LevelOwnAttr[0];
int attrVal = mountStarInfo.LevelOwnAttr.Length > 1 ? mountStarInfo.LevelOwnAttr[1] : 0;
ShowCurAtrrDesc(attrType, attrVal);
var nextMountStarInfo = zuoqiStarTableRepo.Get(mountId * 10000 + curLv + 1);
if (nextMountStarInfo != null)
{
if (nextMountStarInfo?.LevelOwnAttr?.Length > 1)
{
VM.NextMountAttrDescLabel.text = nextMountStarInfo.LevelOwnAttr[1].ToRealFloatPercentage();
}
if (curLv == 0)
{
ShowMountSkillUI(nextMountStarInfo.StarSkill);
}
ShowAdvancedNestedUI(true);
}
else
ShowAdvancedNestedUI(false, false);
VM.AdvanceMountStateCtrl.selectedIndex = bUnlock ? (nextMountStarInfo == null ? 2 : 1) : 0;
if (bUnlock) ShowUpgradePreviewBtn(nextMountStarInfo != null);
}
private void ShowCurAtrrDesc(int attrType, int attrValue)
{
var attributeInfo = AttrDescTableRepo.Get(attrType);
if (attributeInfo != null)
{
VM.MountAttrDescLabel.text = attributeInfo.ShowName + attrValue.ToRealFloatPercentage();
}
}
///
/// 高级坐骑解锁|升级消耗UI
///
///
private void ShowCostUI(int[] costData)
{
_costData = costData;
long num = ItemData.Instance.GetItemNum(costData[0]);
var item = ItemService.Instance.GetItemBase(costData[0], num, 0);
VM.UnlockMountItem.Ctrl.ShowUI(item);
VM.UnlockMountItem.VM.CountLabel.visible = true;
VM.UnlockMountItem.VM.CountLabel.text = $"{num.FormatNumber()}/{((long)costData[1]).FormatNumber()}";
}
///
/// 显示高级坐骑的星级
///
///
private void ShowAdvanceMountStarUI(int starLevel)
{
int starNum = starLevel == 0 ? 0 : starLevel % MountData.MaxStarNum;
if (starLevel > 0 && starNum == 0)
starNum = MountData.MaxStarNum;
// 获取星星的色阶
int starStep = Mathf.FloorToInt((starLevel - 1) / MountData.MaxStarNum) + 1;
int[] array = Enumerable.Repeat(starStep, starNum).ToArray();
VM.MountStar.BindDatas(array);
}
///
/// 选择某个高级坐骑
///
///
private void ClickAdvancedMountItem(int index)
{
ShowSelectAdvancedMount(index);
var mountItemList = VM.AdvancedMountList.GetChildren();
for (int i = 0; i < mountItemList.Length; i++)
{
if (mountItemList[i] != null)
{
(mountItemList[i].asCom).GetController("SelectCtrl").selectedIndex = i == index ? 1 : 0;
}
}
}
///
/// 解锁某个高级坐骑成功
///
///
///
private void UnlockAdvancedMountSucessful(int eventId, object args)
{
ShowAdvancedMountUI(false);
}
///
/// 成功启用某个高级坐骑技能
///
///
///
private void UseMountSkillSucessful(int eventId, object args)
{
ShowEnableSkillBtn(true, _advancedMountsInfoList[_curAdvancedIndex].Id == MountData.Instance.UseSkillMountId);
}
///
/// 高级坐骑升级成功
///
///
///
private void UpgradeAdvancedMountSucessful(int eventId, object args)
{
ShowSelectAdvancedMount(_curAdvancedIndex);
Context.UI.OpenAsync(UIKeys.MountUpgradeSuccessfulPanel);
}
///
/// 骑乘状态UI
///
///
///
private void ShowRideUI(bool bShow, bool bRide = false)
{
VM.RideBtn.visible = bShow;
if (bShow)
{
VM.RideCtrl.selectedIndex = bRide ? 1 : 0;
}
}
///
/// 坐骑骑乘状态变化
///
///
///
private void ChangeRideState(int eventId, object args)
{
int mountId = _advancedMountsInfoList[_curAdvancedIndex].Id;
ShowRideUI(MountData.Instance.IsUnLock(mountId), mountId == MountData.Instance.MountModelId);
ShowMountsListUI();
}
///
/// 道具数据变化
///
///
///
private void OnUpdateItemNum(int eventId, object args)
{
var itemDataList = args as List;
if (itemDataList?.Count > 0)
{
for (int i = 0; i < itemDataList.Count; i++)
{
if (itemDataList[i].id == _costData[0])
{
ShowCostUI(_costData);
}
}
}
}
}
}