39 lines
1.1 KiB
C#
39 lines
1.1 KiB
C#
using UnityEngine;
|
|
|
|
namespace BaseGames.Player.States
|
|
{
|
|
/// <summary>跳跃状态。在 OnStateEnter 触发跳跃,速度降为零时转为 FallState。</summary>
|
|
public class JumpState : PlayerStateBase
|
|
{
|
|
public JumpState(PlayerController owner) : base(owner) { }
|
|
|
|
public override void OnStateEnter()
|
|
{
|
|
if (AnimCfg?.Jump != null)
|
|
Anim.Play(AnimCfg.Jump);
|
|
Move.Jump();
|
|
Input.JumpCancelledEvent += OnJumpCancelled;
|
|
}
|
|
|
|
public override void OnStateUpdate()
|
|
{
|
|
// 上升结束时转为下落
|
|
if (Move.Rb.velocity.y <= 0f)
|
|
{
|
|
_owner.TransitionTo(_owner.GetState<FallState>());
|
|
return;
|
|
}
|
|
// 水平移动
|
|
if (Mathf.Abs(Input.MoveInput.x) > 0.01f)
|
|
Move.Move(Input.MoveInput.x * Cfg.RunSpeed);
|
|
}
|
|
|
|
public override void OnStateExit()
|
|
{
|
|
Input.JumpCancelledEvent -= OnJumpCancelled;
|
|
}
|
|
|
|
private void OnJumpCancelled() => Move.CutJump();
|
|
}
|
|
}
|