forked from OrderAndCh4oS/drone-squadron-elite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.js
61 lines (53 loc) · 1.68 KB
/
functions.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
import { canvas } from './constants/constants.js';
export function isOffCanvas(particle) {
return (
particle.position.x > (canvas.width + particle.radius) ||
particle.position.x < (0 - particle.radius) ||
particle.position.y > (canvas.height + particle.radius) ||
particle.position.y < (0 - particle.radius)
);
}
export function returnToCanvas(drone) {
if(drone.position.x > canvas.width) {
drone.position.x = 0;
}
if(drone.position.x < 0) {
drone.position.x = canvas.width;
}
if(drone.position.y > canvas.height) {
drone.position.y = 0;
}
if(drone.position.y < 0) {
drone.position.y = canvas.height;
}
}
export function distanceTo(p1, p2) {
const dx = p2.position.x - p1.position.x,
dy = p2.position.y - p1.position.y;
return Math.sqrt(dx * dx + dy * dy);
}
export function angleTo(angleOne, angleTwo) {
return Math.atan2(Math.sin(angleOne - angleTwo),
Math.cos(angleOne - angleTwo));
}
export function didCollide(p1, p2) {
return !(p1.squadId === p2.squadId) && distanceTo(p1, p2) < p1.radius +
p2.radius;
}
export function randomItem(items) {
return items[Math.floor(Math.random() * items.length)];
}
export function angleBetweenRange(angleOne, angleTwo, range) {
return angleTo(angleOne, angleTwo) <= range / 2 &&
angleTo(angleOne, angleTwo) >= -(range / 2);
}
export function shuffle(arr) {
let randomizedArray = [];
let array = arr;
while(array.length !== 0) {
let rIndex = Math.floor(array.length * Math.random());
randomizedArray.push(array[rIndex]);
array.splice(rIndex, 1);
}
return randomizedArray;
}