75 lines
2.5 KiB
C#
75 lines
2.5 KiB
C#
using UnityEngine;
|
|
|
|
namespace OnlyScove.Scripts
|
|
{
|
|
public class PlayerJumpState : PlayerBaseState
|
|
{
|
|
private readonly int jumpHash = Animator.StringToHash("Jump");
|
|
private readonly int speedHash = Animator.StringToHash("Speed");
|
|
private readonly int speedXHash = Animator.StringToHash("Velocity X");
|
|
private readonly int speedZHash = Animator.StringToHash("Velocity Z");
|
|
|
|
private float jumpSpeed;
|
|
|
|
public PlayerJumpState(PlayerStateMachine stateMachine, float jumpSpeed = -1f) : base(stateMachine)
|
|
{
|
|
if (jumpSpeed < 0)
|
|
{
|
|
this.jumpSpeed = stateMachine.WalkSpeed;
|
|
}
|
|
else
|
|
{
|
|
this.jumpSpeed = jumpSpeed;
|
|
}
|
|
}
|
|
|
|
public override void Enter()
|
|
{
|
|
Debug.Log("<color=yellow>[PlayerJumpState]</color> Entered Jump State");
|
|
|
|
stateMachine.AnimationHandler.SafeResetTrigger(jumpHash);
|
|
stateMachine.AnimationHandler.SafeSetTrigger(jumpHash);
|
|
stateMachine.AnimationHandler.SafeSetBool("IsJumping", true);
|
|
|
|
// Sử dụng hàm Jump tập trung để đồng bộ vật lý và cooldown
|
|
stateMachine.Movement.Jump();
|
|
|
|
stateMachine.Input.OnSprintEvent += OnAirDash;
|
|
}
|
|
|
|
public override void Tick(float deltaTime)
|
|
{
|
|
stateMachine.VelocityY += stateMachine.Gravity * deltaTime;
|
|
|
|
Vector2 input = stateMachine.MoveInput;
|
|
Vector3 inputDir = new Vector3(input.x, 0, input.y).normalized;
|
|
Vector3 moveDirection = stateMachine.CameraRotation * inputDir;
|
|
moveDirection.y = 0;
|
|
moveDirection.Normalize();
|
|
|
|
Vector3 velocity = moveDirection * jumpSpeed;
|
|
velocity.y = stateMachine.VelocityY;
|
|
|
|
// Đồng bộ di chuyển và xoay
|
|
stateMachine.Move(velocity, 1.0f, deltaTime);
|
|
stateMachine.Rotate(moveDirection, deltaTime);
|
|
|
|
if (stateMachine.VelocityY <= 0f)
|
|
{
|
|
stateMachine.SwitchState(new PlayerFallState(stateMachine, jumpSpeed));
|
|
}
|
|
}
|
|
|
|
private void OnAirDash()
|
|
{
|
|
stateMachine.SwitchState(new PlayerAirDashState(stateMachine));
|
|
}
|
|
|
|
public override void PhysicsTick(float fixedDeltaTime) {}
|
|
|
|
public override void Exit()
|
|
{
|
|
stateMachine.Input.OnSprintEvent -= OnAirDash;
|
|
}
|
|
}
|
|
} |