feat(#8): modularizing
This commit is contained in:
parent
f300fe1be7
commit
32dd2ad599
11 changed files with 271 additions and 94 deletions
50
modules/game-objects/game-object.js
Normal file
50
modules/game-objects/game-object.js
Normal file
|
@ -0,0 +1,50 @@
|
|||
export class GameObject {
|
||||
/**
|
||||
* Initializes a new instance of the GameObject class.
|
||||
*
|
||||
* @param {Object} options - An object containing the initial properties of the game object.
|
||||
* @param {number} [options.x=0] - The initial x-coordinate of the game object.
|
||||
* @param {number} [options.y=0] - The initial y-coordinate of the game object.
|
||||
* @param {GameObject[]} [options.gameObjects=[]] - An array of child game objects.
|
||||
*/
|
||||
constructor(options = {}) {
|
||||
const { x = 0, y = 0, gameObjects = [] } = options;
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.gameObjects = gameObjects;
|
||||
}
|
||||
|
||||
async load() {
|
||||
await Promise.all(
|
||||
this.gameObjects.map((item) => {
|
||||
item.load();
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
update(delta) {
|
||||
this.gameObjects.forEach((item) => {
|
||||
item.update(delta);
|
||||
});
|
||||
}
|
||||
|
||||
render(ctx) {
|
||||
this.gameObjects.forEach((item) => {
|
||||
item.render(ctx);
|
||||
});
|
||||
}
|
||||
|
||||
onKeyReleased(key) {
|
||||
this.gameObjects.forEach((item) => {
|
||||
item.onKeyReleased(key);
|
||||
});
|
||||
}
|
||||
|
||||
onMouseClick(elementId) {
|
||||
this.gameObjects.forEach((item) => {
|
||||
item.onMouseClick(elementId);
|
||||
});
|
||||
}
|
||||
|
||||
onCollide() {}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue