54 lines
1.6 KiB
C#
54 lines
1.6 KiB
C#
using UnityEngine;
|
||
|
||
namespace BaseGames.Player.States
|
||
{
|
||
/// <summary>下落状态。着地后转为 Idle 或 Run,持有郊狼时间允许跳跃。</summary>
|
||
public class FallState : PlayerStateBase
|
||
{
|
||
public FallState(PlayerController owner) : base(owner) { }
|
||
|
||
public override void OnStateEnter()
|
||
{
|
||
if (AnimCfg?.Fall != null)
|
||
Anim.Play(AnimCfg.Fall);
|
||
}
|
||
|
||
public override void OnStateUpdate()
|
||
{
|
||
// 郊狼时间跳跃
|
||
if (Buffer.ConsumeJump() && Move.HasCoyoteTime)
|
||
{
|
||
_owner.TransitionTo(_owner.GetState<JumpState>());
|
||
return;
|
||
}
|
||
|
||
// 着地
|
||
if (Move.IsGrounded)
|
||
{
|
||
Move.ZeroVelocity();
|
||
if (Mathf.Abs(Input.MoveInput.x) > 0.1f)
|
||
_owner.TransitionTo(_owner.GetState<RunState>());
|
||
else
|
||
_owner.TransitionTo(_owner.GetState<IdleState>());
|
||
return;
|
||
}
|
||
|
||
// 空中水平移动
|
||
if (Mathf.Abs(Input.MoveInput.x) > 0.01f)
|
||
Move.Move(Input.MoveInput.x * Cfg.RunSpeed);
|
||
}
|
||
|
||
public override void OnStateFixedUpdate()
|
||
{
|
||
// 增强下落重力
|
||
if (Move.Rb.velocity.y < 0f)
|
||
{
|
||
float extraGrav = Physics2D.gravity.y * (Cfg.FallGravityMult - 1f) * Time.fixedDeltaTime;
|
||
Move.Rb.velocity = new Vector2(
|
||
Move.Rb.velocity.x,
|
||
Mathf.Max(Move.Rb.velocity.y + extraGrav, -Cfg.MaxFallSpeed));
|
||
}
|
||
}
|
||
}
|
||
}
|