This commit is contained in:
2026-04-28 00:07:42 +07:00
parent e051667037
commit 252489f48a
570 changed files with 2423 additions and 10875 deletions

View File

@@ -166,6 +166,7 @@ public class BasicSpawner : MonoBehaviour, INetworkRunnerCallbacks
public void OnSessionListUpdated(NetworkRunner runner, List<SessionInfo> sessionList)
{
if (LobbyManager != null) LobbyManager.DisplayRoomList(sessionList);
// UI.UIEventBus.TriggerRoomListUpdate();
}
public void OnInputMissing(NetworkRunner runner, PlayerRef player, NetworkInput input) { }

View File

@@ -0,0 +1,54 @@
using UnityEngine;
using Fusion;
namespace OnlyScove.Scripts
{
public class PlayerAnimationHandler : NetworkBehaviour
{
[Header("Animator Settings")]
[SerializeField] private string speedParamName = "Speed";
[SerializeField] private string velocityXParamName = "Velocity X";
[SerializeField] private string velocityZParamName = "Velocity Z";
[SerializeField] private float animationDamping = 0.2f;
private Animator anim;
private int speedHash;
private int velocityXHash;
private int velocityZHash;
private bool hasSpeedParam;
private bool hasVelocityXParam;
private bool hasVelocityZParam;
public void Initialize(Animator animator)
{
this.anim = animator;
if (anim != null)
{
foreach (AnimatorControllerParameter param in anim.parameters)
{
if (param.name == speedParamName) hasSpeedParam = true;
if (param.name == velocityXParamName) hasVelocityXParam = true;
if (param.name == velocityZParamName) hasVelocityZParam = true;
}
}
speedHash = Animator.StringToHash(speedParamName);
velocityXHash = Animator.StringToHash(velocityXParamName);
velocityZHash = Animator.StringToHash(velocityZParamName);
}
public void UpdateAnimator(float speed, Vector2 moveInput, float deltaTime)
{
if (anim == null) return;
if (hasSpeedParam) anim.SetFloat(speedHash, speed, animationDamping, deltaTime);
if (hasVelocityXParam) anim.SetFloat(velocityXHash, moveInput.x * speed, animationDamping, deltaTime);
if (hasVelocityZParam) anim.SetFloat(velocityZHash, moveInput.y * speed, animationDamping, deltaTime);
}
public void SetSpeed(float speed)
{
if (anim != null && hasSpeedParam) anim.SetFloat(speedHash, speed);
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 965ec86f3b9695640bdc85e624d0e9e7

View File

@@ -0,0 +1,75 @@
using UnityEngine;
using Fusion;
using System.Collections.Generic;
using System;
namespace OnlyScove.Scripts
{
public class PlayerInteraction : NetworkBehaviour
{
[Header("Interaction Settings")]
[SerializeField] public float InteractionRange = 2f;
[SerializeField] public LayerMask InteractionMask;
public event Action<IInteractable> OnInteractableTargetChanged;
private List<IInteractable> interactablesNearby = new List<IInteractable>();
private int currentInteractableIndex = 0;
private EnvironmentScanner scanner;
public void Initialize(EnvironmentScanner scanner)
{
this.scanner = scanner;
}
public void UpdateInteractables()
{
if (scanner == null) return;
interactablesNearby.Clear();
IInteractable target = scanner.ScanForInteractable(InteractionRange, InteractionMask);
if (target != null) interactablesNearby.Add(target);
OnInteractableTargetChanged?.Invoke(target);
if (Object != null && Object.HasInputAuthority)
{
// UI Placeholder: Interaction UI
// Example: UI.UIEventBus.TriggerInteractionPrompt(target?.InteractionPrompt);
// Example: UI.UIEventBus.TriggerInteractionVisibility(target != null);
}
currentInteractableIndex = 0;
}
public void NextInteract()
{
if (interactablesNearby.Count <= 1) return;
currentInteractableIndex = (currentInteractableIndex + 1) % interactablesNearby.Count;
NotifyTargetChanged();
}
public void PreviousInteract()
{
if (interactablesNearby.Count <= 1) return;
currentInteractableIndex = (currentInteractableIndex - 1 + interactablesNearby.Count) % interactablesNearby.Count;
NotifyTargetChanged();
}
private void NotifyTargetChanged()
{
IInteractable target = GetInteractable();
OnInteractableTargetChanged?.Invoke(target);
if (Object != null && Object.HasInputAuthority)
{
// UI Placeholder: Update Prompt
// Example: UI.UIEventBus.TriggerInteractionPrompt(target?.InteractionPrompt);
}
}
public IInteractable GetInteractable()
{
return (interactablesNearby == null || interactablesNearby.Count == 0) ? null : interactablesNearby[currentInteractableIndex];
}
}
}

View File

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

View File

@@ -0,0 +1,71 @@
using UnityEngine;
using Fusion;
namespace OnlyScove.Scripts
{
public class PlayerMovement : NetworkBehaviour
{
[field: Header("Movement Settings")]
[field: SerializeField] public float WalkSpeed { get; private set; } = 3f;
[field: SerializeField] public float RunSpeed { get; private set; } = 6f;
[field: SerializeField] public float SprintSpeed { get; private set; } = 9f;
[field: SerializeField] public float SneakSpeed { get; private set; } = 1.5f;
[field: SerializeField] public float DashForce { get; private set; } = 10f;
[field: SerializeField] public float RotationSpeed { get; private set; } = 500f;
[field: Header("Airborne Settings")]
[field: SerializeField] public float JumpHeight { get; private set; } = 2f;
[field: SerializeField] public float Gravity { get; private set; } = -15f;
[field: SerializeField] public float ThrustDownwardForce { get; private set; } = -20f;
[field: Header("Ground Check")]
[field: SerializeField] public float GroundCheckRadius { get; private set; } = 0.2f;
[field: SerializeField] public Vector3 GroundCheckOffset { get; private set; }
[field: SerializeField] public LayerMask GroundMask { get; private set; }
[Networked] public bool IsGrounded { get; set; }
[Networked] public bool WasGrounded { get; set; }
[Networked] public float VelocityY { get; set; }
[Networked] public Vector3 NetworkedPosition { get; set; }
private CharacterController controller;
public void Initialize(CharacterController controller)
{
this.controller = controller;
}
public void CheckGround(Transform playerTransform)
{
if (Object == null || (!Object.HasStateAuthority && !Object.HasInputAuthority)) return;
WasGrounded = IsGrounded;
IsGrounded = Physics.CheckSphere(playerTransform.TransformPoint(GroundCheckOffset), GroundCheckRadius, GroundMask);
}
public void Move(CharacterController controller, Vector3 velocity, float deltaTime)
{
if (controller != null && controller.enabled)
{
controller.Move(velocity * deltaTime);
if (Object != null && Object.HasStateAuthority)
{
NetworkedPosition = transform.position;
}
}
}
public void Rotate(Transform playerTransform, Vector3 moveDirection, float deltaTime)
{
if (moveDirection == Vector3.zero) return;
Quaternion targetRot = Quaternion.LookRotation(moveDirection);
playerTransform.rotation = Quaternion.RotateTowards(playerTransform.rotation, targetRot, RotationSpeed * deltaTime);
}
public void SetGroundCheck(float radius, Vector3 offset)
{
GroundCheckRadius = radius;
GroundCheckOffset = offset;
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 79bbcfd4d37b7834ebe0d61bb649714d

View File

@@ -5,6 +5,8 @@ using Fusion;
namespace OnlyScove.Scripts
{
[RequireComponent(typeof(CharacterController), typeof(InputReader), typeof(Animator))]
[RequireComponent(typeof(PlayerStats), typeof(PlayerInteraction), typeof(PlayerMovement))]
[RequireComponent(typeof(PlayerAnimationHandler))]
public class PlayerStateMachine : NetworkBehaviour
{
[field: Header("References")]
@@ -14,61 +16,38 @@ namespace OnlyScove.Scripts
[field: SerializeField] public EnvironmentScanner Scanner { get; private set; }
public CameraController Cam { get; private set; }
[field: Header("Animator Settings")]
[SerializeField] private string speedParamName = "Speed";
[SerializeField] private string velocityXParamName = "Velocity X";
[SerializeField] private string velocityZParamName = "Velocity Z";
private int speedHash;
private int velocityXHash;
private int velocityZHash;
[field: Header("Movement Settings")]
[field: SerializeField] public float WalkSpeed { get; private set; } = 3f;
[field: SerializeField] public float RunSpeed { get; private set; } = 6f;
[field: SerializeField] public float SprintSpeed { get; private set; } = 9f;
[field: SerializeField] public float SneakSpeed { get; private set; } = 1.5f;
[field: SerializeField] public float DashForce { get; private set; } = 10f;
[field: SerializeField] public float RotationSpeed { get; private set; } = 500f;
[field: SerializeField] public float AnimationDamping { get; private set; } = 0.2f;
[field: Header("Airborne Settings")]
[field: SerializeField] public float JumpHeight { get; private set; } = 2f;
[field: SerializeField] public float Gravity { get; private set; } = -15f;
[field: SerializeField] public float ThrustDownwardForce { get; private set; } = -20f;
[field: Header("Ground Check")]
[field: SerializeField] public float GroundCheckRadius { get; private set; } = 0.2f;
[field: SerializeField] public Vector3 GroundCheckOffset { get; private set; }
[field: SerializeField] public LayerMask GroundMask { get; private set; }
[field: Header("Interaction")]
[field: SerializeField] public float InteractionRange { get; private set; } = 2f;
[field: SerializeField] public LayerMask InteractionMask { get; private set; }
[Header("Modules")]
public PlayerStats Stats;
public PlayerInteraction Interaction;
public PlayerMovement Movement;
public PlayerAnimationHandler AnimationHandler;
[Networked] public Quaternion NetworkedCameraRotation { get; set; }
[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;
// Pass-through properties for State Compatibility
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; }
public string CurrentStateName => currentState != null ? currentState.GetType().Name : "None";
public static PlayerStateMachine Local { get; private set; }
public float VelocityY { get => Movement.VelocityY; set => Movement.VelocityY = value; }
public bool IsGrounded => Movement.IsGrounded;
public bool WasGrounded => Movement.WasGrounded;
public float WalkSpeed => Movement.WalkSpeed;
public float RunSpeed => Movement.RunSpeed;
public float SprintSpeed => Movement.SprintSpeed;
public float SneakSpeed => Movement.SneakSpeed;
public float DashForce => Movement.DashForce;
public float JumpHeight => Movement.JumpHeight;
public float ThrustDownwardForce => Movement.ThrustDownwardForce;
public float Gravity => Movement.Gravity;
public float InteractionRange => Interaction.InteractionRange;
public LayerMask InteractionMask => Interaction.InteractionMask;
public static PlayerStateMachine Local { get; private set; }
public string CurrentStateName => currentState != null ? currentState.GetType().Name : "None";
public Quaternion CameraRotation
{
get
@@ -80,11 +59,6 @@ namespace OnlyScove.Scripts
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()
@@ -93,20 +67,15 @@ namespace OnlyScove.Scripts
Input = GetComponent<InputReader>();
Anim = GetComponentInChildren<Animator>();
Scanner = GetComponent<EnvironmentScanner>();
Stats = GetComponent<PlayerStats>();
Interaction = GetComponent<PlayerInteraction>();
Movement = GetComponent<PlayerMovement>();
AnimationHandler = GetComponent<PlayerAnimationHandler>();
if (Anim != null)
{
foreach (AnimatorControllerParameter param in Anim.parameters)
{
if (param.name == speedParamName) hasSpeedParam = true;
if (param.name == velocityXParamName) hasVelocityXParam = true;
if (param.name == velocityZParamName) hasVelocityZParam = true;
}
}
speedHash = Animator.StringToHash(speedParamName);
velocityXHash = Animator.StringToHash(velocityXParamName);
velocityZHash = Animator.StringToHash(velocityZParamName);
AnimationHandler.Initialize(Anim);
Movement.Initialize(Controller);
Interaction.Initialize(Scanner);
}
private void Start()
@@ -123,10 +92,6 @@ namespace OnlyScove.Scripts
}
}
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));
@@ -142,17 +107,15 @@ namespace OnlyScove.Scripts
Cam.followTarget = transform;
Cam.inputReader = Input;
}
Input.OnNextInteractEvent += OnNextInteract;
Input.OnPreviousInteractEvent += OnPreviousInteract;
Input.OnNextInteractEvent += Interaction.NextInteract;
Input.OnPreviousInteractEvent += Interaction.PreviousInteract;
if (Controller != null) Controller.enabled = true;
}
}
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);
Movement.Rotate(transform, moveDirection, deltaTime);
}
public void Move(Vector3 velocity, float animatorSpeed, float deltaTime)
@@ -160,12 +123,8 @@ namespace OnlyScove.Scripts
bool canMove = (Runner == null || !Runner.IsRunning) || Object.HasInputAuthority || Runner.IsServer;
if (!canMove) return;
if (Controller != null && Controller.enabled)
{
Controller.Move(velocity * deltaTime);
if (Object != null && Runner != null && Runner.IsRunning) NetworkedPosition = transform.position;
}
Movement.Move(Controller, velocity, deltaTime);
localAnimatorSpeed = animatorSpeed;
if (Object != null && Object.HasStateAuthority)
{
@@ -177,13 +136,9 @@ namespace OnlyScove.Scripts
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;
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);
AnimationHandler.UpdateAnimator(speedValue, inputVector, deltaTime);
}
public override void FixedUpdateNetwork()
@@ -191,10 +146,11 @@ namespace OnlyScove.Scripts
bool isRunning = Runner != null && Runner.IsRunning;
if (Object == null && isRunning) return;
if (isRunning && NetworkedPosition != Vector3.zero && !Object.HasInputAuthority)
// Proxy Sync
if (isRunning && Movement.NetworkedPosition != Vector3.zero && !Object.HasInputAuthority)
{
Controller.enabled = false;
transform.position = NetworkedPosition;
transform.position = Movement.NetworkedPosition;
Controller.enabled = true;
}
@@ -209,19 +165,13 @@ namespace OnlyScove.Scripts
MoveInput = new Vector2(UnityEngine.Input.GetAxisRaw("Horizontal"), UnityEngine.Input.GetAxisRaw("Vertical"));
IsSprintHeld = UnityEngine.Input.GetKey(KeyCode.LeftShift);
}
else
{
MoveInput = Vector2.zero;
IsSprintHeld = false;
}
if (!isRunning || Object.HasInputAuthority || Runner.IsServer)
{
if (hasControl)
{
WasGrounded = IsGrounded;
CheckGround();
UpdateInteractablesList();
Movement.CheckGround(transform);
Interaction.UpdateInteractables();
currentState?.Tick(isRunning ? Runner.DeltaTime : Time.fixedDeltaTime);
}
}
@@ -236,34 +186,8 @@ namespace OnlyScove.Scripts
if (Runner == null || !Runner.IsRunning) FixedUpdateNetwork();
}
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;
}
private void OnNextInteract()
{
if (interactablesNearby.Count <= 1) return;
currentInteractableIndex = (currentInteractableIndex + 1) % interactablesNearby.Count;
OnInteractableTargetChanged?.Invoke(GetInteractable());
}
private void OnPreviousInteract()
{
if (interactablesNearby.Count <= 1) return;
currentInteractableIndex = (currentInteractableIndex - 1 + interactablesNearby.Count) % interactablesNearby.Count;
OnInteractableTargetChanged?.Invoke(GetInteractable());
}
public IInteractable GetInteractable() => interactablesNearby.Count == 0 ? null : interactablesNearby[currentInteractableIndex];
public void SetGroundCheck(float radius, Vector3 offset) { GroundCheckRadius = radius; GroundCheckOffset = offset; }
public IInteractable GetInteractable() => Interaction.GetInteractable();
public void SetGroundCheck(float radius, Vector3 offset) => Movement.SetGroundCheck(radius, offset);
public void SwitchState(PlayerBaseState newState)
{
@@ -276,13 +200,14 @@ namespace OnlyScove.Scripts
{
hasControl = control;
if (Controller != null) Controller.enabled = control;
if (!control && Anim != null) Anim.SetFloat(speedHash, 0f);
if (!control) AnimationHandler.SetSpeed(0f);
}
private void OnDrawGizmosSelected()
{
if (Movement == null) return;
Gizmos.color = new Color(0, 1, 0, 0.5f);
Gizmos.DrawSphere(transform.TransformPoint(GroundCheckOffset), GroundCheckRadius);
Gizmos.DrawSphere(transform.TransformPoint(Movement.GroundCheckOffset), Movement.GroundCheckRadius);
}
}
}

View File

@@ -0,0 +1,58 @@
using UnityEngine;
using Fusion;
using System;
namespace OnlyScove.Scripts
{
public class PlayerStats : NetworkBehaviour
{
[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 Action<float> OnHealthChanged;
public event Action<float> OnStaminaChanged;
public event Action<float> OnNoiseLevelChanged;
public override void Spawned()
{
// Initial UI sync placeholder
UpdateUI();
}
void OnHealthChangedRender()
{
OnHealthChanged?.Invoke(Health);
if (Object.HasInputAuthority)
{
// UI Placeholder: Trigger Health UI Change
// Example: UI.UIEventBus.TriggerHealthChange(Health / 100f);
}
}
void OnStaminaChangedRender()
{
OnStaminaChanged?.Invoke(Stamina);
if (Object.HasInputAuthority)
{
// UI Placeholder: Trigger Stamina UI Change
// Example: UI.UIEventBus.TriggerStaminaChange(Stamina / 100f);
}
}
void OnNoiseLevelChangedRender()
{
OnNoiseLevelChanged?.Invoke(NoiseLevel);
}
private void UpdateUI()
{
if (Object.HasInputAuthority)
{
OnHealthChangedRender();
OnStaminaChangedRender();
}
}
}
}

View File

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

View File

@@ -0,0 +1,55 @@
using UnityEngine;
using UnityEngine.UIElements;
using PrimeTween;
using System.Threading.Tasks;
namespace Hallucinate.UI
{
public abstract class BaseUIController
{
protected VisualElement root;
protected UIManager uiManager;
public virtual void Initialize(VisualElement uxmlRoot, UIManager manager)
{
root = uxmlRoot;
uiManager = manager;
// Default to hidden
Hide();
}
public virtual void Show()
{
if (root != null)
root.style.display = DisplayStyle.Flex;
}
public virtual void Hide()
{
if (root != null)
root.style.display = DisplayStyle.None;
}
public virtual async Task PlayTransitionIn()
{
if (root == null) return;
Show();
// Fly-in from right using Custom tween for style.translate
root.style.translate = new StyleTranslate(new Translate(Length.Percent(100), 0));
await Tween.Custom(100f, 0f, duration: 0.5f, ease: Ease.OutBack,
onValueChange: val => root.style.translate = new StyleTranslate(new Translate(Length.Percent(val), 0)));
}
public virtual async Task PlayTransitionOut()
{
if (root == null) return;
// Fly-out to left
await Tween.Custom(0f, -100f, duration: 0.5f, ease: Ease.InBack,
onValueChange: val => root.style.translate = new StyleTranslate(new Translate(Length.Percent(val), 0)));
Hide();
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 872f8bfaee91588488a3393579135de9

View File

@@ -1,197 +1,80 @@
using UnityEngine;
using UnityEngine.UIElements;
using OnlyScove.Scripts;
using System.Collections.Generic;
using UnityEngine.InputSystem;
using PrimeTween;
using System.Threading.Tasks;
namespace UI
namespace Hallucinate.UI
{
public class HUDController : MonoBehaviour
public class HUDController : BaseUIController
{
[Header("UI Document")]
public UIDocument hudDocument;
private VisualElement _topLeft;
private VisualElement _bottomLeft;
private ProgressBar _healthBar;
private ProgressBar _staminaBar;
private float _lastActionTime;
private const float FADE_TIMEOUT = 5.0f;
private bool _isFaded = false;
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()
public override void Initialize(VisualElement uxmlRoot, UIManager manager)
{
if (hudDocument == null)
hudDocument = GetComponent<UIDocument>();
base.Initialize(uxmlRoot, manager);
var root = hudDocument.rootVisualElement;
_topLeft = root.Q<VisualElement>("TopLeft");
_bottomLeft = root.Q<VisualElement>("BottomLeft");
_healthBar = root.Q<ProgressBar>("HealthBar");
_staminaBar = root.Q<ProgressBar>("StaminaBar");
_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;
_lastActionTime = Time.time;
}
private void Update()
public void UpdateHUD(float health, float stamina)
{
if (PlayerStateMachine.Local != null)
{
SubscribeToPlayer(PlayerStateMachine.Local);
}
HandleAutoHide();
HandleInventoryInput();
_healthBar.value = health;
_staminaBar.value = stamina;
WakeUpHUD();
}
private void HandleAutoHide()
public void UpdateStats(int ping, int fps)
{
bool inputDetected = false;
root.Q<Label>("PingLabel").text = $"PING: {ping}ms";
root.Q<Label>("FPSLabel").text = $"FPS: {fps}";
}
// 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)
public void WakeUpHUD()
{
_lastActionTime = Time.time;
if (_isFaded)
{
_lastInputTime = Time.time;
SetHUDVisibility(true);
}
else if (Time.time - _lastInputTime > autoHideDelay)
{
SetHUDVisibility(false);
_isFaded = false;
Tween.Custom(_topLeft.style.opacity.value, 1f, duration: 0.3f, onValueChange: val => _topLeft.style.opacity = val);
Tween.Custom(_bottomLeft.style.opacity.value, 1f, duration: 0.3f, onValueChange: val => _bottomLeft.style.opacity = val);
}
}
private void SetHUDVisibility(bool visible)
public void Update()
{
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++)
if (!_isFaded && Time.time - _lastActionTime > FADE_TIMEOUT)
{
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);
_isFaded = true;
Tween.Custom(_topLeft.style.opacity.value, 0.2f, duration: 1.0f, onValueChange: val => _topLeft.style.opacity = val);
Tween.Custom(_bottomLeft.style.opacity.value, 0.2f, duration: 1.0f, onValueChange: val => _bottomLeft.style.opacity = val);
}
}
private void UpdateInteraction(IInteractable interactable)
public override Task PlayTransitionIn()
{
if (_interactionPrompt == null) return;
Show();
_topLeft.style.opacity = 1;
_bottomLeft.style.opacity = 1;
return Task.CompletedTask;
}
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;
}
public override Task PlayTransitionOut()
{
Hide();
return Task.CompletedTask;
}
}
}

View File

@@ -1,75 +1,73 @@
using UnityEngine;
using UnityEngine.UIElements;
using System.Threading.Tasks;
namespace UI
namespace Hallucinate.UI
{
public class LobbyController : MonoBehaviour
public class LobbyController : BaseUIController
{
private VisualElement _joinView;
private VisualElement _createView;
private float _lastInteractionTime;
private bool _isCreateMode = false;
private const float AutoReturnDelay = 5f;
private VisualElement _joinContainer;
private VisualElement _createContainer;
private VisualElement _loungeContainer;
private void OnEnable()
private Button _backBtn;
private Button _createFinalBtn;
private Button _startGameBtn;
private Toggle _hostReady;
private Toggle _guestReady;
public override void Initialize(VisualElement uxmlRoot, UIManager manager)
{
var root = GetComponent<UIDocument>().rootVisualElement;
base.Initialize(uxmlRoot, manager);
_joinView = root.Q<VisualElement>("join-view");
_createView = root.Q<VisualElement>("create-view");
_joinContainer = root.Q<VisualElement>("JoinContainer");
_createContainer = root.Q<VisualElement>("CreateContainer");
_loungeContainer = root.Q<VisualElement>("LoungeContainer");
// Back button
root.Q<Button>("btn-back")?.RegisterCallback<ClickEvent>(evt => UIManager.Instance.GoBack());
root.Q<Button>("btn-settings")?.RegisterCallback<ClickEvent>(evt => UIManager.Instance.ToggleSettings());
_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();
// Create confirm -> Lounge
root.Q<Button>("btn-create-confirm")?.RegisterCallback<ClickEvent>(evt => UIManager.Instance.ShowScreen("Lounge"));
_hostReady.RegisterValueChangedCallback(evt => UpdateStartButton());
_guestReady.RegisterValueChangedCallback(evt => UpdateStartButton());
// 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();
UpdateStartButton();
}
private void Update()
public void ShowJoin()
{
if (_isCreateMode)
_joinContainer.style.display = DisplayStyle.Flex;
_createContainer.style.display = DisplayStyle.None;
_loungeContainer.style.display = DisplayStyle.None;
}
public void ShowCreate()
{
_joinContainer.style.display = DisplayStyle.None;
_createContainer.style.display = DisplayStyle.Flex;
_loungeContainer.style.display = DisplayStyle.None;
}
public void ShowLounge()
{
_joinContainer.style.display = DisplayStyle.None;
_createContainer.style.display = DisplayStyle.None;
_loungeContainer.style.display = DisplayStyle.Flex;
}
private void UpdateStartButton()
{
if (_startGameBtn != null)
{
if (Time.time - _lastInteractionTime > AutoReturnDelay)
{
SetMode(false); // Auto return to Stage 1
}
_startGameBtn.SetEnabled(_hostReady.value && _guestReady.value);
}
}
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

@@ -1,70 +0,0 @@
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

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

View File

@@ -1,36 +0,0 @@
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

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

View File

@@ -1,192 +1,146 @@
using UnityEngine;
using UnityEngine.UIElements;
using System.Collections;
using System.Collections.Generic;
using PrimeTween;
using System.Threading.Tasks;
namespace UI
namespace Hallucinate.UI
{
public class MainMenuController : MonoBehaviour
public class MainMenuController : BaseUIController
{
private VisualElement _root;
private VisualElement _logoContainer;
public enum MenuState { Idle, Ribbon }
private MenuState _currentState = MenuState.Idle;
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 VisualElement _virtualCursor;
private float _lastInteractionTime;
private int _lastClickFrame = -1;
private Coroutine _currentTransition;
private const float IDLE_TIMEOUT = 5.0f;
private void OnEnable()
private Tween _pulseTween;
public override void Initialize(VisualElement uxmlRoot, UIManager manager)
{
_root = GetComponent<UIDocument>().rootVisualElement;
base.Initialize(uxmlRoot, manager);
_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");
_logo = root.Q<VisualElement>("Logo");
_ribbon = root.Q<VisualElement>("Ribbon");
_virtualCursor = root.Q<VisualElement>("VirtualCursor");
// Đảm bảo Logo luôn có thể nhấn được
_logoContainer.pickingMode = PickingMode.Position;
_logo.pickingMode = PickingMode.Position;
_logoContainer.RegisterCallback<ClickEvent>(OnLogoClicked);
if (_logo == null)
{
Debug.LogError($"[MainMenuController] Element 'Logo' not found in UXML! Root children: {root.childCount}");
return;
}
_root.RegisterCallback<MouseMoveEvent>(evt => ResetIdleTimer());
_logo.RegisterCallback<PointerDownEvent>(OnLogoClicked);
// Bind Buttons with null checks
var settingsBtn = root.Q<Button>("SettingsBtn");
if (settingsBtn != null) settingsBtn.clicked += () => uiManager.Push<SettingsController>();
var buttons = _root.Query<Button>().ToList();
foreach (var btn in buttons)
{
btn.RegisterCallback<PointerDownEvent>(evt => ApplyDrumHit(btn, true));
btn.RegisterCallback<PointerUpEvent>(evt => ApplyDrumHit(btn, false));
btn.RegisterCallback<ClickEvent>(evt => ResetIdleTimer());
}
var joinBtn = root.Q<Button>("JoinBtn");
if (joinBtn != null) joinBtn.clicked += () => uiManager.Push<LobbyController>();
var createBtn = root.Q<Button>("CreateBtn");
if (createBtn != null) createBtn.clicked += () => uiManager.Push<LobbyController>();
var profileBtn = root.Q<Button>("ProfileBtn");
if (profileBtn != null) profileBtn.clicked += () => uiManager.Push<ProfileController>();
var exitBtn = root.Q<Button>("ExitBtn");
if (exitBtn != null) exitBtn.clicked += () => Application.Quit();
_logoContainer.RegisterCallback<PointerDownEvent>(evt => ApplyDrumHit(_logoContainer, true));
_logoContainer.RegisterCallback<PointerUpEvent>(evt => ApplyDrumHit(_logoContainer, false));
// 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()
{
float baseScale = _isActive ? 0.35f : 1.0f;
float pulse = Mathf.Sin(Time.time * pulseSpeed) * pulseAmount;
_logo.style.scale = new Scale(new Vector3(baseScale + pulse, baseScale + pulse, 1f));
if (_isActive && _currentTransition == null)
{
if (Time.time - _lastInteractionTime > idleTimeout)
_currentTransition = StartCoroutine(TransitionToIdle());
}
}
private void OnLogoClicked(ClickEvent evt)
{
if (Time.frameCount == _lastClickFrame) return;
_lastClickFrame = Time.frameCount;
ResetIdleTimer();
// QUAN TRỌNG: Chỉ vào Lobby nếu ĐÃ Active và KHÔNG đang chuyển cảnh
if (!_isActive) {
if (_currentTransition != null) StopCoroutine(_currentTransition);
_currentTransition = StartCoroutine(TransitionToActive());
} else if (_currentTransition == null) {
NavigateToLobby(true);
}
}
private void ApplyDrumHit(VisualElement element, bool isDown)
{
if (isDown)
{
element.style.scale = new Scale(new Vector3(0.85f, 0.85f, 1f));
element.style.transitionDuration = new List<TimeValue> { new TimeValue(0.05f, TimeUnit.Second) };
}
else
{
element.style.scale = new Scale(Vector3.one);
element.style.transitionDuration = new List<TimeValue> { new TimeValue(0.15f, TimeUnit.Second) };
}
}
private void ResetIdleTimer()
{
StartPulse();
_lastInteractionTime = Time.time;
}
private void NavigateToLobby(bool isCreate)
public override async Task PlayTransitionIn()
{
var lobby = Object.FindFirstObjectByType<LobbyController>();
lobby?.SetMode(isCreate);
UIManager.Instance.ShowScreen("Lobby");
await base.PlayTransitionIn();
UnityEngine.Cursor.visible = false;
}
private void ResetToIdleState()
public override async Task PlayTransitionOut()
{
_isActive = false;
UIManager.Instance.isMainMenuActive = false;
_ribbon.style.display = DisplayStyle.None;
UnityEngine.Cursor.visible = true;
await base.PlayTransitionOut();
}
private void StartPulse()
{
// Use Vector3.one * 1.1f for target scale
_pulseTween = Tween.Scale(_logo.transform, Vector3.one * 1.1f, duration: 0.8f, cycles: -1, cycleMode: CycleMode.Yoyo, ease: Ease.InOutSine);
}
private void OnLogoClicked(PointerDownEvent evt)
{
_lastInteractionTime = Time.time;
if (_currentState == MenuState.Idle)
{
TransitionToRibbon();
}
else
{
_ = uiManager.Push<LobbyController>();
}
}
private void TransitionToRibbon()
{
_currentState = MenuState.Ribbon;
_root.Add(_logoContainer);
_logoContainer.style.position = Position.Absolute;
_logoContainer.style.width = 300; _logoContainer.style.height = 300;
_logoContainer.style.left = Length.Percent(50);
_logoContainer.style.top = Length.Percent(50);
_logoContainer.style.translate = new Translate(Length.Percent(-50), Length.Percent(-50));
_currentTransition = null;
}
private IEnumerator TransitionToActive()
{
_isActive = true;
UIManager.Instance.isMainMenuActive = true;
ResetIdleTimer();
// Transition Logo using Custom tween for offset
Tween.Custom(0f, -300f, duration: 0.5f, ease: Ease.OutQuad,
onValueChange: val => _logo.style.left = val);
_ribbon.style.display = DisplayStyle.Flex;
_ribbon.style.opacity = 0;
yield return null;
_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) };
// Trượt Logo từ tâm sang vị trí ribbon (#2)
_logoContainer.style.translate = new Translate(Length.Percent(-75f), Length.Percent(-50f));
_ribbon.style.opacity = 1;
yield return new WaitForSeconds(transitionDuration);
// Gán chặt vào placeholder và khóa kích thước để chống giãn
_logoPlaceholder.Add(_logoContainer);
_logoContainer.style.position = Position.Relative;
_logoContainer.style.left = StyleKeyword.Auto;
_logoContainer.style.top = StyleKeyword.Auto;
_logoContainer.style.translate = new Translate(0, 0);
// Khóa kích thước nhỏ để fit vào Ribbon
_logoContainer.style.width = 100;
_logoContainer.style.height = 100;
_currentTransition = null;
// Fade in Ribbon
Tween.Custom(0f, 1f, duration: 0.5f, onValueChange: val => _ribbon.style.opacity = val);
}
private IEnumerator TransitionToIdle()
private void TransitionToIdle()
{
_isActive = false;
UIManager.Instance.isMainMenuActive = false;
_currentState = MenuState.Idle;
Tween.Custom(-300f, 0f, duration: 0.5f, ease: Ease.OutQuad,
onValueChange: val => _logo.style.left = val);
_root.Add(_logoContainer);
_logoContainer.style.position = Position.Absolute;
_logoContainer.style.width = 300; _logoContainer.style.height = 300;
_logoContainer.style.left = Length.Percent(50);
_logoContainer.style.top = Length.Percent(50);
_logoContainer.style.translate = new Translate(Length.Percent(-75f), Length.Percent(-50f));
Tween.Custom(1f, 0f, duration: 0.5f, onValueChange: val => _ribbon.style.opacity = val)
.OnComplete(() => _ribbon.style.display = DisplayStyle.None);
}
yield return null;
public void Update()
{
UpdateVirtualCursor();
_logoContainer.style.translate = new Translate(Length.Percent(-50f), Length.Percent(-50f));
_ribbon.style.opacity = 0;
if (_currentState == MenuState.Ribbon)
{
if (Time.time - _lastInteractionTime > IDLE_TIMEOUT)
{
TransitionToIdle();
}
}
}
yield return new WaitForSeconds(transitionDuration);
_ribbon.style.display = DisplayStyle.None;
_currentTransition = null;
private void UpdateVirtualCursor()
{
if (_virtualCursor == null) return;
Vector2 mousePos = Input.mousePosition;
float x = mousePos.x;
float y = Screen.height - mousePos.y;
if (_currentState == MenuState.Ribbon)
{
y = Screen.height / 2f + 50;
}
_virtualCursor.style.left = x - _virtualCursor.layout.width / 2;
_virtualCursor.style.top = y - _virtualCursor.layout.height / 2;
}
}
}

View File

@@ -1,75 +0,0 @@
using OnlyScove.Scripts;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
namespace UI
{
public class MyUIDisplay : MonoBehaviour
{
[Header("References")]
public PlayerDebugProvider playerDebugProvider;
[Header("UI Prompt")]
public GameObject interactionPromptContainer;
public UnityEngine.UI.Text interactionPromptText;
private void Start()
{
//if (playerDebugProvider == null)
//playerDebugProvider = FindFirstObjectByType<PlayerDebugProvider>();
// Luôn ẩn lúc bắt đầu
if (interactionPromptContainer != null)
interactionPromptContainer.SetActive(false);
TryFindPlayer();
}
private void Update()
{
if (playerDebugProvider == null)
{
TryFindPlayer();
if (playerDebugProvider == null) return;
}
if (interactionPromptContainer == null) return;
IInteractable interactable = playerDebugProvider.GetActiveInteractable();
if (interactable != null)
{
// Hiện UI tại vị trí cố định bạn đã đặt trong Canvas
interactionPromptContainer.SetActive(true);
if (!interactionPromptContainer.activeSelf)
{
interactionPromptContainer.SetActive(true);
}
if (interactionPromptText != null)
interactionPromptText.text = interactable.InteractionPrompt;
}
else
{
if (interactionPromptContainer.activeSelf)
interactionPromptContainer.SetActive(false);
}
}
private void TryFindPlayer()
{
if (PlayerStateMachine.Local != null)
{
playerDebugProvider = PlayerStateMachine.Local.GetComponent<PlayerDebugProvider>();
}
if (playerDebugProvider == null)
{
playerDebugProvider = Object.FindFirstObjectByType<PlayerDebugProvider>();
}
}
}
}

View File

@@ -1,3 +0,0 @@
fileFormatVersion: 2
guid: cd13c5c96000414397dd7d41a73edd62
timeCreated: 1773383951

View File

@@ -1,14 +1,37 @@
using UnityEngine;
using UnityEngine.UIElements;
using System.Threading.Tasks;
namespace UI
namespace Hallucinate.UI
{
public class ProfileController : MonoBehaviour
public class ProfileController : BaseUIController
{
private void OnEnable()
private Label _username;
private Label _rank;
private ProgressBar _winRateBar;
private Label _winRateText;
public override void Initialize(VisualElement uxmlRoot, UIManager manager)
{
var root = GetComponent<UIDocument>().rootVisualElement;
root.Q<Button>("btn-close")?.RegisterCallback<ClickEvent>(ev => UIManager.Instance.GoBack());
base.Initialize(uxmlRoot, manager);
_username = root.Q<Label>("Username");
_rank = root.Q<Label>("Rank");
_winRateBar = root.Q<ProgressBar>("WinRateBar");
_winRateText = root.Q<Label>("WinRateText");
root.Q<Button>("BackBtn").clicked += () => uiManager.Pop();
LoadProfileData();
}
private void LoadProfileData()
{
// Dummy data for now
_username.text = "GamerPro_2026";
_rank.text = "DIAMOND II";
_winRateBar.value = 72;
_winRateText.text = "72%";
}
}
}

View File

@@ -1,63 +1,52 @@
using UnityEngine;
using UnityEngine.UIElements;
using System.Collections.Generic;
using PrimeTween;
using System.Threading.Tasks;
namespace UI
namespace Hallucinate.UI
{
public class SettingsController : MonoBehaviour
public class SettingsController : BaseUIController
{
private VisualElement _contentGeneral;
private VisualElement _contentGraphics;
private VisualElement _contentAudio;
private VisualElement _contentControls;
private VisualElement _sidebar;
private Label _tabTitle;
private ScrollView _content;
private Button _tabGeneral;
private Button _tabGraphics;
private Button _tabAudio;
private Button _tabControls;
private void OnEnable()
public override void Initialize(VisualElement uxmlRoot, UIManager manager)
{
var root = GetComponent<UIDocument>().rootVisualElement;
base.Initialize(uxmlRoot, manager);
// 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");
_sidebar = root.Q<VisualElement>("Sidebar");
_tabTitle = root.Q<Label>("TabTitle");
_content = root.Q<ScrollView>("SettingsContent");
// 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());
root.Q<Button>("GeneralTab").clicked += () => SwitchTab("GENERAL");
root.Q<Button>("VideoTab").clicked += () => SwitchTab("VIDEO");
root.Q<Button>("SoundTab").clicked += () => SwitchTab("SOUND");
root.Q<Button>("ControlTab").clicked += () => SwitchTab("CONTROL");
root.Q<Button>("CloseSettingsBtn").clicked += () => uiManager.Pop();
}
private void SwitchTab(VisualElement targetContent, Button targetTab)
private void SwitchTab(string title)
{
// 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;
_tabTitle.text = title;
// Clear and add specific settings content
_content.Clear();
_content.Add(new Label($"Settings for {title} coming soon..."));
}
_tabGeneral.RemoveFromClassList("active-tab");
_tabGraphics.RemoveFromClassList("active-tab");
_tabAudio.RemoveFromClassList("active-tab");
_tabControls.RemoveFromClassList("active-tab");
public override async Task PlayTransitionIn()
{
Show();
_sidebar.style.translate = new StyleTranslate(new Translate(Length.Percent(-100), 0));
await Tween.Custom(-100f, 0f, duration: 0.4f, ease: Ease.OutQuad,
onValueChange: val => _sidebar.style.translate = new StyleTranslate(new Translate(Length.Percent(val), 0)));
}
// Show target
targetContent.style.display = DisplayStyle.Flex;
targetTab.AddToClassList("active-tab");
public override async Task PlayTransitionOut()
{
await Tween.Custom(0f, -100f, duration: 0.4f, ease: Ease.InQuad,
onValueChange: val => _sidebar.style.translate = new StyleTranslate(new Translate(Length.Percent(val), 0)));
Hide();
}
}
}

View File

@@ -1,246 +1,126 @@
using System.Collections;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.UIElements;
using System.Linq;
namespace UI
namespace Hallucinate.UI
{
[RequireComponent(typeof(UIDocument))]
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;
}
private UIDocument _uiDocument;
private VisualElement _rootElement;
public List<ScreenData> screens = new List<ScreenData>();
public string initialScreen = "MainMenu";
[Header("Cursor & Trail Settings")]
public Sprite trailSprite;
public float trailFadeSpeed = 3f;
public int trailCount = 15;
public float focusRadius = 500f;
private readonly Dictionary<Type, BaseUIController> _controllers = new Dictionary<Type, BaseUIController>();
private readonly Stack<BaseUIController> _history = new Stack<BaseUIController>();
private VisualElement _customCursor;
private List<VisualElement> _trailPool = new List<VisualElement>();
private int _trailIndex = 0;
[Header("UI Templates")]
[SerializeField] private VisualTreeAsset mainMenuTemplate;
[SerializeField] private VisualTreeAsset lobbyTemplate;
[SerializeField] private VisualTreeAsset profileTemplate;
[SerializeField] private VisualTreeAsset settingsTemplate;
[SerializeField] private VisualTreeAsset hudTemplate;
[Header("Editor Preview")]
[Range(0f, 1f)]
public float globalOpacity = 1f;
[Header("Debug Settings")]
[SerializeField] private bool showDebugInfo = true;
private Stack<string> _navigationStack = new Stack<string>();
private string _currentScreenName;
private VisualElement _lastHoveredElement;
public bool isMainMenuActive = false;
private bool _isSettingsOpen = false;
private MainMenuController _mainMenuController;
private LobbyController _lobbyController;
private ProfileController _profileController;
private SettingsController _settingsController;
private HUDController _hudController;
private void Awake()
{
if (Instance == null) Instance = this;
else { Destroy(gameObject); return; }
var myDoc = GetComponent<UIDocument>();
if (myDoc != null) myDoc.sortingOrder = 1000;
SetupCursor();
foreach (var s in screens)
if (Instance == null)
{
if (s.document != null) s.document.rootVisualElement.style.display = DisplayStyle.None;
Instance = this;
DontDestroyOnLoad(gameObject);
}
ShowScreen(initialScreen);
else
{
Destroy(gameObject);
return;
}
_uiDocument = GetComponent<UIDocument>();
_rootElement = _uiDocument.rootVisualElement;
InitializeControllers();
}
private void SetupCursor()
private void InitializeControllers()
{
UIDocument doc = GetComponent<UIDocument>();
if (doc == null && screens.Count > 0) doc = screens[0].document;
if (doc == null) return;
var root = doc.rootVisualElement;
_mainMenuController = RegisterController<MainMenuController>(mainMenuTemplate);
_lobbyController = RegisterController<LobbyController>(lobbyTemplate);
_profileController = RegisterController<ProfileController>(profileTemplate);
_settingsController = RegisterController<SettingsController>(settingsTemplate);
_hudController = RegisterController<HUDController>(hudTemplate);
_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);
// Start with Main Menu
_ = Push<MainMenuController>();
}
for (int i = 0; i < trailCount; i++)
private T RegisterController<T>(VisualTreeAsset template) where T : BaseUIController, new()
{
if (template == null)
{
var trail = new VisualElement();
trail.style.width = 20; trail.style.height = 20;
if (trailSprite != null)
{
trail.style.backgroundImage = new StyleBackground(trailSprite);
trail.style.backgroundColor = Color.clear;
}
else
{
trail.style.backgroundColor = new Color(1, 1, 1, 0.4f);
trail.style.borderTopLeftRadius = 10; trail.style.borderTopRightRadius = 10;
trail.style.borderBottomLeftRadius = 10; trail.style.borderBottomRightRadius = 10;
}
trail.style.position = Position.Absolute;
trail.pickingMode = PickingMode.Ignore;
root.Add(trail);
_trailPool.Add(trail);
Debug.LogWarning($"Template for {typeof(T).Name} is missing!");
return null;
}
_customCursor.BringToFront();
var instance = template.Instantiate();
instance.style.flexGrow = 1;
instance.style.position = Position.Absolute;
instance.style.width = Length.Percent(100);
instance.style.height = Length.Percent(100);
_rootElement.Add(instance);
var controller = new T();
controller.Initialize(instance, this);
_controllers[typeof(T)] = controller;
return controller;
}
private void Update()
{
Vector2 mousePos = Input.mousePosition;
bool isMainMenu = (_currentScreenName == "MainMenu");
bool restrictY = (isMainMenu && isMainMenuActive && !_isSettingsOpen);
float targetY = restrictY ? Screen.height / 2f : mousePos.y;
Vector2 uiPos = new Vector2(mousePos.x, Screen.height - targetY);
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.display = cursorDisplay;
currentTrail.style.left = uiPos.x - 10;
currentTrail.style.top = uiPos.y - 10;
currentTrail.style.opacity = 0.6f;
foreach(var t in _trailPool)
{
float currentOp = t.style.opacity.value;
if (currentOp > 0) t.style.opacity = Mathf.Max(0, currentOp - Time.deltaTime * trailFadeSpeed);
else t.style.display = DisplayStyle.None;
}
_trailIndex = (_trailIndex + 1) % _trailPool.Count;
}
HandleVirtualInput(uiPos);
if ((Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl)) && Input.GetKeyDown(KeyCode.O))
ToggleSettings();
_mainMenuController?.Update();
_hudController?.Update();
}
private void HandleVirtualInput(Vector2 uiPos)
public async Task Push<T>() where T : BaseUIController
{
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)
if (!_controllers.TryGetValue(typeof(T), out var newScreen))
{
Rect worldBounds = element.worldBound;
float dist = Vector2.Distance(uiPos, worldBounds.center);
if (dist < minDistance && dist < focusRadius) {
minDistance = dist;
bestElement = element;
}
Debug.LogError($"Controller of type {typeof(T)} not registered!");
return;
}
if (bestElement != _lastHoveredElement)
if (_history.Count > 0)
{
_lastHoveredElement?.RemoveFromClassList("hover");
bestElement?.AddToClassList("hover");
_lastHoveredElement = bestElement;
var currentScreen = _history.Peek();
await currentScreen.PlayTransitionOut();
}
if (Input.GetMouseButtonDown(0) && _lastHoveredElement != null)
{
using (var clickEvent = ClickEvent.GetPooled()) {
clickEvent.target = _lastHoveredElement;
_lastHoveredElement.SendEvent(clickEvent);
}
}
_history.Push(newScreen);
await newScreen.PlayTransitionIn();
}
// --- Editor Support Methods (Restored) ---
public void SyncScreens()
public async Task Pop()
{
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;
}
}
if (_history.Count <= 1) return;
var currentScreen = _history.Pop();
await currentScreen.PlayTransitionOut();
var previousScreen = _history.Peek();
await previousScreen.PlayTransitionIn();
}
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 != null) 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 prevData = screens.Find(s => s.screenName == _currentScreenName);
if (prevData != null) prevData.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;
if (_isSettingsOpen) settings.document.sortingOrder = 999;
}
// Custom Inspector features can be added here with [ContextMenu] or CustomEditor
}
}