220 lines
7.7 KiB
C#
220 lines
7.7 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();
|
||
if (panel_PanelParent != null) 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()
|
||
{
|
||
// 场次由大厅主页手动选座时传入(UIHomeMain.currLeave)
|
||
var home = UIManager.Instance.Get<UIHomeMain>(UIPanel.HomeMainPanel);
|
||
return home != null ? home.currLeave : 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;
|
||
|
||
UpdateRoomItemUI(tran, item.currentOnlinePlayerCount, item.robotCount, item.maxPlayerCount, item.roomNumber);
|
||
i++;
|
||
}
|
||
|
||
text_PageNum.text = "1/" + itemPage.Count;
|
||
scroll.horizontalNormalizedPosition = 0;
|
||
pageNum = 1;
|
||
}
|
||
|
||
/// <summary>
|
||
/// FishRoomHub RoomChange 通知:更新单个房间项的人数显示
|
||
/// </summary>
|
||
public void OnRoomChange(RoomChangeResponse room)
|
||
{
|
||
if (room == null || string.IsNullOrEmpty(room.roomId)) return;
|
||
if (!itemListDic.TryGetValue(room.roomId, out var tran) || tran == null) return;
|
||
UpdateRoomItemUI(tran, room.currentOnlinePlayerCount, room.robotCount, room.maxPlayerCount, room.roomNumber);
|
||
}
|
||
|
||
/// <summary>更新单个房间项的 UI(slider + 人数文本 + 房间号)</summary>
|
||
private void UpdateRoomItemUI(Transform tran, int currentOnline, int robot, int maxPlayer, int roomNumber)
|
||
{
|
||
var slider = tran.transform.GetChild(0).GetComponent<Slider>();
|
||
int allPlayerCount = currentOnline + robot;
|
||
if (slider != null)
|
||
{
|
||
slider.value = (float)allPlayerCount / (float)maxPlayer;
|
||
if (allPlayerCount >= maxPlayer && slider.fillRect != null && slider.fillRect.childCount > 0)
|
||
slider.fillRect.GetChild(0).gameObject.SetActive(true);
|
||
else if (allPlayerCount < maxPlayer && slider.fillRect != null && slider.fillRect.childCount > 0)
|
||
slider.fillRect.GetChild(0).gameObject.SetActive(false);
|
||
}
|
||
|
||
if (tran.transform.GetChild(0).childCount > 3 && tran.transform.GetChild(0).GetChild(3) != null)
|
||
tran.transform.GetChild(0).GetChild(3).GetComponent<TextMeshProUGUI>().text = allPlayerCount + "/" + maxPlayer;
|
||
if (tran.transform.childCount > 1 && tran.transform.GetChild(1) != null)
|
||
tran.transform.GetChild(1).GetComponent<TextMeshProUGUI>().text = roomNumber.ToString();
|
||
}
|
||
}
|