206 lines
7.4 KiB
C#
206 lines
7.4 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)
|
|
{
|
|
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 if (temp == 1)
|
|
{
|
|
uIManager.ShowAsync(UIPanel.ZGXQJoinGame, item);
|
|
}
|
|
break;
|
|
case 2:
|
|
UIManager.Instance.ShowBoardError("房间已经开始");
|
|
break;
|
|
case 3:
|
|
case 0:
|
|
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>();
|
|
int allPlayerCount = item.users.Count;
|
|
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)
|
|
{
|
|
string roomID = item.roomId;
|
|
ChineseChessMainPanel.dic_RoomData[roomID] = item;
|
|
|
|
var tran = itemListDic[roomID];
|
|
var slider = tran.transform.GetChild(1).GetComponent<Slider>();
|
|
int allPlayerCount = item.users.Count;
|
|
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(2).GetComponent<TextMeshProUGUI>().text = allPlayerCount + "/" + 2;
|
|
}
|
|
}
|