diff options
Diffstat (limited to 'src/engine/systems/Input.ts')
-rw-r--r-- | src/engine/systems/Input.ts | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/src/engine/systems/Input.ts b/src/engine/systems/Input.ts new file mode 100644 index 0000000..9b88378 --- /dev/null +++ b/src/engine/systems/Input.ts @@ -0,0 +1,63 @@ +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<string>; + 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<Control>( + 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; + } +} |