feat(#10): remove redundancy

This commit is contained in:
Juan Sebastián Montoya 2024-09-17 23:16:36 -05:00
parent 9f1d83bf45
commit 37a7b76d38
5 changed files with 22 additions and 45 deletions

View file

@ -1,32 +1,27 @@
class EventEmitterSingleton { export class EventEmitter {
static instance; static instance;
eventEmitter; events;
constructor() { constructor() {
this.eventEmitter = {}; if (!EventEmitter.instance) {
EventEmitter.instance = this;
this.events = {};
} }
return EventEmitter.instance;
static getInstance() {
if (!EventEmitterSingleton.instance) {
EventEmitterSingleton.instance = new EventEmitterSingleton();
}
return EventEmitterSingleton.instance;
} }
on(eventName, callback) { on(eventName, callback) {
if (!this.eventEmitter[eventName]) { if (!this.events[eventName]) {
this.eventEmitter[eventName] = []; this.events[eventName] = [];
} }
this.eventEmitter[eventName].push(callback); this.events[eventName].push(callback);
} }
emit(eventName, ...args) { emit(eventName, ...args) {
if (this.eventEmitter[eventName]) { if (this.events[eventName]) {
this.eventEmitter[eventName].forEach((callback) => { this.events[eventName].forEach((callback) => {
callback(...args); callback(...args);
}); });
} }
} }
} }
export const eventEmitter = EventEmitterSingleton.getInstance();

View file

@ -10,12 +10,7 @@ export class Camera extends GameObject {
height = GAME_HEIGHT, height = GAME_HEIGHT,
speed = 1, speed = 1,
}) { }) {
super({ x, y }); super({ x, y, gameObjects, width, height });
this.gameObjects = gameObjects;
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.speed = speed; this.speed = speed;
this.keys = [ this.keys = [
{ key: "ArrowUp", pressed: false, value: [0, -1] }, { key: "ArrowUp", pressed: false, value: [0, -1] },

View file

@ -1,4 +1,4 @@
import { eventEmitter } from "../event-emitter.js"; import { EventEmitter } from "../event-emitter.js";
export class GameObject { export class GameObject {
constructor(options = {}) { constructor(options = {}) {
@ -16,7 +16,7 @@ export class GameObject {
this.height = height; this.height = height;
this.gameObjects = gameObjects; this.gameObjects = gameObjects;
this.debug = debug; this.debug = debug;
this.eventEmitter = eventEmitter; this.eventEmitter = new EventEmitter();
} }
async load() { async load() {
@ -52,9 +52,7 @@ export class GameObject {
} }
onMouseClick(elementId) { onMouseClick(elementId) {
if (elementId === "debug") { if (elementId === "debug") this.debug = !this.debug;
this.debug = !this.debug;
}
this.gameObjects.forEach((item) => { this.gameObjects.forEach((item) => {
item.onMouseClick(elementId); item.onMouseClick(elementId);
}); });

View file

@ -3,8 +3,7 @@ import { Map } from "./map.js";
export class MapManagement extends GameObject { export class MapManagement extends GameObject {
constructor({ maps = [] }) { constructor({ maps = [] }) {
super(); super({ gameObjects: maps.map((item) => new Map(item)) });
this.gameObjects = maps.map((item) => new Map(item));
this.elementsId = this.gameObjects.map((item) => item.elementId); this.elementsId = this.gameObjects.map((item) => item.elementId);
} }

View file

@ -3,15 +3,8 @@ import { createCanvas } from "../utils.js";
import { GameObject } from "./game-object.js"; import { GameObject } from "./game-object.js";
export class Map extends GameObject { export class Map extends GameObject {
constructor({ constructor({ name, imageId, elementId, layer = 0, selected = false }) {
name, super();
imageId,
elementId,
layer = 0,
selected = false,
debug = false,
}) {
super({ debug });
this.name = name; this.name = name;
this.imageId = imageId; this.imageId = imageId;
this.layer = layer; this.layer = layer;
@ -20,7 +13,6 @@ export class Map extends GameObject {
this.imageHeight = this.image.height; this.imageHeight = this.image.height;
this.selected = selected; this.selected = selected;
this.elementId = elementId; this.elementId = elementId;
this.debug = debug;
} }
async load() { async load() {
@ -52,7 +44,8 @@ export class Map extends GameObject {
if (row < 0 || col < 0 || row >= this.height || col >= this.width) if (row < 0 || col < 0 || row >= this.height || col >= this.width)
continue; continue;
if (this.debug) { const tile = this.data[row * this.width + col] - 1;
if (this.debug && tile > -1) {
ctx.strokeRect( ctx.strokeRect(
col * TILE_SIZE, col * TILE_SIZE,
row * 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( ctx.drawImage(
this.image, this.image,
(tile * TILE_SIZE) % this.imageWidth, (tile * TILE_SIZE) % this.imageWidth,
@ -80,10 +72,8 @@ export class Map extends GameObject {
} }
onMouseClick(elementId) { onMouseClick(elementId) {
if (elementId === "debug") { super.onMouseClick(elementId);
this.debug = !this.debug; if (elementId === "debug") this._level = null;
this._level = null;
}
} }
render(ctx, sourceX, sourceY, width, height) { render(ctx, sourceX, sourceY, width, height) {