Update
This commit is contained in:
@@ -1,73 +1,129 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Fusion;
|
||||
|
||||
namespace Hallucinate.UI
|
||||
{
|
||||
public class LobbyController : BaseUIController
|
||||
{
|
||||
private VisualElement _joinContainer;
|
||||
private VisualElement _createContainer;
|
||||
private VisualElement _loungeContainer;
|
||||
private VisualTreeAsset _roomItemTemplate;
|
||||
private _BasicSpawner _spawner;
|
||||
|
||||
private Button _backBtn;
|
||||
private Button _createFinalBtn;
|
||||
private Button _startGameBtn;
|
||||
|
||||
private Toggle _hostReady;
|
||||
private Toggle _guestReady;
|
||||
// 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");
|
||||
|
||||
_backBtn = root.Q<Button>("BackToMenuBtn");
|
||||
_createFinalBtn = root.Q<Button>("CreateRoomFinalBtn");
|
||||
_startGameBtn = root.Q<Button>("StartGameBtn");
|
||||
|
||||
_hostReady = root.Q<Toggle>("HostReady");
|
||||
_guestReady = root.Q<Toggle>("GuestReady");
|
||||
|
||||
_backBtn.clicked += () => uiManager.Pop();
|
||||
_createFinalBtn.clicked += () => ShowLounge();
|
||||
_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");
|
||||
|
||||
_hostReady.RegisterValueChangedCallback(evt => UpdateStartButton());
|
||||
_guestReady.RegisterValueChangedCallback(evt => UpdateStartButton());
|
||||
_joinPassInput = root.Q<TextField>("JoinPassInput");
|
||||
_joinPassError = root.Q<Label>("JoinPassError");
|
||||
|
||||
UpdateStartButton();
|
||||
// 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;
|
||||
_loungeContainer.style.display = DisplayStyle.None;
|
||||
}
|
||||
|
||||
public void ShowLounge()
|
||||
private async void OnCreateRoomClicked()
|
||||
{
|
||||
_joinContainer.style.display = DisplayStyle.None;
|
||||
_createContainer.style.display = DisplayStyle.None;
|
||||
_loungeContainer.style.display = DisplayStyle.Flex;
|
||||
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 UpdateStartButton()
|
||||
private void UpdateRoomList(List<SessionInfo> sessions)
|
||||
{
|
||||
if (_startGameBtn != null)
|
||||
_roomList.Clear();
|
||||
foreach (var session in sessions)
|
||||
{
|
||||
_startGameBtn.SetEnabled(_hostReady.value && _guestReady.value);
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user