91 lines
3.1 KiB
C#
91 lines
3.1 KiB
C#
using UnityEngine;
|
|
|
|
namespace OnlyScove.Scripts
|
|
{
|
|
public class PlayerFallState : PlayerBaseState
|
|
{
|
|
private readonly int fallHash = Animator.StringToHash("Fall");
|
|
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 fallSpeed;
|
|
|
|
public PlayerFallState(PlayerStateMachine stateMachine, float fallSpeed = -1f) : base(stateMachine)
|
|
{
|
|
if (fallSpeed < 0)
|
|
{
|
|
this.fallSpeed = stateMachine.WalkSpeed;
|
|
}
|
|
else
|
|
{
|
|
this.fallSpeed = fallSpeed;
|
|
}
|
|
}
|
|
|
|
public override void Enter()
|
|
{
|
|
stateMachine.Anim.SetTrigger(fallHash);
|
|
stateMachine.Input.OnDodgeEvent += OnThrustPressed;
|
|
stateMachine.Input.OnSprintEvent += OnAirDash;
|
|
}
|
|
|
|
public override void Tick(float deltaTime)
|
|
{
|
|
stateMachine.VelocityY += stateMachine.Gravity * deltaTime;
|
|
|
|
// ĐÚNG: Sử dụng MoveInput đã đồng bộ mạng
|
|
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 * fallSpeed;
|
|
velocity.y = stateMachine.VelocityY;
|
|
|
|
// Sử dụng hàm Move tập trung của StateMachine để đồng bộ
|
|
stateMachine.Move(velocity, 0.5f, deltaTime);
|
|
stateMachine.Rotate(moveDirection, deltaTime);
|
|
|
|
if (stateMachine.IsGrounded)
|
|
{
|
|
// Landing Shake
|
|
if (!stateMachine.WasGrounded && stateMachine.VelocityY < -1f)
|
|
{
|
|
if (stateMachine.Cam != null)
|
|
{
|
|
stateMachine.Cam.Shake(0.2f, 0.15f);
|
|
}
|
|
}
|
|
|
|
stateMachine.VelocityY = -2f;
|
|
|
|
if (input == Vector2.zero)
|
|
stateMachine.SwitchState(new PlayerIdleState(stateMachine));
|
|
else
|
|
{
|
|
if (stateMachine.IsSprintHeld)
|
|
stateMachine.SwitchState(new PlayerRunState(stateMachine));
|
|
else
|
|
stateMachine.SwitchState(new PlayerMoveState(stateMachine));
|
|
}
|
|
}
|
|
}
|
|
|
|
private void OnThrustPressed() => stateMachine.SwitchState(new PlayerThrustState(stateMachine));
|
|
|
|
private void OnAirDash()
|
|
{
|
|
stateMachine.SwitchState(new PlayerAirDashState(stateMachine));
|
|
}
|
|
|
|
public override void PhysicsTick(float fixedDeltaTime) {}
|
|
|
|
public override void Exit()
|
|
{
|
|
stateMachine.Input.OnDodgeEvent -= OnThrustPressed;
|
|
stateMachine.Input.OnSprintEvent -= OnAirDash;
|
|
}
|
|
}
|
|
} |