103 lines
3.9 KiB
C#
103 lines
3.9 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 += Physics.gravity.y * deltaTime;
|
|
|
|
Vector2 input = stateMachine.Input.MoveInput;
|
|
Vector3 inputDir = new Vector3(input.x, 0, input.y).normalized;
|
|
Vector3 moveDirection = stateMachine.Cam != null ? stateMachine.Cam.PlanarRotation * inputDir : inputDir;
|
|
|
|
Vector3 velocity = moveDirection * fallSpeed;
|
|
velocity.y = stateMachine.VelocityY;
|
|
|
|
stateMachine.Controller.Move(velocity * deltaTime);
|
|
|
|
// Cập nhật Animator cho việc rơi trên không
|
|
float multiplier = stateMachine.Input.IsSprintHeld ? 2f : 0.5f;
|
|
stateMachine.Anim.SetFloat(speedHash, stateMachine.Input.IsSprintHeld ? 1f : 0.7f, stateMachine.AnimationDamping, deltaTime);
|
|
stateMachine.Anim.SetFloat(speedXHash, input.x * multiplier, stateMachine.AnimationDamping, deltaTime);
|
|
stateMachine.Anim.SetFloat(speedZHash, input.y * multiplier, stateMachine.AnimationDamping, deltaTime);
|
|
|
|
if (moveDirection != Vector3.zero)
|
|
{
|
|
Quaternion targetRot = Quaternion.LookRotation(moveDirection);
|
|
stateMachine.transform.rotation = Quaternion.RotateTowards(
|
|
stateMachine.transform.rotation,
|
|
targetRot,
|
|
stateMachine.RotationSpeed * deltaTime
|
|
);
|
|
}
|
|
|
|
if (stateMachine.IsGrounded)
|
|
{
|
|
// Landing Shake from PlayerController.cs
|
|
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
|
|
{
|
|
// Return to the appropriate movement state based on sprint input
|
|
if (stateMachine.Input.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;
|
|
}
|
|
}
|
|
} |