Refactor updateBullets function to simplify

This commit is contained in:
2025-12-18 11:03:31 -08:00
parent 0282a0b721
commit 660bfba1b9

View File

@@ -184,24 +184,22 @@
}
function updateBullets(elapsed) {
const deleteCount = 1;
bullets.forEach((bullet, index) => {
const deleteCount = 1;
bullets[index].time -= elapsed;
bullet.time -= elapsed;
if (bullets[index].time > 0) {
bullets[index].y += 0.001 * elapsed * bullets[index].vy;
bullets[index].x += 0.001 * elapsed * bullets[index].vx;
if (bullet.time > 0) {
let y = bullet.y + 0.001 * elapsed * bullet.vy;
let x = bullet.x + 0.001 * elapsed * bullet.vx;
let [bx, by] = wrapPos(bullets[index].x, bullets[index].y)
bullets[index].x = bx;
bullets[index].y = by;
bullet.node.style.transform = `translate(${bx}px, ${by}px)`;
[bullet.x, bullet.y] = wrapPos(x, y);
bullet.node.style.transform = `translate(${bullet.x}px, ${bullet.y}px)`;
} else {
bullet.node.remove();
bullets.splice(index, deleteCount);
}
});
}
function updateLines([positionX, positionY]) {
@@ -219,7 +217,6 @@
line.setAttribute('y2', line.getAttribute('y1'));
}
});
}
requestAnimationFrame(firstFrame);
@@ -240,7 +237,7 @@
info.innerText = `velocity ${velocity}\n`
+ 'bullets\nx\ty\tvx\tvy\n'
+ bullets.map((b, index) => {
+ bullets.map(b => {
return `${b.x.toFixed(2)}\t${b.y.toFixed(2)}\t${b.vx.toFixed(2)}\t${b.vy.toFixed(2)}`;
}).join("\n");

Before

Width:  |  Height:  |  Size: 13 KiB

After

Width:  |  Height:  |  Size: 13 KiB