Skip to content

Instantly share code, notes, and snippets.

@naosim
Created October 11, 2024 22:57
Show Gist options
  • Save naosim/f8a1cb20d07ce60a7cb111b50ba877ad to your computer and use it in GitHub Desktop.
Save naosim/f8a1cb20d07ce60a7cb111b50ba877ad to your computer and use it in GitHub Desktop.
【microStudio】小要素を管理するオブジェクト
class GameObjects {
children = new Set();
constructor() {
this.children = new Set();
}
add(child) {
this.children.add(child);
}
remove(child) {
this.children.delete(child);
if(child.destroy) {
child.destroy();
}
}
init() {
[...this.children.values()].filter(v => v.init).forEach(v => v.init());
}
update() {
[...this.children.values()].filter(v => v.update).forEach(v => {
v.update();
if(v.isDead) {// 死んでたら削除
this.remove(v);
}
});
}
draw() {
[...this.children.values()].filter(v => v.draw).reverse().forEach(v => v.draw());
}
destroy() {
this.children.forEach(v => this.remove(v));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment