This commit is contained in:
manhduyhoang90
2026-04-27 15:48:21 +07:00
90 changed files with 14604 additions and 900 deletions

View File

@@ -47,45 +47,45 @@ namespace OnlyScove.Scripts
[field: SerializeField] public LayerMask InteractionMask { get; private set; }
[Networked] public Quaternion NetworkedCameraRotation { get; set; }
// Thuộc tính hỗ trợ lấy rotation của Camera an toàn cho cả Online và Offline
public Quaternion CameraRotation
{
get
{
if (Runner != null && Runner.IsRunning && Object != null)
return NetworkedCameraRotation;
if (Cam != null)
return Cam.PlanarRotation;
return transform.rotation;
}
}
[Networked] public Vector2 NetworkedMoveInput { get; set; }
[Networked] public float NetworkedSpeed { get; set; }
[Networked] public Vector3 NetworkedPosition { get; set; }
[Header("Player Stats")]
[Networked, OnChangedRender(nameof(OnHealthChangedRender))] public float Health { get; set; } = 100f;
[Networked, OnChangedRender(nameof(OnStaminaChangedRender))] public float Stamina { get; set; } = 100f;
[Networked, OnChangedRender(nameof(OnNoiseLevelChangedRender))] public float NoiseLevel { get; set; } = 0f;
public event System.Action<float> OnHealthChanged;
public event System.Action<float> OnStaminaChanged;
public event System.Action<float> OnNoiseLevelChanged;
public event System.Action<IInteractable> OnInteractableTargetChanged;
public Vector2 MoveInput { get; private set; }
public bool IsSprintHeld { get; private set; }
public float VelocityY { get; set; }
public bool IsGrounded { get; private set; }
public bool WasGrounded { get; private set; }
private List<IInteractable> interactablesNearby = new List<IInteractable>();
private int currentInteractableIndex = 0;
public string CurrentStateName => currentState != null ? currentState.GetType().Name : "None";
public static PlayerStateMachine Local { get; private set; }
public Quaternion CameraRotation
{
get
{
if (Runner != null && Runner.IsRunning && Object != null) return NetworkedCameraRotation;
return Cam != null ? Cam.PlanarRotation : transform.rotation;
}
}
private PlayerBaseState currentState;
private bool hasControl = true;
private bool hasSpeedParam;
private bool hasVelocityXParam;
private bool hasVelocityZParam;
private List<IInteractable> interactablesNearby = new List<IInteractable>();
private int currentInteractableIndex = 0;
private float localAnimatorSpeed;
protected virtual void Awake()
{
@@ -94,7 +94,6 @@ namespace OnlyScove.Scripts
Anim = GetComponentInChildren<Animator>();
Scanner = GetComponent<EnvironmentScanner>();
// Kiểm tra tham số có tồn tại trong Animator không để tránh lỗi log gây Disconnect
if (Anim != null)
{
foreach (AnimatorControllerParameter param in Anim.parameters)
@@ -112,40 +111,30 @@ namespace OnlyScove.Scripts
private void Start()
{
// Nếu chạy Offline (kéo prefab vào scene), Spawned() sẽ không được gọi.
// Chúng ta khởi tạo tại đây để đảm bảo nhân vật hoạt động.
if (Runner == null || !Runner.IsRunning)
{
InitializePlayer();
}
if (Runner == null || !Runner.IsRunning) InitializePlayer();
}
public override void Spawned()
{
// Fusion gọi Spawned khi object được nạp vào mạng.
InitializePlayer();
// Nếu không có quyền điều khiển và đang ở Client, tắt Controller để tránh xung đột
if (Object != null && !Object.HasInputAuthority && Runner.IsClient)
{
if (Controller != null) Controller.enabled = false;
}
}
void OnHealthChangedRender() => OnHealthChanged?.Invoke(Health);
void OnStaminaChangedRender() => OnStaminaChanged?.Invoke(Stamina);
void OnNoiseLevelChangedRender() => OnNoiseLevelChanged?.Invoke(NoiseLevel);
private void InitializePlayer()
{
if (currentState == null)
{
SwitchState(new PlayerIdleState(this));
}
if (currentState == null) SwitchState(new PlayerIdleState(this));
bool isOffline = Runner == null || !Runner.IsRunning;
bool hasAuthority = Object != null && Object.HasInputAuthority;
if (isOffline || hasAuthority)
if (isOffline || (Object != null && Object.HasInputAuthority))
{
Local = this;
CameraController cameraController = GameObject.FindAnyObjectByType<CameraController>();
if (cameraController != null)
{
@@ -153,78 +142,45 @@ namespace OnlyScove.Scripts
Cam.followTarget = transform;
Cam.inputReader = Input;
}
Input.OnNextInteractEvent += OnNextInteract;
Input.OnPreviousInteractEvent += OnPreviousInteract;
// Đảm bảo Controller được bật
if (Controller != null) Controller.enabled = true;
}
}
private float localAnimatorSpeed;
public void Rotate(Vector3 moveDirection, float deltaTime)
{
if (moveDirection == Vector3.zero) return;
Quaternion targetRot = Quaternion.LookRotation(moveDirection);
transform.rotation = Quaternion.RotateTowards(
transform.rotation,
targetRot,
RotationSpeed * deltaTime
);
transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRot, RotationSpeed * deltaTime);
}
public void Move(Vector3 velocity, float animatorSpeed, float deltaTime)
{
// Cho phép di chuyển nếu:
// 1. Không có mạng (Offline test)
// 2. Có quyền điều khiển (Input Authority)
// 3. Là Server (State Authority)
bool canMove = (Runner == null || !Runner.IsRunning) || Object.HasInputAuthority || Runner.IsServer;
if (!canMove) return;
if (Controller != null && Controller.enabled)
{
Controller.Move(velocity * deltaTime);
// Cập nhật vị trí mạng ngay sau khi di chuyển
if (Object != null && Runner != null && Runner.IsRunning)
{
NetworkedPosition = transform.position;
}
if (Object != null && Runner != null && Runner.IsRunning) NetworkedPosition = transform.position;
}
localAnimatorSpeed = animatorSpeed;
if (Object != null && Object.HasStateAuthority)
{
NetworkedSpeed = animatorSpeed;
NetworkedMoveInput = MoveInput;
}
UpdateAnimator(deltaTime);
}
private void UpdateAnimator(float deltaTime)
{
if (Anim == null) return;
float speedValue = (Runner == null || !Runner.IsRunning || Object.HasInputAuthority) ? localAnimatorSpeed : NetworkedSpeed;
Vector2 inputVector = (Runner == null || !Runner.IsRunning || Object.HasInputAuthority) ? MoveInput : NetworkedMoveInput;
float speedValue;
Vector2 inputVector;
if (Runner == null || !Runner.IsRunning || Object.HasInputAuthority)
{
speedValue = localAnimatorSpeed;
inputVector = MoveInput;
}
else
{
speedValue = NetworkedSpeed;
inputVector = NetworkedMoveInput;
}
// Chỉ Set nếu tham số thực sự tồn tại (Tránh lỗi Hash does not exist)
if (hasSpeedParam) Anim.SetFloat(speedHash, speedValue, AnimationDamping, deltaTime);
if (hasVelocityXParam) Anim.SetFloat(velocityXHash, inputVector.x * speedValue, AnimationDamping, deltaTime);
if (hasVelocityZParam) Anim.SetFloat(velocityZHash, inputVector.y * speedValue, AnimationDamping, deltaTime);
@@ -235,30 +191,23 @@ namespace OnlyScove.Scripts
bool isRunning = Runner != null && Runner.IsRunning;
if (Object == null && isRunning) return;
// ĐỒNG BỘ VỊ TRÍ: Ép nhân vật về vị trí mạng trước khi tính toán tick mới
if (isRunning && NetworkedPosition != Vector3.zero)
if (isRunning && NetworkedPosition != Vector3.zero && !Object.HasInputAuthority)
{
if (Controller != null && !Object.HasInputAuthority)
{
Controller.enabled = false;
transform.position = NetworkedPosition;
Controller.enabled = true;
}
Controller.enabled = false;
transform.position = NetworkedPosition;
Controller.enabled = true;
}
if (GetInput(out PlayerInputData data))
{
MoveInput = data.Direction;
IsSprintHeld = data.sprint;
// Chỉ gán biến Networked nếu đang chạy mạng
if (isRunning) NetworkedCameraRotation = data.rot;
}
else if (!isRunning)
{
// FALLBACK INPUT: Nếu không có Fusion, lấy input trực tiếp từ Unity để Test
MoveInput = new Vector2(UnityEngine.Input.GetAxisRaw("Horizontal"), UnityEngine.Input.GetAxisRaw("Vertical"));
IsSprintHeld = UnityEngine.Input.GetKey(KeyCode.LeftShift);
// Ở chế độ offline, chúng ta không gán vào NetworkedCameraRotation nữa
}
else
{
@@ -266,44 +215,35 @@ namespace OnlyScove.Scripts
IsSprintHeld = false;
}
bool isSimulating = !isRunning || Object.HasInputAuthority || Runner.IsServer;
if (!isSimulating)
if (!isRunning || Object.HasInputAuthority || Runner.IsServer)
{
if (hasControl)
{
WasGrounded = IsGrounded;
CheckGround();
UpdateInteractablesList();
currentState?.Tick(isRunning ? Runner.DeltaTime : Time.fixedDeltaTime);
}
}
else
{
UpdateAnimator(Runner.DeltaTime);
return;
}
if (!hasControl) return;
WasGrounded = IsGrounded;
CheckGround();
UpdateInteractablesList();
float dt = isRunning ? Runner.DeltaTime : Time.fixedDeltaTime;
currentState?.Tick(dt);
}
private void Update()
{
// Nếu không có NetworkRunner, Fusion sẽ không gọi FixedUpdateNetwork.
// Chúng ta gọi thủ công để logic StateMachine vẫn chạy được khi Test Offline.
if (Runner == null || !Runner.IsRunning)
{
FixedUpdateNetwork();
}
if (Runner == null || !Runner.IsRunning) FixedUpdateNetwork();
}
private void CheckGround()
{
IsGrounded = Physics.CheckSphere(transform.TransformPoint(GroundCheckOffset), GroundCheckRadius, GroundMask);
}
private void CheckGround() => IsGrounded = Physics.CheckSphere(transform.TransformPoint(GroundCheckOffset), GroundCheckRadius, GroundMask);
private void UpdateInteractablesList()
{
interactablesNearby.Clear();
IInteractable target = Scanner.ScanForInteractable(InteractionRange, InteractionMask);
if (target != null) interactablesNearby.Add(target);
OnInteractableTargetChanged?.Invoke(target);
currentInteractableIndex = 0;
}
@@ -311,26 +251,19 @@ namespace OnlyScove.Scripts
{
if (interactablesNearby.Count <= 1) return;
currentInteractableIndex = (currentInteractableIndex + 1) % interactablesNearby.Count;
OnInteractableTargetChanged?.Invoke(GetInteractable());
}
private void OnPreviousInteract()
{
if (interactablesNearby.Count <= 1) return;
currentInteractableIndex--;
if (currentInteractableIndex < 0) currentInteractableIndex = interactablesNearby.Count - 1;
currentInteractableIndex = (currentInteractableIndex - 1 + interactablesNearby.Count) % interactablesNearby.Count;
OnInteractableTargetChanged?.Invoke(GetInteractable());
}
public IInteractable GetInteractable()
{
if (interactablesNearby.Count == 0) return null;
return interactablesNearby[currentInteractableIndex];
}
public IInteractable GetInteractable() => interactablesNearby.Count == 0 ? null : interactablesNearby[currentInteractableIndex];
public void SetGroundCheck(float radius, Vector3 offset)
{
GroundCheckRadius = radius;
GroundCheckOffset = offset;
}
public void SetGroundCheck(float radius, Vector3 offset) { GroundCheckRadius = radius; GroundCheckOffset = offset; }
public void SwitchState(PlayerBaseState newState)
{
@@ -352,4 +285,4 @@ namespace OnlyScove.Scripts
Gizmos.DrawSphere(transform.TransformPoint(GroundCheckOffset), GroundCheckRadius);
}
}
}
}

View File

@@ -0,0 +1,197 @@
using UnityEngine;
using UnityEngine.UIElements;
using OnlyScove.Scripts;
using System.Collections.Generic;
using UnityEngine.InputSystem;
namespace UI
{
public class HUDController : MonoBehaviour
{
[Header("UI Document")]
public UIDocument hudDocument;
private VisualElement _healthFill;
private VisualElement _staminaFill;
private Label _healthText;
private VisualElement _interactionPrompt;
private Label _interactionLabel;
private VisualElement _statsArea;
private VisualElement _inventoryArea;
private VisualElement _infoArea;
private float _lastInputTime;
private bool _isHUDVisible = true;
public float autoHideDelay = 5f;
private void OnEnable()
{
if (hudDocument == null)
hudDocument = GetComponent<UIDocument>();
var root = hudDocument.rootVisualElement;
_healthFill = root.Q<VisualElement>("health-fill");
_staminaFill = root.Q<VisualElement>("stamina-fill");
_healthText = root.Q<Label>("health-text");
_interactionPrompt = root.Q<VisualElement>("interaction-prompt");
_interactionLabel = root.Q<Label>("interaction-text");
_statsArea = root.Q<VisualElement>("hud-stats");
_inventoryArea = root.Q<VisualElement>("hud-inventory");
_infoArea = root.Q<VisualElement>("hud-info");
_lastInputTime = Time.time;
}
private void Update()
{
if (PlayerStateMachine.Local != null)
{
SubscribeToPlayer(PlayerStateMachine.Local);
}
HandleAutoHide();
HandleInventoryInput();
}
private void HandleAutoHide()
{
bool inputDetected = false;
// Check for mouse movement
if (Mouse.current != null && Mouse.current.delta.ReadValue().sqrMagnitude > 0.01f)
inputDetected = true;
// Check for any key press (including mouse buttons)
if (!inputDetected && Keyboard.current != null && Keyboard.current.anyKey.isPressed)
inputDetected = true;
if (!inputDetected && Mouse.current != null && (Mouse.current.leftButton.isPressed || Mouse.current.rightButton.isPressed))
inputDetected = true;
if (inputDetected)
{
_lastInputTime = Time.time;
SetHUDVisibility(true);
}
else if (Time.time - _lastInputTime > autoHideDelay)
{
SetHUDVisibility(false);
}
}
private void SetHUDVisibility(bool visible)
{
if (_isHUDVisible == visible) return;
_isHUDVisible = visible;
float targetOpacity = visible ? 1f : 0.2f;
_statsArea.style.opacity = targetOpacity;
_inventoryArea.style.opacity = targetOpacity;
_infoArea.style.opacity = targetOpacity;
_statsArea.style.transitionProperty = new List<StylePropertyName> { "opacity" };
_statsArea.style.transitionDuration = new List<TimeValue> { new TimeValue(0.5f, TimeUnit.Second) };
_inventoryArea.style.transitionProperty = new List<StylePropertyName> { "opacity" };
_inventoryArea.style.transitionDuration = new List<TimeValue> { new TimeValue(0.5f, TimeUnit.Second) };
_infoArea.style.transitionProperty = new List<StylePropertyName> { "opacity" };
_infoArea.style.transitionDuration = new List<TimeValue> { new TimeValue(0.5f, TimeUnit.Second) };
}
private void HandleInventoryInput()
{
if (Keyboard.current == null) return;
if (Keyboard.current.digit1Key.wasPressedThisFrame) SelectSlot(1);
if (Keyboard.current.digit2Key.wasPressedThisFrame) SelectSlot(2);
if (Keyboard.current.digit3Key.wasPressedThisFrame) SelectSlot(3);
}
private void SelectSlot(int index)
{
// Mock logic: Highlight the selected slot
var root = hudDocument.rootVisualElement;
for (int i = 1; i <= 3; i++)
{
var slot = root.Q<VisualElement>($"slot-{i}");
if (slot != null)
{
float width = (i == index) ? 2f : 1f;
Color color = (i == index) ? Color.white : new Color(0.5f, 0.5f, 0.5f);
slot.style.borderTopWidth = width;
slot.style.borderBottomWidth = width;
slot.style.borderLeftWidth = width;
slot.style.borderRightWidth = width;
slot.style.borderTopColor = color;
slot.style.borderBottomColor = color;
slot.style.borderLeftColor = color;
slot.style.borderRightColor = color;
}
}
_lastInputTime = Time.time;
SetHUDVisibility(true);
}
private PlayerStateMachine _currentPlayer;
private void SubscribeToPlayer(PlayerStateMachine player)
{
if (_currentPlayer == player) return;
if (_currentPlayer != null)
{
_currentPlayer.OnHealthChanged -= UpdateHealth;
_currentPlayer.OnStaminaChanged -= UpdateStamina;
_currentPlayer.OnInteractableTargetChanged -= UpdateInteraction;
}
_currentPlayer = player;
_currentPlayer.OnHealthChanged += UpdateHealth;
_currentPlayer.OnStaminaChanged += UpdateStamina;
_currentPlayer.OnInteractableTargetChanged += UpdateInteraction;
UpdateHealth(_currentPlayer.Health);
UpdateStamina(_currentPlayer.Stamina);
}
private void UpdateHealth(float health)
{
if (_healthFill != null) _healthFill.style.width = Length.Percent(health);
_lastInputTime = Time.time;
SetHUDVisibility(true);
}
private void UpdateStamina(float stamina)
{
if (_staminaFill != null) _staminaFill.style.width = Length.Percent(stamina);
if (stamina < 99f) // Only wake up HUD if stamina is being used
{
_lastInputTime = Time.time;
SetHUDVisibility(true);
}
}
private void UpdateInteraction(IInteractable interactable)
{
if (_interactionPrompt == null) return;
if (interactable != null)
{
_interactionPrompt.style.display = DisplayStyle.Flex;
if (_interactionLabel != null) _interactionLabel.text = interactable.InteractionPrompt;
_lastInputTime = Time.time;
SetHUDVisibility(true);
}
else
{
_interactionPrompt.style.display = DisplayStyle.None;
}
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: e79b70607af6eeb458c8eb6605e39b56

View File

@@ -0,0 +1,75 @@
using UnityEngine;
using UnityEngine.UIElements;
namespace UI
{
public class LobbyController : MonoBehaviour
{
private VisualElement _joinView;
private VisualElement _createView;
private float _lastInteractionTime;
private bool _isCreateMode = false;
private const float AutoReturnDelay = 5f;
private void OnEnable()
{
var root = GetComponent<UIDocument>().rootVisualElement;
_joinView = root.Q<VisualElement>("join-view");
_createView = root.Q<VisualElement>("create-view");
// Back button
root.Q<Button>("btn-back")?.RegisterCallback<ClickEvent>(evt => UIManager.Instance.GoBack());
root.Q<Button>("btn-settings")?.RegisterCallback<ClickEvent>(evt => UIManager.Instance.ToggleSettings());
// Create confirm -> Lounge
root.Q<Button>("btn-create-confirm")?.RegisterCallback<ClickEvent>(evt => UIManager.Instance.ShowScreen("Lounge"));
// Register Interaction Resetters
var textFields = root.Query<TextField>().ToList();
foreach (var field in textFields)
field.RegisterValueChangedCallback(evt => ResetInteractionTimer());
var toggles = root.Query<Toggle>().ToList();
foreach (var t in toggles)
t.RegisterValueChangedCallback(evt => ResetInteractionTimer());
// Password Toggle Logic
var passToggle = root.Q<Toggle>("toggle-password");
var passField = root.Q<TextField>("field-password");
passToggle?.RegisterValueChangedCallback(evt => {
if(passField != null) passField.style.display = evt.newValue ? DisplayStyle.Flex : DisplayStyle.None;
});
ResetInteractionTimer();
}
private void Update()
{
if (_isCreateMode)
{
if (Time.time - _lastInteractionTime > AutoReturnDelay)
{
SetMode(false); // Auto return to Stage 1
}
}
}
public void SetMode(bool isCreate)
{
_isCreateMode = isCreate;
if (_joinView == null) return;
_joinView.style.display = isCreate ? DisplayStyle.None : DisplayStyle.Flex;
_createView.style.display = isCreate ? DisplayStyle.Flex : DisplayStyle.None;
if (isCreate) ResetInteractionTimer();
}
private void ResetInteractionTimer()
{
_lastInteractionTime = Time.time;
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 9c37c552a9c18a242bcc8860a0a5212f

View File

@@ -0,0 +1,70 @@
using System;
using System.Collections.Generic;
using UnityEngine;
namespace UI
{
public class LocalizationManager : MonoBehaviour
{
public static LocalizationManager Instance { get; private set; }
private Dictionary<string, string> _localizedText;
private string _currentLanguage = "en";
public event Action OnLanguageChanged;
private void Awake()
{
if (Instance == null)
{
Instance = this;
if (transform.parent == null)
DontDestroyOnLoad(gameObject);
LoadLanguage(_currentLanguage);
}
else
{
Destroy(gameObject);
}
}
public void LoadLanguage(string langCode)
{
TextAsset targetFile = Resources.Load<TextAsset>($"Localization/{langCode}");
if (targetFile != null)
{
// Simple JSON parsing (For production, consider using a proper JSON library like Newtonsoft)
string json = targetFile.text;
_localizedText = ParseJson(json);
_currentLanguage = langCode;
OnLanguageChanged?.Invoke();
}
}
public string Get(string key)
{
if (_localizedText != null && _localizedText.ContainsKey(key))
return _localizedText[key];
return $"[{key}]";
}
private Dictionary<string, string> ParseJson(string json)
{
// Dummy parser for demonstration, replace with JsonUtility if using wrapper class
// or Newtonsoft for direct dictionary parsing
var dict = new Dictionary<string, string>();
string[] lines = json.Split(new[] { ',', '{', '}', '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
foreach (var line in lines)
{
string[] parts = line.Split(':');
if (parts.Length == 2)
{
string key = parts[0].Trim(' ', '"');
string val = parts[1].Trim(' ', '"');
dict[key] = val;
}
}
return dict;
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 5c17a3f09ee49ff48a0e3e2b45080257

View File

@@ -0,0 +1,36 @@
using UnityEngine;
using UnityEngine.UIElements;
namespace UI
{
public class LoungeController : MonoBehaviour
{
private Toggle _readyHost;
private Toggle _readyGuest;
private Button _btnStart;
private void OnEnable()
{
var root = GetComponent<UIDocument>().rootVisualElement;
_readyHost = root.Q<Toggle>("ready-host");
_readyGuest = root.Q<Toggle>("ready-guest");
_btnStart = root.Q<Button>("btn-start");
_readyHost?.RegisterValueChangedCallback(evt => UpdateStartButton());
_readyGuest?.RegisterValueChangedCallback(evt => UpdateStartButton());
_btnStart?.RegisterCallback<ClickEvent>(evt => UIManager.Instance.ShowScreen("HUD"));
root.Q<Button>("btn-back")?.RegisterCallback<ClickEvent>(evt => UIManager.Instance.GoBack());
UpdateStartButton();
}
private void UpdateStartButton()
{
if (_btnStart == null) return;
bool bothReady = (_readyHost != null && _readyHost.value) && (_readyGuest != null && _readyGuest.value);
_btnStart.SetEnabled(bothReady);
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: ac201603ede0899488995be3d88ea0dc

View File

@@ -0,0 +1,136 @@
using UnityEngine;
using UnityEngine.UIElements;
using System.Collections;
using System.Collections.Generic;
namespace UI
{
public class MainMenuController : MonoBehaviour
{
private VisualElement _logoContainer;
private VisualElement _logo;
private VisualElement _ribbon;
private VisualElement _logoPlaceholder;
private bool _isActive = false;
[Header("Animation Settings")]
public float transitionDuration = 0.5f;
public float idleTimeout = 5f;
public float pulseSpeed = 2f;
public float pulseAmount = 0.05f;
private float _lastInteractionTime;
private void OnEnable()
{
var root = GetComponent<UIDocument>().rootVisualElement;
_logoContainer = root.Q<VisualElement>("beat-logo-container");
_logo = root.Q<VisualElement>("beat-logo");
_ribbon = root.Q<VisualElement>("menu-ribbon");
_logoPlaceholder = root.Q<VisualElement>("logo-placeholder");
_logoContainer.RegisterCallback<ClickEvent>(OnLogoClicked);
// Register interactions to reset idle timer
root.RegisterCallback<MouseMoveEvent>(evt => ResetIdleTimer());
var buttons = root.Query<Button>().ToList();
foreach (var btn in buttons)
{
btn.RegisterCallback<ClickEvent>(evt => ResetIdleTimer());
}
// Routing
root.Q<Button>("btn-create")?.RegisterCallback<ClickEvent>(ev => NavigateToLobby(true));
root.Q<Button>("btn-join")?.RegisterCallback<ClickEvent>(ev => NavigateToLobby(false));
root.Q<Button>("btn-settings")?.RegisterCallback<ClickEvent>(ev => UIManager.Instance.ToggleSettings());
root.Q<Button>("btn-profile")?.RegisterCallback<ClickEvent>(ev => UIManager.Instance.ShowScreen("Profile"));
root.Q<Button>("btn-exit")?.RegisterCallback<ClickEvent>(ev => Application.Quit());
ResetToIdleState();
}
private void Update()
{
// 1. Logic Pulsing (Luôn chạy)
float baseScale = _isActive ? 0.38f : 1.0f;
float pulse = Mathf.Sin(Time.time * pulseSpeed) * pulseAmount;
_logo.style.scale = new Scale(new Vector3(baseScale + pulse, baseScale + pulse, 1f));
// 2. Logic Idle Timeout
if (_isActive)
{
if (Time.time - _lastInteractionTime > idleTimeout)
{
StartCoroutine(TransitionToIdle());
}
}
}
private void NavigateToLobby(bool isCreate)
{
var lobby = Object.FindFirstObjectByType<LobbyController>();
lobby?.SetMode(isCreate);
UIManager.Instance.ShowScreen("Lobby");
}
private void OnLogoClicked(ClickEvent evt)
{
ResetIdleTimer();
if (!_isActive) {
// Chỉ chuyển từ Idle sang Active
StartCoroutine(TransitionToActive());
} else {
// Khi đã trong dải Ribbon, nhấn để vào Create Room
NavigateToLobby(true);
}
}
private void ResetIdleTimer()
{
_lastInteractionTime = Time.time;
}
private void ResetToIdleState()
{
_isActive = false;
_ribbon.style.display = DisplayStyle.None;
_logoContainer.style.translate = new Translate(0, 0);
_logoContainer.pickingMode = PickingMode.Position;
}
private IEnumerator TransitionToActive()
{
_isActive = true;
ResetIdleTimer();
_ribbon.style.display = DisplayStyle.Flex;
_ribbon.style.opacity = 0;
_logoContainer.style.transitionProperty = new List<StylePropertyName> { "translate", "opacity" };
_logoContainer.style.transitionDuration = new List<TimeValue> { new TimeValue(transitionDuration, TimeUnit.Second) };
_ribbon.style.transitionProperty = new List<StylePropertyName> { "opacity" };
_ribbon.style.transitionDuration = new List<TimeValue> { new TimeValue(transitionDuration, TimeUnit.Second) };
yield return null;
// Di chuyển logo sang vị trí thứ 2 (-20%)
_logoContainer.style.translate = new Translate(Length.Percent(-20f), 0);
_ribbon.style.opacity = 1;
yield return new WaitForSeconds(transitionDuration);
}
private IEnumerator TransitionToIdle()
{
_isActive = false;
_logoContainer.style.translate = new Translate(0, 0);
_ribbon.style.opacity = 0;
yield return new WaitForSeconds(transitionDuration);
_ribbon.style.display = DisplayStyle.None;
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 691980524acfc544f9660cfc35ce3616

View File

@@ -0,0 +1,14 @@
using UnityEngine;
using UnityEngine.UIElements;
namespace UI
{
public class ProfileController : MonoBehaviour
{
private void OnEnable()
{
var root = GetComponent<UIDocument>().rootVisualElement;
root.Q<Button>("btn-close")?.RegisterCallback<ClickEvent>(ev => UIManager.Instance.GoBack());
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: fdea16b110511ef45889ed832b63560b

View File

@@ -0,0 +1,63 @@
using UnityEngine;
using UnityEngine.UIElements;
using System.Collections.Generic;
namespace UI
{
public class SettingsController : MonoBehaviour
{
private VisualElement _contentGeneral;
private VisualElement _contentGraphics;
private VisualElement _contentAudio;
private VisualElement _contentControls;
private Button _tabGeneral;
private Button _tabGraphics;
private Button _tabAudio;
private Button _tabControls;
private void OnEnable()
{
var root = GetComponent<UIDocument>().rootVisualElement;
// Tabs
_tabGeneral = root.Q<Button>("tab-general");
_tabGraphics = root.Q<Button>("tab-graphics");
_tabAudio = root.Q<Button>("tab-audio");
_tabControls = root.Q<Button>("tab-controls");
// Content
_contentGeneral = root.Q<VisualElement>("content-general");
_contentGraphics = root.Q<VisualElement>("content-graphics");
_contentAudio = root.Q<VisualElement>("content-audio");
_contentControls = root.Q<VisualElement>("content-controls");
// Register Tab Events
_tabGeneral?.RegisterCallback<ClickEvent>(evt => SwitchTab(_contentGeneral, _tabGeneral));
_tabGraphics?.RegisterCallback<ClickEvent>(evt => SwitchTab(_contentGraphics, _tabGraphics));
_tabAudio?.RegisterCallback<ClickEvent>(evt => SwitchTab(_contentAudio, _tabAudio));
_tabControls?.RegisterCallback<ClickEvent>(evt => SwitchTab(_contentControls, _tabControls));
// Close
root.Q<Button>("btn-close")?.RegisterCallback<ClickEvent>(evt => UIManager.Instance.ToggleSettings());
}
private void SwitchTab(VisualElement targetContent, Button targetTab)
{
// Hide all
_contentGeneral.style.display = DisplayStyle.None;
if(_contentGraphics != null) _contentGraphics.style.display = DisplayStyle.None;
if(_contentAudio != null) _contentAudio.style.display = DisplayStyle.None;
if(_contentControls != null) _contentControls.style.display = DisplayStyle.None;
_tabGeneral.RemoveFromClassList("active-tab");
_tabGraphics.RemoveFromClassList("active-tab");
_tabAudio.RemoveFromClassList("active-tab");
_tabControls.RemoveFromClassList("active-tab");
// Show target
targetContent.style.display = DisplayStyle.Flex;
targetTab.AddToClassList("active-tab");
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 5534bcf4869df944883c6fd2a17a6a5a

View File

@@ -0,0 +1,235 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
using System.Linq;
namespace UI
{
public class UIManager : MonoBehaviour
{
public static UIManager Instance { get; private set; }
[System.Serializable]
public class ScreenData
{
public string screenName;
public UIDocument document;
public bool isOverlay;
public bool isActive;
}
public List<ScreenData> screens = new List<ScreenData>();
public string initialScreen = "MainMenu";
[Header("Cursor Settings")]
private VisualElement _customCursor;
private List<VisualElement> _trailPool = new List<VisualElement>();
private int _trailIndex = 0;
public int trailCount = 15;
public float focusRadius = 500f;
[Header("Editor Preview")]
[Range(0f, 1f)]
public float globalOpacity = 1f;
private Stack<string> _navigationStack = new Stack<string>();
private string _currentScreenName;
private VisualElement _lastHoveredElement;
private bool _isSettingsOpen = false;
private void Awake()
{
if (Instance == null) Instance = this;
else { Destroy(gameObject); return; }
SetupCursor();
foreach (var s in screens)
{
if (s.document != null) s.document.rootVisualElement.style.display = DisplayStyle.None;
}
ShowScreen(initialScreen);
}
private void SetupCursor()
{
UIDocument doc = GetComponent<UIDocument>();
if (doc == null && screens.Count > 0) doc = screens[0].document;
if (doc == null) return;
var root = doc.rootVisualElement;
_customCursor = new VisualElement();
_customCursor.style.width = 25;
_customCursor.style.height = 25;
_customCursor.style.backgroundColor = Color.white;
_customCursor.style.borderTopLeftRadius = 13; _customCursor.style.borderTopRightRadius = 13;
_customCursor.style.borderBottomLeftRadius = 13; _customCursor.style.borderBottomRightRadius = 13;
_customCursor.style.position = Position.Absolute;
_customCursor.pickingMode = PickingMode.Ignore;
root.Add(_customCursor);
for (int i = 0; i < trailCount; i++)
{
var trail = new VisualElement();
trail.style.width = 18; trail.style.height = 18;
trail.style.backgroundColor = new Color(1, 1, 1, 0.4f);
trail.style.borderTopLeftRadius = 9; trail.style.borderTopRightRadius = 9;
trail.style.borderBottomLeftRadius = 9; trail.style.borderBottomRightRadius = 9;
trail.style.position = Position.Absolute;
trail.pickingMode = PickingMode.Ignore;
root.Add(trail);
_trailPool.Add(trail);
}
_customCursor.BringToFront();
}
private void Update()
{
// Calculate Virtual Mouse Position
Vector2 mousePos = Input.mousePosition;
bool isMainMenu = (_currentScreenName == "MainMenu");
bool restrictY = (isMainMenu && !_isSettingsOpen);
float targetY = restrictY ? Screen.height / 2f : mousePos.y;
Vector2 uiPos = new Vector2(mousePos.x, Screen.height - targetY);
// Visibility Logic for Cursor & Trail
bool showCursor = !isMainMenu || _isSettingsOpen;
DisplayStyle cursorDisplay = showCursor ? DisplayStyle.Flex : DisplayStyle.None;
if (_customCursor != null)
{
_customCursor.style.display = cursorDisplay;
_customCursor.style.left = uiPos.x - 12.5f;
_customCursor.style.top = uiPos.y - 12.5f;
}
if (_trailPool.Count > 0)
{
var currentTrail = _trailPool[_trailIndex];
currentTrail.style.left = uiPos.x - 9;
currentTrail.style.top = uiPos.y - 9;
currentTrail.style.opacity = 0.5f;
foreach(var t in _trailPool)
{
t.style.display = cursorDisplay;
t.style.opacity = Mathf.Max(0, t.style.opacity.value - Time.deltaTime * 4f);
}
_trailIndex = (_trailIndex + 1) % _trailPool.Count;
}
// Handle Focus & Clicks using virtual coordinates
HandleVirtualInput(uiPos);
if ((Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl)) && Input.GetKeyDown(KeyCode.O))
ToggleSettings();
}
private void HandleVirtualInput(Vector2 uiPos)
{
UIDocument activeDoc = null;
var settings = screens.Find(s => s.screenName == "Settings");
if (_isSettingsOpen) activeDoc = settings.document;
else activeDoc = screens.Find(s => s.screenName == _currentScreenName)?.document;
if (activeDoc == null) return;
VisualElement bestElement = null;
float minDistance = float.MaxValue;
var interactables = activeDoc.rootVisualElement.Query<VisualElement>()
.Where(e => e.focusable && e.pickingMode != PickingMode.Ignore).ToList();
foreach (var element in interactables)
{
Rect worldBounds = element.worldBound;
float dist = Vector2.Distance(uiPos, worldBounds.center);
if (dist < minDistance && dist < focusRadius) {
minDistance = dist;
bestElement = element;
}
}
if (bestElement != _lastHoveredElement)
{
_lastHoveredElement?.RemoveFromClassList("hover");
bestElement?.AddToClassList("hover");
_lastHoveredElement = bestElement;
}
if (Input.GetMouseButtonDown(0) && _lastHoveredElement != null)
{
using (var clickEvent = ClickEvent.GetPooled()) {
clickEvent.target = _lastHoveredElement;
_lastHoveredElement.SendEvent(clickEvent);
}
}
}
// --- Editor Support Methods ---
public void SyncScreens()
{
foreach (var screen in screens)
{
if (screen.document != null && screen.document.rootVisualElement != null)
{
screen.document.rootVisualElement.style.display =
screen.isActive ? DisplayStyle.Flex : DisplayStyle.None;
screen.document.rootVisualElement.style.opacity = globalOpacity;
}
}
}
public void ShowOnly(string name)
{
foreach (var screen in screens)
{
screen.isActive = (screen.screenName == name);
}
SyncScreens();
}
// --- Runtime Logic ---
public void ShowScreen(string name)
{
var screen = screens.Find(s => s.screenName == name);
if (screen == null) return;
if (!screen.isOverlay)
{
foreach(var s in screens) if(!s.isOverlay) s.document.rootVisualElement.style.display = DisplayStyle.None;
_navigationStack.Push(name);
_currentScreenName = name;
}
screen.document.rootVisualElement.style.display = DisplayStyle.Flex;
screen.isActive = true;
UnityEngine.Cursor.visible = false;
}
public void GoBack()
{
if (_navigationStack.Count <= 1) return;
string current = _navigationStack.Pop();
var currentData = screens.Find(s => s.screenName == current);
if (currentData != null) currentData.document.rootVisualElement.style.display = DisplayStyle.None;
_currentScreenName = _navigationStack.Peek();
var prev = screens.Find(s => s.screenName == _currentScreenName);
if (prev != null) prev.document.rootVisualElement.style.display = DisplayStyle.Flex;
}
public void ToggleSettings()
{
var settings = screens.Find(s => s.screenName == "Settings");
if (settings == null) return;
_isSettingsOpen = settings.document.rootVisualElement.style.display == DisplayStyle.None;
settings.document.rootVisualElement.style.display = _isSettingsOpen ? DisplayStyle.Flex : DisplayStyle.None;
settings.isActive = _isSettingsOpen;
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: bcb7b8ed439bb4546b0648c627c2ce5d