This commit is contained in:
2026-05-01 20:07:12 +07:00
parent 3226eab649
commit afd629cd53
13 changed files with 1028 additions and 1693 deletions

View File

@@ -1,4 +1,4 @@
using UnityEngine;
using UnityEngine;
using UnityEngine.UIElements;
using System.Collections.Generic;
using System.Threading.Tasks;
@@ -18,6 +18,8 @@ namespace Hallucinate.UI
// Create Room Fields
private TextField _roomIDInput, _roomNameInput, _roomPassInput;
private Toggle _passToggle;
private Label _createErrorLabel;
private Button _confirmCreateBtn;
// Join Room Fields
private ScrollView _roomList;
@@ -57,6 +59,7 @@ namespace Hallucinate.UI
_roomNameInput = root.Q<TextField>("RoomNameInput");
_roomPassInput = root.Q<TextField>("RoomPassInput");
_passToggle = root.Q<Toggle>("PassToggle");
_createErrorLabel = root.Q<Label>("CreateErrorLabel");
// Join Room Fields
_roomList = root.Q<ScrollView>("RoomList");
@@ -87,7 +90,10 @@ namespace Hallucinate.UI
root.Q<Button>("GoToCreateBtn").clicked += ShowCreate;
root.Q<Button>("CancelCreateBtn").clicked += ShowJoin;
root.Q<Button>("BackToMenuBtn").clicked += async () => await uiManager.Pop();
root.Q<Button>("ConfirmCreateBtn").clicked += OnCreateRoomClicked;
_confirmCreateBtn = root.Q<Button>("ConfirmCreateBtn");
if (_confirmCreateBtn != null) _confirmCreateBtn.clicked += OnCreateRoomClicked;
root.Q<Button>("ConfirmJoinBtn").clicked += OnConfirmPasswordClicked;
root.Q<Button>("ClosePassBtn").clicked += () => { if(_passOverlay != null) _passOverlay.style.display = DisplayStyle.None; };
root.Q<Button>("LeaveLoungeBtn").clicked += OnLeaveLoungeClicked;
@@ -210,13 +216,9 @@ namespace Hallucinate.UI
if (goToCreateBtn != null) goToCreateBtn.text = GetT("LOBBY_CREATE_NEW");
// CREATE VIEW
var createHeading = root.Q<Label>(null, "text-heading"); // Header in CreateContainer
// Note: Querying by class might be ambiguous if multiple exist, better to find within container
var createHeader = _createContainer?.Q<Label>(null, "text-heading");
if (createHeader != null) createHeader.text = GetT("LOBBY_CREATE_HEADER");
var roomIdLabel = _createContainer?.Q<Label>(null, "text-label"); // First label is usually ID
// Since they don't have unique names, we'll try to find them by order or text match
_createContainer?.Query<Label>().ForEach(l => {
if (l.text.Contains("ROOM ID")) l.text = GetT("LOBBY_ROOM_ID_LABEL");
if (l.text.Contains("ROOM NAME")) l.text = GetT("LOBBY_ROOM_NAME_LABEL");
@@ -230,8 +232,7 @@ namespace Hallucinate.UI
var cancelCreateBtn = root.Q<Button>("CancelCreateBtn");
if (cancelCreateBtn != null) cancelCreateBtn.text = GetT("LOBBY_CANCEL");
var confirmCreateBtn = root.Q<Button>("ConfirmCreateBtn");
if (confirmCreateBtn != null) confirmCreateBtn.text = GetT("LOBBY_CREATE_BTN");
if (_confirmCreateBtn != null) _confirmCreateBtn.text = GetT("LOBBY_CREATE_BTN");
// LOUNGE VIEW
if (_loungeRoomName != null && _loungeRoomName.text == "SESSION NAME")
@@ -300,6 +301,14 @@ namespace Hallucinate.UI
{
if (_joinContainer != null) _joinContainer.style.display = DisplayStyle.None;
if (_createContainer != null) _createContainer.style.display = DisplayStyle.Flex;
if (_createErrorLabel != null) _createErrorLabel.style.display = DisplayStyle.None;
if (_confirmCreateBtn != null) _confirmCreateBtn.SetEnabled(true);
// Auto-generate ID when showing create screen
if (_roomIDInput != null)
{
_roomIDInput.value = "ROOM_" + Random.Range(1000, 9999).ToString();
}
}
private void ShowLounge(string roomName)
@@ -309,6 +318,13 @@ namespace Hallucinate.UI
if (_loungeContainer != null) _loungeContainer.style.display = DisplayStyle.Flex;
if (_loungeRoomName != null) _loungeRoomName.text = roomName.ToUpper();
var runner = Object.FindFirstObjectByType<NetworkRunner>();
if (runner != null)
{
var loungeIdLabel = root.Q<Label>("LoungeID");
if (loungeIdLabel != null) loungeIdLabel.text = GetT("LOBBY_ID_PREFIX") + runner.SessionInfo.Name;
}
_playerDataManager = Object.FindFirstObjectByType<PlayerDataManager>();
if (_playerDataManager != null)
{
@@ -318,33 +334,58 @@ namespace Hallucinate.UI
private async void OnCreateRoomClicked()
{
Debug.Log("[LobbyController] Create Room Clicked");
if (_confirmCreateBtn != null) _confirmCreateBtn.SetEnabled(false);
if (_createErrorLabel != null) _createErrorLabel.style.display = DisplayStyle.None;
var spawner = BasicSpawner.Instance;
if (spawner == null)
{
Debug.LogError("[LobbyController] Spawner Instance is NULL!");
ShowCreateError("System Error: Spawner missing.");
return;
}
string id = _roomIDInput != null && !string.IsNullOrEmpty(_roomIDInput.value)
// Auto-generate ID if empty
string id = (_roomIDInput != null && !string.IsNullOrEmpty(_roomIDInput.value))
? _roomIDInput.value.Trim()
: Random.Range(1000, 9999).ToString();
: "ROOM_" + Random.Range(1000, 9999).ToString();
if (_roomIDInput != null) _roomIDInput.value = id;
string name = _roomNameInput != null && !string.IsNullOrEmpty(_roomNameInput.value)
? _roomNameInput.value
: $"Room {id}";
// Name defaults to ID if empty
string name = (_roomNameInput != null && !string.IsNullOrEmpty(_roomNameInput.value))
? _roomNameInput.value.Trim()
: id;
string pass = (_passToggle != null && _passToggle.value && _roomPassInput != null)
? _roomPassInput.value
: null;
bool success = await spawner.StartHost(id, name, pass);
if (success)
try
{
ShowLounge(name);
bool success = await spawner.StartHost(id, name, pass);
if (success)
{
ShowLounge(name);
}
else
{
ShowCreateError("Failed to create room. ID might be taken.");
}
}
catch (System.Exception ex)
{
ShowCreateError("Network Error: " + ex.Message);
}
}
private void ShowCreateError(string message)
{
if (_createErrorLabel != null)
{
_createErrorLabel.text = message;
_createErrorLabel.style.display = DisplayStyle.Flex;
}
if (_confirmCreateBtn != null) _confirmCreateBtn.SetEnabled(true);
}
private void UpdateRoomList(List<SessionInfo> sessions)
@@ -356,7 +397,6 @@ namespace Hallucinate.UI
if (_roomItemTemplate == null) continue;
var item = _roomItemTemplate.Instantiate();
// Hiển thị tên phòng thân thiện nếu có
string displayName = session.Name;
if (session.Properties.TryGetValue("rn", out var rnProp))
{
@@ -475,18 +515,15 @@ namespace Hallucinate.UI
PlayerRef hostRef = PlayerRef.None;
PlayerRef guestRef = PlayerRef.None;
// Trong Host Mode, chủ phòng luôn là người có PlayerId = 1
var sortedPlayers = runner.ActivePlayers.OrderBy(p => p.PlayerId).ToList();
if (sortedPlayers.Count > 0) hostRef = sortedPlayers[0];
if (sortedPlayers.Count > 1) guestRef = sortedPlayers[1];
// Update Room Name for Guest
if (runner.SessionInfo != null && runner.SessionInfo.Properties.TryGetValue("rn", out var rnProp))
{
_loungeRoomName.text = rnProp.ToString().ToUpper();
}
// Update Host UI
if (hostRef != PlayerRef.None && _playerDataManager.TryGetPlayerMetaData(hostRef, out var hostData))
{
_hostNameLabel.text = hostData.Name.ToString().ToUpper();
@@ -499,7 +536,6 @@ namespace Hallucinate.UI
_hostStatusLabel.text = "-";
}
// Update Guest UI
if (guestRef != PlayerRef.None && _playerDataManager.TryGetPlayerMetaData(guestRef, out var guestData))
{
_guestNameLabel.text = guestData.Name.ToString().ToUpper();
@@ -518,7 +554,6 @@ namespace Hallucinate.UI
_guestStatusLabel.style.color = Color.gray;
}
// Start Button visibility logic
bool allReady = true;
int playerCount = 0;
foreach (var p in runner.ActivePlayers)
@@ -547,7 +582,6 @@ namespace Hallucinate.UI
{
if (_playerDataManager.TryGetPlayerMetaData(runner.LocalPlayer, out var myData))
{
// Style for Ready Button
if (myData.IsReady)
{
_readyBtn.text = GetT("LOBBY_UNREADY_BTN");