2024-09-13 21:45:59 -05:00
|
|
|
export class GameObject {
|
|
|
|
constructor(options = {}) {
|
2024-09-14 18:38:50 -05:00
|
|
|
const {
|
|
|
|
x = 0,
|
|
|
|
y = 0,
|
|
|
|
width = 0,
|
|
|
|
height = 0,
|
|
|
|
debug = false,
|
|
|
|
gameObjects = [],
|
|
|
|
} = options;
|
2024-09-13 21:45:59 -05:00
|
|
|
this.x = x;
|
|
|
|
this.y = y;
|
2024-09-14 18:38:50 -05:00
|
|
|
this.width = width;
|
|
|
|
this.height = height;
|
2024-09-13 21:45:59 -05:00
|
|
|
this.gameObjects = gameObjects;
|
2024-09-14 18:38:50 -05:00
|
|
|
this.debug = debug;
|
2024-09-13 21:45:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2024-09-14 18:38:50 -05:00
|
|
|
if (elementId === "debug") {
|
|
|
|
this.debug = !this.debug;
|
|
|
|
}
|
2024-09-13 21:45:59 -05:00
|
|
|
this.gameObjects.forEach((item) => {
|
|
|
|
item.onMouseClick(elementId);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
onCollide() {}
|
|
|
|
}
|