-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
57 lines (51 loc) · 1.59 KB
/
utils.js
File metadata and controls
57 lines (51 loc) · 1.59 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
// a object with some helper functions
// assisting in grid based movement
const utils = {
user : {
name: "aku", // change this later
src: ""
},
withGrid(n) {
return n * 16; //to be used in OverworldMaps
},
asGridCoord(x, y) {
return `${x*16}, ${y*16}` // kinda like f strings
},
nextPosition(initalX, initalY, direction) { // used in checking if space is occupied - gets the "would be" next position
let x = initalX; // to prevent changing values
let y = initalY;
const size = 16; // probably the scale
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)
})
},
emitEvent(name, detail) {
const event = new CustomEvent(name, { // use JS built in CustomEvent to make a .. custom event to listen for whatever we want
detail // send in any additional details about this event - must use "detail" tag -- will be sent by the parameter
});
document.dispatchEvent(event);
}
}