Files
BABA_YAGA/Assets/Scripts/Player Controller/PlayerDashState.cs
2026-04-15 19:53:29 +07:00

85 lines
3.0 KiB
C#

using UnityEngine;
namespace OnlyScove.Scripts
{
public class PlayerDashState : PlayerBaseState
{
private readonly int dashHash = Animator.StringToHash("Dash"); // Trigger parameter
private float dashDuration = 0.25f; // How long the burst lasts (tweak as needed)
private float dashTimer;
private Vector3 dashDirection;
public PlayerDashState(PlayerStateMachine stateMachine) : base(stateMachine) {}
public override void Enter()
{
dashTimer = dashDuration;
// Fire the Dash animation trigger
stateMachine.Anim.SetTrigger(dashHash);
stateMachine.Input.OnJumpEvent += OnJump;
// ĐÚNG: Sử dụng MoveInput đã đồng bộ mạng (Quan trọng để Client không bị phụ thuộc vào phím của Host)
Vector2 input = stateMachine.MoveInput;
if (input != Vector2.zero)
{
dashDirection = new Vector3(input.x, 0f, input.y).normalized;
dashDirection = stateMachine.CameraRotation * dashDirection;
dashDirection.y = 0;
dashDirection.Normalize();
// Instantly snap rotation to face the dash direction
stateMachine.transform.rotation = Quaternion.LookRotation(dashDirection);
}
else
{
dashDirection = stateMachine.transform.forward;
}
}
public override void Tick(float deltaTime)
{
dashTimer -= deltaTime;
// Apply high speed for the dash (Burst speed)
// Sử dụng Move tập trung để đồng bộ mạng
stateMachine.Move(dashDirection * stateMachine.SprintSpeed, 1.0f, deltaTime);
// When the dash finishes, decide the next state
if (dashTimer <= 0f)
{
if (stateMachine.IsSprintHeld && stateMachine.MoveInput != Vector2.zero)
{
// Kept holding Shift -> Go to Sprint
stateMachine.SwitchState(new PlayerRunState(stateMachine));
}
else if (stateMachine.MoveInput != Vector2.zero)
{
// Released Shift but still moving -> Go to Walk
stateMachine.SwitchState(new PlayerMoveState(stateMachine));
}
else
{
// Stopped moving -> Go to Idle
stateMachine.SwitchState(new PlayerIdleState(stateMachine));
}
}
}
public override void PhysicsTick(float fixedDeltaTime) {}
public override void Exit()
{
stateMachine.Input.OnJumpEvent -= OnJump;
}
private void OnJump()
{
if (stateMachine.IsGrounded)
{
stateMachine.SwitchState(new PlayerJumpState(stateMachine, stateMachine.SprintSpeed));
}
}
}
}