-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
42 lines (42 loc) · 882 Bytes
/
utils.js
File metadata and controls
42 lines (42 loc) · 882 Bytes
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
const utils = {
withGrid(n) {
return n * 16;
},
asGridCoord(x, y) {
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 };
},
// returns the opposite directions
oppositeDirection(direction) {
if (direction === "left") {
return "right";
}
if (direction === "right") {
return "left";
}
if (direction === "up") {
return "down";
}
return "up";
},
emitEvent(name, detail) {
const event = new CustomEvent(name, {
detail,
});
document.dispatchEvent(event);
},
};