game-engine/modules/game-objects/map-management.js

47 lines
1 KiB
JavaScript

import { GameObject } from "./game-object.js";
import { Map } from "./map.js";
export class MapManagement extends GameObject {
constructor({ maps = [] }) {
super();
this.gameObjects = maps.map((item) => new Map(item));
this.elementsId = this.gameObjects.map((item) => item.elementId);
}
get selected() {
return this.gameObjects.find((item) => item.selected);
}
set selected(name) {
this.gameObjects.forEach((item) => {
item.selected = item.name === name;
});
}
update(delta) {
this.gameObjects.forEach((item) => {
if (item.selected) {
item.update(delta);
}
});
}
render(ctx) {
this.gameObjects.forEach((item) => {
if (item.selected) {
item.render(ctx);
}
});
}
onMouseClick(elementId) {
if (
!this.elementsId.includes(elementId) ||
this.selected.elementId === elementId
) {
return;
}
const map = this.gameObjects.find((item) => item.elementId === elementId);
this.selected = map.name;
}
}