Files
zeling_v2/Assets/_Game/Scripts/Player/States/RunState.cs
Joywayer f1fc356d99 Merge branch 'agents/wall-jump-logic-optimization'
# Conflicts:
#	Assets/_Game/Scripts/Player/PlayerMovement.cs
#	Assets/_Game/Scripts/Player/States/IdleState.cs
#	Assets/_Game/Scripts/Player/States/RunState.cs
#	Assets/_Game/Scripts/Player/States/WallJumpState.cs
#	Assets/_Game/Scripts/Player/States/WallSlideState.cs
2026-05-19 13:03:04 +08:00

76 lines
2.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using UnityEngine;
namespace BaseGames.Player.States
{
/// <summary>跑步状态。播放 Run 动画并驱动水平移动。</summary>
public class RunState : PlayerStateBase
{
public RunState(PlayerController owner) : base(owner) { }
public override void OnStateEnter()
{
if (AnimCfg?.Run != null)
Anim.Play(AnimCfg.Run);
// 落地时重置空中能力计数器及抓墙记录(水平落地直接进入 RunState 时)
Owner.ResetAirJumps();
Owner.GetState<DashState>()?.ResetAirDash();
Owner.GetState<WallSlideState>()?.ResetWallGrab();
Owner.SetPostWallJump(false);
}
public override void OnStateUpdate()
{
if (!Move.IsGrounded)
{
_owner.TransitionTo(_owner.GetState<FallState>());
return;
}
if (Buffer.ConsumeJump())
{
_owner.TransitionTo(_owner.GetState<JumpState>());
return;
}
// 地面攻击Move Y > 0 → 上劈;其余 → 普通连击
if (Buffer.ConsumeAttack())
{
if (Input.MoveInput.y > 0.5f)
_owner.TransitionTo(_owner.GetState<UpAttackState>());
else
_owner.TransitionTo(_owner.GetState<AttackState>());
return;
}
// 地面冲刺:先确认能力与冷却均满足,再消耗缓冲,避免无操作时静默吃掉输入
var ds = Owner.GetState<DashState>();
if (ds != null && ds.CanDash
&& Stats != null && Stats.HasAbility(AbilityType.Dash)
&& Buffer.ConsumeDash())
{
_owner.TransitionTo(ds);
return;
}
if (Mathf.Abs(Input.MoveInput.x) < 0.1f)
{
Move.ZeroHorizontalVelocity();
_owner.TransitionTo(_owner.GetState<IdleState>());
return;
}
}
public override void OnStateFixedUpdate()
{
// 离地检测(与 CheckGrounded 同帧在 FixedUpdate 中执行,立即响应离地事件)
if (!Move.IsGrounded)
{
_owner.TransitionTo(_owner.GetState<FallState>());
return;
}
float inputX = Input.MoveInput.x;
if (Mathf.Abs(inputX) > 0.1f)
Move.Move(inputX * Cfg.RunSpeed);
else
Move.ZeroHorizontalVelocity();
}
}
}