2024-09-13 21:45:59 -05:00
|
|
|
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);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-09-13 23:30:28 -05:00
|
|
|
render(ctx, ...args) {
|
2024-09-13 21:45:59 -05:00
|
|
|
this.gameObjects.forEach((item) => {
|
2024-09-13 23:30:28 -05:00
|
|
|
item.render(ctx, ...args);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
onKeyPressed(key) {
|
|
|
|
this.gameObjects.forEach((item) => {
|
|
|
|
item.onKeyPressed(key);
|
2024-09-13 21:45:59 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
onKeyReleased(key) {
|
|
|
|
this.gameObjects.forEach((item) => {
|
|
|
|
item.onKeyReleased(key);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
onMouseClick(elementId) {
|
|
|
|
this.gameObjects.forEach((item) => {
|
|
|
|
item.onMouseClick(elementId);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
onCollide() {}
|
|
|
|
}
|