86 lines
2.9 KiB
C#
86 lines
2.9 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;
|
||
|
|
|
||
|
|
// Determine dash direction based on input, or default to forward if no input
|
||
|
|
Vector2 input = stateMachine.Input.MoveInput;
|
||
|
|
if (input != Vector2.zero)
|
||
|
|
{
|
||
|
|
dashDirection = new Vector3(input.x, 0f, input.y).normalized;
|
||
|
|
|
||
|
|
if (stateMachine.Cam != null)
|
||
|
|
{
|
||
|
|
dashDirection = stateMachine.Cam.PlanarRotation * dashDirection;
|
||
|
|
}
|
||
|
|
|
||
|
|
// 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)
|
||
|
|
stateMachine.Controller.Move(dashDirection * stateMachine.SprintSpeed * deltaTime);
|
||
|
|
|
||
|
|
// When the dash finishes, decide the next state
|
||
|
|
if (dashTimer <= 0f)
|
||
|
|
{
|
||
|
|
if (stateMachine.Input.IsSprintHeld && stateMachine.Input.MoveInput != Vector2.zero)
|
||
|
|
{
|
||
|
|
// Kept holding Shift -> Go to Sprint
|
||
|
|
stateMachine.SwitchState(new PlayerRunState(stateMachine));
|
||
|
|
}
|
||
|
|
else if (stateMachine.Input.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));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|