216 lines
8.2 KiB
C#
216 lines
8.2 KiB
C#
using Assets.MyGame.Scripts.Net;
|
||
using Assets.MyGame.Scripts.Net.WebResponses;
|
||
using Newtonsoft.Json;
|
||
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using TMPro;
|
||
using UnityEngine;
|
||
using UnityEngine.UI;
|
||
using static Assets.MyGame.Scripts.Net.WebResponses.ChineseChess.ChineseChessGameClassList;
|
||
|
||
public class UIChineseChessDistrict : SubUIBase
|
||
{
|
||
public Button panel_RoomItemPrefab;
|
||
public Transform panel_RoomItemParent;
|
||
public ScrollRect scroll;
|
||
public TextMeshProUGUI tmp_CurrGold;
|
||
public Dictionary<string, Transform> itemListDic = new Dictionary<string, Transform>();
|
||
float horizontalNormalizedPosition;
|
||
|
||
public Transform tra_DaQuParent;
|
||
public Transform tra_FenQuItem; // child0=Toggle, child0.child0=TextMeshProUGUI(象棋n区)
|
||
public int maxDistrict = 10; // 最大分区数
|
||
|
||
private bool _tabsBuilt = false;
|
||
|
||
public override void Awake()
|
||
{
|
||
base.Awake();
|
||
}
|
||
|
||
private void Update()
|
||
{
|
||
scroll.horizontalNormalizedPosition = Mathf.Lerp(scroll.horizontalNormalizedPosition, horizontalNormalizedPosition, 0.5f);
|
||
}
|
||
|
||
public override void Hide()
|
||
{
|
||
base.Hide();
|
||
}
|
||
|
||
protected override void OnShow(object args = null)
|
||
{
|
||
base.OnShow(args);
|
||
tmp_CurrGold.text = GameManager.Instance.user.assets.gold.ToString();
|
||
|
||
if (!_tabsBuilt)
|
||
{
|
||
// 从后端 API 获取分区总数
|
||
APIRequestList.GetChessRoomPartitionCount(count =>
|
||
{
|
||
maxDistrict = count > 0 ? count : 1;
|
||
BuildChessFenQuTabs();
|
||
_tabsBuilt = true;
|
||
});
|
||
}
|
||
}
|
||
|
||
public override void Show(object args = null)
|
||
{
|
||
base.Show(args);
|
||
tmp_CurrGold.text = GameManager.Instance.user.assets.gold.ToString();
|
||
|
||
if (args != null)
|
||
{
|
||
// 4.4 全量覆盖:先清空旧列表,再用服务端返回的列表重建(防止多次刷新叠加/分区错乱)
|
||
for (int c = panel_RoomItemParent.childCount - 1; c >= 0; c--)
|
||
Destroy(panel_RoomItemParent.GetChild(c).gameObject);
|
||
itemListDic.Clear();
|
||
|
||
int i = 0;
|
||
var v = JsonConvert.DeserializeObject<WebSocketResponseBase<List<ChessGameRoom>>>(args.ToString());
|
||
var basePanel = (ChineseChessMainPanel)uIManager.parentUIBase;
|
||
foreach (var item in v.arguments[0])
|
||
{
|
||
var tran = Instantiate(panel_RoomItemPrefab.gameObject, panel_RoomItemParent).transform;
|
||
|
||
tran.GetComponent<Button>().onClick.AddListener(() =>
|
||
{
|
||
switch (ChineseChessMainPanel.dic_RoomData[item.roomId].state)
|
||
{
|
||
case 1: // 未开始
|
||
int temp = ChineseChessMainPanel.dic_RoomData[item.roomId].users.Count;
|
||
if (temp >= 2)
|
||
{
|
||
UIManager.Instance.ShowBoardError("玩家满员");
|
||
}
|
||
else
|
||
{
|
||
uIManager.ShowAsync(UIPanel.ZGXQJoinGame, item);
|
||
}
|
||
break;
|
||
case 2:
|
||
UIManager.Instance.ShowBoardError("房间已经开始");
|
||
break;
|
||
case 0: // 空房间
|
||
case 3:
|
||
uIManager.ShowAsync(UIPanel.ZGXQCreateRoom, item);
|
||
break;
|
||
default:
|
||
break;
|
||
}
|
||
});
|
||
tran.name = item.roomId;
|
||
itemListDic[item.roomId] = tran;
|
||
ChineseChessMainPanel.dic_RoomData[item.roomId] = item;
|
||
|
||
var slider = tran.transform.GetChild(0).GetComponent<Slider>();
|
||
// 人数 = 真实玩家 + 机器人(users.Count 只含真实玩家,robotCount 是 AI)
|
||
int allPlayerCount = item.users.Count + item.robotCount;
|
||
slider.value = (float)allPlayerCount / 2;
|
||
if (allPlayerCount == 2)
|
||
slider.fillRect.GetChild(0).gameObject.SetActive(true);
|
||
else
|
||
slider.fillRect.GetChild(0).gameObject.SetActive(false);
|
||
|
||
tran.transform.GetChild(1).GetComponent<TextMeshProUGUI>().text = allPlayerCount + "/" + 2;
|
||
tran.transform.GetChild(2).GetComponent<TextMeshProUGUI>().text = item.roomId.Substring(2).ToString();
|
||
i++;
|
||
}
|
||
scroll.horizontalNormalizedPosition = 0;
|
||
}
|
||
}
|
||
|
||
// ==================== 分区 Tab ====================
|
||
|
||
private void BuildChessFenQuTabs()
|
||
{
|
||
if (tra_DaQuParent == null || tra_FenQuItem == null) return;
|
||
|
||
// 清空旧列表
|
||
for (int i = panel_RoomItemParent.childCount - 1; i >= 0; i--)
|
||
Destroy(panel_RoomItemParent.GetChild(i).gameObject);
|
||
itemListDic.Clear();
|
||
|
||
for (int d = 1; d <= maxDistrict; d++)
|
||
{
|
||
var tab = Instantiate(tra_FenQuItem, tra_DaQuParent);
|
||
// child0 = Toggle容器, child0.child0 = TextMeshProUGUI
|
||
if (tab.childCount > 0 && tab.GetChild(0) != null && tab.GetChild(0).childCount > 0)
|
||
{
|
||
tab.GetChild(0).GetChild(0).GetComponent<TextMeshProUGUI>().text = "象棋" + ToChineseNumber(d) + "区";
|
||
tab.GetChild(0).GetChild(2).GetComponent<TextMeshProUGUI>().text = "象棋" + ToChineseNumber(d) + "区";
|
||
}
|
||
|
||
|
||
int district = d;
|
||
var toggle = tab.GetChild(0).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).GetChild(0).GetComponent<Toggle>();
|
||
if (t != null && t != toggle)
|
||
t.SetIsOnWithoutNotify(false);
|
||
}
|
||
|
||
// 清空房间列表
|
||
for (int i = panel_RoomItemParent.childCount - 1; i >= 0; i--)
|
||
Destroy(panel_RoomItemParent.GetChild(i).gameObject);
|
||
itemListDic.Clear();
|
||
|
||
WebSocketSendList.GetRoomListChineseChess(district);
|
||
});
|
||
}
|
||
}
|
||
|
||
// 默认选中一区
|
||
if (tra_DaQuParent.childCount > 0)
|
||
{
|
||
var firstTab = tra_DaQuParent.GetChild(0);
|
||
if (firstTab.childCount > 0)
|
||
{
|
||
var firstToggle = firstTab.GetChild(0).GetComponent<Toggle>();
|
||
if (firstToggle != null)
|
||
firstToggle.isOn = true;
|
||
}
|
||
}
|
||
}
|
||
|
||
private static string ToChineseNumber(int n)
|
||
{
|
||
if (n <= 0) return n.ToString();
|
||
if (n <= 10)
|
||
{
|
||
string[] units = { "", "一", "二", "三", "四", "五", "六", "七", "八", "九", "十" };
|
||
return units[n];
|
||
}
|
||
return n.ToString();
|
||
}
|
||
|
||
public void UpdataRoom(ChessGameRoom item)
|
||
{
|
||
if (item == null || string.IsNullOrEmpty(item.roomId)) return;
|
||
string roomID = item.roomId;
|
||
ChineseChessMainPanel.dic_RoomData[roomID] = item;
|
||
|
||
// 不在当前列表(其他分区/列表已清空)→ 只更新缓存,不动 UI
|
||
if (!itemListDic.TryGetValue(roomID, out var tran)) return;
|
||
// 子节点索引与 Show 保持一致:0=Slider, 1=人数文本, 2=房间号
|
||
var slider = tran.transform.GetChild(0).GetComponent<Slider>();
|
||
// 人数 = 真实玩家 + 机器人
|
||
int allPlayerCount = item.users.Count + item.robotCount;
|
||
slider.value = (float)allPlayerCount / 2;
|
||
if (allPlayerCount == 2)
|
||
slider.fillRect.GetChild(0).gameObject.SetActive(true);
|
||
else
|
||
slider.fillRect.GetChild(0).gameObject.SetActive(false);
|
||
tran.transform.GetChild(1).GetComponent<TextMeshProUGUI>().text = allPlayerCount + "/" + 2;
|
||
}
|
||
}
|