Refactor updateBullets function to simplify
This commit is contained in:
@@ -184,24 +184,22 @@
|
||||
}
|
||||
|
||||
function updateBullets(elapsed) {
|
||||
bullets.forEach((bullet, index) => {
|
||||
const deleteCount = 1;
|
||||
bullets[index].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;
|
||||
bullets.forEach((bullet, index) => {
|
||||
bullet.time -= elapsed;
|
||||
|
||||
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)`;
|
||||
if (bullet.time > 0) {
|
||||
let y = bullet.y + 0.001 * elapsed * bullet.vy;
|
||||
let x = bullet.x + 0.001 * elapsed * bullet.vx;
|
||||
|
||||
[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 |
Reference in New Issue
Block a user