130 lines
5.1 KiB
C#
130 lines
5.1 KiB
C#
using UnityEngine;
|
|
using UnityEngine.UIElements;
|
|
using System.Collections.Generic;
|
|
using System.Threading.Tasks;
|
|
using Fusion;
|
|
|
|
namespace Hallucinate.UI
|
|
{
|
|
public class LobbyController : BaseUIController
|
|
{
|
|
private VisualTreeAsset _roomItemTemplate;
|
|
private _BasicSpawner _spawner;
|
|
|
|
// 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;
|
|
|
|
public override void Initialize(VisualElement uxmlRoot, UIManager manager)
|
|
{
|
|
base.Initialize(uxmlRoot, manager);
|
|
_spawner = Object.FindFirstObjectByType<_BasicSpawner>();
|
|
|
|
// Query Elements
|
|
_joinContainer = root.Q<VisualElement>("JoinContainer");
|
|
_createContainer = root.Q<VisualElement>("CreateContainer");
|
|
_loungeContainer = root.Q<VisualElement>("LoungeContainer");
|
|
_passOverlay = root.Q<VisualElement>("PasswordOverlay");
|
|
|
|
_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");
|
|
|
|
// 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;
|
|
|
|
_passToggle.RegisterValueChangedCallback(evt =>
|
|
_roomPassInput.style.display = evt.newValue ? DisplayStyle.Flex : DisplayStyle.None);
|
|
|
|
// Đăng ký sự kiện từ Spawner
|
|
if (_spawner != null)
|
|
{
|
|
_spawner.OnSessionListUpdatedEvent += UpdateRoomList;
|
|
_spawner.OnJoinFailedEvent += () => _joinPassError.style.display = DisplayStyle.Flex;
|
|
}
|
|
}
|
|
|
|
public void SetRoomTemplate(VisualTreeAsset template) => _roomItemTemplate = template;
|
|
|
|
public void ShowJoin()
|
|
{
|
|
_joinContainer.style.display = DisplayStyle.Flex;
|
|
_createContainer.style.display = DisplayStyle.None;
|
|
_loungeContainer.style.display = DisplayStyle.None;
|
|
_spawner?.StartLobby();
|
|
}
|
|
|
|
public void ShowCreate()
|
|
{
|
|
_joinContainer.style.display = DisplayStyle.None;
|
|
_createContainer.style.display = DisplayStyle.Flex;
|
|
}
|
|
|
|
private async void OnCreateRoomClicked()
|
|
{
|
|
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
|
|
}
|
|
|
|
private void UpdateRoomList(List<SessionInfo> sessions)
|
|
{
|
|
_roomList.Clear();
|
|
foreach (var session in sessions)
|
|
{
|
|
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);
|
|
}
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|
|
}
|