178 lines
6.0 KiB
C#
178 lines
6.0 KiB
C#
using System.Threading.Tasks;
|
||
using Activity;
|
||
using asap.core;
|
||
using Event1v1Battle.Data;
|
||
using game;
|
||
using TMPro;
|
||
using UnityEngine;
|
||
using Random = UnityEngine.Random;
|
||
|
||
namespace Event1v1Battle.Panel.MainPanel
|
||
{
|
||
/// <summary>
|
||
/// 1v1扇耳光大赛匹配面板 Event1v1BattleSlapLoadingPanel
|
||
/// </summary>
|
||
public class Event1v1BattleSlapMatchPanel : BasePanel
|
||
{
|
||
#region UI Elements
|
||
|
||
private DuelMatchPlayer _myself;
|
||
private DuelMatchPlayer _enemy;
|
||
|
||
#endregion
|
||
|
||
#region 匹配动画配置
|
||
|
||
/// <summary>总动画时长(秒)</summary>
|
||
[SerializeField] private float _matchAnimationDuration = 2.0f;
|
||
|
||
/// <summary>NPC切换次数</summary>
|
||
[SerializeField] private int _matchSwitchCount = 20;
|
||
|
||
/// <summary>起始间隔时间(秒)- 快速</summary>
|
||
[SerializeField] private float _matchStartInterval = 0.05f;
|
||
|
||
/// <summary>结束间隔时间(秒)- 慢速</summary>
|
||
[SerializeField] private float _matchEndInterval = 0.3f;
|
||
|
||
/// <summary>结 // 展示时间(秒)</summary>
|
||
[SerializeField] private float showTime = 1f; // 展示时间(秒)
|
||
[SerializeField] private GameObject _matchFx;
|
||
#endregion
|
||
|
||
private Event1v1BattleSlapData _data;
|
||
public void SetData(Event1v1BattleSlapData data)
|
||
{
|
||
_data = data;
|
||
}
|
||
private void Awake()
|
||
{
|
||
_myself = gameObject.FindChildGameObject("myself").GetComponentInChildren<DuelMatchPlayer>();
|
||
_enemy = gameObject.FindChildGameObject("enermy").GetComponentInChildren<DuelMatchPlayer>();
|
||
_matchFx = gameObject.FindChildGameObject("fx_event1v1battle_slap_match_success");
|
||
_matchFx.SetActive(false);
|
||
}
|
||
|
||
protected override void Start()
|
||
{
|
||
base.Start();
|
||
ShowUI();
|
||
}
|
||
|
||
private void ShowUI()
|
||
{
|
||
IUserService userService = GContext.container.Resolve<IUserService>();
|
||
var uiService = GContext.container.Resolve<IUIService>();
|
||
uiService.SetHeadImage(_myself.icon_head, userService.AvatarUrl);
|
||
_myself.text_name.text = userService.DisplayName;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 播放匹配随机动画 - 快速切换NPC,由快变慢,最终停在目标NPC
|
||
/// </summary>
|
||
public async Task PlayMatchAnimationAsync()
|
||
{
|
||
// 获取所有NPC配置列表
|
||
var npcList =_data.Tables.TbRobot.DataList;
|
||
int totalNpcCount = npcList.Count;
|
||
|
||
if (totalNpcCount == 0 || _data == null)
|
||
{
|
||
Debug.LogError("[MatchAnimation] NPC列表为空或Data为null");
|
||
return;
|
||
}
|
||
|
||
// 目标NPC数据(最终要显示的)
|
||
string targetIcon = _data.NpcIcon;
|
||
string targetName = _data.NpcName;
|
||
|
||
// 执行随机切换
|
||
for (int i = 0; i < _matchSwitchCount; i++)
|
||
{
|
||
// 计算当前间隔(使用线性插值实现由快变慢)
|
||
float t = (float)i / _matchSwitchCount;
|
||
float currentInterval = Mathf.Lerp(_matchStartInterval, _matchEndInterval, t);
|
||
|
||
// 随机选择一个NPC显示
|
||
int randomIndex = Random.Range(0, totalNpcCount);
|
||
var randomNpc = npcList[randomIndex];
|
||
|
||
if (randomNpc != null)
|
||
{
|
||
// 更新UI显示随机NPC
|
||
var uiService = GContext.container.Resolve<IUIService>();
|
||
_=uiService.SetHeadImage(_enemy.icon_head, randomNpc.Avatar);
|
||
_enemy.text_name.text = randomNpc.Name;
|
||
}
|
||
|
||
// 异步等待当前间隔时间
|
||
await Awaiters.Seconds(currentInterval);
|
||
}
|
||
|
||
// 动画结束,显示目标NPC
|
||
_matchFx.SetActive(true);
|
||
var finalUiService = GContext.container.Resolve<IUIService>();
|
||
_=finalUiService.SetHeadImage(_enemy.icon_head, targetIcon);
|
||
_enemy.text_name.text = targetName;
|
||
|
||
await Awaiters.Seconds(showTime);
|
||
}
|
||
|
||
#if UNITY_EDITOR
|
||
|
||
/// <summary>
|
||
/// 编辑器调试方法 - 在Inspector中右键组件调用此方法测试动画
|
||
/// </summary>
|
||
[ContextMenu("调试匹配动画")]
|
||
private void DebugPlayMatchAnimation()
|
||
{
|
||
var _manager = ActivityResolver.Resolve<Event1v1BattleSlapManager>();
|
||
// 确保有管理器数据
|
||
if (_manager == null || _manager.LoadData() == null)
|
||
{
|
||
Debug.LogError("[MatchAnimation] 请先设置 _manager 和 Data");
|
||
return;
|
||
}
|
||
|
||
// 在编辑器中启动动画Task
|
||
_ = PlayMatchAnimationAsync();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 编辑器调试方法 - 随机设置一个新的目标NPC并播放动画
|
||
/// </summary>
|
||
[ContextMenu("随机NPC并播放动画")]
|
||
private void DebugRandomNpcAndPlay()
|
||
{
|
||
var _manager = ActivityResolver.Resolve<Event1v1BattleSlapManager>();
|
||
if (_manager == null)
|
||
{
|
||
Debug.LogError("[MatchAnimation] 请先设置 _manager");
|
||
return;
|
||
}
|
||
|
||
// 随机选择一个NPC作为目标
|
||
var npcList = _manager.Tables.TbRobot.DataList;
|
||
if (npcList.Count == 0)
|
||
{
|
||
Debug.LogError("[MatchAnimation] NPC列表为空");
|
||
return;
|
||
}
|
||
|
||
int randomIndex = Random.Range(0, npcList.Count);
|
||
var randomNpc = npcList[randomIndex];
|
||
|
||
// 更新Data中的目标NPC
|
||
_manager.LoadData().NpcIcon = randomNpc.Avatar;
|
||
_manager.LoadData().NpcName = randomNpc.Name;
|
||
|
||
Debug.Log($"[MatchAnimation] 设置目标NPC: {randomNpc.Name}");
|
||
|
||
// 播放动画
|
||
_ = PlayMatchAnimationAsync();
|
||
}
|
||
|
||
#endif
|
||
}
|
||
}
|