角色能力,存档

This commit is contained in:
2026-05-19 11:50:21 +08:00
parent d25f237e76
commit 2dcb7a961a
136 changed files with 36035 additions and 27551 deletions

View File

@@ -22,24 +22,74 @@ namespace BaseGames.Player
/// <summary>触碰到的墙壁方向:+1 = 右墙,-1 = 左墙0 = 无墙。</summary>
public int WallDirection { get; private set; }
// 每侧"任意一根射线命中 OR 物理接触点命中"的结果,用于防止下落时卡在矮墙边角
private bool _anyRightContact;
private bool _anyLeftContact;
// 物理接触点缓冲区(避免每帧 GC
private Rigidbody2D _rb;
private static readonly ContactPoint2D[] _contactBuffer = new ContactPoint2D[8];
/// <summary>
/// 指定方向上是否存在墙壁接触(射线命中 OR 物理接触点命中,任一为 true
/// 用于在 FallState / JumpState 中防止角色卡在矮墙边角:<br/>
/// • 射线检测覆盖"检测点略高于墙顶、仅一根射线命中"的情况;<br/>
/// • 物理接触点覆盖"两根射线均高于墙顶,但碰撞体底角已卡在墙顶角"的极端情况。
/// </summary>
public bool HasPartialContact(int direction) =>
direction > 0 ? _anyRightContact : _anyLeftContact;
private void Awake()
{
Debug.Assert(_config != null, "[PlayerWallDetector] _config 未赋值,请在 Inspector 中指定 PlayerMovementConfigSO。", this);
_rb = GetComponent<Rigidbody2D>();
}
private void FixedUpdate()
{
bool rightWall = CheckSide(Vector2.right);
bool leftWall = CheckSide(Vector2.left);
bool rightWall = CheckSide(Vector2.right, out bool anyRightRay);
bool leftWall = CheckSide(Vector2.left, out bool anyLeftRay);
// 物理接触点兜底:两根射线都在墙顶以上时,仍可通过接触点检测到卡角
bool physRight = CheckPhysicalContact(1);
bool physLeft = CheckPhysicalContact(-1);
_anyRightContact = anyRightRay || physRight;
_anyLeftContact = anyLeftRay || physLeft;
IsTouchingWall = rightWall || leftWall;
WallDirection = rightWall ? 1 : (leftWall ? -1 : 0);
}
/// <summary>
/// 每侧发两根射线TopRay + BottomRay两根均命中才返回 true
/// 通过物理接触点判断指定方向是否有墙壁(法线 X 分量超过 0.5 的水平接触)
/// direction = +1 检查右侧接触法线指向左normal.x &lt; -0.5
/// direction = -1 检查左侧接触法线指向右normal.x &gt; +0.5)。
/// </summary>
private bool CheckSide(Vector2 dir)
private bool CheckPhysicalContact(int direction)
{
if (_rb == null) return false;
LayerMask mask = _wallLayer != 0 ? _wallLayer : LayerMask.GetMask("Wall", "Ground");
var filter = new ContactFilter2D();
filter.SetLayerMask(mask);
filter.useTriggers = false;
int count = _rb.GetContacts(filter, _contactBuffer);
for (int i = 0; i < count; i++)
{
float nx = _contactBuffer[i].normal.x;
// 右侧墙接触法线指向左nx < -0.5左侧墙接触法线指向右nx > +0.5
if (direction > 0 && nx < -0.5f) return true;
if (direction < 0 && nx > 0.5f) return true;
}
return false;
}
/// <summary>
/// 每侧发两根射线TopRay + BottomRay两根均命中才返回 true。
/// <paramref name="anyContact"/> 在任意一根命中时为 true用于防卡角判断
/// </summary>
private bool CheckSide(Vector2 dir, out bool anyContact)
{
Vector2 center = transform.position;
float len = _config.WallRayLength;
@@ -48,6 +98,7 @@ namespace BaseGames.Player
bool top = Physics2D.Raycast(center + Vector2.up * oy, dir, len, layer);
bool bot = Physics2D.Raycast(center + Vector2.down * oy, dir, len, layer);
anyContact = top || bot;
return top && bot;
}