v13 全量评审(终章):修复 TD-19/TD-20 + 编写 v13 评审文档
This commit is contained in:
@@ -19,13 +19,17 @@ namespace BaseGames.World
|
||||
private IInteractable _nearest;
|
||||
private IInteractable _previousNearest;
|
||||
|
||||
// 预分配检测缓冲区,避免 OverlapCircleAll 每帧 GC 分配
|
||||
private readonly Collider2D[] _overlapBuffer = new Collider2D[16];
|
||||
|
||||
private void OnEnable() => _inputReader.InteractEvent += TryInteract;
|
||||
private void OnDisable() => _inputReader.InteractEvent -= TryInteract;
|
||||
|
||||
private void Update()
|
||||
{
|
||||
var hits = Physics2D.OverlapCircleAll(transform.position, _detectRadius, _interactableLayer);
|
||||
_nearest = FindNearest(hits);
|
||||
int count = Physics2D.OverlapCircleNonAlloc(
|
||||
transform.position, _detectRadius, _overlapBuffer, _interactableLayer);
|
||||
_nearest = FindNearest(_overlapBuffer, count);
|
||||
|
||||
if (_nearest != _previousNearest)
|
||||
{
|
||||
@@ -51,13 +55,14 @@ namespace BaseGames.World
|
||||
_nearest.Interact(transform);
|
||||
}
|
||||
|
||||
private IInteractable FindNearest(Collider2D[] hits)
|
||||
private IInteractable FindNearest(Collider2D[] hits, int count)
|
||||
{
|
||||
IInteractable best = null;
|
||||
float bestDist = float.MaxValue;
|
||||
|
||||
foreach (var col in hits)
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
var col = hits[i];
|
||||
var interactable = col.GetComponentInParent<IInteractable>();
|
||||
if (interactable == null || !interactable.CanInteract) continue;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user