422 lines
14 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;
namespace GameLogic
{
[Window(UILayer.UI)]
class UIGameBattle : UIWindow
{
#region
private RectTransform m_rect_TopPanel;
private Image m_img_topTitle;
private Text m_text_ScoreValue;
private Text m_text_WinValue;
private Text m_text_TopTitle;
private Text m_text_TimerValue;
private Button m_btn_Setting;
private Button m_btn_Rank;
private Button m_btn_Back;
private Image m_img_TopHeadBg;
private Image m_img_TopHeadIcon;
private Text m_text_topName;
private RectTransform m_rect_GiftList;
private Image m_img_GiftImg;
private RectTransform m_rect_List;
private Button m_btn_GameStart;
private RectTransform m_rect_JiaRuPanel;
private RectTransform m_rect_TopTips;
private RectTransform m_rect_GiftPanel;
protected override void ScriptGenerator()
{
m_rect_TopPanel = FindChildComponent<RectTransform>("m_rect_TopPanel");
m_img_topTitle = FindChildComponent<Image>("m_rect_TopPanel/m_img_topTitle");
m_text_ScoreValue = FindChildComponent<Text>("m_rect_TopPanel/m_text_ScoreValue");
m_text_WinValue = FindChildComponent<Text>("m_rect_TopPanel/m_text_WinValue");
m_text_TopTitle = FindChildComponent<Text>("m_rect_TopPanel/m_text_TopTitle");
m_text_TimerValue = FindChildComponent<Text>("m_rect_TopPanel/m_text_TimerValue");
m_btn_Setting = FindChildComponent<Button>("m_rect_TopPanel/m_btn_Setting");
m_btn_Rank = FindChildComponent<Button>("m_rect_TopPanel/m_btn_Rank");
m_btn_Back = FindChildComponent<Button>("m_rect_TopPanel/m_btn_Back");
m_img_TopHeadBg = FindChildComponent<Image>("TopBg/m_img_TopHeadBg");
m_img_TopHeadIcon = FindChildComponent<Image>("TopBg/Mask/m_img_TopHeadIcon");
m_text_topName = FindChildComponent<Text>("TopBg/topBgImg/m_text_topName");
m_rect_GiftList = FindChildComponent<RectTransform>("m_rect_GiftList");
m_img_GiftImg = FindChildComponent<Image>("m_rect_GiftList/m_img_GiftImg");
m_rect_List = FindChildComponent<RectTransform>("Image (1)/Image (2)/m_rect_List");
m_btn_GameStart = FindChildComponent<Button>("m_btn_GameStart");
m_rect_JiaRuPanel = FindChildComponent<RectTransform>("SubPanel/m_rect_JiaRuPanel");
m_rect_TopTips = FindChildComponent<RectTransform>("SubPanel/m_rect_TopTips");
m_rect_GiftPanel = FindChildComponent<RectTransform>("SubPanel/m_rect_GiftPanel");
m_btn_Setting.onClick.AddListener(OnClick_SettingBtn);
m_btn_Rank.onClick.AddListener(OnClick_RankBtn);
m_btn_Back.onClick.AddListener(OnClick_BackBtn);
m_btn_GameStart.onClick.AddListener(OnClick_GameStartBtn);
}
#endregion
#region
private void OnClick_BackBtn()
{
if (DataGameSceneManager.Instance.GetGameState())
{
UITip.ShowMessageBox("中途退出游戏,游戏内数据将会丢失!是否继续?", MessageShowType.TwoButton, () =>
{
GameSceneProcedure onEnterGameAppProcedure = GameModule.Procedure.CurrentProcedure as GameSceneProcedure;
onEnterGameAppProcedure.GotoMain();
GameModule.UI.CloseUI<UIGameBattle>();
GameModule.UI.CloseUI<UIGameRankForm>();
}, () =>
{
});
}
else
{
GameSceneProcedure onEnterGameAppProcedure = GameModule.Procedure.CurrentProcedure as GameSceneProcedure;
onEnterGameAppProcedure.GotoMain();
GameModule.UI.CloseUI<UIGameBattle>();
GameModule.UI.CloseUI<UIGameRankForm>();
}
}
private void OnClick_SettingBtn()
{
GameModule.UI.ShowUI<UISettingForm>();
}
private async void OnClick_RankBtn()
{
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);
}
}
private void OnClick_GameStartBtn()
{
DataGameSceneManager.Instance.SetGameState(true);
m_btn_GameStart.gameObject.SetActive(false);
}
#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();
// if (!DataGameSceneManager.Instance.GetGameState())
// {
// return;
// }
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()
{
// m_btn_GameStart.gameObject.SetActive(true);
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>
/// 更新最强玩家or阵营信息
/// </summary>
private void UpdateTopActorTest(Actor actor)
{
if (actor == null || DataGameSceneManager.Instance.GetTeamActors().Count <= 0)
{
m_img_TopHeadIcon.sprite = GameModule.Resource.LoadAsset<Sprite>("1222");
m_img_TopHeadIcon.transform.parent.parent.gameObject.SetActive(false);
m_text_topName.text = "暂无";
return;
}
if (EventConts.gameStateType == GameStateType.)
{
// var actor = DataGameSceneManager.Instance.GetTopActor();
if (m_img_TopHeadIcon.sprite != actor.m_HeadImg.sprite)
{
m_img_TopHeadIcon.sprite = actor.m_HeadImg.sprite;
m_img_TopHeadIcon.transform.parent.parent.gameObject.SetActive(true);
}
m_text_topName.text = actor.m_NameMesh.text;
}
else if (EventConts.gameStateType == GameStateType.)
{
// var data = DataGameSceneManager.Instance.GetOneRankUnitPlayerData();
// if (m_img_TopHeadIcon.sprite.name != AsyncImageDownload.GetBigMd5(data.protCustMessageData.imgURL))
// {
// AsyncImageDownload.Instance.SetAsyncImage(data.protCustMessageData.imgURL, m_img_TopHeadIcon);
// }
// m_text_topName.text = data.protCustMessageData.nickName;
// 也应该是最高阵营才对,看要哪种
// var actor = DataGameSceneManager.Instance.GetTopActor();
if (m_img_TopHeadIcon.sprite != actor.m_HeadImg.sprite)
{
m_img_TopHeadIcon.transform.parent.parent.gameObject.SetActive(true);
m_img_TopHeadIcon.sprite = actor.m_HeadImg.sprite;
}
m_text_topName.text = actor.m_NameMesh.text;
}
}
/// <summary>
/// 更新游戏时间
/// </summary>
private void UpdateGameTiemr()
{
// 计算分钟和剩余秒数
int minutes = (int)DataGameSceneManager.Instance.m_GameTimerLen / 60;
int remainingSeconds = (int)DataGameSceneManager.Instance.m_GameTimerLen % 60;
// 返回格式化的字符串
m_text_TimerValue.text = $"{minutes}:{remainingSeconds:D2}";
}
/// <summary>
/// 更新游戏公用数据
/// </summary>
private void UpdateGameScoreValue()
{
// 积分池更新
m_text_ScoreValue.text = UITool.ConvertToString(DataGameSceneManager.Instance.ScoreAllValue);
// 胜点池
m_text_WinValue.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);
}
}
}