75 lines
2.4 KiB
C#
75 lines
2.4 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()
|
|
{
|
|
// Sử dụng dữ liệu đồng bộ
|
|
Vector2 input = stateMachine.MoveInput;
|
|
|
|
stateMachine.Anim.ResetTrigger(jumpHash);
|
|
stateMachine.Anim.SetTrigger(jumpHash);
|
|
|
|
// Physic formula: v = sqrt(h * -2 * g)
|
|
stateMachine.VelocityY = Mathf.Sqrt(stateMachine.JumpHeight * -2f * stateMachine.Gravity);
|
|
|
|
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;
|
|
}
|
|
}
|
|
} |