Files
5.24BuYu/Assets/MyGame/aa/UI/Home/Sub/UIHome_BY_MySelectRoomPanel.cs
T
2026-07-07 03:20:30 +08:00

202 lines
6.8 KiB
C#

using Assets.MyGame.Scripts.Net;
using Assets.MyGame.Scripts.Net.API.GameConfig_SubClass;
using Assets.MyGame.Scripts.Net.WebResponses;
using Newtonsoft.Json;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
public class UIHome_BY_MySelectRoomPanel : SubUIBase
{
public GameObject panel_PanelParent;
public Button panel_RoomItemPrefab;
public Button btn_NextPage;
public Button btn_UpPage;
public TextMeshProUGUI text_PageNum;
public ScrollRect scroll;
public List<Transform> itemPage = new List<Transform>();
public GameObject itemPagePrefab;
public Transform itemPageParent;
public Transform tra_DaQuParent;
public Transform tra_FenQuItem; // child1=场次名, child2=分区n
public Dictionary<string, Transform> itemListDic = new Dictionary<string, Transform>();
int pageNum = 1;
float horizontalNormalizedPosition;
private int _lastLeave = -1;
public override void Awake()
{
btn_NextPage.onClick.AddListener(() =>
{
if (pageNum > 1) pageNum--;
text_PageNum.text = pageNum + "/" + itemPage.Count;
horizontalNormalizedPosition = (float)(pageNum - 1) / (float)(itemPage.Count - 1);
});
btn_UpPage.onClick.AddListener(() =>
{
if (pageNum < itemPage.Count) pageNum++;
text_PageNum.text = pageNum + "/" + itemPage.Count;
horizontalNormalizedPosition = (float)(pageNum - 1) / (float)(itemPage.Count - 1);
});
btn_Close.onClick.AddListener(() => Hide());
}
private void Update()
{
scroll.horizontalNormalizedPosition = Mathf.Lerp(scroll.horizontalNormalizedPosition, horizontalNormalizedPosition, 0.5f);
}
public override void Hide()
{
base.Hide();
panel_PanelParent.SetActive(true);
}
protected override void OnShow(object args = null)
{
base.OnShow(args);
int leave = GetCurrentLeave();
if (_lastLeave != leave)
{
// 场次变了 → 重建分区 Tab
for (int i = tra_DaQuParent.childCount - 1; i >= 0; i--)
Destroy(tra_DaQuParent.GetChild(i).gameObject);
BuildFenQuTabs();
_lastLeave = leave;
}
}
public override void Show(object args = null)
{
base.Show(args);
ClearRoomPages();
if (args != null)
{
var v = JsonConvert.DeserializeObject<WebSocketResponseBase<List<GetRoomList>>>(args.ToString());
BuildRoomPages(v.arguments[0]);
}
}
// ==================== 分区 Tab ====================
private void BuildFenQuTabs()
{
if (tra_DaQuParent == null || tra_FenQuItem == null) return;
var cfg = GameManager.Instance?.gameConfig?.roomConfigs;
if (cfg == null) return;
int gameType = (int)GameManager.Instance.gameData.currGameType;
int leave = GetCurrentLeave();
int partitionCount = 1;
foreach (var c in cfg)
{
if (c.gameType == gameType && c.roomLevel == leave)
{
partitionCount = c.roomPartitionNum > 0 ? c.roomPartitionNum : 1;
break;
}
}
for (int d = 1; d <= partitionCount; d++)
{
var tab = Instantiate(tra_FenQuItem, tra_DaQuParent);
if (tab.childCount > 1 && tab.GetChild(1) != null)
tab.GetChild(1).GetComponent<TextMeshProUGUI>().text = LevelNameFor(leave);
if (tab.childCount > 2 && tab.GetChild(2) != null)
tab.GetChild(2).GetComponent<TextMeshProUGUI>().text = "分区" + d;
int district = d;
var toggle = tab.GetComponent<Toggle>();
if (toggle != null)
{
toggle.onValueChanged.AddListener((isOn) =>
{
if (!isOn) return;
for (int i = 0; i < tra_DaQuParent.childCount; i++)
{
var t = tra_DaQuParent.GetChild(i).GetComponent<Toggle>();
if (t != null && t != toggle)
t.SetIsOnWithoutNotify(false);
}
ClearRoomPages();
WebSocketSendList.GetRoomListFish(leave, district);
});
}
}
}
private int GetCurrentLeave()
{
var selectPanel = UIManager.Instance.Get<UIHomeMain>(UIPanel.HomeMainPanel)
?.subUIManager.Get<UIHome_BY_SelectRoomLevelPanel>(UIPanel.SelectRoomLeavePenel);
return selectPanel != null ? selectPanel.leave : 1;
}
private static string LevelNameFor(int leave)
{
return leave switch { 1 => "低级场", 2 => "中级场", 3 => "高级场", _ => "未知场" };
}
// ==================== 房间列表 ====================
private void ClearRoomPages()
{
foreach (var item in itemPage)
Destroy(item.gameObject);
itemPage.Clear();
itemListDic.Clear();
text_PageNum.text = "0/0";
pageNum = 1;
}
private void BuildRoomPages(List<GetRoomList> rooms)
{
if (rooms == null || rooms.Count == 0) return;
int pageMax = Mathf.CeilToInt(rooms.Count / 15f);
for (int j = 0; j < pageMax; j++)
itemPage.Add(Instantiate(itemPagePrefab, itemPageParent).transform);
int i = 0;
foreach (var item in rooms)
{
var tran = Instantiate(panel_RoomItemPrefab.gameObject, itemPage[i / 15]).transform;
tran.GetComponent<Button>().onClick.AddListener(() =>
{
WebSocketSendList.GetRoomInfo(item.roomId);
});
tran.name = item.roomId;
itemListDic[item.roomId] = tran;
var slider = tran.transform.GetChild(0).GetComponent<Slider>();
int allPlayerCount = item.currentOnlinePlayerCount + item.robotCount;
if (slider != null)
{
slider.value = (float)allPlayerCount / (float)item.maxPlayerCount;
if (allPlayerCount >= item.maxPlayerCount && slider.fillRect != null && slider.fillRect.childCount > 0)
slider.fillRect.GetChild(0).gameObject.SetActive(true);
}
if (tran.transform.GetChild(0).childCount > 3 && tran.transform.GetChild(0).GetChild(3) != null)
tran.transform.GetChild(0).GetChild(3).GetComponent<TextMeshProUGUI>().text = allPlayerCount + "/" + item.maxPlayerCount;
if (tran.transform.childCount > 1 && tran.transform.GetChild(1) != null)
tran.transform.GetChild(1).GetComponent<TextMeshProUGUI>().text = item.roomNumber.ToString();
i++;
}
text_PageNum.text = "1/" + itemPage.Count;
scroll.horizontalNormalizedPosition = 0;
pageNum = 1;
}
}