malinovsky,
class Game {
constructor(canvas, ...entities) {
// ...
this.world = new Set(entities);
this.cache = new Set();
// ...
}
// ...
update() {
for (let entity of this.world) if (entity.update) entity.update(this);
for (let entity of this.cache) {
this.world.delete(entity);
this.cache.delete(entity);
}
}
// ...
}
class Ship extends Rect {
// ...
update(game) {
// ...
if (game.collide(this, PLAYER | SHOT | LINE )) game.cache.add(this);
}
}
const game = new Game(canvas, new Player(), new Attack());