Files
zeling_v2/Assets/_Game/Scripts/Player/States/IdleState.cs

81 lines
3.1 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>闲置状态。默认入口状态,播放 Idle 动画。</summary>
public class IdleState : PlayerStateBase
{
public IdleState(PlayerController owner) : base(owner) { }
public override void OnStateEnter()
{
if (AnimCfg?.Idle != null)
Anim.Play(AnimCfg.Idle);
Move?.ZeroHorizontalVelocity();
// 落地时重置空中能力计数器及抓墙记录
Owner.ResetAirJumps();
Owner.GetState<DashState>()?.ResetDashCharge();
Owner.GetState<WallSlideState>()?.ResetWallGrab();
Owner.SetPostWallJump(false);
}
public override void OnStateUpdate()
{
if (!Move.IsGrounded)
{
_owner.TransitionTo(_owner.GetState<FallState>());
return;
}
// 单向平台穿落:↓ + 跳跃键,优先于普通跳跃,避免误消耗跳跃缓冲
if (Move.OnOneWayPlatform && Input.MoveInput.y < -0.5f && Buffer.ConsumeJump())
{
Move.DropThroughPlatform();
_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)
{
_owner.TransitionTo(_owner.GetState<RunState>());
}
}
public override void OnStateFixedUpdate()
{
// 离地检测(与 CheckGrounded 同帧在 FixedUpdate 中执行,立即响应离地事件)
// OnStateUpdate 中仍保留同样的检测作为跨帧补充,两者不会发生双重转换:
// 本帧若在 FixedUpdate 中转换到 FallStateUpdate 调用的将是 FallState.OnStateUpdate()。
if (!Move.IsGrounded)
{
_owner.TransitionTo(_owner.GetState<FallState>());
return;
}
// 无水平输入时仍需每帧调用 Move(0),使 _platformVelocity.x 被叠加进 velocity.x
// 确保站在移动平台上的玩家随平台同步移动,不产生相对位移。
Move.Move(0f);
}
}
}