using UnityEngine; namespace OnlyScove.Scripts { public class PlayerMoveState : PlayerBaseState { private readonly int speedHash = Animator.StringToHash("Speed"); public PlayerMoveState(PlayerStateMachine stateMachine) : base(stateMachine) {} public override void Enter() { stateMachine.Input.OnJumpEvent += OnJump; stateMachine.Input.OnDodgeEvent += OnDodge; stateMachine.Input.OnCrouchEvent += OnCrouch; stateMachine.Input.OnInteractEvent += OnInteract; } private readonly int speedXHash = Animator.StringToHash("Velocity X"); private readonly int speedZHash = Animator.StringToHash("Velocity Z"); public override void Tick(float deltaTime) { // QUAN TRỌNG: Đọc trực tiếp từ stateMachine (Dữ liệu đã đồng bộ mạng) Vector2 input = stateMachine.MoveInput; float moveAmount = Mathf.Clamp01(Mathf.Abs(input.x) + Mathf.Abs(input.y)); if (moveAmount <= 0.01f) { stateMachine.SwitchState(new PlayerIdleState(stateMachine)); return; } if (stateMachine.IsSprintHeld) { stateMachine.SwitchState(new PlayerDashState(stateMachine)); return; } Vector3 inputDir = new Vector3(input.x, 0, input.y).normalized; Vector3 moveDirection = stateMachine.CameraRotation * inputDir; moveDirection.y = 0; moveDirection.Normalize(); Vector3 velocity = moveDirection * stateMachine.WalkSpeed; if (stateMachine.IsGrounded && stateMachine.VelocityY < 0) { stateMachine.VelocityY = -2f; } else { stateMachine.VelocityY += stateMachine.Gravity * deltaTime; } velocity.y = stateMachine.VelocityY; // Sử dụng hàm Move tập trung (0.7f là giá trị Speed cho Animator khi đi bộ) stateMachine.Move(velocity, 0.7f, deltaTime); stateMachine.Rotate(moveDirection, deltaTime); } public override void PhysicsTick(float fixedDeltaTime) {} public override void Exit() { stateMachine.Input.OnJumpEvent -= OnJump; stateMachine.Input.OnDodgeEvent -= OnDodge; stateMachine.Input.OnCrouchEvent -= OnCrouch; stateMachine.Input.OnInteractEvent -= OnInteract; } private void OnJump() { if (stateMachine.IsGrounded) { if (stateMachine.Scanner != null) { var hitData = stateMachine.Scanner.ObstacleCheck(); if (hitData.forwardHitFound) { stateMachine.SwitchState(new PlayerParkourState(stateMachine)); return; } } stateMachine.SwitchState(new PlayerJumpState(stateMachine, stateMachine.WalkSpeed)); } } private void OnDodge() => stateMachine.SwitchState(new PlayerDodgeState(stateMachine)); private void OnCrouch() => stateMachine.SwitchState(new PlayerCrouchState(stateMachine)); private void OnInteract() => stateMachine.SwitchState(new PlayerInteractState(stateMachine)); } }