import { SystemNames, System } from "."; import { Game } from ".."; import { ComponentNames } from "../components"; import { Control } from "../components/Control"; import { Action, KeyConstants } from "../config"; import { Entity } from "../entities"; import { Coord2D } from "../interfaces"; export class Input extends System { private keys: Set; private mousePosition: Coord2D; constructor() { super(SystemNames.Input); this.keys = new Set(); this.mousePosition = { x: 0, y: 0 }; } public clearKeys() { this.keys.clear(); } public keyPressed(key: string) { this.keys.add(key); } public keyReleased(key: string) { this.keys.delete(key); } public update(_dt: number, game: Game) { game.forEachEntityWithComponent(ComponentNames.Control, (entity) => this.handleInput(entity), ); } public handleInput(entity: Entity) { const controlComponent = entity.getComponent( ComponentNames.Control, ); if (!controlComponent.isControllable) return; if (this.hasSomeKey(KeyConstants.ActionKeys.get(Action.INTERACT))) { console.log("interact"); } } private hasSomeKey(keys?: string[]): boolean { if (keys) { return keys.some((key) => this.keys.has(key)); } return false; } public setMousePosition(mousePosition: Coord2D) { this.mousePosition = mousePosition; } public getMousePosition(): Coord2D { return this.mousePosition; } }