blob: df7eff6cd37ca7ffb0adfb6a408de7b7da5bdf62 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
'use strict';
import Direction from '../model/Direction';
import Entity from '../model/Entity';
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';
export default class MapContext {
view: MapView;
entities: {[key: string]: EntityPosition} = {};
playerEntity: EntityPosition;
constructor(private map: MapData, private inputHandler: InputHandler) {
this.playerEntity = new EntityPosition(
new Entity('square'),
new Position(8, 8),
Direction.East
);
this.addEntity(this.playerEntity);
this.view = new MapView(map, this.entities, (time: number) => this.updateState(time));
inputHandler.addListener(() => {
if (this.updateState(performance.now()))
this.view.redraw();
});
}
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;
}
}
|