Files
BABA_YAGA/Assets/Scripts/Player Controller/PlayerFallState.cs

91 lines
3.1 KiB
C#
Raw Normal View History

2026-03-26 20:27:19 +07:00
using UnityEngine;
namespace OnlyScove.Scripts
{
public class PlayerFallState : PlayerBaseState
{
private readonly int fallHash = Animator.StringToHash("Fall");
2026-03-31 08:16:46 +07:00
private readonly int speedHash = Animator.StringToHash("Speed");
private readonly int speedXHash = Animator.StringToHash("Velocity X");
private readonly int speedZHash = Animator.StringToHash("Velocity Z");
2026-03-26 20:27:19 +07:00
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;
2026-03-31 08:16:46 +07:00
stateMachine.Input.OnSprintEvent += OnAirDash;
2026-03-26 20:27:19 +07:00
}
public override void Tick(float deltaTime)
{
2026-04-08 12:38:39 +07:00
stateMachine.VelocityY += stateMachine.Gravity * deltaTime;
2026-03-26 20:27:19 +07:00
2026-04-08 12:38:39 +07:00
// ĐÚNG: Sử dụng MoveInput đã đồng bộ mạng
Vector2 input = stateMachine.MoveInput;
2026-03-26 20:27:19 +07:00
Vector3 inputDir = new Vector3(input.x, 0, input.y).normalized;
2026-04-15 19:53:29 +07:00
Vector3 moveDirection = stateMachine.CameraRotation * inputDir;
2026-04-08 12:38:39 +07:00
moveDirection.y = 0;
moveDirection.Normalize();
2026-03-26 20:27:19 +07:00
Vector3 velocity = moveDirection * fallSpeed;
velocity.y = stateMachine.VelocityY;
2026-04-08 12:38:39 +07:00
// Sử dụng hàm Move tập trung của StateMachine để đồng bộ
stateMachine.Move(velocity, 0.5f, deltaTime);
stateMachine.Rotate(moveDirection, deltaTime);
2026-03-26 20:27:19 +07:00
if (stateMachine.IsGrounded)
{
2026-04-08 12:38:39 +07:00
// Landing Shake
2026-03-26 20:27:19 +07:00
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
{
2026-04-08 12:38:39 +07:00
if (stateMachine.IsSprintHeld)
2026-03-26 20:27:19 +07:00
stateMachine.SwitchState(new PlayerRunState(stateMachine));
else
stateMachine.SwitchState(new PlayerMoveState(stateMachine));
}
}
}
private void OnThrustPressed() => stateMachine.SwitchState(new PlayerThrustState(stateMachine));
2026-03-31 08:16:46 +07:00
private void OnAirDash()
{
stateMachine.SwitchState(new PlayerAirDashState(stateMachine));
}
2026-03-26 20:27:19 +07:00
public override void PhysicsTick(float fixedDeltaTime) {}
public override void Exit()
{
stateMachine.Input.OnDodgeEvent -= OnThrustPressed;
2026-03-31 08:16:46 +07:00
stateMachine.Input.OnSprintEvent -= OnAirDash;
2026-03-26 20:27:19 +07:00
}
}
}