summaryrefslogtreecommitdiff
path: root/src/engine/systems/Input.ts
diff options
context:
space:
mode:
authorElizabeth Hunt <elizabeth.hunt@simponic.xyz>2024-03-01 19:45:33 -0700
committerElizabeth Hunt <elizabeth.hunt@simponic.xyz>2024-03-01 19:45:33 -0700
commitd08e0105cbc59c6cc804f04aaf1e4e625a13960c (patch)
tree57d5270e146ce2e0c5cfba30e172ab87a2210514 /src/engine/systems/Input.ts
parenta8d07a790395e14fe7aedd3ba638db466f9c0842 (diff)
downloadthe-abstraction-engine-d08e0105cbc59c6cc804f04aaf1e4e625a13960c.tar.gz
the-abstraction-engine-d08e0105cbc59c6cc804f04aaf1e4e625a13960c.zip
eyes follow cursor
Diffstat (limited to 'src/engine/systems/Input.ts')
-rw-r--r--src/engine/systems/Input.ts63
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;
+ }
+}