324 lines
12 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-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;
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_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);
//// 设置顶部条
////m_img_topTitle.sprite = EventConts.gameStateType == GameStateType.个人赛 ? GameModule.Resource.LoadAsset<Sprite>("Z_jfc_box") :
//// GameModule.Resource.LoadAsset<Sprite>("Z_jfc_box_2");
//m_text_TopTitle.text = EventConts.gameStateType == GameStateType.个人赛 ? "发送加入即可参与" : "发送加入+数字或者加入+姓氏即可参与";
//// 设置礼物UI(不同平台)
//m_img_GiftImg.sprite = GameModule.Resource.LoadAsset<Sprite>("Z_LW_gift_" + (int)EventConts.PlatformType);
//// 刷新
//UpdateGameTiemr();
//UpdateGameScoreValue();
//RestHItemFunc();
// 创建横板列表
}
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);
}
}
protected override void RegisterEvent()
{
base.RegisterEvent();
GameEvent.AddEventListener(EventConts.RestGameBattle, RestGameBattleFunc);
GameEvent.AddEventListener<long>(EventConts.UpdateHItem, UpdateHItemFunc);
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<long>(EventConts.UpdateHItem, UpdateHItemFunc);
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);
}
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);
//}
}
private void UpdateHItemFunc(long allValue)
{
//for (int i = 0; i < m_rect_List.childCount; i++)
//{
// if (m_rect_List.GetChild(i) != null)
// {
// m_rect_List.GetChild(i).GetComponent<HItemActor>().OnUpdate(allValue);
// }
//}
}
/// <summary>
/// 收到游戏重开的消息
/// </summary>
private void RestGameBattleFunc()
{
//targetPositions.Clear();
//// 清理横版
//for (int i = 0; i < m_rect_List.childCount; i++)
//{
// m_rect_List.GetChild(i).GetComponent<HItemActor>().OnRemove();
//}
//RestHItemFunc();
//UpdateGameTiemr();
//UpdateGameScoreValue();
//DataGameSceneManager.Instance.SetGameState(true);
}
/// <summary>
/// 重置横版列表
/// </summary>
private void RestHItemFunc()
{
//if (EventConts.gameStateType == GameStateType.团队赛)
//{
// // 移除旧的
// for (int i = 0; i < m_rect_List.childCount; i++)
// {
// m_rect_List.GetChild(i).GetComponent<HItemActor>().OnRemove();
// }
// // 生成新的
// for (int i = 0; i < EventConts.MaxPlayerIds; i++)
// {
// CreateHItem(i.ToString());
// }
//}
//else if (EventConts.gameStateType == GameStateType.个人赛)
//{
//}
}
/// <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<(HItemActor actor, long score)> hItemActors = new List<(HItemActor, long)>();
//// Populate the list with HItemActors and their scores
//for (int i = 0; i < m_rect_List.childCount; i++)
//{
// var child = m_rect_List.GetChild(i);
// if (child != null)
// {
// var hItemActor = child.GetComponent<HItemActor>();
// long 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.GetComponent<RectTransform>();
// Vector2 targetPosition = new Vector2(0, 151 - i * 50.5945f);
// targetPositions[rectTransform] = targetPosition;
//}
//if (hItemActors.Count > 0)
//{
// UpdateTopActorTest(hItemActors[0].actor.m_Actor);
//}
//else
//{
// UpdateTopActorTest(null);
//}
}
private void CreateHItem(string oid)
{
//int i = m_rect_List.childCount;
//var go = GameModule.Resource.LoadAsset<GameObject>("H_Item");
//var item = GameObject.Instantiate(go);
//item.transform.SetParent(m_rect_List);
//item.transform.localScale = Vector3.one;
//var r = item.GetComponent<RectTransform>();
//// 计算位置
//r.anchorMin = new Vector2(0.5f, 0.5f);
//r.anchorMax = new Vector2(0.5f, 0.5f);
//r.anchoredPosition = new Vector2(0, 151 - i * 50.5945f);
//r.localPosition = new Vector3(r.localPosition.x, r.localPosition.y, 0);
//HItemActor hItemActor = item.GetComponent<HItemActor>();
//hItemActor.OnInit(oid);
}
}
2025-04-18 19:18:15 +08:00
}