Files
ft/Client/Assets/Scripts/Event1v1Battle/Panel/Event1v1BattleSlapPreviewPopupPanel.cs
2026-06-29 21:18:33 +08:00

185 lines
5.3 KiB
C#

using System;
using System.Threading.Tasks;
using Activity;
using asap.core;
using Event1v1Battle.Data;
using GameCore;
using TMPro;
using TimeManager;
using UniRx;
using UnityEngine;
using UnityEngine.UI;
namespace Event1v1Battle.Panel
{
/// <summary>
/// 1v1扇耳光大赛结算弹窗面板 Event1v1BattleSlapPreviewPopupPanel
/// </summary>
public class Event1v1BattleSlapPreviewPopupPanel : BasePanel
{
#region UI Elements
private Button _btnFight; // 重试按钮
private Button _btnRest; // 关闭按钮
private TMP_Text _textRound; // 当前轮次
private Image[] _lifeImages; // 剩余命数
private TMP_Text _textEventTime; // 活动剩余时间
private RewardItemNew[] _rewards; // 关卡奖励
#endregion
#region Manager
private Event1v1BattleSlapManager _manager;
private Event1v1BattleSlapManager Manager => _manager ??= ActivityResolver.Resolve<Event1v1BattleSlapManager>();
private Event1v1BattleSlapData Data => Manager?.LoadData();
#endregion
private Animation _uiAnimation;
private void Awake()
{
// Buttons
_btnFight = gameObject.FindChildGameObject("btn_fight")?.GetComponentInChildren<Button>();
if (_btnFight != null)
_btnFight.onClick.AddListener(OnClickFight);
_btnRest=gameObject.FindChildGameObject("btn_rest")?.GetComponentInChildren<Button>();
if (_btnRest != null)
_btnRest.onClick.AddListener(OnClickRest);
// Text elements
_textRound = gameObject.FindChildGameObject("p_text_rond")?.GetComponent<TMP_Text>();
_lifeImages = gameObject.FindChildGameObject("p_lives_icon")?.GetComponentsInChildren<Image>(true);
_textEventTime = gameObject.FindChildGameObject("text_time")?.GetComponent<TMP_Text>();
// Rewards
_rewards = gameObject.GetComponentsInChildren<RewardItemNew>(true);
foreach (var reward in _rewards)
{
reward?.gameObject.SetActive(false);
}
// Animation
_uiAnimation = GetComponent<Animation>();
// Start timer
GContext.container.Resolve<ITimeTickService>()?.SecondTick?.Subscribe(UpdateTimer).AddTo(disposables);
}
protected override void OnDestroy()
{
base.OnDestroy();
if (_btnFight != null)
_btnFight.onClick.RemoveAllListeners();
if (_btnRest != null)
_btnRest.onClick.RemoveAllListeners();
}
protected override void Start()
{
// 播放入场动画
if (_uiAnimation != null)
_uiAnimation.Play("popup_common_show");
RefreshUI();
}
#region UI Refresh
private void RefreshUI()
{
if (Data == null) return;
UpdateTimer(0);
UpdateRoundInfo();
UpdateRemainingLives();
UpdateRewards();
}
private void UpdateTimer(long _ = 0)
{
if (_textEventTime == null || Data == null) return;
_textEventTime.text = ConvertTools.ConvertTime2(Data.RemainingTime);
}
private void UpdateRoundInfo()
{
if (!_textRound|| Data == null) return;
_textRound.text = LocalizationMgr.GetFormatTextValue("UI_Event1v1BattleSlapPanel_5",Data.StageHistory?.Count+1 ?? 1,
Data.GetAllStageCount());
}
private void UpdateRemainingLives()
{
if (_lifeImages == null || Data == null) return;
int count = Mathf.Clamp(Data.PlayerLifeCount, 0, _lifeImages.Length);
for (int i = 0; i < _lifeImages.Length; i++)
{
_lifeImages[i].gameObject.SetActive(i >= _lifeImages.Length - count);
}
}
private void UpdateRewards()
{
var rewards = Data.GetCurrentStageRewards();
if (rewards == null) return;
_rewards?.SetData(rewards);
}
#endregion
#region Button Actions
private void OnClickFight()
{
Manager.StartBattle(Data);
ClosePanelThenShowLoading();
}
private async void OnClickRest()
{
try
{
await ClosePanel();
UIManager.Instance.DestroyUI(UITypes.Event1v1BattleSlapPanel);
}
catch (Exception e)
{
Debug.LogError(e);
}
}
#endregion
#region Panel Navigation
private async Task ClosePanel()
{
if (_uiAnimation != null)
_uiAnimation.Play("popup_common_out");
await Awaiters.Seconds(0.25f);
UIManager.Instance.DestroyUI(UITypes.Event1v1BattleSlapPreviewPopupPanel);
}
private async void ClosePanelThenShowLoading()
{
try
{
await ClosePanel();
await Manager.ShowSlapMatchPanelAndRandom(Data);
}
catch (Exception e)
{
Debug.LogError(e);
}
}
#endregion
}
}