-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.js
More file actions
231 lines (190 loc) · 7.35 KB
/
functions.js
File metadata and controls
231 lines (190 loc) · 7.35 KB
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
import { aStarAlgorithm } from "./Astar.js";
import { Worldoffset, grid } from "./shared.js";
// Calculate the length of a vector
export function GetLength(obj) {
return Math.sqrt(obj.x * obj.x + obj.y * obj.y);
}
// Subtract two vectors
export function Subtract(A, B) {
return { x: A.x - B.x, y: A.y - B.y };
}
// Get the unit vector of a vector
export function GetUnitVect(A) {
const length = GetLength(A);
if (length === 0) return { x: A.x, y: A.y };
return { x: A.x / length, y: A.y / length };
}
// Add two vectors
export function AddVect(A, B) {
return { x: A.x + B.x, y: A.y + B.y };
}
// Calculate the dot product of two vectors
export function Dot(A, B) {
return A.x * B.x + A.y * B.y;
}
// Scale a vector by a scalar value
export function Scale(A, scalar) {
return { x: A.x * scalar, y: A.y * scalar };
}
// Set an object's properties to look at a target object
export function LookAt(obj, target) {
const direction = Subtract(target, obj);
obj.dx = direction.x;
obj.dy = direction.y;
obj.angle = Math.atan2(obj.dy, obj.dx);
}
// Check if an object is looking towards a target object
export function IsLooking(obj, target) {
const normalizedDisplacement = GetUnitVect(Subtract(target, obj));
const normalizedObjOrientation = GetUnitVect({ x: obj.dx, y: obj.dy });
return Math.round(Dot(normalizedDisplacement, normalizedObjOrientation) * 1000);
}
// Calculate the distance between two points
export function CalculateDistance(pointA, pointB) {
const dx = pointB.x - pointA.x;
const dy = pointB.y - pointA.y;
return Math.sqrt(dx * dx + dy * dy);
}
// Check collision between two circles
export function CheckCollision(circleA, circleB) {
const distance = CalculateDistance(circleA, circleB);
return distance <= circleA.radius + circleB.radius;
}
export function CheckCollisionRectangleCircle(rectangle, circle) {
// Calculate the closest point on the rectangle to the circle's center
let closestX = Math.max(rectangle.x, Math.min(circle.x, rectangle.x + rectangle.width));
let closestY = Math.max(rectangle.y, Math.min(circle.y, rectangle.y + rectangle.height));
// Calculate the distance between the closest point and the circle's center
let distanceX = circle.x - closestX;
let distanceY = circle.y - closestY;
let distance = Math.sqrt(distanceX * distanceX + distanceY * distanceY);
// Check if the distance is less than or equal to the circle's radius
return distance <= circle.radius;
}
export function CheckCollisionRectangles(rectangleA, rectangleB) {
// Check if the rectangles overlap on the x-axis
if (rectangleA.x + rectangleA.width < rectangleB.x || rectangleB.x + rectangleB.width < rectangleA.x) {
return false;
}
// Check if the rectangles overlap on the y-axis
if (rectangleA.y + rectangleA.height < rectangleB.y || rectangleB.y + rectangleB.height < rectangleA.y) {
return false;
}
// Rectangles are colliding
return true;
}
export function paintGridOnCanvas(graph, context) {
const cellSize = 32;
// Clear the canvas
// Loop through the grid and paint each cell
for (let cellName in graph) {
const cell = graph[cellName];
const x = (cell.x + Worldoffset.offsetX) * cellSize
const y = (cell.y + Worldoffset.offsetY) * cellSize
// Draw cell borders
context.beginPath()
context.strokeStyle = 'black';
context.lineWidth = 1;
context.strokeRect(x, y, cellSize, cellSize);
context.font = `${10}px Arial`;
context.fillStyle = "black";
context.textAlign = "center";
context.fillText(`${cell.x},${cell.y}`, x + 16 , y + 16);
}
}
// Get a random number within a range
export function GetRandomNumber(min, max) {
return Math.random() * (max - min) + min;
}
// Get the mouse position relative to the canvas
export function GetMousePosition(event, canvas) {
const rect = canvas.getBoundingClientRect();
return {
x: event.clientX - rect.left,
y: event.clientY - rect.top
};
}
export function Locate(targetX, targetY, rocketX, rocketY, Speed) {
const angle = Math.atan2(targetY - rocketY, targetX - rocketX);
const direction = {
x: Math.cos(angle) * Speed,
y: Math.sin(angle) * Speed
};
return { direction, angle };
}
function RoundUpNegativeValues(x, y) {
if (x <= 0) x = 0
if (x > 99) x = 99
if (y <= 0) y = 0
if (y > 99) y = 99
return { x, y }
}
export function findCellKey(obj, reciprocal) {
const cellRow = Math.floor(obj.x * reciprocal);
const cellCol = Math.floor(obj.y * reciprocal);
let { x, y } = RoundUpNegativeValues(cellRow, cellCol)
const cellId = `${x},${y}`;
return cellId;
}
export function findCellRealXY(cellx, celly, cellsize) {
const x = Math.floor(cellx * cellsize) + (cellsize / 2);
const y = Math.floor(celly * cellsize) + (cellsize / 2);
return { x, y }
}
export function updateCharacterPosition(character, cellWidth, cellHeight) {
let cellName = character.path[character.currentPathIndex];
let currentCell = grid[cellName]
// if (currentCell.occupied.size > 0) {
// let previous = grid[character.currentGridLocation]
// let filtered = previous.adj.filter(value => currentCell.adj.includes(value) && grid[value].blocked == false);
// if (!filtered[0]) {
// character.isMoving = false
// return
// }
// cellName = filtered[0]
// currentCell = grid[cellName]
// character.path[character.currentPathIndex] = cellName
// if (!cellName) {
// console.log(character.path.length, character.currentPathIndex)
// console.log(character.path)
// }
// }
const targetX = (currentCell.x * cellWidth) + (cellWidth / 2) // Adjust based on your grid cell size
const targetY = (currentCell.y * cellHeight) + (cellWidth / 2); // Adjust based on your grid cell size
// Calculate the distance between the character's current position and the target position
const dx = (targetX - character.x)
const dy = (targetY - character.y)
const distance = Math.sqrt(dx * dx + dy * dy);
let angle = Math.atan2(dy, dx);
// Check if the character has reached the target cell
if (distance <= character.speed) {
character.x = targetX;
character.y = targetY;
// Move to the next cell in the path
character.currentPathIndex++;
// Check if the character has reached the final target cell
if (character.currentPathIndex == character.path.length) {
// Character has reached the destination, clear the path
character.path.length = 0
character.currentPathIndex = 0;
character.speed = character.originalSpeed
character.running = false
character.needsToMove = false
// Stop moving and perform any necessary actions
character.isMoving = false
return;
}
character.isMoving = false
// character.running = false
return
}
// Move towards the target cell
// if (currentCell.occupied.size == 0) {
let vx = (dx / distance) * character.speed;
let vy = (dy / distance) * character.speed;
character.x += vx;
character.y += vy;
character.angle = angle
character.isMoving = true
// }
}