272 lines
10 KiB
C#
Raw Normal View History

2025-04-18 19:18:15 +08:00
using UnityEngine;
using UnityEngine.UI;
using TEngine;
using System.Collections.Generic;
using GameConfig.giftConfig;
using System.Threading.Tasks;
using Cysharp.Threading.Tasks;
2025-04-24 16:50:21 +08:00
using TMPro;
2025-05-07 15:16:19 +08:00
using Coffee.UIExtensions;
2025-04-18 19:18:15 +08:00
namespace GameLogic
{
2025-04-24 16:50:21 +08:00
[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;
2025-05-08 23:23:31 +08:00
private RectTransform m_rect_GiftPanel;
2025-04-24 16:50:21 +08:00
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;
2025-05-08 23:23:31 +08:00
m_rect_GiftPanel = FindChildComponent<RectTransform>("Bg/m_rect_GiftPanel");
2025-04-24 16:50:21 +08:00
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();
2025-04-24 16:50:21 +08:00
//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);
2025-04-25 14:37:15 +08:00
DataGameSceneManager.Instance.m_DicTeamList.Add((i + 1).ToString(), actorHItem);
DataGameSceneManager.Instance.m_TeamSortList.Add(actorHItem);
}
2025-04-24 16:50:21 +08:00
}
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);
}
2025-04-28 15:07:13 +08:00
DataGameSceneManager.Instance.m_TeamSortList.Sort((a, b) =>
{
if (a.GetAllPower() > b.GetAllPower())
{
return -1;
}
else
{
return 1;
}
});
2025-05-04 21:26:33 +08:00
2025-04-24 16:50:21 +08:00
}
protected override void RegisterEvent()
{
base.RegisterEvent();
GameEvent.AddEventListener(EventConts.RestGameBattle, RestGameBattleFunc);
2025-05-07 15:16:19 +08:00
GameEvent.AddEventListener<GiftConfig>(EventConts.TtqEffectCreate, TtqEffectCreateFunc);
2025-04-24 16:50:21 +08:00
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);
2025-05-07 15:16:19 +08:00
GameEvent.RemoveEventListener<GiftConfig>(EventConts.TtqEffectCreate, TtqEffectCreateFunc);
2025-04-24 16:50:21 +08:00
GameEvent.RemoveEventListener<UnitPlayerData>(EventConts.AddPlayer, AddPlayerFunc);
GameEvent.RemoveEventListener<UnitPlayerData, GiftConfig, int>(EventConts.AddGiftMessage, AddGiftMessageFunc);
}
private void AddPlayerFunc(UnitPlayerData unitPlayerData)
{
2025-05-09 00:35:25 +08:00
// 显示加入UI
var jiaRuItem = GameObject.Instantiate(m_rect_GiftPanel.transform.GetChild(1));
jiaRuItem.transform.SetParent(m_rect_GiftPanel);
jiaRuItem.transform.localScale = Vector3.one * 1.5f;
jiaRuItem.transform.localPosition = new Vector3(-258, 5, 0);
jiaRuItem.gameObject.SetActive(true);
jiaRuItem.GetComponent<UIJiaRuItem>().OnInit(unitPlayerData);
2025-04-24 16:50:21 +08:00
}
2025-05-07 15:16:19 +08:00
/// <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);
}
2025-04-24 16:50:21 +08:00
private async void AddGiftMessageFunc(UnitPlayerData unitPlayerData, GiftConfig giftConfig, int num)
{
2025-05-08 23:23:31 +08:00
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);
2025-04-24 16:50:21 +08:00
//// 生成飘屏
//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
2025-05-07 20:54:39 +08:00
List<(ActorHItem actor, double score)> hItemActors = new List<(ActorHItem, double)>();
2025-04-24 16:50:21 +08:00
// Populate the list with HItemActors and their scores
for (int i = 0; i < m_rectHPanel.childCount; i++)
{
if (i < DataGameSceneManager.Instance.m_TeamSortList.Count)
{
2025-04-28 15:07:13 +08:00
var hItemActor = DataGameSceneManager.Instance.m_DicTeamList[(i + 1).ToString()];
2025-05-07 20:54:39 +08:00
double score = hItemActor.GetAllPower(); // Assume GetScore() returns the score of the actor
hItemActors.Add((hItemActor, score));
}
}
2025-04-24 16:50:21 +08:00
// 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;
}
2025-04-24 16:50:21 +08:00
}
}
2025-04-18 19:18:15 +08:00
}