-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
50 lines (46 loc) · 1.21 KB
/
utils.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
const utils = {
withGrid(n) {
return n * 16;
},
asGridCoord(x,y) {
//Se usa la "," entre ${x*16} y ${y*16} para habilitar numeros negativos
return `${x*16},${y*16}`
},
nextPosition(initialX, initialY, direction) {
let x = initialX;
let y = initialY;
const size = 16;
if (direction === "left") {
x -= size;
}else if (direction === "right") {
x += size;
}else if (direction === "up") {
y -= size;
}else if (direction === "down") {
y += size;
}
return {x,y};
},
oppositeDirection(direction) {
if (direction === "left") {return "right"};
if (direction === "right") {return "left"};
if (direction === "up") {return "down"};
return "up";
},
wait(ms) {
return new Promise(resolve => {
setTimeout(() => {
resolve()
}, ms)
})
},
randomFromArray(array) {
return array[ Math.floor(Math.random()*array.length) ]
},
emitEvent(name, detail) {
const event = new CustomEvent(name, {
detail
});
document.dispatchEvent(event);
}
}