diff options
Diffstat (limited to 'src/engine/systems/Input.ts')
-rw-r--r-- | src/engine/systems/Input.ts | 42 |
1 files changed, 39 insertions, 3 deletions
diff --git a/src/engine/systems/Input.ts b/src/engine/systems/Input.ts index 9b88378..e9691e0 100644 --- a/src/engine/systems/Input.ts +++ b/src/engine/systems/Input.ts @@ -4,7 +4,7 @@ import { ComponentNames } from "../components"; import { Control } from "../components/Control"; import { Action, KeyConstants } from "../config"; import { Entity } from "../entities"; -import { Coord2D } from "../interfaces"; +import { Coord2D, Direction } from "../interfaces"; export class Input extends System { private keys: Set<string>; @@ -41,8 +41,44 @@ export class Input extends System { ); if (!controlComponent.isControllable) return; - if (this.hasSomeKey(KeyConstants.ActionKeys.get(Action.INTERACT))) { - console.log("interact"); + const hasGrid = entity.hasComponent(ComponentNames.Grid); + + // TODO: check grid via controlComponent to notify entity of interaction + const [moveUp, moveLeft, moveRight, moveDown] = [ + Action.MOVE_UP, + Action.MOVE_LEFT, + 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); } } |