From d08e0105cbc59c6cc804f04aaf1e4e625a13960c Mon Sep 17 00:00:00 2001 From: Elizabeth Hunt Date: Fri, 1 Mar 2024 19:45:33 -0700 Subject: eyes follow cursor --- src/engine/systems/Input.ts | 63 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 src/engine/systems/Input.ts (limited to 'src/engine/systems/Input.ts') 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; + 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; + } +} -- cgit v1.2.3-70-g09d2