-
Notifications
You must be signed in to change notification settings - Fork 0
/
dom_racer.js
107 lines (83 loc) · 2.21 KB
/
dom_racer.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
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
////////////////////////////////////////////////////////////////////////////////
// Dom Racer //
///////////////////////////////////////////////////////////////////////////////
// Globals
let doc = document;
let b;
let d;
// State
window.mobs = new Set([]);
window.score = 0;
// Game settings
let start = 0;
let t = 0;
let difficulty = 0.02;
// Init document
//
let init = () => {
b = doc.createElement("body");
b.id = "BODDAY";
doc.body = b;
d = doc.createElement("div");
d.id = "main";
d.style.minWidth = "99vw";
d.style.minHeight = "99vh";
d.style.overflow = "clip";
d.style.position = "absolute";
b.appendChild(d);
let scoreEl = doc.createElement("div");
scoreEl.id = "score";
scoreEl.style.position = "relative";
scoreEl.style.bottom = "5%";
scoreEl.style.left = "50%";
scoreEl.innerHTML = window.score;
d.appendChild(scoreEl);
create();
main();
}
// Create mob
//
let create = () => {
let r = Math.random();
if (r < 0.6)
import('./mobs/squareOkBtn.js').then((mod) => {let newEl = mod.create(Math.round(Math.random() * d.clientWidth), d.clientHeight)});
else if (r < 0.9)
import('./mobs/decrement.js').then((mod) => {let newEl = mod.create(Math.round(Math.random() * d.clientWidth), d.clientHeight)});
else
import('./mobs/textBox.js').then((mod) => {let newEl = mod.create(Math.round(Math.random() * d.clientWidth), d.clientHeight)});
}
// Main loop
//
window.main = (ts) => {
if (start == undefined) {
start = ts;
}
if (t == undefined) {
t = ts;
}
if ((ts - t) * difficulty > 100) {
create();
t = ts;
}
if ((ts - start) * difficulty > 1000) {
difficulty = difficulty * 2;
start = ts;
}
doc.getElementById("score").innerHTML = window.score;
frame = window.requestAnimationFrame(main);
for (id of window.mobs) {
el = document.getElementById(id);
x = el.style.left;
y = parseInt(el.style.top.split("px")[0])
if (y > 0) {
y = y - 1;
el.style.top = y + "px";
}
else if (y == 0) {
window.cancelAnimationFrame(frame);
alert("YOU LOST!!!!!!!!");
document.body.style.pointerEvents = "none";
}
}
}
doc.onload = init();