game-engine/modules/game-objects/game-object.js

56 lines
1.3 KiB
JavaScript

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, ...args) {
this.gameObjects.forEach((item) => {
item.render(ctx, ...args);
});
}
onKeyPressed(key) {
this.gameObjects.forEach((item) => {
item.onKeyPressed(key);
});
}
onKeyReleased(key) {
this.gameObjects.forEach((item) => {
item.onKeyReleased(key);
});
}
onMouseClick(elementId) {
this.gameObjects.forEach((item) => {
item.onMouseClick(elementId);
});
}
onCollide() {}
}