Files
BABA_YAGA/Assets/Scripts/UI/LobbyController.cs

130 lines
5.1 KiB
C#
Raw Normal View History

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-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;
private _BasicSpawner _spawner;
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
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-28 19:04:09 +07:00
_spawner = Object.FindFirstObjectByType<_BasicSpawner>();
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-28 19:04:09 +07:00
// Event Bindings
root.Q<Button>("GoToCreateBtn").clicked += ShowCreate;
root.Q<Button>("CancelCreateBtn").clicked += ShowJoin;
root.Q<Button>("BackToMenuBtn").clicked += () => uiManager.Pop();
root.Q<Button>("ConfirmCreateBtn").clicked += OnCreateRoomClicked;
root.Q<Button>("ConfirmJoinBtn").clicked += OnConfirmPasswordClicked;
root.Q<Button>("ClosePassBtn").clicked += () => _passOverlay.style.display = DisplayStyle.None;
2026-04-26 05:20:47 +07:00
2026-04-28 19:04:09 +07:00
_passToggle.RegisterValueChangedCallback(evt =>
_roomPassInput.style.display = evt.newValue ? DisplayStyle.Flex : DisplayStyle.None);
2026-04-26 05:20:47 +07:00
2026-04-28 19:04:09 +07:00
// Đăng ký sự kiện từ Spawner
if (_spawner != null)
{
_spawner.OnSessionListUpdatedEvent += UpdateRoomList;
_spawner.OnJoinFailedEvent += () => _joinPassError.style.display = DisplayStyle.Flex;
}
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-28 00:07:42 +07:00
public void ShowJoin()
{
_joinContainer.style.display = DisplayStyle.Flex;
_createContainer.style.display = DisplayStyle.None;
_loungeContainer.style.display = DisplayStyle.None;
2026-04-28 19:04:09 +07:00
_spawner?.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-28 00:07:42 +07:00
_joinContainer.style.display = DisplayStyle.None;
_createContainer.style.display = DisplayStyle.Flex;
2026-04-25 18:20:16 +07:00
}
2026-04-28 19:04:09 +07:00
private async void OnCreateRoomClicked()
2026-04-25 18:20:16 +07:00
{
2026-04-28 19:04:09 +07:00
string id = _roomIDInput.value.Trim();
string name = string.IsNullOrEmpty(_roomNameInput.value) ? id : _roomNameInput.value;
string pass = _passToggle.value ? _roomPassInput.value : null;
if (string.IsNullOrEmpty(id)) return;
await _spawner.StartHost(id, pass);
// Logic chuyển sang Lounge sẽ được xử lý qua sự kiện OnPlayerJoined trong Spawner
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-28 19:04:09 +07:00
_roomList.Clear();
foreach (var session in sessions)
2026-04-28 00:07:42 +07:00
{
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}";
bool needsPass = session.Properties.ContainsKey("pw"); // Giả sử dùng metadata
item.Q<Label>("LockIcon").style.display = needsPass ? DisplayStyle.Flex : DisplayStyle.None;
var joinBtn = item.Q<Button>("JoinBtn");
joinBtn.clicked += () => OnRoomItemClicked(session);
_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
private void OnRoomItemClicked(SessionInfo session)
{
_selectedSession = session;
// Fusion dùng Password trong StartGameArgs, nên ta hiện popup nhập trước
_passOverlay.style.display = DisplayStyle.Flex;
_joinPassError.style.display = DisplayStyle.None;
_joinPassInput.value = "";
}
private async void OnConfirmPasswordClicked()
{
if (_selectedSession == null) return;
string pass = _joinPassInput.value;
await _spawner.StartClient(_selectedSession.Name, pass);
}
2026-04-25 18:20:16 +07:00
}
}