2026-04-25 18:20:16 +07:00
|
|
|
using UnityEngine;
|
|
|
|
|
using UnityEngine.UIElements;
|
2026-04-28 19:04:09 +07:00
|
|
|
using System.Collections.Generic;
|
2026-04-28 00:07:42 +07:00
|
|
|
using System.Threading.Tasks;
|
2026-04-28 19:04:09 +07:00
|
|
|
using Fusion;
|
2026-04-29 13:10:00 +07:00
|
|
|
using System.Linq;
|
2026-04-25 18:20:16 +07:00
|
|
|
|
2026-04-28 00:07:42 +07:00
|
|
|
namespace Hallucinate.UI
|
2026-04-25 18:20:16 +07:00
|
|
|
{
|
2026-04-28 00:07:42 +07:00
|
|
|
public class LobbyController : BaseUIController
|
2026-04-25 18:20:16 +07:00
|
|
|
{
|
2026-04-28 19:04:09 +07:00
|
|
|
private VisualTreeAsset _roomItemTemplate;
|
2026-04-30 00:55:16 +07:00
|
|
|
private PlayerDataManager _playerDataManager;
|
2026-04-25 18:20:16 +07:00
|
|
|
|
2026-04-28 19:04:09 +07:00
|
|
|
// Containers
|
|
|
|
|
private VisualElement _joinContainer, _createContainer, _loungeContainer, _passOverlay;
|
|
|
|
|
|
|
|
|
|
// Create Room Fields
|
|
|
|
|
private TextField _roomIDInput, _roomNameInput, _roomPassInput;
|
|
|
|
|
private Toggle _passToggle;
|
|
|
|
|
|
|
|
|
|
// Join Room Fields
|
|
|
|
|
private ScrollView _roomList;
|
|
|
|
|
private TextField _joinPassInput;
|
|
|
|
|
private Label _joinPassError;
|
|
|
|
|
private SessionInfo _selectedSession;
|
2026-04-28 00:07:42 +07:00
|
|
|
|
2026-04-29 13:10:00 +07:00
|
|
|
// Lounge Elements
|
|
|
|
|
private VisualElement _playerListContainer;
|
|
|
|
|
private Button _readyBtn, _startBtn;
|
|
|
|
|
private Label _loungeRoomName;
|
|
|
|
|
|
2026-04-28 00:07:42 +07:00
|
|
|
public override void Initialize(VisualElement uxmlRoot, UIManager manager)
|
2026-04-25 18:20:16 +07:00
|
|
|
{
|
2026-04-28 00:07:42 +07:00
|
|
|
base.Initialize(uxmlRoot, manager);
|
2026-04-26 05:02:49 +07:00
|
|
|
|
2026-04-28 19:04:09 +07:00
|
|
|
// Query Elements
|
2026-04-28 00:07:42 +07:00
|
|
|
_joinContainer = root.Q<VisualElement>("JoinContainer");
|
|
|
|
|
_createContainer = root.Q<VisualElement>("CreateContainer");
|
|
|
|
|
_loungeContainer = root.Q<VisualElement>("LoungeContainer");
|
2026-04-28 19:04:09 +07:00
|
|
|
_passOverlay = root.Q<VisualElement>("PasswordOverlay");
|
2026-04-26 05:02:49 +07:00
|
|
|
|
2026-04-28 19:04:09 +07:00
|
|
|
_roomIDInput = root.Q<TextField>("RoomIDInput");
|
|
|
|
|
_roomNameInput = root.Q<TextField>("RoomNameInput");
|
|
|
|
|
_roomPassInput = root.Q<TextField>("RoomPassInput");
|
|
|
|
|
_passToggle = root.Q<Toggle>("PassToggle");
|
|
|
|
|
_roomList = root.Q<ScrollView>("RoomList");
|
|
|
|
|
|
|
|
|
|
_joinPassInput = root.Q<TextField>("JoinPassInput");
|
|
|
|
|
_joinPassError = root.Q<Label>("JoinPassError");
|
2026-04-26 05:02:49 +07:00
|
|
|
|
2026-04-29 13:10:00 +07:00
|
|
|
// Lounge Elements
|
|
|
|
|
_playerListContainer = root.Q<VisualElement>("PlayerList");
|
|
|
|
|
_readyBtn = root.Q<Button>("ReadyBtn");
|
|
|
|
|
_startBtn = root.Q<Button>("StartBtn");
|
|
|
|
|
_loungeRoomName = root.Q<Label>("LoungeRoomName");
|
|
|
|
|
|
2026-04-28 19:04:09 +07:00
|
|
|
// Event Bindings
|
2026-04-29 13:10:00 +07:00
|
|
|
var goToCreateBtn = root.Q<Button>("GoToCreateBtn");
|
|
|
|
|
if (goToCreateBtn != null) goToCreateBtn.clicked += ShowCreate;
|
|
|
|
|
|
|
|
|
|
var cancelCreateBtn = root.Q<Button>("CancelCreateBtn");
|
|
|
|
|
if (cancelCreateBtn != null) cancelCreateBtn.clicked += ShowJoin;
|
|
|
|
|
|
|
|
|
|
var backToMenuBtn = root.Q<Button>("BackToMenuBtn");
|
|
|
|
|
if (backToMenuBtn != null) backToMenuBtn.clicked += async () => await uiManager.Pop();
|
|
|
|
|
|
|
|
|
|
var confirmCreateBtn = root.Q<Button>("ConfirmCreateBtn");
|
|
|
|
|
if (confirmCreateBtn != null) confirmCreateBtn.clicked += OnCreateRoomClicked;
|
|
|
|
|
|
|
|
|
|
var confirmJoinBtn = root.Q<Button>("ConfirmJoinBtn");
|
|
|
|
|
if (confirmJoinBtn != null) confirmJoinBtn.clicked += OnConfirmPasswordClicked;
|
|
|
|
|
|
|
|
|
|
var closePassBtn = root.Q<Button>("ClosePassBtn");
|
|
|
|
|
if (closePassBtn != null) closePassBtn.clicked += () => { if(_passOverlay != null) _passOverlay.style.display = DisplayStyle.None; };
|
|
|
|
|
|
|
|
|
|
var leaveLoungeBtn = root.Q<Button>("LeaveLoungeBtn");
|
|
|
|
|
if (leaveLoungeBtn != null) leaveLoungeBtn.clicked += OnLeaveLoungeClicked;
|
2026-04-26 05:20:47 +07:00
|
|
|
|
2026-04-29 13:10:00 +07:00
|
|
|
if (_readyBtn != null) _readyBtn.clicked += OnReadyClicked;
|
|
|
|
|
if (_startBtn != null) _startBtn.clicked += OnStartClicked;
|
|
|
|
|
|
|
|
|
|
if (_passToggle != null)
|
|
|
|
|
{
|
|
|
|
|
_passToggle.RegisterValueChangedCallback(evt =>
|
|
|
|
|
{
|
|
|
|
|
if (_roomPassInput != null)
|
|
|
|
|
_roomPassInput.style.display = evt.newValue ? DisplayStyle.Flex : DisplayStyle.None;
|
|
|
|
|
});
|
|
|
|
|
}
|
2026-04-26 05:20:47 +07:00
|
|
|
|
2026-04-30 01:30:58 +07:00
|
|
|
// Đăng ký sự kiện từ Spawner
|
2026-04-30 00:55:16 +07:00
|
|
|
if (BasicSpawner.Instance != null)
|
2026-04-28 19:04:09 +07:00
|
|
|
{
|
2026-04-30 00:55:16 +07:00
|
|
|
RegisterSpawnerEvents();
|
2026-04-30 01:30:58 +07:00
|
|
|
_ = BasicSpawner.Instance.StartLobby();
|
2026-04-28 19:04:09 +07:00
|
|
|
}
|
2026-04-30 00:55:16 +07:00
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Invoke(nameof(RegisterSpawnerEvents), 0.1f);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void RegisterSpawnerEvents()
|
|
|
|
|
{
|
|
|
|
|
if (BasicSpawner.Instance == null) return;
|
|
|
|
|
BasicSpawner.Instance.OnSessionListUpdatedEvent += UpdateRoomList;
|
|
|
|
|
BasicSpawner.Instance.OnJoinFailedEvent += () => { if(_joinPassError != null) _joinPassError.style.display = DisplayStyle.Flex; };
|
|
|
|
|
BasicSpawner.Instance.OnJoinStartedEvent += () => { /* Show loading if needed */ };
|
2026-04-30 01:30:58 +07:00
|
|
|
_ = BasicSpawner.Instance.StartLobby();
|
2026-04-28 00:07:42 +07:00
|
|
|
}
|
2026-04-26 05:20:47 +07:00
|
|
|
|
2026-04-28 19:04:09 +07:00
|
|
|
public void SetRoomTemplate(VisualTreeAsset template) => _roomItemTemplate = template;
|
|
|
|
|
|
2026-04-29 13:10:00 +07:00
|
|
|
public override async Task PlayTransitionIn()
|
|
|
|
|
{
|
|
|
|
|
await base.PlayTransitionIn();
|
|
|
|
|
ShowJoin();
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-28 00:07:42 +07:00
|
|
|
public void ShowJoin()
|
|
|
|
|
{
|
2026-04-30 00:55:16 +07:00
|
|
|
if (_joinContainer != null) _joinContainer.style.display = DisplayStyle.Flex;
|
|
|
|
|
if (_createContainer != null) _createContainer.style.display = DisplayStyle.None;
|
|
|
|
|
if (_loungeContainer != null) _loungeContainer.style.display = DisplayStyle.None;
|
2026-04-30 01:30:58 +07:00
|
|
|
_ = BasicSpawner.Instance?.StartLobby();
|
2026-04-26 05:20:47 +07:00
|
|
|
}
|
|
|
|
|
|
2026-04-28 00:07:42 +07:00
|
|
|
public void ShowCreate()
|
2026-04-26 05:20:47 +07:00
|
|
|
{
|
2026-04-30 00:55:16 +07:00
|
|
|
if (_joinContainer != null) _joinContainer.style.display = DisplayStyle.None;
|
|
|
|
|
if (_createContainer != null) _createContainer.style.display = DisplayStyle.Flex;
|
2026-04-25 18:20:16 +07:00
|
|
|
}
|
|
|
|
|
|
2026-04-29 13:10:00 +07:00
|
|
|
private void ShowLounge(string roomName)
|
|
|
|
|
{
|
2026-04-30 00:55:16 +07:00
|
|
|
if (_joinContainer != null) _joinContainer.style.display = DisplayStyle.None;
|
|
|
|
|
if (_createContainer != null) _createContainer.style.display = DisplayStyle.None;
|
|
|
|
|
if (_loungeContainer != null) _loungeContainer.style.display = DisplayStyle.Flex;
|
|
|
|
|
if (_loungeRoomName != null) _loungeRoomName.text = $"Room: {roomName}";
|
2026-04-29 13:10:00 +07:00
|
|
|
|
2026-04-30 00:55:16 +07:00
|
|
|
_playerDataManager = Object.FindFirstObjectByType<PlayerDataManager>();
|
2026-04-29 13:10:00 +07:00
|
|
|
}
|
|
|
|
|
|
2026-04-28 19:04:09 +07:00
|
|
|
private async void OnCreateRoomClicked()
|
2026-04-25 18:20:16 +07:00
|
|
|
{
|
2026-04-30 00:55:16 +07:00
|
|
|
var spawner = BasicSpawner.Instance;
|
2026-04-30 01:30:58 +07:00
|
|
|
if (spawner == null) return;
|
2026-04-30 00:55:16 +07:00
|
|
|
|
|
|
|
|
string id = _roomIDInput != null && !string.IsNullOrEmpty(_roomIDInput.value)
|
|
|
|
|
? _roomIDInput.value.Trim()
|
|
|
|
|
: Random.Range(1000, 9999).ToString();
|
2026-04-28 19:04:09 +07:00
|
|
|
|
2026-04-30 00:55:16 +07:00
|
|
|
if (_roomIDInput != null) _roomIDInput.value = id;
|
|
|
|
|
|
|
|
|
|
string name = _roomNameInput != null && !string.IsNullOrEmpty(_roomNameInput.value)
|
|
|
|
|
? _roomNameInput.value
|
|
|
|
|
: $"Room {id}";
|
|
|
|
|
|
|
|
|
|
string pass = (_passToggle != null && _passToggle.value && _roomPassInput != null)
|
|
|
|
|
? _roomPassInput.value
|
|
|
|
|
: null;
|
2026-04-28 19:04:09 +07:00
|
|
|
|
2026-04-30 00:55:16 +07:00
|
|
|
bool success = await spawner.StartHost(id, pass);
|
2026-04-30 01:30:58 +07:00
|
|
|
if (success) ShowLounge(name);
|
2026-04-26 05:20:47 +07:00
|
|
|
}
|
|
|
|
|
|
2026-04-28 19:04:09 +07:00
|
|
|
private void UpdateRoomList(List<SessionInfo> sessions)
|
2026-04-26 05:20:47 +07:00
|
|
|
{
|
2026-04-29 13:10:00 +07:00
|
|
|
if (_roomList == null) return;
|
2026-04-28 19:04:09 +07:00
|
|
|
_roomList.Clear();
|
|
|
|
|
foreach (var session in sessions)
|
2026-04-28 00:07:42 +07:00
|
|
|
{
|
2026-04-30 00:55:16 +07:00
|
|
|
if (_roomItemTemplate == null) continue;
|
2026-04-28 19:04:09 +07:00
|
|
|
var item = _roomItemTemplate.Instantiate();
|
|
|
|
|
item.Q<Label>("RoomName").text = session.Name;
|
|
|
|
|
item.Q<Label>("PlayerCount").text = $"{session.PlayerCount}/{session.MaxPlayers}";
|
|
|
|
|
|
2026-04-29 01:04:28 +07:00
|
|
|
bool needsPass = session.Properties.ContainsKey("pw");
|
2026-04-30 00:55:16 +07:00
|
|
|
var lockIcon = item.Q<Label>("LockIcon");
|
|
|
|
|
if (lockIcon != null) lockIcon.style.display = needsPass ? DisplayStyle.Flex : DisplayStyle.None;
|
2026-04-28 19:04:09 +07:00
|
|
|
|
|
|
|
|
var joinBtn = item.Q<Button>("JoinBtn");
|
2026-04-30 00:55:16 +07:00
|
|
|
if (joinBtn != null) joinBtn.clicked += () => OnRoomItemClicked(session);
|
2026-04-28 19:04:09 +07:00
|
|
|
|
|
|
|
|
_roomList.Add(item);
|
2026-04-28 00:07:42 +07:00
|
|
|
}
|
2026-04-25 18:20:16 +07:00
|
|
|
}
|
2026-04-28 19:04:09 +07:00
|
|
|
|
2026-04-30 00:55:16 +07:00
|
|
|
private async void OnRoomItemClicked(SessionInfo session)
|
2026-04-28 19:04:09 +07:00
|
|
|
{
|
2026-04-29 13:10:00 +07:00
|
|
|
bool needsPass = session.Properties.ContainsKey("pw");
|
|
|
|
|
if (needsPass)
|
|
|
|
|
{
|
|
|
|
|
_selectedSession = session;
|
2026-04-30 00:55:16 +07:00
|
|
|
if (_passOverlay != null) _passOverlay.style.display = DisplayStyle.Flex;
|
|
|
|
|
if (_joinPassError != null) _joinPassError.style.display = DisplayStyle.None;
|
|
|
|
|
if (_joinPassInput != null) _joinPassInput.value = "";
|
2026-04-29 13:10:00 +07:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2026-04-30 00:55:16 +07:00
|
|
|
await JoinRoom(session.Name, null);
|
2026-04-29 13:10:00 +07:00
|
|
|
}
|
2026-04-28 19:04:09 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async void OnConfirmPasswordClicked()
|
|
|
|
|
{
|
|
|
|
|
if (_selectedSession == null) return;
|
2026-04-30 00:55:16 +07:00
|
|
|
string pass = _joinPassInput != null ? _joinPassInput.value : "";
|
|
|
|
|
if (_passOverlay != null) _passOverlay.style.display = DisplayStyle.None;
|
2026-04-29 13:10:00 +07:00
|
|
|
await JoinRoom(_selectedSession.Name, pass);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task JoinRoom(string sessionName, string password)
|
|
|
|
|
{
|
2026-04-30 00:55:16 +07:00
|
|
|
if (BasicSpawner.Instance != null)
|
|
|
|
|
{
|
|
|
|
|
bool success = await BasicSpawner.Instance.StartClient(sessionName, password);
|
|
|
|
|
if (success) ShowLounge(sessionName);
|
|
|
|
|
}
|
2026-04-29 13:10:00 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnReadyClicked()
|
|
|
|
|
{
|
|
|
|
|
if (_playerDataManager != null)
|
|
|
|
|
{
|
|
|
|
|
var runner = Object.FindFirstObjectByType<NetworkRunner>();
|
|
|
|
|
if (runner != null)
|
|
|
|
|
{
|
|
|
|
|
_playerDataManager.TryGetPlayerMetaData(runner.LocalPlayer, out var myData);
|
|
|
|
|
_playerDataManager.RPC_SetReady(runner.LocalPlayer, !myData.IsReady);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnStartClicked()
|
|
|
|
|
{
|
2026-04-30 00:55:16 +07:00
|
|
|
BasicSpawner.Instance?.StartGame();
|
2026-04-29 13:10:00 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnLeaveLoungeClicked()
|
|
|
|
|
{
|
|
|
|
|
var runner = Object.FindFirstObjectByType<NetworkRunner>();
|
|
|
|
|
runner?.Shutdown();
|
|
|
|
|
ShowJoin();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void Update()
|
|
|
|
|
{
|
2026-04-30 00:55:16 +07:00
|
|
|
if (_loungeContainer != null && _loungeContainer.style.display == DisplayStyle.Flex)
|
2026-04-29 13:10:00 +07:00
|
|
|
{
|
|
|
|
|
UpdateLoungeUI();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void UpdateLoungeUI()
|
|
|
|
|
{
|
|
|
|
|
if (_playerDataManager == null)
|
|
|
|
|
{
|
2026-04-30 00:55:16 +07:00
|
|
|
_playerDataManager = Object.FindFirstObjectByType<PlayerDataManager>();
|
2026-04-29 13:10:00 +07:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var runner = Object.FindFirstObjectByType<NetworkRunner>();
|
|
|
|
|
if (runner == null) return;
|
|
|
|
|
|
2026-04-30 00:55:16 +07:00
|
|
|
if (_playerListContainer != null)
|
2026-04-29 13:10:00 +07:00
|
|
|
{
|
2026-04-30 00:55:16 +07:00
|
|
|
_playerListContainer.Clear();
|
|
|
|
|
bool allReady = true;
|
|
|
|
|
int playerCount = 0;
|
2026-04-29 13:10:00 +07:00
|
|
|
|
2026-04-30 00:55:16 +07:00
|
|
|
foreach (var kvp in _playerDataManager.Players)
|
|
|
|
|
{
|
|
|
|
|
playerCount++;
|
|
|
|
|
var data = kvp.Value;
|
|
|
|
|
|
|
|
|
|
var playerItem = new VisualElement();
|
|
|
|
|
playerItem.style.flexDirection = FlexDirection.Row;
|
|
|
|
|
playerItem.style.justifyContent = Justify.SpaceBetween;
|
|
|
|
|
playerItem.style.paddingBottom = 5;
|
2026-04-29 13:10:00 +07:00
|
|
|
|
2026-04-30 00:55:16 +07:00
|
|
|
var nameLabel = new Label(data.Name.ToString());
|
|
|
|
|
var readyLabel = new Label(data.IsReady ? "READY" : "WAITING...");
|
|
|
|
|
readyLabel.style.color = data.IsReady ? Color.green : Color.yellow;
|
2026-04-29 13:10:00 +07:00
|
|
|
|
2026-04-30 00:55:16 +07:00
|
|
|
playerItem.Add(nameLabel);
|
|
|
|
|
playerItem.Add(readyLabel);
|
|
|
|
|
_playerListContainer.Add(playerItem);
|
|
|
|
|
|
|
|
|
|
if (!data.IsReady) allReady = false;
|
|
|
|
|
}
|
2026-04-29 13:10:00 +07:00
|
|
|
|
2026-04-30 00:55:16 +07:00
|
|
|
if (_startBtn != null)
|
|
|
|
|
_startBtn.style.display = (runner.IsServer && allReady && playerCount >= 2) ? DisplayStyle.Flex : DisplayStyle.None;
|
|
|
|
|
|
|
|
|
|
if (_readyBtn != null)
|
|
|
|
|
{
|
|
|
|
|
_playerDataManager.TryGetPlayerMetaData(runner.LocalPlayer, out var myData);
|
|
|
|
|
_readyBtn.text = myData.IsReady ? "UNREADY" : "READY UP";
|
|
|
|
|
}
|
2026-04-29 13:10:00 +07:00
|
|
|
}
|
2026-04-30 00:55:16 +07:00
|
|
|
}
|
2026-04-29 13:10:00 +07:00
|
|
|
|
2026-04-30 00:55:16 +07:00
|
|
|
private async void Invoke(string methodName, float delay)
|
|
|
|
|
{
|
|
|
|
|
await Task.Delay((int)(delay * 1000));
|
|
|
|
|
if (methodName == nameof(RegisterSpawnerEvents)) RegisterSpawnerEvents();
|
2026-04-28 19:04:09 +07:00
|
|
|
}
|
2026-04-25 18:20:16 +07:00
|
|
|
}
|
|
|
|
|
}
|