Add additional walls
This commit is contained in:
@@ -159,20 +159,13 @@
|
|||||||
}
|
}
|
||||||
));
|
));
|
||||||
|
|
||||||
// console.log(wallCorners);
|
|
||||||
// console.log(allWallCorners);
|
|
||||||
|
|
||||||
const edgePts = wallCorners.map((pt, i, arr) => [pt, arr[(i + 1) % arr.length]]);
|
const edgePts = wallCorners.map((pt, i, arr) => [pt, arr[(i + 1) % arr.length]]);
|
||||||
const allEdgePts = allWallCorners.map(w =>
|
const allEdgePts = allWallCorners.map(w =>
|
||||||
w.map((pt, i, arr) => [pt, arr[(i + 1) % arr.length]])
|
w.map((pt, i, arr) => [pt, arr[(i + 1) % arr.length]])
|
||||||
);
|
);
|
||||||
// console.log(edgePts);
|
|
||||||
// console.log(allEdgePts);
|
|
||||||
|
|
||||||
const startingEdges = findEdges(edgePts, position);
|
const startingEdges = findEdges(edgePts, position);
|
||||||
const allStartingEdges = findAllEdges(allEdgePts, position);
|
const allStartingEdges = findAllEdges(allEdgePts, position);
|
||||||
// console.log(startingEdges);
|
|
||||||
// console.log(allStartingEdges);
|
|
||||||
|
|
||||||
function distance(x1, y1, x2, y2) {
|
function distance(x1, y1, x2, y2) {
|
||||||
return Math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2);
|
return Math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2);
|
||||||
@@ -199,13 +192,15 @@
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function drawTriangles(container, pts, [positionX, positionY]) {
|
function drawTriangles(container, walls, [positionX, positionY]) {
|
||||||
pts.forEach(([[x1, y1], [x2, y2]]) => {
|
walls.forEach(pts =>
|
||||||
const el = document.createElementNS(namespaceURIsvg, 'polygon');
|
pts.forEach(([[x1, y1], [x2, y2]]) => {
|
||||||
const attr = `${x1},${y1} ${x2},${y2} ${positionX},${positionY}`
|
const el = document.createElementNS(namespaceURIsvg, 'polygon');
|
||||||
el.setAttribute('points', attr);
|
const attr = `${x1},${y1} ${x2},${y2} ${positionX},${positionY}`
|
||||||
container.appendChild(el)
|
el.setAttribute('points', attr);
|
||||||
});
|
container.appendChild(el);
|
||||||
|
})
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Triangle has a clockwise orientation
|
// Triangle has a clockwise orientation
|
||||||
@@ -252,7 +247,7 @@
|
|||||||
const className = 'clockwise-orientation';
|
const className = 'clockwise-orientation';
|
||||||
|
|
||||||
if (!triangleContainer.childElementCount)
|
if (!triangleContainer.childElementCount)
|
||||||
drawTriangles(triangleContainer, edgePts, position);
|
drawTriangles(triangleContainer, allEdgePts, position);
|
||||||
|
|
||||||
const triangles = triangleContainer.querySelectorAll('polygon');
|
const triangles = triangleContainer.querySelectorAll('polygon');
|
||||||
|
|
||||||
@@ -344,7 +339,7 @@
|
|||||||
bulletPt.x = x;
|
bulletPt.x = x;
|
||||||
bulletPt.y = y;
|
bulletPt.y = y;
|
||||||
|
|
||||||
if (bullet.time > 0 && !wall.isPointInFill(bulletPt)) {
|
if (bullet.time > 0 && ![...walls].some(w => w.isPointInFill(bulletPt))) {
|
||||||
[bullet.x, bullet.y] = wrapPos(x, y);
|
[bullet.x, bullet.y] = wrapPos(x, y);
|
||||||
bullet.node.style.transform = `translate(${bullet.x}px, ${bullet.y}px)`;
|
bullet.node.style.transform = `translate(${bullet.x}px, ${bullet.y}px)`;
|
||||||
} else {
|
} else {
|
||||||
@@ -354,33 +349,35 @@
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function detectCollision(corners, [xc, yc], edges) {
|
function detectCollision(corners, [xc, yc], edge) {
|
||||||
const shipRadius = 5;
|
const shipRadius = 5;
|
||||||
|
const [[xa, ya], [xb, yb]] = edge.split(' ').map(n => n.split(',').map(n => +n));
|
||||||
|
|
||||||
// const cornerCollision = corners.some(([x, y]) => {
|
const cornerCollision = corners.some(([[ax, ay], [bx, by]]) => {
|
||||||
// cornerPt.x = x - xc;
|
cornerPt.x = ax - xc;
|
||||||
// cornerPt.y = y - yc;
|
cornerPt.y = ay - yc;
|
||||||
//
|
|
||||||
// return shipBody.isPointInFill(cornerPt);
|
|
||||||
// });
|
|
||||||
|
|
||||||
const cornerCollision = false;
|
const collideWithA = shipBody.isPointInFill(cornerPt);
|
||||||
|
|
||||||
const sideCollision = edges.reduce((acc, e) => {
|
cornerPt.x = bx - xc;
|
||||||
const [[xa, ya], [xb, yb]] = e.split(' ').map(n => n.split(',').map(n => +n));
|
cornerPt.y = by - yc;
|
||||||
|
|
||||||
const da = distance(xa, ya, xc, yc);
|
const collideWithB = shipBody.isPointInFill(cornerPt);
|
||||||
const db = distance(xb, yb, xc, yc);
|
|
||||||
// TODO: calculate this one ahead of time
|
|
||||||
const dc = distance(xa, ya, xb, yb);
|
|
||||||
|
|
||||||
// https://en.wikipedia.org/wiki/Altitude_(triangle)#Altitude_in_terms_of_the_sides
|
return collideWithA || collideWithB;
|
||||||
// Find altitude of side c (the base)
|
});
|
||||||
const s = (1 / 2) * (da + db + dc);
|
|
||||||
const hc = (2 / dc) * Math.sqrt(s * (s - da) * (s - db) * (s - dc));
|
|
||||||
|
|
||||||
return [...acc, hc];
|
const da = distance(xa, ya, xc, yc);
|
||||||
}, []).some(h => h <= shipRadius);
|
const db = distance(xb, yb, xc, yc);
|
||||||
|
// TODO: calculate this one ahead of time
|
||||||
|
const dc = distance(xa, ya, xb, yb);
|
||||||
|
|
||||||
|
// https://en.wikipedia.org/wiki/Altitude_(triangle)#Altitude_in_terms_of_the_sides
|
||||||
|
// Find altitude of side c (the base)
|
||||||
|
const s = (1 / 2) * (da + db + dc);
|
||||||
|
const hc = (2 / dc) * Math.sqrt(s * (s - da) * (s - db) * (s - dc));
|
||||||
|
|
||||||
|
const sideCollision = hc <= shipRadius;
|
||||||
|
|
||||||
return cornerCollision || sideCollision;
|
return cornerCollision || sideCollision;
|
||||||
}
|
}
|
||||||
@@ -486,7 +483,8 @@
|
|||||||
|
|
||||||
// const collision = detectCollision(wallCorners, position, findEdges(edgePts, position));
|
// const collision = detectCollision(wallCorners, position, findEdges(edgePts, position));
|
||||||
const collision = detectAllCollisions(allWallCorners, position, findAllEdges(allEdgePts, position));
|
const collision = detectAllCollisions(allWallCorners, position, findAllEdges(allEdgePts, position));
|
||||||
wall.setAttribute('fill', collision ? 'red' : 'black');
|
// wall.setAttribute('fill', collision ? 'red' : 'black');
|
||||||
|
walls.forEach(w => w.setAttribute('fill', collision ? 'red' : 'black'));
|
||||||
|
|
||||||
if (collision) {
|
if (collision) {
|
||||||
// restart game
|
// restart game
|
||||||
|
|||||||
|
Before Width: | Height: | Size: 20 KiB After Width: | Height: | Size: 20 KiB |
Reference in New Issue
Block a user