Elo system update
This commit is contained in:
@@ -45,6 +45,8 @@ namespace Hallucinate.UI
|
||||
return key;
|
||||
}
|
||||
|
||||
public virtual void Update() { }
|
||||
|
||||
public virtual async Task PlayTransitionIn()
|
||||
{
|
||||
if (root == null) return;
|
||||
|
||||
@@ -53,7 +53,7 @@ namespace Hallucinate.UI
|
||||
}
|
||||
}
|
||||
|
||||
public void Update()
|
||||
public override void Update()
|
||||
{
|
||||
if (!_isFaded && Time.time - _lastActionTime > FADE_TIMEOUT)
|
||||
{
|
||||
|
||||
@@ -3,6 +3,7 @@ using UnityEngine.UIElements;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Fusion;
|
||||
using System.Linq;
|
||||
|
||||
namespace Hallucinate.UI
|
||||
{
|
||||
@@ -10,6 +11,7 @@ namespace Hallucinate.UI
|
||||
{
|
||||
private VisualTreeAsset _roomItemTemplate;
|
||||
private _BasicSpawner _spawner;
|
||||
private _PlayerDataManager _playerDataManager;
|
||||
|
||||
// Containers
|
||||
private VisualElement _joinContainer, _createContainer, _loungeContainer, _passOverlay;
|
||||
@@ -24,6 +26,11 @@ namespace Hallucinate.UI
|
||||
private Label _joinPassError;
|
||||
private SessionInfo _selectedSession;
|
||||
|
||||
// Lounge Elements
|
||||
private VisualElement _playerListContainer;
|
||||
private Button _readyBtn, _startBtn;
|
||||
private Label _loungeRoomName;
|
||||
|
||||
public override void Initialize(VisualElement uxmlRoot, UIManager manager)
|
||||
{
|
||||
base.Initialize(uxmlRoot, manager);
|
||||
@@ -44,27 +51,63 @@ namespace Hallucinate.UI
|
||||
_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 += async () => await uiManager.Pop();
|
||||
root.Q<Button>("ConfirmCreateBtn").clicked += OnCreateRoomClicked;
|
||||
root.Q<Button>("ConfirmJoinBtn").clicked += OnConfirmPasswordClicked;
|
||||
root.Q<Button>("ClosePassBtn").clicked += () => _passOverlay.style.display = DisplayStyle.None;
|
||||
// Lounge Elements
|
||||
_playerListContainer = root.Q<VisualElement>("PlayerList");
|
||||
_readyBtn = root.Q<Button>("ReadyBtn");
|
||||
_startBtn = root.Q<Button>("StartBtn");
|
||||
_loungeRoomName = root.Q<Label>("LoungeRoomName");
|
||||
|
||||
_passToggle.RegisterValueChangedCallback(evt =>
|
||||
_roomPassInput.style.display = evt.newValue ? DisplayStyle.Flex : DisplayStyle.None);
|
||||
// Event Bindings
|
||||
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;
|
||||
|
||||
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;
|
||||
});
|
||||
}
|
||||
|
||||
// Đăng ký sự kiện từ Spawner
|
||||
if (_spawner != null)
|
||||
{
|
||||
_spawner.OnSessionListUpdatedEvent += UpdateRoomList;
|
||||
_spawner.OnJoinFailedEvent += () => _joinPassError.style.display = DisplayStyle.Flex;
|
||||
_spawner.OnJoinStartedEvent += () => { /* Show loading if needed */ };
|
||||
}
|
||||
}
|
||||
|
||||
public void SetRoomTemplate(VisualTreeAsset template) => _roomItemTemplate = template;
|
||||
|
||||
public override async Task PlayTransitionIn()
|
||||
{
|
||||
await base.PlayTransitionIn();
|
||||
ShowJoin();
|
||||
}
|
||||
|
||||
public void ShowJoin()
|
||||
{
|
||||
_joinContainer.style.display = DisplayStyle.Flex;
|
||||
@@ -79,6 +122,16 @@ namespace Hallucinate.UI
|
||||
_createContainer.style.display = DisplayStyle.Flex;
|
||||
}
|
||||
|
||||
private void ShowLounge(string roomName)
|
||||
{
|
||||
_joinContainer.style.display = DisplayStyle.None;
|
||||
_createContainer.style.display = DisplayStyle.None;
|
||||
_loungeContainer.style.display = DisplayStyle.Flex;
|
||||
_loungeRoomName.text = $"Room: {roomName}";
|
||||
|
||||
_playerDataManager = Object.FindFirstObjectByType<_PlayerDataManager>();
|
||||
}
|
||||
|
||||
private async void OnCreateRoomClicked()
|
||||
{
|
||||
string id = _roomIDInput.value.Trim();
|
||||
@@ -88,10 +141,12 @@ namespace Hallucinate.UI
|
||||
if (string.IsNullOrEmpty(id)) return;
|
||||
|
||||
await _spawner.StartHost(id, pass);
|
||||
ShowLounge(name);
|
||||
}
|
||||
|
||||
private void UpdateRoomList(List<SessionInfo> sessions)
|
||||
{
|
||||
if (_roomList == null) return;
|
||||
_roomList.Clear();
|
||||
foreach (var session in sessions)
|
||||
{
|
||||
@@ -111,17 +166,111 @@ namespace Hallucinate.UI
|
||||
|
||||
private void OnRoomItemClicked(SessionInfo session)
|
||||
{
|
||||
_selectedSession = session;
|
||||
_passOverlay.style.display = DisplayStyle.Flex;
|
||||
_joinPassError.style.display = DisplayStyle.None;
|
||||
_joinPassInput.value = "";
|
||||
bool needsPass = session.Properties.ContainsKey("pw");
|
||||
if (needsPass)
|
||||
{
|
||||
_selectedSession = session;
|
||||
_passOverlay.style.display = DisplayStyle.Flex;
|
||||
_joinPassError.style.display = DisplayStyle.None;
|
||||
_joinPassInput.value = "";
|
||||
}
|
||||
else
|
||||
{
|
||||
JoinRoom(session.Name, null);
|
||||
}
|
||||
}
|
||||
|
||||
private async void OnConfirmPasswordClicked()
|
||||
{
|
||||
if (_selectedSession == null) return;
|
||||
string pass = _joinPassInput.value;
|
||||
await _spawner.StartClient(_selectedSession.Name, pass);
|
||||
_passOverlay.style.display = DisplayStyle.None;
|
||||
await JoinRoom(_selectedSession.Name, pass);
|
||||
}
|
||||
|
||||
private async Task JoinRoom(string sessionName, string password)
|
||||
{
|
||||
await _spawner.StartClient(sessionName, password);
|
||||
ShowLounge(sessionName);
|
||||
}
|
||||
|
||||
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()
|
||||
{
|
||||
_spawner.StartGame();
|
||||
}
|
||||
|
||||
private void OnLeaveLoungeClicked()
|
||||
{
|
||||
var runner = Object.FindFirstObjectByType<NetworkRunner>();
|
||||
runner?.Shutdown();
|
||||
ShowJoin();
|
||||
}
|
||||
|
||||
public override void Update()
|
||||
{
|
||||
if (_loungeContainer.style.display == DisplayStyle.Flex)
|
||||
{
|
||||
UpdateLoungeUI();
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdateLoungeUI()
|
||||
{
|
||||
if (_playerDataManager == null)
|
||||
{
|
||||
_playerDataManager = Object.FindFirstObjectByType<_PlayerDataManager>();
|
||||
return;
|
||||
}
|
||||
|
||||
var runner = Object.FindFirstObjectByType<NetworkRunner>();
|
||||
if (runner == null) return;
|
||||
|
||||
// Update Player List
|
||||
_playerListContainer.Clear();
|
||||
bool allReady = true;
|
||||
int playerCount = 0;
|
||||
|
||||
foreach (var kvp in _playerDataManager.Players)
|
||||
{
|
||||
playerCount++;
|
||||
var playerRef = kvp.Key;
|
||||
var data = kvp.Value;
|
||||
|
||||
var playerItem = new VisualElement();
|
||||
playerItem.style.flexDirection = FlexDirection.Row;
|
||||
playerItem.style.justifyContent = Justify.SpaceBetween;
|
||||
playerItem.style.paddingBottom = 5;
|
||||
|
||||
var nameLabel = new Label(data.Name.ToString());
|
||||
var readyLabel = new Label(data.IsReady ? "READY" : "WAITING...");
|
||||
readyLabel.style.color = data.IsReady ? Color.green : Color.yellow;
|
||||
|
||||
playerItem.Add(nameLabel);
|
||||
playerItem.Add(readyLabel);
|
||||
_playerListContainer.Add(playerItem);
|
||||
|
||||
if (!data.IsReady) allReady = false;
|
||||
}
|
||||
|
||||
// Update Buttons
|
||||
_startBtn.style.display = (runner.IsServer && allReady && playerCount >= 2) ? DisplayStyle.Flex : DisplayStyle.None;
|
||||
|
||||
_playerDataManager.TryGetPlayerMetaData(runner.LocalPlayer, out var myData);
|
||||
_readyBtn.text = myData.IsReady ? "UNREADY" : "READY UP";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -230,7 +230,7 @@ namespace Hallucinate.UI
|
||||
.OnComplete(() => _ribbon.style.display = DisplayStyle.None);
|
||||
}
|
||||
|
||||
public void Update()
|
||||
public override void Update()
|
||||
{
|
||||
if (Input.GetAxis("Mouse X") != 0 || Input.GetAxis("Mouse Y") != 0 || Input.anyKey)
|
||||
{
|
||||
|
||||
@@ -65,13 +65,30 @@ namespace Hallucinate.UI
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
if (Instance != null && Instance != this)
|
||||
{
|
||||
Destroy(gameObject);
|
||||
return;
|
||||
}
|
||||
Instance = this;
|
||||
DontDestroyOnLoad(gameObject);
|
||||
|
||||
_uiDocument = GetComponent<UIDocument>();
|
||||
UnityEngine.Cursor.visible = false;
|
||||
|
||||
ApplySavedUIScale();
|
||||
}
|
||||
|
||||
public void OnGameStarted()
|
||||
{
|
||||
_ = Push<HUDController>();
|
||||
}
|
||||
|
||||
public void OnBackToMenu()
|
||||
{
|
||||
_ = Push<MainMenuController>();
|
||||
}
|
||||
|
||||
public void SetUIScale(float scale)
|
||||
{
|
||||
if (_uiDocument == null || _uiDocument.panelSettings == null) return;
|
||||
@@ -187,7 +204,7 @@ namespace Hallucinate.UI
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (_mainMenuController != null) _mainMenuController.Update();
|
||||
if (_history.Count > 0) _history.Peek().Update();
|
||||
UpdateCursorAndTrail();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user