修复内容:

PlayerMovement:新增 _facingLocked 字段 + LockFacing(bool) 方法;UpdateFacing() 锁定时直接返回
WallSlideState:OnStateEnter 调用 LockFacing(true) + FlipFacing(_wallDir);OnStateExit 调用 LockFacing(false) 解锁
WallJumpState:OnStateEnter 保险性再调一次 LockFacing(false);WallJumpAway/Toward 同步写入 _inputVelocityX,确保解锁后 UpdateFacing 朝向正确(背墙跳 = 离墙方向,对墙跳 = 朝墙方向)
This commit is contained in:
2026-05-22 10:48:52 +08:00
parent 285ac46e31
commit 68d4c699ae
15 changed files with 235 additions and 418 deletions

View File

@@ -1,3 +1,4 @@
using System.Collections.Generic;
using BaseGames.Core.Events;
using BaseGames.Input;
using UnityEngine;
@@ -22,8 +23,19 @@ namespace BaseGames.World
// 预分配检测缓冲区,避免 OverlapCircleAll 每帧 GC 分配
private readonly Collider2D[] _overlapBuffer = new Collider2D[16];
private void OnEnable() => _inputReader.InteractEvent += TryInteract;
private void OnDisable() => _inputReader.InteractEvent -= TryInteract;
// Collider → IInteractable 缓存,避免 FindNearest 每帧重复 GetComponentInParent
private readonly Dictionary<Collider2D, IInteractable> _componentCache = new();
private void OnEnable()
{
_inputReader.InteractEvent += TryInteract;
}
private void OnDisable()
{
_inputReader.InteractEvent -= TryInteract;
_componentCache.Clear(); // 清理缓存,防止跨场景持有旧引用
}
private void Update()
{
@@ -57,20 +69,29 @@ namespace BaseGames.World
private IInteractable FindNearest(Collider2D[] hits, int count)
{
IInteractable best = null;
float bestDist = float.MaxValue;
IInteractable best = null;
float bestSqrDist = float.MaxValue;
for (int i = 0; i < count; i++)
{
var col = hits[i];
var interactable = col.GetComponentInParent<IInteractable>();
if (col == null) continue;
// 查缓存,未命中时才调用 GetComponentInParent避免每帧反射开销
if (!_componentCache.TryGetValue(col, out var interactable))
{
interactable = col.GetComponentInParent<IInteractable>();
_componentCache[col] = interactable;
}
if (interactable == null || !interactable.CanInteract) continue;
float dist = Vector2.Distance(transform.position, col.transform.position);
if (dist < bestDist)
// 用 sqrMagnitude 比较距离,省去 Distance 的 sqrt 开销
float sqrDist = ((Vector2)transform.position - (Vector2)col.transform.position).sqrMagnitude;
if (sqrDist < bestSqrDist)
{
bestDist = dist;
best = interactable;
bestSqrDist = sqrDist;
best = interactable;
}
}