375 lines
14 KiB
C#
375 lines
14 KiB
C#
using Assets.MyGame.Scripts.Net;
|
||
using Assets.MyGame.Scripts.Net.API.GameConfig_SubClass;
|
||
using Assets.MyGame.Scripts.Net.APIRequest.Params;
|
||
using Assets.MyGame.Scripts.Net.APIResponses;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using TMPro;
|
||
using UnityEngine;
|
||
using UnityEngine.UI;
|
||
|
||
public class ZhanLing_Panel : SubUIBase
|
||
{
|
||
[Header("战令等级奖励页面")]
|
||
public TextMeshProUGUI tmp_Level;
|
||
public TextMeshProUGUI tmp_Exp;
|
||
public Slider slider_LevelExp;
|
||
|
||
public LevelGiftitem levelGiftitemPrefab;
|
||
public Transform levelGiftitemParent;
|
||
public LevelGiftitem levelGiftitem_100;
|
||
|
||
public Button btn_GetAllGift;
|
||
public Button btn_BuyJinJieZhanLing;
|
||
public TextMeshProUGUI btn_BuyZhanLing_Label;
|
||
public TextMeshProUGUI tmp_ActiveTime; //战令刷新时间 30天10:01:50
|
||
|
||
[Header("任务页面")]
|
||
public RenWuItem renWuItemPrefab;
|
||
public Transform renWuItemParent;
|
||
|
||
[Header("购买等级礼包页面")]
|
||
public BackPackPropItem buyLevelGiftitemPrefab;
|
||
public Transform buyLevelGiftitemMianFeiParent;
|
||
public Transform buyLevelGiftitemJingYingParent;
|
||
public Transform buyLevelGiftitemZhiZunParent;
|
||
public Slider slider_BuyLevelExpCount;
|
||
public Button btn_Buy;
|
||
public TextMeshProUGUI tmp_PriceNum;
|
||
public TextMeshProUGUI tmp_BuyLabel; //购买<color=yellow> 99999 </color>点经验值升至 <color=green>100 </color>级
|
||
|
||
[Header("活动切换项")]
|
||
public GameObject huoDongToggleItemPrefab;
|
||
public Transform huoDongToggleItemParent;
|
||
|
||
[Header("购买进阶战令页面")]
|
||
public Button btn_JinJie;
|
||
public Button btn_ZhiZun;
|
||
public Button btn_ZhiZunAndJinJie;
|
||
|
||
private BattleOrderConfigInfo _config;
|
||
private UserBattleOrderInfo _userData;
|
||
private int _selectedBuyLevel;
|
||
|
||
// ==================== 生命周期 ====================
|
||
|
||
protected override void OnShow(object args = null)
|
||
{
|
||
base.OnShow(args);
|
||
RefreshAll();
|
||
}
|
||
|
||
private void RefreshAll()
|
||
{
|
||
APIRequestList.GetBattleOrder(res =>
|
||
{
|
||
if (res == null || !res.isOpen) return;
|
||
_config = res.config;
|
||
_userData = res.userBattleOrder;
|
||
|
||
RefreshLevelPage();
|
||
RefreshTasksPage();
|
||
RefreshBuyPage();
|
||
RefreshBtnLabels();
|
||
});
|
||
}
|
||
|
||
// ==================== 等级奖励页面 ====================
|
||
|
||
private int _maxLevel = 100;
|
||
|
||
private void RefreshLevelPage()
|
||
{
|
||
if (_config?.levelConfig == null || _userData == null) return;
|
||
|
||
int level = _userData.level;
|
||
int exp = _userData.exp;
|
||
var lvCfg = _config.levelConfig.Find(c => c.level == level);
|
||
int expNeeded = lvCfg?.exp ?? 100;
|
||
|
||
tmp_Level.text = $"Lv.{level}";
|
||
tmp_Exp.text = $"<color=yellow>{exp}</color>/{expNeeded}";
|
||
slider_LevelExp.maxValue = expNeeded;
|
||
slider_LevelExp.value = Mathf.Clamp(exp, 0, expNeeded);
|
||
|
||
// 战令剩余时间
|
||
RefreshActiveTime();
|
||
|
||
// 清空旧列表
|
||
for (int i = levelGiftitemParent.childCount - 1; i >= 0; i--)
|
||
Destroy(levelGiftitemParent.GetChild(i).gameObject);
|
||
|
||
// 已领取的等级集合
|
||
var claimedSet = _userData.receivedLevels != null
|
||
? new HashSet<int>(_userData.receivedLevels)
|
||
: new HashSet<int>();
|
||
|
||
// 构建等级礼包列表(1-99级显示礼包内容,100级占位)
|
||
foreach (var lv in _config.levelConfig)
|
||
{
|
||
if (lv.level > _maxLevel) continue;
|
||
|
||
if (lv.level == _maxLevel)
|
||
{
|
||
// 100级占位:创建一个空对象保持布局对齐
|
||
var placeholder = new GameObject("Placeholder_Lv100", typeof(RectTransform));
|
||
placeholder.transform.SetParent(levelGiftitemParent, false);
|
||
var rt = placeholder.GetComponent<RectTransform>();
|
||
var prefabRt = levelGiftitemPrefab.GetComponent<RectTransform>();
|
||
if (rt != null && prefabRt != null)
|
||
{
|
||
rt.sizeDelta = prefabRt.sizeDelta;
|
||
rt.anchorMin = prefabRt.anchorMin;
|
||
rt.anchorMax = prefabRt.anchorMax;
|
||
rt.pivot = prefabRt.pivot;
|
||
}
|
||
// 如果 prefab 有 LayoutElement,也复制
|
||
var prefabLE = levelGiftitemPrefab.GetComponent<LayoutElement>();
|
||
if (prefabLE != null)
|
||
{
|
||
var le = placeholder.AddComponent<LayoutElement>();
|
||
le.preferredWidth = prefabLE.preferredWidth;
|
||
le.preferredHeight = prefabLE.preferredHeight;
|
||
le.minWidth = prefabLE.minWidth;
|
||
le.minHeight = prefabLE.minHeight;
|
||
}
|
||
continue;
|
||
}
|
||
|
||
var item = Instantiate(levelGiftitemPrefab, levelGiftitemParent);
|
||
item.gameObject.SetActive(true);
|
||
|
||
bool showDuiGou = claimedSet.Contains(lv.level);
|
||
bool reachedLevel = lv.level <= level;
|
||
|
||
// 用 level 作为"已达成+已领取"的组合标记:达成则显示礼包类型,已领取则显示对勾
|
||
LevelGiftType giftType = 0;
|
||
if (lv.normalGiftType > 0) giftType |= LevelGiftType.MianFei;
|
||
if (lv.luxuryGiftType > 0 && _userData.isLuxury) giftType |= LevelGiftType.JingYing;
|
||
if (lv.supremeGiftType > 0 && _userData.isSupreme) giftType |= LevelGiftType.ZhiZun;
|
||
|
||
item.Init(
|
||
GetIconUrl(lv.normalGiftType), lv.normalGiftCount,
|
||
GetIconUrl(lv.luxuryGiftType), lv.luxuryGiftCount,
|
||
GetIconUrl(lv.supremeGiftType), lv.supremeGiftCount,
|
||
lv.level, giftType, reachedLevel && showDuiGou);
|
||
}
|
||
|
||
// 100级礼包
|
||
var lv100 = _config.levelConfig.Find(c => c.level == _maxLevel);
|
||
if (lv100 != null && levelGiftitem_100 != null)
|
||
{
|
||
bool showDuiGou = claimedSet.Contains(_maxLevel);
|
||
bool reachedLevel = level >= _maxLevel;
|
||
|
||
LevelGiftType giftType100 = LevelGiftType.MianFei;
|
||
if (lv100.luxuryGiftType > 0 && _userData.isLuxury) giftType100 |= LevelGiftType.JingYing;
|
||
if (lv100.supremeGiftType > 0 && _userData.isSupreme) giftType100 |= LevelGiftType.ZhiZun;
|
||
|
||
levelGiftitem_100.Init(
|
||
GetIconUrl(lv100.normalGiftType), lv100.normalGiftCount,
|
||
GetIconUrl(lv100.luxuryGiftType), lv100.luxuryGiftCount,
|
||
GetIconUrl(lv100.supremeGiftType), lv100.supremeGiftCount,
|
||
_maxLevel, giftType100, reachedLevel && showDuiGou);
|
||
levelGiftitem_100.gameObject.SetActive(true);
|
||
}
|
||
}
|
||
|
||
// ==================== 任务页面 ====================
|
||
|
||
private void RefreshTasksPage()
|
||
{
|
||
if (renWuItemPrefab == null || renWuItemParent == null) return;
|
||
|
||
for (int i = renWuItemParent.childCount - 1; i >= 0; i--)
|
||
Destroy(renWuItemParent.GetChild(i).gameObject);
|
||
|
||
APIRequestList.GetUserTaskList(tasks =>
|
||
{
|
||
if (tasks == null) return;
|
||
foreach (var t in tasks)
|
||
{
|
||
float progress = t.taskCount > 0 ? (float)t.count / t.taskCount : 0f;
|
||
CreateTaskItem(t.exp, t.count, t.taskCount, t.remark, progress, UIPanel.None);
|
||
}
|
||
});
|
||
}
|
||
|
||
|
||
private void CreateTaskItem(int expCount, int currJinDu, int maxJinDu, string label, float progress, UIPanel targetPanel)
|
||
{
|
||
var item = Instantiate(renWuItemPrefab, renWuItemParent);
|
||
item.gameObject.SetActive(true);
|
||
item.Init(expCount, currJinDu, maxJinDu, label, progress, targetPanel, uIManager);
|
||
}
|
||
|
||
// ==================== 购买等级页面 ====================
|
||
|
||
private void RefreshBuyPage()
|
||
{
|
||
if (_config?.levelConfig == null) return;
|
||
|
||
slider_BuyLevelExpCount.minValue = 1;
|
||
slider_BuyLevelExpCount.maxValue = _maxLevel;
|
||
slider_BuyLevelExpCount.value = _userData.level;
|
||
slider_BuyLevelExpCount.wholeNumbers = true;
|
||
slider_BuyLevelExpCount.onValueChanged.RemoveAllListeners();
|
||
slider_BuyLevelExpCount.onValueChanged.AddListener(OnBuyLevelSliderChanged);
|
||
|
||
_selectedBuyLevel = _userData.level;
|
||
UpdateBuyLevelGifts(_selectedBuyLevel);
|
||
UpdateBuyPrice();
|
||
}
|
||
|
||
private void OnBuyLevelSliderChanged(float value)
|
||
{
|
||
_selectedBuyLevel = Mathf.RoundToInt(value);
|
||
UpdateBuyLevelGifts(_selectedBuyLevel);
|
||
UpdateBuyPrice();
|
||
}
|
||
|
||
private void UpdateBuyLevelGifts(int level)
|
||
{
|
||
ClearBuyGiftParent(buyLevelGiftitemMianFeiParent);
|
||
ClearBuyGiftParent(buyLevelGiftitemJingYingParent);
|
||
ClearBuyGiftParent(buyLevelGiftitemZhiZunParent);
|
||
|
||
var cfg = _config?.levelConfig?.Find(c => c.level == level);
|
||
if (cfg == null) return;
|
||
|
||
AddBuyGiftByType(buyLevelGiftitemMianFeiParent, cfg.normalGiftType, cfg.normalGiftCount);
|
||
AddBuyGiftByType(buyLevelGiftitemJingYingParent, cfg.luxuryGiftType, cfg.luxuryGiftCount);
|
||
AddBuyGiftByType(buyLevelGiftitemZhiZunParent, cfg.supremeGiftType, cfg.supremeGiftCount);
|
||
}
|
||
|
||
private void ClearBuyGiftParent(Transform parent)
|
||
{
|
||
if (parent == null) return;
|
||
for (int i = parent.childCount - 1; i >= 0; i--)
|
||
Destroy(parent.GetChild(i).gameObject);
|
||
}
|
||
|
||
private void AddBuyGiftByType(Transform parent, int giftType, int giftCount)
|
||
{
|
||
if (parent == null || giftType <= 0) return;
|
||
var obj = Instantiate(buyLevelGiftitemPrefab, parent);
|
||
// 1=金币 2=钻石 3=道具,金钻用本地图标
|
||
string url = "";
|
||
PropEnum pe = PropEnum.none;
|
||
obj.Init(url, giftCount, pe);
|
||
}
|
||
|
||
private void UpdateBuyPrice()
|
||
{
|
||
int levelsToBuy = _selectedBuyLevel - _userData.level;
|
||
if (levelsToBuy <= 0)
|
||
{
|
||
tmp_PriceNum.text = "已达成";
|
||
if (tmp_BuyLabel != null) tmp_BuyLabel.text = "";
|
||
btn_Buy.interactable = false;
|
||
}
|
||
else
|
||
{
|
||
// 计算从当前等级到目标等级所需总经验
|
||
int totalExp = 0;
|
||
if (_config?.levelConfig != null)
|
||
{
|
||
for (int lv = _userData.level + 1; lv <= _selectedBuyLevel; lv++)
|
||
{
|
||
var cfg = _config.levelConfig.Find(c => c.level == lv);
|
||
if (cfg != null) totalExp += cfg.exp;
|
||
}
|
||
}
|
||
|
||
tmp_PriceNum.text = $"{levelsToBuy * 100}钻石";
|
||
if (tmp_BuyLabel != null)
|
||
tmp_BuyLabel.text = $"购买<color=yellow> {totalExp} </color>点经验值升至 <color=green>{_selectedBuyLevel} </color>级";
|
||
btn_Buy.interactable = true;
|
||
}
|
||
}
|
||
|
||
// ==================== 按钮事件 ====================
|
||
|
||
private void RefreshBtnLabels()
|
||
{
|
||
if (btn_BuyZhanLing_Label != null)
|
||
{
|
||
btn_BuyZhanLing_Label.text = (_userData.isLuxury || _userData.isSupreme) ? "已购买" : "购买战令";
|
||
}
|
||
|
||
btn_GetAllGift.onClick.RemoveAllListeners();
|
||
btn_GetAllGift.onClick.AddListener(() =>
|
||
{
|
||
APIRequestList.ReceiveAllBattleOrderRewards(res =>
|
||
{
|
||
Debug.Log($"[ZhanLing] 一键领取完成, 获得 {res?.Count ?? 0} 件奖励");
|
||
RefreshAll();
|
||
});
|
||
});
|
||
|
||
btn_Buy.onClick.RemoveAllListeners();
|
||
btn_Buy.onClick.AddListener(() =>
|
||
{
|
||
int levelsToBuy = _selectedBuyLevel - _userData.level;
|
||
if (levelsToBuy <= 0) return;
|
||
|
||
APIRequestList.BuyBattleOrderLevel(new BuyBattleOrderLevelParam { levelCount = levelsToBuy }, res =>
|
||
{
|
||
Debug.Log($"[ZhanLing] 购买等级完成: 新等级={res.newLevel}, 花费={res.cost}");
|
||
RefreshAll();
|
||
});
|
||
});
|
||
|
||
btn_JinJie.onClick.RemoveAllListeners();
|
||
btn_JinJie.onClick.AddListener(() => BuyAdvance(1));
|
||
|
||
btn_ZhiZun.onClick.RemoveAllListeners();
|
||
btn_ZhiZun.onClick.AddListener(() => BuyAdvance(2));
|
||
|
||
btn_ZhiZunAndJinJie.onClick.RemoveAllListeners();
|
||
btn_ZhiZunAndJinJie.onClick.AddListener(() => BuyAdvance(3));
|
||
}
|
||
|
||
private void BuyAdvance(int type)
|
||
{
|
||
APIRequestList.BuyBattleOrderAdvance(new BuyBattleOrderAdvanceParam { type = type }, res =>
|
||
{
|
||
Debug.Log($"[ZhanLing] 购买进阶 type={type}, success={res.success}");
|
||
RefreshAll();
|
||
});
|
||
}
|
||
|
||
// ==================== 工具 ====================
|
||
|
||
/// <summary>根据礼物类型返回图标路径(空串=金币/钻石用本地Sprite)</summary>
|
||
private static string GetIconUrl(int giftType)
|
||
{
|
||
return giftType switch
|
||
{
|
||
1 => "", // 金币 - 本地图标
|
||
2 => "", // 钻石 - 本地图标
|
||
_ => "", // 道具 - 本地图标
|
||
};
|
||
}
|
||
|
||
/// <summary>刷新战令剩余时间(格式:30天10:01:50)</summary>
|
||
private void RefreshActiveTime()
|
||
{
|
||
if (tmp_ActiveTime == null || _config == null) return;
|
||
|
||
long now = BeijingTimeManager.Instance.CurrentTimestampMs / 1000;
|
||
long remain = _config.endTime - now;
|
||
if (remain <= 0)
|
||
{
|
||
tmp_ActiveTime.text = "已结束";
|
||
return;
|
||
}
|
||
|
||
int days = (int)(remain / 86400);
|
||
int hours = (int)((remain % 86400) / 3600);
|
||
int mins = (int)((remain % 3600) / 60);
|
||
int secs = (int)(remain % 60);
|
||
tmp_ActiveTime.text = $"{days}天{hours:D2}:{mins:D2}:{secs:D2}";
|
||
}
|
||
}
|