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

93 lines
2.2 KiB
JavaScript

import { TILE_SIZE } from "../constants.js";
import { createCanvas } from "../utils.js";
import { GameObject } from "./game-object.js";
export class Map extends GameObject {
constructor({ name, imageId, elementId, selected = false, debug = false }) {
super({ debug });
this.name = name;
this.imageId = imageId;
this.image = document.getElementById(imageId);
this.imageWidth = this.image.width;
this.imageHeight = this.image.height;
this.selected = selected;
this.elementId = elementId;
this.debug = debug;
}
async load() {
const levelConfig = await fetch("/resources/" + this.name + ".json");
this.levelConfig = await levelConfig.json();
const layer = this.levelConfig.layers[0];
const { data, height, width } = layer;
this.width = width;
this.height = height;
this.data = data;
}
get level() {
if (this._level) {
return this._level;
}
const { ctx, canvas } = createCanvas(
this.width * TILE_SIZE,
this.height * TILE_SIZE
);
for (let row = 0; row < this.height; row++) {
for (let col = 0; col < this.width; col++) {
if (row < 0 || col < 0 || row >= this.height || col >= this.width)
continue;
if (this.debug) {
ctx.strokeRect(
col * TILE_SIZE,
row * TILE_SIZE,
TILE_SIZE,
TILE_SIZE
);
}
const tile = this.data[row * this.width + col] - 1;
ctx.drawImage(
this.image,
(tile * TILE_SIZE) % this.imageWidth,
Math.floor((tile * TILE_SIZE) / this.imageWidth) * TILE_SIZE,
TILE_SIZE,
TILE_SIZE,
col * TILE_SIZE,
row * TILE_SIZE,
TILE_SIZE,
TILE_SIZE
);
}
}
return (this._level = canvas);
}
onMouseClick(elementId) {
if (elementId === "debug") {
this.debug = !this.debug;
this._level = null;
}
}
render(ctx, sourceX, sourceY, width, height) {
if (!this.levelConfig || !this.selected) {
return;
}
ctx.drawImage(
this.level,
sourceX,
sourceY,
width,
height,
0,
0,
width,
height
);
}
}