Refactor function

This commit is contained in:
2026-01-07 13:33:22 -08:00
parent 625cae288c
commit d17d0f0a08

View File

@@ -683,6 +683,21 @@
};
}
function detectCollision(edges, corners, [xc, yc], [x, y], velocity, radius) {
// edges oriented clockwise with ship
const fwdEdges = getForwardEdges(edges, { x, y })
const edgeColl = fwdEdges.find(detectEdgeCollision([xc, yc], [x, y], radius));
if (edgeColl) return edgeColl;
// corners ahead of ship
const fwdCorners = getForwardCorners(corners, { x, y }, velocity);
const cornersInPath = fwdCorners.filter(withinCollisionDistance({ x, y }, velocity, radius));
const cornerColl = cornersInPath.find(detectCornerCollision([xc, yc], [x, y], radius));
if (cornerColl) return cornerColl;
}
function withinCollisionDistance({ x: x1, y: y1 }, { x: x2, y: y2 }, distance) {
const diffx = x2;
const diffy = y2;
@@ -780,17 +795,8 @@
let [xc, yc] = position;
// console.log("future position", xc, yc);
// edges oriented clockwise with ship
const fwdEdges = getForwardEdges(mapEdges, { x, y })
const edgeColl = fwdEdges.find(detectEdgeCollision([xc, yc], [x, y], shipRadius));
// corners ahead of ship
const fwdCorners = getForwardCorners(mapCorners, { x, y }, s.velocity);
const cornersInPath = fwdCorners.filter(withinCollisionDistance({ x, y }, s.velocity, shipRadius));
const cornerColl = cornersInPath.find(detectCornerCollision([xc, yc], [x, y], shipRadius));
current = s.collision;
s.collision = edgeColl || cornerColl;
s.collision = detectCollision(mapEdges, mapCorners, [xc, yc], [x, y], s.velocity, shipRadius);
if (s.collision) console.log("COLLISION", s.collision);
legs.style.display = s.gearDown ? "initial" : "none";

Before

Width:  |  Height:  |  Size: 37 KiB

After

Width:  |  Height:  |  Size: 37 KiB