diff options
author | Elizabeth Hunt <elizabeth.hunt@simponic.xyz> | 2024-03-01 21:29:40 -0700 |
---|---|---|
committer | Elizabeth Hunt <elizabeth.hunt@simponic.xyz> | 2024-03-01 21:29:40 -0700 |
commit | c3242b171cdbb36a26fda04c7148b9b40a5f5c33 (patch) | |
tree | 5060cb6d34e01f36687c0ce79e5ae0b1b8767e63 /src/engine/systems/Input.ts | |
parent | d08e0105cbc59c6cc804f04aaf1e4e625a13960c (diff) | |
download | the-abstraction-engine-c3242b171cdbb36a26fda04c7148b9b40a5f5c33.tar.gz the-abstraction-engine-c3242b171cdbb36a26fda04c7148b9b40a5f5c33.zip |
player movement
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); } } |