From 37a7b76d3859d07863fabcefbf527e37c032fd9b Mon Sep 17 00:00:00 2001 From: Juan Sebastian Montoya Date: Tue, 17 Sep 2024 23:16:36 -0500 Subject: [PATCH] feat(#10): remove redundancy --- modules/event-emitter.js | 27 +++++++++++--------------- modules/game-objects/camera.js | 7 +------ modules/game-objects/game-object.js | 8 +++----- modules/game-objects/map-management.js | 3 +-- modules/game-objects/map.js | 22 ++++++--------------- 5 files changed, 22 insertions(+), 45 deletions(-) diff --git a/modules/event-emitter.js b/modules/event-emitter.js index 1795c1a..70013fa 100644 --- a/modules/event-emitter.js +++ b/modules/event-emitter.js @@ -1,32 +1,27 @@ -class EventEmitterSingleton { +export class EventEmitter { static instance; - eventEmitter; + events; constructor() { - this.eventEmitter = {}; - } - - static getInstance() { - if (!EventEmitterSingleton.instance) { - EventEmitterSingleton.instance = new EventEmitterSingleton(); + if (!EventEmitter.instance) { + EventEmitter.instance = this; + this.events = {}; } - return EventEmitterSingleton.instance; + return EventEmitter.instance; } on(eventName, callback) { - if (!this.eventEmitter[eventName]) { - this.eventEmitter[eventName] = []; + if (!this.events[eventName]) { + this.events[eventName] = []; } - this.eventEmitter[eventName].push(callback); + this.events[eventName].push(callback); } emit(eventName, ...args) { - if (this.eventEmitter[eventName]) { - this.eventEmitter[eventName].forEach((callback) => { + if (this.events[eventName]) { + this.events[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 e290a00..b2816c6 100644 --- a/modules/game-objects/camera.js +++ b/modules/game-objects/camera.js @@ -10,12 +10,7 @@ export class Camera extends GameObject { height = GAME_HEIGHT, speed = 1, }) { - super({ x, y }); - this.gameObjects = gameObjects; - this.x = x; - this.y = y; - this.width = width; - this.height = height; + super({ x, y, gameObjects, width, height }); this.speed = speed; this.keys = [ { key: "ArrowUp", pressed: false, value: [0, -1] }, diff --git a/modules/game-objects/game-object.js b/modules/game-objects/game-object.js index 609726b..cbb738b 100644 --- a/modules/game-objects/game-object.js +++ b/modules/game-objects/game-object.js @@ -1,4 +1,4 @@ -import { eventEmitter } from "../event-emitter.js"; +import { EventEmitter } from "../event-emitter.js"; export class GameObject { constructor(options = {}) { @@ -16,7 +16,7 @@ export class GameObject { this.height = height; this.gameObjects = gameObjects; this.debug = debug; - this.eventEmitter = eventEmitter; + this.eventEmitter = new EventEmitter(); } async load() { @@ -52,9 +52,7 @@ export class GameObject { } onMouseClick(elementId) { - if (elementId === "debug") { - this.debug = !this.debug; - } + if (elementId === "debug") this.debug = !this.debug; this.gameObjects.forEach((item) => { item.onMouseClick(elementId); }); diff --git a/modules/game-objects/map-management.js b/modules/game-objects/map-management.js index 163f280..a6dd3f7 100644 --- a/modules/game-objects/map-management.js +++ b/modules/game-objects/map-management.js @@ -3,8 +3,7 @@ import { Map } from "./map.js"; export class MapManagement extends GameObject { constructor({ maps = [] }) { - super(); - this.gameObjects = maps.map((item) => new Map(item)); + super({ gameObjects: maps.map((item) => new Map(item)) }); this.elementsId = this.gameObjects.map((item) => item.elementId); } diff --git a/modules/game-objects/map.js b/modules/game-objects/map.js index 153b883..e48e212 100644 --- a/modules/game-objects/map.js +++ b/modules/game-objects/map.js @@ -3,15 +3,8 @@ import { createCanvas } from "../utils.js"; import { GameObject } from "./game-object.js"; export class Map extends GameObject { - constructor({ - name, - imageId, - elementId, - layer = 0, - selected = false, - debug = false, - }) { - super({ debug }); + constructor({ name, imageId, elementId, layer = 0, selected = false }) { + super(); this.name = name; this.imageId = imageId; this.layer = layer; @@ -20,7 +13,6 @@ export class Map extends GameObject { this.imageHeight = this.image.height; this.selected = selected; this.elementId = elementId; - this.debug = debug; } async load() { @@ -52,7 +44,8 @@ export class Map extends GameObject { if (row < 0 || col < 0 || row >= this.height || col >= this.width) continue; - if (this.debug) { + const tile = this.data[row * this.width + col] - 1; + if (this.debug && tile > -1) { ctx.strokeRect( col * TILE_SIZE, row * TILE_SIZE, @@ -61,7 +54,6 @@ export class Map extends GameObject { ); } - const tile = this.data[row * this.width + col] - 1; ctx.drawImage( this.image, (tile * TILE_SIZE) % this.imageWidth, @@ -80,10 +72,8 @@ export class Map extends GameObject { } onMouseClick(elementId) { - if (elementId === "debug") { - this.debug = !this.debug; - this._level = null; - } + super.onMouseClick(elementId); + if (elementId === "debug") this._level = null; } render(ctx, sourceX, sourceY, width, height) {