From abf334607e17885ff0a7a28a0bf3a27068a82035 Mon Sep 17 00:00:00 2001 From: Juan Sebastian Montoya Date: Sat, 14 Sep 2024 23:51:24 -0500 Subject: [PATCH] feat(#10): reset camera position after changing level --- index.js | 1 + modules/event-emitter.js | 32 ++++++++++++++++++++++++++ modules/game-objects/camera.js | 5 ++++ modules/game-objects/game-object.js | 3 +++ modules/game-objects/map-management.js | 1 + modules/game-objects/map.js | 7 ++++-- 6 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 modules/event-emitter.js diff --git a/index.js b/index.js index 756299c..f392821 100644 --- a/index.js +++ b/index.js @@ -26,6 +26,7 @@ const foregroundMaps = [ layer: 1, selected: true, }, + { name: "ocean", imageId: "overworld", elementId: "level2", layer: 2 }, ]; const clicableObjects = ["debug", "level1", "level2"]; diff --git a/modules/event-emitter.js b/modules/event-emitter.js new file mode 100644 index 0000000..1795c1a --- /dev/null +++ b/modules/event-emitter.js @@ -0,0 +1,32 @@ +class EventEmitterSingleton { + static instance; + eventEmitter; + + constructor() { + this.eventEmitter = {}; + } + + static getInstance() { + if (!EventEmitterSingleton.instance) { + EventEmitterSingleton.instance = new EventEmitterSingleton(); + } + return EventEmitterSingleton.instance; + } + + on(eventName, callback) { + if (!this.eventEmitter[eventName]) { + this.eventEmitter[eventName] = []; + } + this.eventEmitter[eventName].push(callback); + } + + emit(eventName, ...args) { + if (this.eventEmitter[eventName]) { + this.eventEmitter[eventName].forEach((callback) => { + callback(...args); + }); + } + } +} + +export const eventEmitter = EventEmitterSingleton.getInstance(); diff --git a/modules/game-objects/camera.js b/modules/game-objects/camera.js index 5e24e70..75b316d 100644 --- a/modules/game-objects/camera.js +++ b/modules/game-objects/camera.js @@ -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) { diff --git a/modules/game-objects/game-object.js b/modules/game-objects/game-object.js index 932f923..609726b 100644 --- a/modules/game-objects/game-object.js +++ b/modules/game-objects/game-object.js @@ -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() { diff --git a/modules/game-objects/map-management.js b/modules/game-objects/map-management.js index fd4c952..163f280 100644 --- a/modules/game-objects/map-management.js +++ b/modules/game-objects/map-management.js @@ -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"); } } diff --git a/modules/game-objects/map.js b/modules/game-objects/map.js index 70f0933..153b883 100644 --- a/modules/game-objects/map.js +++ b/modules/game-objects/map.js @@ -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; }