-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
182 lines (167 loc) · 4.84 KB
/
index.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
let spriteCount = 0;
class sprite {
constructor(x) {
let root = document.createElement("div")
root.classList.add("fix");
root.id = "sprite_" + spriteCount;
this.id = root.id;
spriteCount++;
root.innerHTML = `<img></img>`;
document.documentElement.append(root);
Object.assign(this, x);
}
update() {
let ele = document.querySelector("#" + this.id);
ele.style = `top:${this._y}px;left:${this._x}px`;
ele.children[0].width = this.width;
ele.children[0].height = this.height;
ele.children[0].src = this._img;
}
move(x, y) {
this.x = x;
this.y = y;
}
collision(spriteA) {
function inline(a, b, c, d) {
function mid(a, b, c) {
if (a > c) { let tmp = a; a = c; c = tmp; }
return (a <= b) && (b <= c);
}
return (mid(a, c, b) || mid(a, d, b)) && (mid(c, a, d) || mid(c, b, d))
}
return inline(this.x, this.x + this.width, spriteA.x, spriteA.x + spriteA.width) && inline(this.y, this.y + this.height, spriteA.y, spriteA.y + spriteA.height)
}
delete() {
let ele = document.querySelector("#" + this.id);
ele.remove();
}
static isSprite(x) {
return (x instanceof sprite);
}
get img() {
return this._img;
}
set img(x) {
this._img = x;
this.update();
}
get visible() {
return this._visible;
}
set visible(x) {
this._visible = x;
this.update();
}
set width(x) {
this._width = x;
this.update();
}
get width() {
return this._width;
}
get height() {
return this._height;
}
set height(x) {
this._height = x;
this.update();
}
get y() {
return this._y;
}
set y(y) {
this._y = y;
this.update();
}
get x() {
return this._x;
}
set x(x) {
this._x = x;
this.update();
}
}
let timer = 0;
let timerClockId = setInterval(() => timer++, 1000);
let dino = new sprite({ x: 0, y: 70, width: 30, height: 30, img: "/source/dino.png" });
let isFailed = false;
function activateFail() {
clearInterval(timerClockId)
isFailed = true;
alert("You failed! (score: " + timer + ")");
window.location.reload();
}
let events = [{
exec: function () {
let bird = new sprite({ x: 300, y: 10, width: 30, height: 30, img: "/source/bird.png" });
for (let i = 0; i < 45; i++)
setTimeout(() => {
bird.x -= 7.5;
}, i * 50);
let clockId = setInterval(() => {
if (isFailed) clearInterval(clockId);
else if (bird.collision(dino)) activateFail();
})
setTimeout(() => {
bird.delete();
clearInterval(clockId);
}, 46 * 50);
}, duration: () => (Math.random() * 1000 + 1000)
}, {
exec: function () {
console.log("!")
let block = new sprite({ x: 300, y: 75, width: 25, height: 25, img: "/source/short.png" });
for (let i = 0; i < 45; i++)
setTimeout(() => {
block.x -= 7.5;
}, i * 50);
let clockId = setInterval(() => {
if (isFailed) clearInterval(clockId);
else if (block.collision(dino)) activateFail();
})
setTimeout(() => {
block.delete();
clearInterval(clockId);
}, 46 * 50);
}, duration: () => (Math.random() * 1000 + 1000)
}];
function eventsTrigger() {
let c = events[Math.floor(Math.random() * events.length)]
c.exec();
setTimeout(eventsTrigger, c.duration());
}
eventsTrigger();
let dinoCtrlLocked = false;
const ctrlKeycodes = ["ArrowUp"]
function moveDino(e) {
if (!ctrlKeycodes.includes(e.code)) return;
let initinalY = dino.y;
if (!dinoCtrlLocked) {
dinoCtrlLocked = true;
setTimeout(() => { dinoCtrlLocked = false; }, 800)
switch (e.code) {
case "ArrowUp":
for (let i = 0; i < 15; i++)
setTimeout(() => {
dino.y -= 45 / 15;
}, i * 20);
for (let i = 15; i < 20; i++)
setTimeout(() => {
dino.y -= 5 / 5;
}, i * 20);
for (let i = 20; i < 25; i++)
setTimeout(() => {
dino.y -= 5 / 5;
}, i * 20);
for (let i = 25; i < 40; i++)
setTimeout(() => {
dino.y += 45 / 15;
}, i * 20);
setTimeout(() => {
dino.y = initinalY;
}, 40 * 20);
// case "ArrowDown":
}
}
}
document.addEventListener('keydown', moveDino);