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

Before

Width:  |  Height:  |  Size: 13 KiB

After

Width:  |  Height:  |  Size: 13 KiB