diff options
author | Matthias Schiffer <mschiffer@universe-factory.net> | 2016-01-07 11:22:59 +0100 |
---|---|---|
committer | Matthias Schiffer <mschiffer@universe-factory.net> | 2016-01-07 11:22:59 +0100 |
commit | 584728a1293a5215baaca4e7de813b2a85b8253d (patch) | |
tree | 61fe7f216fa857d0a86d8ed28f3346d242f6b992 /src/control | |
parent | b7fec57c0463e58f1ad8db126ff4879f59b057d0 (diff) | |
download | rpgedit-584728a1293a5215baaca4e7de813b2a85b8253d.tar rpgedit-584728a1293a5215baaca4e7de813b2a85b8253d.zip |
Implement smooth transitions
Diffstat (limited to 'src/control')
-rw-r--r-- | src/control/MapContext.ts | 54 |
1 files changed, 39 insertions, 15 deletions
diff --git a/src/control/MapContext.ts b/src/control/MapContext.ts index df7eff6..1e417e7 100644 --- a/src/control/MapContext.ts +++ b/src/control/MapContext.ts @@ -6,6 +6,7 @@ import Entity from '../model/Entity'; import EntityPosition from '../model/EntityPosition'; import MapData from '../model/MapData'; import Position from '../model/Position'; +import Transition from '../model/Transition'; import InputHandler from '../view/InputHandler'; import MapView from '../view/MapView'; @@ -38,24 +39,47 @@ export default class MapContext { this.entities[entity.position.asString()] = entity; } + private addTransition(entity: EntityPosition, dest: Position, start: number, dur: number) { + entity.transition = new Transition(start, start+dur, entity.position, dest); + } + + private finishTransition(entity: EntityPosition) { + entity.position = entity.transition.dest; + entity.transition = null; + } + 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) { + + while (true) { + var origTime = time; + + if (this.playerEntity.transition && this.playerEntity.transition.end <= time) { + origTime = this.playerEntity.transition.end; + this.finishTransition(this.playerEntity); + ret = true; + } + + if (this.playerEntity.transition) + return true; + + var dir: Direction = null; + + 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 (dir === null) + return ret; + this.playerEntity.direction = dir; - ret = true; + this.addTransition(this.playerEntity, this.playerEntity.position.translate(dir, 1), + origTime, 250); } - - return ret; } } |