Files
zeling_v2/Assets/Scripts/Player/States/DashState.cs
2026-05-12 15:34:08 +08:00

78 lines
2.5 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;
using System.Collections;
namespace BaseGames.Player.States
{
/// <summary>
/// 地面冲刺状态(架构 05_PlayerModule §2 + §12
/// 施加水平位移 + 无敌帧;冲刺期间重力归零,结束后恢复;
/// 需解锁 AbilityType.Dash 才能进入PlayerController 负责条件检查)。
/// </summary>
public class DashState : PlayerStateBase
{
private float _timer;
private float _cooldownTimer;
private int _facingDir;
public bool CanDash => _cooldownTimer <= 0f;
/// <summary>冲刺状态期间应视为无敌PlayerController 据此跳过受击硬直。</summary>
public override bool IsInvincible => true;
public DashState(PlayerController owner) : base(owner) { }
public override void OnStateEnter()
{
Debug.Assert(Cfg != null, "[DashState] MovementConfig 未配置,请在 PlayerController Inspector 中绑定 MovementConfig SO。", _owner);
_facingDir = Owner.FacingDirection;
_timer = Cfg.DashDuration;
// 无敌帧
Stats?.BeginInvincibility(Cfg.DashDuration + 0.05f);
// 关闭重力,施加冲刺速度
Move?.SetGravityScale(0f);
Move?.Dash(new Vector2(_facingDir, 0f), Cfg.DashSpeed);
// 播放冲刺动画
if (AnimCfg?.Dash != null) Anim?.Play(AnimCfg.Dash);
}
public override void OnStateUpdate()
{
_timer -= Time.deltaTime;
if (_timer <= 0f)
EndDash();
}
public override void OnStateExit()
{
// 恢复默认重力
Move?.SetGravityScale(Cfg.DefaultGravityScale);
_cooldownTimer = Cfg.DashCooldown;
}
public override void OnStateFixedUpdate()
{
// 冲刺期间保持冲刺速度(防止摩擦力减速)
if (_timer > 0f)
Move?.Dash(new Vector2(_facingDir, 0f), Cfg.DashSpeed);
}
private void EndDash()
{
if (Move != null && Move.IsGrounded)
Owner.TransitionTo(Owner.GetState<IdleState>());
else
Owner.TransitionTo(Owner.GetState<FallState>());
}
/// <summary>每帧减少冷却(由 PlayerController.Update 调用)。</summary>
public void TickCooldown(float dt)
{
if (_cooldownTimer > 0f) _cooldownTimer -= dt;
}
}
}