-
Notifications
You must be signed in to change notification settings - Fork 4
/
state.ts
141 lines (114 loc) · 4.3 KB
/
state.ts
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
namespace tileUtil {
export class _TileMapState {
connections: _TileMapConnection[];
constructor(public map: tiles.TileMapData) {
this.connections = [];
}
}
export class _TileMapConnection {
constructor(public readonly id: number, public target: _TileMapState) {}
}
export class _CoveredTile {
constructor(public column: number, public row: number, public image: Image) { }
}
export class _TileUtilState {
coveredTiles: _CoveredTile[];
mapStates: _TileMapState[];
constructor() {
this.coveredTiles = [];
this.mapStates = [];
onMapUnloaded(() => {
this.coveredTiles = [];
})
scene.createRenderable(-.9, (target, camera) => this.draw(target, camera));
}
coverTile(column: number, row: number, image: Image) {
for (const tile of this.coveredTiles) {
if (tile.column === column && tile.row === row) {
tile.image = image;
return;
}
}
this.coveredTiles.push(new _CoveredTile(column, row, image));
}
clearCoveredTiles() {
this.coveredTiles = [];
}
draw(canvas: Image, camera: scene.Camera) {
const tilemap = game.currentScene().tileMap;
if (!tilemap || !tilemap.enabled) return;
let leftColumn = camera.left >> tilemap.scale;
let rightColumn = camera.right >> tilemap.scale;
let topRow = camera.top >> tilemap.scale;
let bottomRow = camera.bottom >> tilemap.scale;
for (const tile of this.coveredTiles) {
if (tile.column >= leftColumn && tile.column <= rightColumn && tile.row >= topRow && tile.row <= bottomRow) {
canvas.drawTransparentImage(
tile.image,
(tile.column << tilemap.scale) - camera.drawOffsetX,
(tile.row << tilemap.scale) - camera.drawOffsetY
);
}
}
}
getStateForMap(map: tiles.TileMapData) {
for (const state of this.mapStates) {
if (state.map === map) return state;
}
const newState = new _TileMapState(map);
this.mapStates.push(newState);
return newState;
}
connectMaps(a: tiles.TileMapData, b: tiles.TileMapData, connectionId: number) {
const aState = this.getStateForMap(a);
const bState = this.getStateForMap(b);
let foundIt = false;
for (const connection of aState.connections) {
if (connection.id === connectionId) {
foundIt = true;
connection.target = bState;
break;
}
}
if (!foundIt) {
aState.connections.push(new _TileMapConnection(connectionId, bState));
}
foundIt = false;
for (const connection of bState.connections) {
if (connection.id === connectionId) {
foundIt = true;
connection.target = aState;
break;
}
}
if (!foundIt) {
bState.connections.push(new _TileMapConnection(connectionId, aState));
}
}
getConnectedMap(map: tiles.TileMapData, connectionId: number) {
const state = this.getStateForMap(map);
for (const connection of state.connections) {
if (connection.id === connectionId) return connection.target.map;
}
return undefined;
}
}
let _stateStack: _TileUtilState[];
export function _state() {
_init();
return _stateStack[_stateStack.length - 1];
}
export function _init() {
if (_stateStack) return;
_stateStack = [new _TileUtilState()];
game.addScenePushHandler(() => {
_stateStack.push(new _TileUtilState());
});
game.addScenePopHandler(() => {
_stateStack.pop();
if (_stateStack.length === 0) {
_stateStack.push(new _TileUtilState());
}
});
}
}