-
Notifications
You must be signed in to change notification settings - Fork 1
/
search.js
171 lines (157 loc) · 4.03 KB
/
search.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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// 参考: https://codepen.io/clindsey/pen/yNvYxE
export default class Pathfinder{
constructor(gridData, targetPosition, foundCallback) {
this.gridData = gridData;
this.targetPosition = targetPosition;
this.foundCallback = foundCallback;
this.width = this.gridData[0].length;
this.height = this.gridData.length;
}
parseGridData() {
var cell, data, index, node, row, x, y, _i, _j, _len, _len1, _ref;
data = [];
_ref = this.gridData;
for (y = _i = 0, _len = _ref.length; _i < _len; y = ++_i) {
row = _ref[y];
for (x = _j = 0, _len1 = row.length; _j < _len1; x = ++_j) {
cell = row[x];
index = y * this.width + x;
node = {
open: !cell, // 如果是墙,直接不能访问这个节点
index: index,
fromIndex: null, // 当前访问的这个节点的上一个节点是谁
x: x,
y: y
};
data[index] = node;
}
}
return data;
};
beginFill(wolf) {
let {x, y} = wolf;
x = x / window.CELL_W;
y = y / window.CELL_W;
this.startPosition = [x, y];
this.reset();
this.closeNode(x, y);
this.addNeighbors(x, y);
while (!this.isFinished) {
this.nextStep();
}
};
/**
* @description 找出距startPosition节点step范围内的一个可用节点
* @return [x,y]
*/
findSteps(startPosition, step) {
let [x, y] = startPosition;
this.startPosition = startPosition;
this.reset();
this.closeNode(x, y);
this.addNeighbors(x, y);
while (step--) {
this.nextStep();
}
};
nextStep() {
var x, y, _ref;
if (this.open.length === 0) {
this.isFinished = true;
return;
}
_ref = this.iToC(this.open.shift()), x = _ref[0], y = _ref[1];
this.closeNode(x, y);
return this.addNeighbors(x, y);
};
addNeighbors(x, y) {
var index;
index = this.data[this.cToI(x, y)].index;
this.addOpen(x + 1, y, index);
x - 1 >= 0 && this.addOpen(x - 1, y, index);
this.addOpen(x, y + 1, index);
y - 1 >= 0 && this.addOpen(x, y - 1, index);
};
// 访问到这个节点
addOpen(x, y, nodeIndex) {
var index, node, tX, tY, _ref;
if (x < 0 || x > this.width - 1 || y < 0 || y > this.height - 1) return;
index = this.cToI(x, y);
// console.log(x, y)
node = this.data[index];
// console.log(node)
if (node.open) {
this.open.push(index);
node.fromIndex = nodeIndex;
node.open = false;
}
_ref = this.targetPosition, tX = _ref[0], tY = _ref[1];
if (x === tX && y === tY) {
return this.targetFound();
}
};
closeNode(x, y) {
var node;
node = this.data[this.cToI(x, y)];
try {
node.open = false;
} catch (err) {
console.log(this);
console.log(x, y);
}
};
targetFound() {
var _results;
this.isFinished = true;
this.solutionPath = [];
this.solutionPath.push(this.targetPosition);
_results = [];
while (this.walkSolution()) {
_results.push(true);
}
return _results;
};
pathFound() {
var path;
path = this.solutionPath.reverse();
this.path = path.slice(1);
this.foundCallback && this.foundCallback(path);
};
walkSolution() {
var lastNode, nextNode, _ref;
_ref = this.solutionPath[this.solutionPath.length - 1];
let [x, y] = _ref;
lastNode = this.data[this.cToI(x, y)];
try {
nextNode = this.data[lastNode.fromIndex];
this.solutionPath.push([nextNode.x, nextNode.y]);
if (!nextNode.fromIndex) {
this.isFinished = true;
this.pathFound();
return false;
}
} catch(err) {
throw(err);
}
// console.log(nextNode.index);
return true;
};
reset() {
this.open = [];
this.data = this.parseGridData();
this.isFinished = false;
return ;
};
// 列转换成idx
cToI(x, y) {
return y * this.width + x;
};
// idx转换成坐标
iToC(index) {
var x, y;
x = index % this.width;
y = Math.floor(index / this.width);
return [x, y];
};
}
module.exports = Pathfinder;