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

61 lines
1.1 KiB
JavaScript

export class GameObject {
constructor(options = {}) {
const {
x = 0,
y = 0,
width = 0,
height = 0,
debug = false,
gameObjects = [],
} = options;
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.gameObjects = gameObjects;
this.debug = debug;
}
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) {
if (elementId === "debug") {
this.debug = !this.debug;
}
this.gameObjects.forEach((item) => {
item.onMouseClick(elementId);
});
}
onCollide() {}
}