-
Notifications
You must be signed in to change notification settings - Fork 0
/
mapHelper.js
80 lines (67 loc) · 2.38 KB
/
mapHelper.js
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
const pathFinder = new Worker('pathFinder.js', {type: 'module'});
const mapData = [
['f0_w0', 'f3_w1', 'f2_w1', 'f3_w1', 'f4_w1'],
['f1_w2', 'f5 ', 'f0 ', 'f5 ', 'f1 '],
['f0_w2', 'f0 ', 'f5 ', 'f0 ', 'f0 '],
['f1_w2', 'f0 ', 'f0_w0', 'f0 ', 'f1_w3'],
['f0_w2', 'f3 ', 'f2 ', 'f3 ', 'f0 '],
['f0 ', 'f0 ', 'f0 ', 'f0 ', 'f0 '],
['f0 ', 'f0_w0', 'f0_w1', 'f0_w1', 'f0_w1'],
['f0 ', 'f0_w2', 'f0 ', 'f0 ', 'f0 '],
['f0 ', 'f0_w2', 'f0 ', 'f0_w3', 'f0 '],
['f0 ', 'f0_w2', 'f0 ', 'f0_w2', 'f0 '],
['f0 ', 'f0_w0', 'f0_w1', 'f0_w0', 'f0 '],
['f0 ', 'f0 ', 'f0 ', 'f0 ', 'f0 '],
];
export default {
mapData,
buildMap,
initPathfinder,
getPath,
};
function buildMap(grid, tileConstructor) {
grid.forEach((gridRow, rowIndex) => {
gridRow.forEach((tileData, columnIndex) => {
tileData = tileData.trim();
if (tileData) {
tileData = tileConstructor(tileData, rowIndex, columnIndex);
}
});
});
}
function initPathfinder(grid, tileWidth, tileHeight) {
const pathFindingGrid = grid.map((gridRow) => gridRow.map((tileData) => {
tileData = tileData.trim();
return Number(!tileData || !!tileData.split('_')[1]);
}));
pathFinder.postMessage({
type: 'init',
payload: {
grid: pathFindingGrid,
tileWidth,
tileHeight,
},
});
}
function getPath(originPosition, targetPosition, assetWidth, assetHeight) {
return new Promise((resolve, reject) => {
function errorHandler(e) {
pathFinder.removeEventListener('error', errorHandler);
pathFinder.removeEventListener('message', messageHandler);
reject(e);
}
function messageHandler({data: response}) {
pathFinder.removeEventListener('error', errorHandler);
pathFinder.removeEventListener('message', messageHandler);
if (response.type === 'path') {
resolve(response.path);
}
}
pathFinder.addEventListener('error', errorHandler);
pathFinder.addEventListener('message', messageHandler);
pathFinder.postMessage({
type: 'getPath',
payload: {originPosition, targetPosition, assetWidth, assetHeight},
});
});
}