2024-09-13 21:45:59 -05:00
|
|
|
import { COLS, ROWS, TILE_SIZE } from "../constants.js";
|
2024-09-13 21:45:59 -05:00
|
|
|
import { createCanvas } from "../utils.js";
|
2024-09-13 21:45:59 -05:00
|
|
|
import { GameObject } from "./game-object.js";
|
|
|
|
|
|
|
|
export class Debug extends GameObject {
|
|
|
|
constructor({ debug = false }) {
|
|
|
|
super();
|
|
|
|
this.debug = debug;
|
|
|
|
this.fps = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
toggle() {
|
|
|
|
this.debug = !this.debug;
|
|
|
|
}
|
|
|
|
|
|
|
|
update(delta) {
|
|
|
|
this.fps = Math.floor(1 / delta);
|
|
|
|
}
|
|
|
|
|
2024-09-13 21:45:59 -05:00
|
|
|
get grid() {
|
|
|
|
if (this._grid) {
|
|
|
|
return this._grid;
|
2024-09-13 21:45:59 -05:00
|
|
|
}
|
2024-09-13 21:45:59 -05:00
|
|
|
const { ctx, canvas } = createCanvas(COLS * TILE_SIZE, ROWS * TILE_SIZE);
|
2024-09-13 21:45:59 -05:00
|
|
|
for (let row = 0; row < ROWS; row++) {
|
|
|
|
for (let col = 0; col < COLS; col++) {
|
|
|
|
ctx.strokeRect(col * TILE_SIZE, row * TILE_SIZE, TILE_SIZE, TILE_SIZE);
|
|
|
|
}
|
|
|
|
}
|
2024-09-13 21:45:59 -05:00
|
|
|
return (this._grid = canvas);
|
|
|
|
}
|
2024-09-13 21:45:59 -05:00
|
|
|
|
2024-09-13 21:45:59 -05:00
|
|
|
render(ctx) {
|
|
|
|
if (!this.debug) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ctx.drawImage(this.grid, 0, 0);
|
2024-09-13 21:45:59 -05:00
|
|
|
ctx.fillStyle = "Red";
|
|
|
|
ctx.font = "normal 16pt Arial";
|
|
|
|
ctx.fillText(this.fps + " fps", 10, 26);
|
|
|
|
}
|
|
|
|
|
|
|
|
onMouseClick(elementId) {
|
|
|
|
if (elementId === "debug") {
|
|
|
|
this.toggle();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|