-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.html
275 lines (222 loc) · 7.46 KB
/
test.html
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
<html>
<head>
<style>
canvas {
border: 1px solid black;
}
</style>
</head>
<body>
<p>Hello</p>
<canvas id="canvas"></canvas>
<script>
function getMousePosition(evt) {
var rect = c.getBoundingClientRect();
return {
x: evt.clientX - rect.left,
y: evt.clientY - rect.top,
}
};
function getAlignedPos(pos) {
off = {'x': pos.x % TILE_SIZE,
'y': pos.y % TILE_SIZE};
return {
'x': pos.x + (off.x < TILE_SIZE ? -off.x : off.x),
'y': pos.y + (off.y < TILE_SIZE ? -off.y : off.y)
};
}
var Node = function() {
this.parent = null; // Node
this.tile = null; // vector
this.cost = 0; // heuristic + move_count
this.move_count = 0; // int
this.heuristic = 0; // double [distance]
};
var c = document.getElementById("canvas");
c.width = 800;
c.height = 600;
var ctx = c.getContext("2d");
var TILE_SIZE = 32;
function drawRect(x, y, w, h, color, fill) {
w = w || TILE_SIZE;
h = h || TILE_SIZE;
color = color || "black";
fill = fill || false;
ctx.beginPath();
ctx.rect(x, y, w, h);
if (fill) {
ctx.fillStyle = color;
ctx.fill();
} else {
ctx.strokeStyle = color;
ctx.stroke();
}
}
tiles = {};
for (var x = 0; x < c.width; x += TILE_SIZE) {
tiles[x] = {};
for (var y = 0; y < c.height; y += TILE_SIZE) {
tiles[x][y] = { 'x': x, 'y': y, 'taken': false };
}
}
function getTile(x, y) {
if (!(x in tiles)) return null;
if (!(y in tiles[x])) return null;
return tiles[x][y];
}
function render() {
for (var x in tiles) {
for (var y in tiles[x]) {
var t = tiles[x][y];
drawRect(t.x, t.y, 0, 0, 0, t.taken);
}
}
drawRect(START.x, START.y, 0, 0, '#FFFF00', true);
drawRect(END.x, END.y, 0, 0, '#FF00FF', true);
}
function getNeighbors(node) {
var n = [];
n.push(getTile(node.x - TILE_SIZE, node.y - TILE_SIZE));
n.push(getTile(node.x, node.y - TILE_SIZE));
n.push(getTile(node.x + TILE_SIZE, node.y - TILE_SIZE));
n.push(getTile(node.x - TILE_SIZE, node.y));
n.push(getTile(node.x + TILE_SIZE, node.y));
n.push(getTile(node.x - TILE_SIZE, node.y + TILE_SIZE));
n.push(getTile(node.x, node.y + TILE_SIZE));
n.push(getTile(node.x + TILE_SIZE, node.y + TILE_SIZE));
return n.filter(function(e) { return e !== null; });
}
function findPath(start, end, openList, closedList) {
function cmp(a, b) {
return parseInt(a.x) == parseInt(b.x) &&
parseInt(a.y) == parseInt(b.y);
}
ctx.clearRect(0, 0, c.width, c.height);
render();
var path = [];
var openList = [];
var closedList = [];
var node = new Node();
node.tile = start;
openList.push(node);
while (openList.length) {
var idx = 0;
for (var i in openList) {
if (openList[i].cost <= openList[idx].cost) {
idx = i;
}
}
currentNode = openList[idx];
closedList.push(currentNode);
drawRect(currentNode.tile.x, currentNode.tile.y,
0, 0, '#AAAAAA', true);
openList.splice(idx, 1);
if (cmp(currentNode.tile, end)) {
break;
}
var neighbors = getNeighbors(currentNode.tile);
for (var i in neighbors) {
if (neighbors[i].taken) continue;
drawRect(neighbors[i].x+1, neighbors[i].y+1,
TILE_SIZE-2, TILE_SIZE-2, '#FF0000', true);
// Manhattan heuristic.
var h = Math.abs(end.x - neighbors[i].x) +
Math.abs(end.y - neighbors[i].y);
// Distance from last tile.
var g = 14;
if (currentNode.tile.x == neighbors[i].x ||
currentNode.tile.y == neighbors[i].y) {
g = 10;
}
g += currentNode.move_count;
// Is this tile already in the closed (potential path) list?
var closed = null;
for (var j in closedList) {
// We check the references because the .tile attribute
// will be a direct reference to the map object so comparing
// them will work.
if (cmp(closedList[j].tile, neighbors[i])) {
closed = closedList[j];
break;
}
}
if (closed !== null && g >= closed.move_count) continue;
var open = false;
for (var j in openList) {
if (cmp(openList[j].tile, neighbors[i])) {
if (openList[j].move_count > g) {
openList[j].parent = currentNode;
openList[j].heuristic = h;
openList[j].move_count = g;
openList[j].cost = h + g;
}
open = true;
break;
}
}
if (!open) {
var node = new Node();
node.parent = currentNode;
node.tile = neighbors[i];
node.heuristic = h;
node.move_count = g;
node.cost = h + g;
openList.push(node);
}
}
}
if (openList.length == 0) {
alert('no path');
}
for (var i = closedList[closedList.length - 1];
i !== null;
i = i.parent) {
path.push(i.tile);
}
return {
'openList': openList,
'closedList': closedList,
'path': path
};
}
START = { 'x': 0, 'y': 0 };
END = getAlignedPos({ 'x': 200, 'y': 200 });
var path = [];
c.oncontextmenu = function(event) {
event.preventDefault();
};
c.onmousedown = function(evt) {
// rmb
if (evt.button == 2) {
var al = getAlignedPos(getMousePosition(evt));
var t = getTile(al.x, al.y);
if (t === null) return;
t.taken = !t.taken;
// lmb
} else if (evt.button == 0) {
START = getAlignedPos(getMousePosition(evt));
ctx.clearRect(0, 0, 400, 400);
path = [];
// mmb
} else if (evt.button == 1) {
END = getAlignedPos(getMousePosition(evt));
ctx.clearRect(0, 0, 400, 400);
path = [];
evt.preventDefault();
}
}
function loop() {
render();
if (!path.length) {
var ret = findPath(START, END);
path = ret.path;
}
for (var i in path) {
//log(path[i]);
drawRect(path[i].x, path[i].y, 0, 0, '#00FF00', true);
}
requestAnimationFrame(loop);
}
requestAnimationFrame(loop);
</script>
</body>