283 lines
11 KiB
C#
283 lines
11 KiB
C#
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using TEngine;
|
|
using System.Collections.Generic;
|
|
using GameConfig.giftConfig;
|
|
using System.Threading.Tasks;
|
|
using Cysharp.Threading.Tasks;
|
|
using TMPro;
|
|
using Coffee.UIExtensions;
|
|
|
|
namespace GameLogic
|
|
{
|
|
[Window(UILayer.UI)]
|
|
class UIGameBattle : UIWindow
|
|
{
|
|
#region 脚本工具生成的代码
|
|
private TextMeshProUGUI m_tmpTimer;
|
|
private TextMeshProUGUI m_tmpWinPoint;
|
|
private TextMeshProUGUI m_tmpScoreAll;
|
|
private Button m_btnSetting;
|
|
private Button m_btnRank;
|
|
private RectTransform m_rectHPanel;
|
|
private GameObject m_itemActorHItem;
|
|
private RectTransform m_rect_GiftPanel;
|
|
protected override void ScriptGenerator()
|
|
{
|
|
m_tmpTimer = FindChildComponent<TextMeshProUGUI>("Bg/Top/TopImg/m_tmpTimer");
|
|
m_tmpWinPoint = FindChildComponent<TextMeshProUGUI>("Bg/Top/TopImg/m_tmpWinPoint");
|
|
m_tmpScoreAll = FindChildComponent<TextMeshProUGUI>("Bg/Top/TopImg/m_tmpScoreAll");
|
|
m_btnSetting = FindChildComponent<Button>("Bg/Top/m_btnSetting");
|
|
m_btnRank = FindChildComponent<Button>("Bg/Top/m_btnRank");
|
|
m_rectHPanel = FindChildComponent<RectTransform>("Bg/Main/m_rectHPanel");
|
|
m_itemActorHItem = FindChild("Bg/Main/m_rectHPanel/m_itemActorHItem").gameObject;
|
|
m_rect_GiftPanel = FindChildComponent<RectTransform>("Bg/m_rect_GiftPanel");
|
|
m_btnSetting.onClick.AddListener(OnClickSettingBtn);
|
|
m_btnRank.onClick.AddListener(OnClickRankBtn);
|
|
}
|
|
#endregion
|
|
|
|
#region 事件
|
|
private void OnClickSettingBtn()
|
|
{
|
|
GameModule.UI.ShowUI<UISettingForm>();
|
|
}
|
|
private async void OnClickRankBtn()
|
|
{
|
|
List<PlayRankData> playRankDatas = await DataGameModelManager.Instance.C2S_GetResultRank(1);
|
|
if (playRankDatas != null)
|
|
{
|
|
// 整理参数传进来
|
|
object[] objects = new object[2];
|
|
objects[0] = GameRankType.本周排行榜;
|
|
objects[1] = playRankDatas;
|
|
GameModule.UI.ShowUI<UIGameRankForm>(objects);
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
private Dictionary<RectTransform, Vector2> targetPositions = new Dictionary<RectTransform, Vector2>();
|
|
private float animationSpeed = 150f; // Adjust this value to control the speed of the animation
|
|
protected override void OnCreate()
|
|
{
|
|
base.OnCreate();
|
|
|
|
DataGameSceneManager.Instance.SetGameState(true);
|
|
|
|
// 刷新
|
|
UpdateGameTiemr();
|
|
UpdateGameScoreValue();
|
|
//RestHItemFunc();
|
|
|
|
// 创建横板列表
|
|
for (int i = 0; i < DataGameSceneManager.Instance.m_Strs.Count; i++)
|
|
{
|
|
ActorHItem actorHItem = CreateWidgetByPrefab<ActorHItem>(m_itemActorHItem, m_rectHPanel);
|
|
var r = actorHItem.gameObject.GetComponent<RectTransform>();
|
|
// 计算位置
|
|
r.anchorMin = new Vector2(0.5f, 0.5f);
|
|
r.anchorMax = new Vector2(0.5f, 0.5f);
|
|
r.anchoredPosition = new Vector2(0, 478 - i * (109 - 5f));
|
|
r.localPosition = new Vector3(r.localPosition.x, r.localPosition.y, 0);
|
|
actorHItem.OnInit(i);
|
|
DataGameSceneManager.Instance.m_DicTeamList.Add((i + 1).ToString(), actorHItem);
|
|
DataGameSceneManager.Instance.m_TeamSortList.Add(actorHItem);
|
|
}
|
|
}
|
|
|
|
protected override void OnUpdate()
|
|
{
|
|
base.OnUpdate();
|
|
|
|
|
|
UpdateGameTiemr();
|
|
UpdateGameScoreValue();
|
|
UpdateHItem();
|
|
|
|
// Move each HItemActor towards its target position
|
|
foreach (var kvp in targetPositions)
|
|
{
|
|
if (kvp.Key == null)
|
|
{
|
|
continue;
|
|
}
|
|
var rectTransform = kvp.Key;
|
|
var targetPosition = kvp.Value;
|
|
float distance = Vector2.Distance(rectTransform.localPosition, targetPosition);
|
|
float speed = distance / 0.2f; // Calculate speed to ensure movement completes in 1 second
|
|
rectTransform.localPosition = Vector2.MoveTowards(rectTransform.localPosition, targetPosition, speed * Time.deltaTime);
|
|
}
|
|
|
|
DataGameSceneManager.Instance.m_TeamSortList.Sort((a, b) =>
|
|
{
|
|
if (a.GetAllPower() > b.GetAllPower())
|
|
{
|
|
return -1;
|
|
}
|
|
else
|
|
{
|
|
return 1;
|
|
}
|
|
});
|
|
|
|
|
|
}
|
|
|
|
protected override void RegisterEvent()
|
|
{
|
|
base.RegisterEvent();
|
|
GameEvent.AddEventListener(EventConts.RestGameBattle, RestGameBattleFunc);
|
|
GameEvent.AddEventListener<GiftConfig>(EventConts.TtqEffectCreate, TtqEffectCreateFunc);
|
|
GameEvent.AddEventListener<UnitPlayerData>(EventConts.AddPlayer, AddPlayerFunc);
|
|
GameEvent.AddEventListener<UnitPlayerData, GiftConfig, int>(EventConts.AddGiftMessage, AddGiftMessageFunc);
|
|
}
|
|
|
|
|
|
protected override void OnDestroy()
|
|
{
|
|
base.OnDestroy();
|
|
GameEvent.RemoveEventListener(EventConts.RestGameBattle, RestGameBattleFunc);
|
|
GameEvent.RemoveEventListener<GiftConfig>(EventConts.TtqEffectCreate, TtqEffectCreateFunc);
|
|
GameEvent.RemoveEventListener<UnitPlayerData>(EventConts.AddPlayer, AddPlayerFunc);
|
|
GameEvent.RemoveEventListener<UnitPlayerData, GiftConfig, int>(EventConts.AddGiftMessage, AddGiftMessageFunc);
|
|
}
|
|
|
|
private void AddPlayerFunc(UnitPlayerData unitPlayerData)
|
|
{
|
|
//if (EventConts.gameStateType == GameStateType.个人赛)
|
|
//{
|
|
// // 生成横版
|
|
// CreateHItem(unitPlayerData.protCustMessageData.openId);
|
|
//}
|
|
|
|
//// 显示加入UI
|
|
//var jiaRuItem = GameObject.Instantiate(m_rect_JiaRuPanel.transform.GetChild(0));
|
|
//jiaRuItem.transform.SetParent(m_rect_JiaRuPanel);
|
|
//jiaRuItem.transform.localScale = Vector3.one * 1.5f;
|
|
//jiaRuItem.transform.localPosition = new Vector3(-258, 5, 0);
|
|
//jiaRuItem.gameObject.SetActive(true);
|
|
//jiaRuItem.GetComponent<UIJiaRuItem>().OnInit(unitPlayerData);
|
|
|
|
|
|
//var topTipsTime = GameObject.Instantiate(m_rect_TopTips.transform.GetChild(0));
|
|
//topTipsTime.transform.SetParent(m_rect_TopTips);
|
|
//topTipsTime.transform.localScale = Vector3.one;
|
|
//topTipsTime.transform.localPosition = new Vector3(600, Random.Range(-70, 95));
|
|
//topTipsTime.gameObject.SetActive(true);
|
|
//topTipsTime.GetComponent<UITopTipsItem>().OnInit(unitPlayerData, null, 0);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 甜甜圈特效
|
|
/// </summary>
|
|
private void TtqEffectCreateFunc(GiftConfig giftConfig)
|
|
{
|
|
GameModule.Timer.AddTimer((e) =>
|
|
{
|
|
var m_Ego = GameModule.Resource.LoadGameObject("TTQ01");
|
|
m_Ego.transform.SetParent(this.transform);
|
|
m_Ego.transform.localPosition = Vector3.zero;
|
|
m_Ego.transform.localScale = Vector3.one;
|
|
m_Ego.transform.GetComponent<UIParticle>().scale = 1;
|
|
}, giftConfig.TimerLen);
|
|
}
|
|
|
|
private async void AddGiftMessageFunc(UnitPlayerData unitPlayerData, GiftConfig giftConfig, int num)
|
|
{
|
|
var giftItem = GameObject.Instantiate(m_rect_GiftPanel.transform.GetChild(0));
|
|
giftItem.transform.SetParent(m_rect_GiftPanel);
|
|
giftItem.transform.localScale = Vector3.one;
|
|
giftItem.transform.localPosition = Vector3.zero;
|
|
giftItem.gameObject.SetActive(true);
|
|
giftItem.GetComponent<UIGiftItem>().OnInit(unitPlayerData, giftConfig, num);
|
|
|
|
|
|
//// 生成飘屏
|
|
//for (int i = 0; i < 4; i++)
|
|
//{
|
|
// var topTipsTime = GameObject.Instantiate(m_rect_TopTips.transform.GetChild(0));
|
|
// topTipsTime.transform.SetParent(m_rect_TopTips);
|
|
// topTipsTime.transform.localScale = Vector3.one;
|
|
// topTipsTime.transform.localPosition = new Vector3(600, Random.Range(-70, 95));
|
|
// topTipsTime.gameObject.SetActive(true);
|
|
// topTipsTime.GetComponent<UITopTipsItem>().OnInit(unitPlayerData, giftConfig, num);
|
|
// await UniTask.Delay(500);
|
|
//}
|
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 收到游戏重开的消息
|
|
/// </summary>
|
|
private void RestGameBattleFunc()
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// 更新游戏时间
|
|
/// </summary>
|
|
private void UpdateGameTiemr()
|
|
{
|
|
|
|
// 计算分钟和剩余秒数
|
|
int minutes = (int)DataGameSceneManager.Instance.m_GameTimerLen / 60;
|
|
int remainingSeconds = (int)DataGameSceneManager.Instance.m_GameTimerLen % 60;
|
|
|
|
// 返回格式化的字符串
|
|
m_tmpTimer.text = $"{minutes}:{remainingSeconds:D2}";
|
|
}
|
|
|
|
/// <summary>
|
|
/// 更新游戏公用数据
|
|
/// </summary>
|
|
private void UpdateGameScoreValue()
|
|
{
|
|
// 积分池更新
|
|
m_tmpScoreAll.text = UITool.ConvertToString(DataGameSceneManager.Instance.ScoreAllValue);
|
|
|
|
// 胜点池
|
|
m_tmpWinPoint.text = UITool.ConvertToString(DataGameSceneManager.Instance.WinAllValue);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 更新横版排版移动
|
|
/// </summary>
|
|
private void UpdateHItem()
|
|
{
|
|
// Create a list to hold the HItemActors and their scores
|
|
List<(ActorHItem actor, double score)> hItemActors = new List<(ActorHItem, double)>();
|
|
|
|
// Populate the list with HItemActors and their scores
|
|
for (int i = 0; i < m_rectHPanel.childCount; i++)
|
|
{
|
|
if (i < DataGameSceneManager.Instance.m_TeamSortList.Count)
|
|
{
|
|
var hItemActor = DataGameSceneManager.Instance.m_DicTeamList[(i + 1).ToString()];
|
|
double score = hItemActor.GetAllPower(); // Assume GetScore() returns the score of the actor
|
|
hItemActors.Add((hItemActor, score));
|
|
}
|
|
}
|
|
|
|
// Sort the list based on scores in descending order
|
|
hItemActors.Sort((a, b) => b.score.CompareTo(a.score));
|
|
|
|
// Set the target positions for the HItemActors based on their new order
|
|
for (int i = 0; i < hItemActors.Count; i++)
|
|
{
|
|
var hItemActor = hItemActors[i].actor;
|
|
hItemActor.m_Index = i + 1;
|
|
var rectTransform = hItemActor.gameObject.GetComponent<RectTransform>();
|
|
Vector2 targetPosition = new Vector2(0, 478 - i * (109 - 5f));
|
|
targetPositions[rectTransform] = targetPosition;
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
}
|