Merge pull request 'feat(#10): remove redundancy' (#14) from dev into main

Reviewed-on: #14
This commit is contained in:
Juan Sebastián Montoya 2024-09-20 00:45:38 -05:00
commit 27b2732446
5 changed files with 22 additions and 45 deletions

View file

@ -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();

View file

@ -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] },

View file

@ -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);
});

View file

@ -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);
}

View file

@ -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) {