summaryrefslogtreecommitdiffstats
path: root/src/control
diff options
context:
space:
mode:
Diffstat (limited to 'src/control')
-rw-r--r--src/control/MapContext.ts54
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;
}
}