diff options
author | Matthias Schiffer <mschiffer@universe-factory.net> | 2016-01-06 21:39:15 +0100 |
---|---|---|
committer | Matthias Schiffer <mschiffer@universe-factory.net> | 2016-01-06 21:39:15 +0100 |
commit | b7fec57c0463e58f1ad8db126ff4879f59b057d0 (patch) | |
tree | 1fce6d1471b27a91cce16bf42f91af3d6aa9bd1e /src/control | |
parent | c64ead08a36fccc36a42b3e74362aa423cfb62ed (diff) | |
download | rpgedit-b7fec57c0463e58f1ad8db126ff4879f59b057d0.tar rpgedit-b7fec57c0463e58f1ad8db126ff4879f59b057d0.zip |
Start implementing input handling
Diffstat (limited to 'src/control')
-rw-r--r-- | src/control/MapContext.ts | 33 |
1 files changed, 30 insertions, 3 deletions
diff --git a/src/control/MapContext.ts b/src/control/MapContext.ts index 4bc75c7..df7eff6 100644 --- a/src/control/MapContext.ts +++ b/src/control/MapContext.ts @@ -7,6 +7,7 @@ import EntityPosition from '../model/EntityPosition'; import MapData from '../model/MapData'; import Position from '../model/Position'; +import InputHandler from '../view/InputHandler'; import MapView from '../view/MapView'; @@ -16,7 +17,7 @@ export default class MapContext { entities: {[key: string]: EntityPosition} = {}; playerEntity: EntityPosition; - constructor(public map: MapData) { + constructor(private map: MapData, private inputHandler: InputHandler) { this.playerEntity = new EntityPosition( new Entity('square'), new Position(8, 8), @@ -25,10 +26,36 @@ export default class MapContext { this.addEntity(this.playerEntity); - this.view = new MapView(map, this.entities); + this.view = new MapView(map, this.entities, (time: number) => this.updateState(time)); + + inputHandler.addListener(() => { + if (this.updateState(performance.now())) + this.view.redraw(); + }); } - addEntity(entity: EntityPosition) { + private addEntity(entity: EntityPosition) { this.entities[entity.position.asString()] = entity; } + + private updateState(time: number): boolean { + var ret = false; + var dir = this.playerEntity.direction; + + if (this.inputHandler.keys[InputHandler.Up]) + dir = Direction.North; + else if (this.inputHandler.keys[InputHandler.Right]) + dir = Direction.East; + else if (this.inputHandler.keys[InputHandler.Down]) + dir = Direction.South; + else if (this.inputHandler.keys[InputHandler.Left]) + dir = Direction.West; + + if (this.playerEntity.direction !== dir) { + this.playerEntity.direction = dir; + ret = true; + } + + return ret; + } } |