feat(#10): reset camera position after changing level

This commit is contained in:
Juan Sebastián Montoya 2024-09-14 23:51:24 -05:00
parent e2c439d1f3
commit abf334607e
6 changed files with 47 additions and 2 deletions

View file

@ -27,6 +27,11 @@ export class Camera extends GameObject {
(acc, item) => ({ ...acc, [item.key]: item }),
{}
);
this.eventEmitter.on("changeLevel", () => {
this.x = x;
this.y = y;
this.moveCamera(0, 0, 0);
});
}
update(delta) {

View file

@ -1,3 +1,5 @@
import { eventEmitter } from "../event-emitter.js";
export class GameObject {
constructor(options = {}) {
const {
@ -14,6 +16,7 @@ export class GameObject {
this.height = height;
this.gameObjects = gameObjects;
this.debug = debug;
this.eventEmitter = eventEmitter;
}
async load() {

View file

@ -28,5 +28,6 @@ export class MapManagement extends GameObject {
}
const map = this.gameObjects.find((item) => item.elementId === elementId);
this.selected = map.name;
this.eventEmitter.emit("changeLevel");
}
}

View file

@ -29,7 +29,7 @@ export class Map extends GameObject {
});
this.levelConfig = await response.json();
const layer = this.levelConfig.layers[this.layer];
const { data, height, width } = layer;
const { data, height, width } = layer ?? {};
this.width = width;
this.height = height;
this.data = data;
@ -39,6 +39,9 @@ export class Map extends GameObject {
if (this._level) {
return this._level;
}
if (!this.width || !this.height) {
return null;
}
const { ctx, canvas } = createCanvas(
this.width * TILE_SIZE,
this.height * TILE_SIZE
@ -84,7 +87,7 @@ export class Map extends GameObject {
}
render(ctx, sourceX, sourceY, width, height) {
if (!this.levelConfig || !this.selected) {
if (!this.levelConfig || !this.selected || !this.level) {
return;
}