-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplane.js
143 lines (115 loc) · 5.55 KB
/
plane.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
"use strict";
class Plane {
constructor(canvas, amountX, amountY) {
this.canvas = canvas;
this.ctx = this.canvas.getContext("2d");
this.amountX = amountX;
this.amountY = amountY;
this.data = [];
}
createEmptyNodes() {
// Creates a 2D Array of empty nodes for the plane.
for(let y = 0; y < this.amountY; y++) {
const currentArray = [];
for(let x = 0; x < this.amountX; x++) {
const node = new Node(this.canvas, x, y, this.canvas.width / this.amountX, this.canvas.height / this.amountY, false);
currentArray.push(node);
}
this.data.push(currentArray);
}
}
initialize(blueprint = null) {
if(blueprint !== null) {
// Blueprint format = xSize,ySize;data (Binary: 0b[binary data]) (Hexadecimal: 0x[hex data])
blueprint = blueprint.split(";");
// Gets the x and y size of the plane and then creates the empty 2D Array
const planeSize = blueprint[0].split(",");
this.amountX = planeSize[0];
this.amountY = planeSize[1];
this.createEmptyNodes();
const amountNodes = this.amountX * this.amountY;
// Gets the actual data from the blueprint. Also converts the text to lowercase so that 0B or 0X is treated same as 0b or 0x.
let data = blueprint[1].toLowerCase();
if(data.at(1) === "x") {
data = convertHexToBin(data);
}
data = data.replace("0b", "");
// If the length of the data does not match the length it should be, it means that 0s have been added to the start of the binary number to easily convert it to hexadecimal.
// This removes the leading zeros, since all data would be pushed x forward if the leading zeros weren't to be removed.
if(data.length > amountNodes) {
const delta = Math.abs(data.length - amountNodes);
const searchRegExp = new RegExp(`^0{${delta}}`);
data = data.replace(searchRegExp, "");
}
// Splits the data into a list, where each entry equals one node.
data = data.split("");
// Repeats over all of the nodes inside this plane.
for(let x = 0; x < this.amountX; x++) {
for(let y = 0; y < this.amountY; y++) {
// Converts the x & y indices into the actual position inside the binary string. Then checks whatever or not the current node should be a wall and stores that in currentNodeWall.
const currentNodeWall = parseInt(data[(y * this.amountX) + x], 10);
this.data[y][x].wall = (currentNodeWall === 1) ? true:false;
}
}
} else {this.createEmptyNodes();}
}
draw() {
// Loops through each node and draws them.
for(let y = 0; y < this.amountY; y++) {
const currentArray = this.data[y];
for(let x = 0; x < this.amountX; x++) {
const currentNode = currentArray[x];
currentNode.draw();
//new CanvasText(this.canvas, `X: ${currentNode.x}, Y: ${currentNode.y}`, undefined, currentNode.x * 100 + 100, currentNode.y * 100 + 100, 100, "black", 1).draw();
}
}
}
drawNodesInArray(nodes, color) {
for(let nodeID of nodes) {
const nodeCoords = nodeID.split(",");
const node = this.data[nodeCoords[1]][nodeCoords[0]];
node.draw(color);
}
}
inBounds(coord) {
if(coord.x < 0 || coord.x >= this.amountX || coord.y < 0 || coord.y >= this.amountY) {
return(false);
} else {
return(true);
}
}
distance(coord1, coord2) {
// Returns the Euclidean Distance between two points.
return(Math.sqrt(Math.pow(coord1.x - coord2.x, 2) + Math.pow(coord1.y - coord2.y, 2)));
}
lazyDistance(coord1, coord2) {
// Gets the distance between two nodes by adding how many times you need to take a 45° Angled Diagonal (weighted 14) and a 90° Angled Straight (weighted 10) from the first node to the second one.
let deltaX = Math.abs(coord1.x - coord2.x);
let deltaY = Math.abs(coord1.y - coord2.y);
// Gets the amount of 45° Diagonals you'd have to take (By just taking the larger of the two deltas)
const diagonals = (deltaX < deltaY) ? deltaX:deltaY;
// Gets the amount of Straights you'd have to take (By taking the larger of the two deltas and subtracting the smaller of the two deltas)
const straights = (deltaX < deltaY) ? deltaY - diagonals:deltaX - diagonals;
const distance = (diagonals * 14) + (straights * 10);
return(distance);
}
getNodeAtCoords(x, y) {
const coordX = Math.floor(x * this.amountX / this.canvas.width);
const coordY = Math.floor(y * this.amountY / this.canvas.height);
return(this.data[coordY][coordX]);
}
savePlaneAsBlueprint() {
// Blueprint format = xSize,ySize;data (Binary: 0b[binary data]) (Hexadecimal: 0x[hex data])
let blueprint = "";
blueprint += `${this.amountX},${this.amountY};`
let data = "0b";
for(let y = 0; y < this.amountY; y++) {
const currentArray = this.data[y];
for(let x = 0; x < this.amountX; x++) {
data += (currentArray[x].wall) ? "1":"0";
}
}
blueprint += convertBinToHex(data);
return(blueprint);
}
}