import {
    generateRandomNumber
} from "../../utils/numbers.js";
import {
    fetchTime
} from "../../utils/customConsole.js";
import Config from "./Config.js";
import { HealthComponent, MovementComponent } from "./components/Components.js";
import { EventEmitter } from 'events';
const eventEmitter = new EventEmitter();

class Entity {
    constructor(type, uid, x, y) {
        this.type = type;
        this.uid = Number(uid);
        this.x = Number(x);
        this.y = Number(y);
        this.entityValues = {
            isDead: false
        }
        this.components = {};
    }

    addComponent(componentName, component) {
        this.components[componentName] = component;
    }

    removeComponent(componentName) {
        delete this.components[componentName];
    }
}

class PlayerEntity extends Entity {
    constructor(uid, x, y) {
        super('player', uid, x, y);

        this.addComponent('health', new HealthComponent(100, this));
        this.addComponent('movement', new MovementComponent(this));
    }
}

class ZombieEntity extends Entity {
    constructor(uid, x, y, health) {
        super('zombie', uid, x, y);

        this.addComponent('health', new HealthComponent(health));
    }
}

class World {
    constructor() {
        this.worldX = 100;
        this.worldY = 100;
        this.entities = [];
        this.randomMove = null;

        if (Config.debug == true) { 
            eventEmitter.on('entityChange', (newEntities) => {
                console.log('Entities have been updated at', fetchTime());
            });
        }
    }

    addEntity(entity) {
        this.entities.push(entity);
        eventEmitter.emit('entityChange', this.entities);
    }

    spawnPlayer(uid) {
        const player = new PlayerEntity(uid, generateRandomNumber(2), generateRandomNumber(2));
        this.addEntity(player);
    }

    movePlayer(uid, direction) {
        const player = this.entities.find(entity => entity instanceof PlayerEntity && entity.uid === uid)
        
        switch (direction) {
            case 'up':
                player.components.movement.moveEntity(0, 1);
                break;
            case 'down':
                player.components.movement.moveEntity(0, -1);
                break;
            case 'left':
                player.components.movement.moveEntity(-1, 0);
                break;
            case 'right':
                player.components.movement.moveEntity(1, 0);
                break;
            case 'test':
                player.components.movement.moveEntity(1, 1);

                console.log(player.x, player.y)
                break;
        }
    }

    playerUpdateHealth(uid, type, amount) {
        const player = this.entities.find(entity => entity instanceof PlayerEntity && entity.uid === uid)

        switch (type) {
            case 'decreaseHealth':
                player.components.health.decreaseHealth(amount);
                this.checkState(player);
                break;
            case 'increaseHealth':
                player.components.health.increaseHealth(amount);
                break;
        }
    }

    checkState(player) {
        if (player.entityValues.isDead == true) {
            console.log('Player is dead!');
        }
    }
}

const world = new World();

export default world;

Optimize and improve this code