This repository has been archived by the owner on Oct 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunits.js
53 lines (49 loc) · 1.81 KB
/
units.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
class Unit {
constructor(level, type, start, speed) {
this.start = start;
this.type = type;
this.level = level;
this.speed = speed;
}
}
function finder(x1, y1, x2, y2, allow) {
/*
x1 and y1 are the coordinates of the start position
x2 and y2 are the end coordinates
Allow is an array of all tiles that can be walked on
*/
let grid = new PF.Grid(256, 256);
for (let i = 0; i < 256; i++) {
for (let j = 0; j < 256; j++) {
grid.setWalkableAt(i, j, allow.indexOf(map.matrix[i][j]) > -1);
}
}
let findery = new PF.AStarFinder();
let found = findery.findPath(x1, y1, x2, y2, grid);
return found;
// console.log(found)
// draw pathfinding
mixed2dDraw.strokeStyle = "rgb(51, 51, 51)";
mixed2dDraw.lineWidth = 5;
mixed2dDraw.beginPath();
let hConst = mixed2dC.height / 256;
let wConst = mixed2dC.width / 256;
mixed2dDraw.moveTo(x1 * wConst+wConst/2, y1 * hConst+hConst/2);
for (let i = 0; i < found.length; i++) {
mixed2dDraw.lineTo(found[i][0] * wConst+wConst/2, found[i][1] * hConst+hConst/2);
}
mixed2dDraw.stroke();
}
// ISSUE: It makes the map SUPER laggy - PARTIALLY FIXED!!
setInterval(async () => {
// let t0=performance.now();
if (map.highlighted[0]) {
let t = translate((mouseX - (c.width / 2))*scale, (mouseY - (c.height / 2))*scale, -mapInfo.x, -mapInfo.y + (osC.height / 2)); // Inverts translation
let out = map.whichSquare(t[0], t[1], false);
// mixed2dDraw.drawImage(osC2, 0, 0);
finder(map.highlighted[0], map.highlighted[1], out[0], out[1], ["Grassland", "Desert", "Forest"]); // this causes at least 30ms of lag
// osDraw.drawImage(mixed2dC, 0, 0);
}
// let t1 = performance.now();
// console.log(t1-t0);
}, 500);