diff options
Diffstat (limited to 'src/engine/systems/Input.ts')
-rw-r--r-- | src/engine/systems/Input.ts | 85 |
1 files changed, 53 insertions, 32 deletions
diff --git a/src/engine/systems/Input.ts b/src/engine/systems/Input.ts index e9691e0..df4d651 100644 --- a/src/engine/systems/Input.ts +++ b/src/engine/systems/Input.ts @@ -1,6 +1,6 @@ import { SystemNames, System } from "."; import { Game } from ".."; -import { ComponentNames } from "../components"; +import { ComponentNames, Grid, Interactable } from "../components"; import { Control } from "../components/Control"; import { Action, KeyConstants } from "../config"; import { Entity } from "../entities"; @@ -31,11 +31,30 @@ export class Input extends System { public update(_dt: number, game: Game) { game.forEachEntityWithComponent(ComponentNames.Control, (entity) => - this.handleInput(entity), + this.handleMovement(entity), ); + game.forEachEntityWithComponent(ComponentNames.Interactable, (entity) => + this.handleInteraction(entity), + ); + } + + private handleInteraction(entity: Entity) { + const interactable = entity.getComponent<Interactable>( + ComponentNames.Interactable, + ); + + const interact = this.hasSomeKey( + KeyConstants.ActionKeys.get(Action.INTERACT), + ); + + if (!interact) { + return; + } + + interactable.interact(); } - public handleInput(entity: Entity) { + public handleMovement(entity: Entity) { const controlComponent = entity.getComponent<Control>( ComponentNames.Control, ); @@ -50,36 +69,38 @@ export class Input extends System { Action.MOVE_RIGHT, Action.MOVE_DOWN, ].map((action) => this.hasSomeKey(KeyConstants.ActionKeys.get(action))); - if (hasGrid) { - const gridComponent = entity.getComponent(ComponentNames.Grid); - if (gridComponent.movingDirection !== Direction.NONE) { - return; - } - - if (moveUp) { - gridComponent.movingDirection = Direction.UP; - KeyConstants.ActionKeys.get(Action.MOVE_UP)!.forEach((key) => - this.keyReleased(key), - ); - } else if (moveLeft) { - gridComponent.movingDirection = Direction.LEFT; - KeyConstants.ActionKeys.get(Action.MOVE_LEFT)!.forEach((key) => - this.keyReleased(key), - ); - } else if (moveRight) { - gridComponent.movingDirection = Direction.RIGHT; - KeyConstants.ActionKeys.get(Action.MOVE_RIGHT)!.forEach((key) => - this.keyReleased(key), - ); - } else if (moveDown) { - gridComponent.movingDirection = Direction.DOWN; - KeyConstants.ActionKeys.get(Action.MOVE_DOWN)!.forEach((key) => - this.keyReleased(key), - ); - } - - entity.addComponent(gridComponent); + if (!hasGrid) { + return; + } + + const gridComponent = entity.getComponent<Grid>(ComponentNames.Grid)!; + if (gridComponent.movingDirection !== Direction.NONE) { + return; } + + if (moveUp) { + gridComponent.movingDirection = Direction.UP; + KeyConstants.ActionKeys.get(Action.MOVE_UP)!.forEach((key) => + this.keyReleased(key), + ); + } else if (moveLeft) { + gridComponent.movingDirection = Direction.LEFT; + KeyConstants.ActionKeys.get(Action.MOVE_LEFT)!.forEach((key) => + this.keyReleased(key), + ); + } else if (moveRight) { + gridComponent.movingDirection = Direction.RIGHT; + KeyConstants.ActionKeys.get(Action.MOVE_RIGHT)!.forEach((key) => + this.keyReleased(key), + ); + } else if (moveDown) { + gridComponent.movingDirection = Direction.DOWN; + KeyConstants.ActionKeys.get(Action.MOVE_DOWN)!.forEach((key) => + this.keyReleased(key), + ); + } + + entity.addComponent(gridComponent); } private hasSomeKey(keys?: string[]): boolean { |