-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
210 lines (179 loc) · 5.42 KB
/
main.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
var jet = document.querySelector("#jet");
var board = document.querySelector("#board");
var text = document.querySelector("#phrase");
var score = document.querySelector("#score > span");
var footer = document.querySelector("footer");
var image = document.querySelector("footer > img");
var audio = document.querySelector("audio#soundtrack");
var menu = document.querySelector("section#menu");
var startButton = menu.querySelector("button");
var gameOver = false;
var OBSTACLE_DOWN_VELOCITY = 400;
var OBSTACLE_CREATE_VELOCITY = 1000;
const LOOPING_IMAGE = 1.2 * 60 * 1000;
const LOOPING_TEXT = 0.4 * 60 * 1000;
const getProperty = ({ element, property }) => {
return parseInt(window.getComputedStyle(element).getPropertyValue(property));
};
const TEXTS = {
spyke: [
"Im a bounty hunter",
"Come on jet let's hunt a bounty",
"Goodnight...julia...",
"Do we really need to take her?",
"This one came close!",
"Whatever happens happens",
],
faye: [
"Ann??",
"I keep 80% and you 20%",
"Let's go boys",
"Someone needs to hold this guy",
"Oh, fuck you!",
"Mochi mochi?",
],
jet: [
"Damn spyke!",
"Dinner is ready",
"Really?",
"My ship, my rules",
"Hehehe, that was good, spyke!",
"2,000,000.00...spyke...faye...",
],
};
const CONTROLLER = {
jet: {
move: (v) => {
jet.style.left = v;
},
},
};
window.addEventListener("load", () => {
setInterval(async () => {
await audio.play();
}, 500);
});
window.addEventListener("keydown", ({ key, keyCode }) => {
var left = getProperty({
element: jet,
property: "left",
});
if (gameOver) {
return;
}
if ((key === "ArrowLeft" || key === "Left") && left > 0) {
CONTROLLER.jet.move(left - 20 + "px");
} else if ((key === "ArrowRight" || key === "Right") && left <= 460) {
CONTROLLER.jet.move(left + 20 + "px");
} else if (key === "ArrowUp" || key === "Up" || keyCode === 32) {
let fireshot = document.createElement("div");
fireshot.classList.add("fireshot");
fireshot.style.left = left + 10 + "px";
board.appendChild(fireshot);
let fireshotPath = setInterval(() => {
let rocks = document.querySelectorAll(".rocks");
for (var i = 0; i < rocks.length; i++) {
let rock = rocks[i];
let rock_bound = rock.getBoundingClientRect();
let fireshot_bound = fireshot.getBoundingClientRect();
if (
fireshot_bound.left >= rock_bound.left &&
fireshot_bound.right <= rock_bound.right &&
fireshot_bound.top <= rock_bound.top &&
fireshot_bound.bottom <= rock_bound.bottom
) {
rock.parentElement.removeChild(rock);
fireshot.parentElement.removeChild(fireshot);
score.innerHTML = parseInt(score.innerHTML) + 1;
} else if (
board.getBoundingClientRect().top >=
fireshot.getBoundingClientRect().top
) {
fireshot.parentElement.removeChild(fireshot);
}
}
let fireshotBottom = getProperty({
element: fireshot,
property: "bottom",
});
fireshot.style.bottom = fireshotBottom + 30 + "px";
}, 30);
}
});
startButton.addEventListener("click", () => {
start();
});
function updateCharacter() {
let key = Array.from(footer.classList)[0];
let actualTexts = TEXTS[String(key)];
text.innerHTML = actualTexts[Math.floor(Math.random() * actualTexts.length)];
}
function reset() {
gameOver = false;
score.innerHTML = "0";
let fireshots = document.querySelectorAll(".fireshot");
let rocks = document.querySelectorAll(".rocks");
for (rock of rocks) {
rock.parentElement.removeChild(rock);
}
for (fireshot of fireshots) {
fireshot.parentElement.removeChild(fireshot);
}
}
function start() {
gameOver = false;
menu.parentElement.removeChild(menu);
var generateObstacle = setInterval(() => {
var rock = document.createElement("div");
rock.classList.add("rocks");
var rockLeft = getProperty({
element: rock,
property: "left",
});
rock.style.left = Math.floor(Math.random() * 450) + "px";
board.appendChild(rock);
}, OBSTACLE_CREATE_VELOCITY);
var moveObstacles = setInterval(() => {
let rocks = document.querySelectorAll(".rocks");
if (rocks !== undefined) {
for (var i = 0; i < rocks.length; i++) {
let rock = rocks[i];
let rockTop = getProperty({
element: rock,
property: "top",
});
if (rockTop > 430) {
gameOver = true;
if (
confirm(
`Game Over! Your score is ${score.innerHTML}. Want to try again?`
)
) {
reset();
start();
} else {
clearInterval(generateObstacle);
clearInterval(moveObstacles);
}
}
rock.style.top = rockTop + 20 + "px";
}
}
}, OBSTACLE_DOWN_VELOCITY);
var changePhrase = setInterval(() => {
updateCharacter();
}, LOOPING_TEXT);
var changeCharacter = setInterval(() => {
footer.classList = "";
let position = Math.floor(Math.random() * Object.keys(TEXTS).length);
let key = Object.keys(TEXTS).find((item, index) => index === position);
image.src = "./assets/" + key + ".webp";
image.alt = key;
footer.classList.add(key);
clearInterval(changePhrase);
updateCharacter();
changePhrase = setInterval(() => {
updateCharacter();
}, LOOPING_TEXT);
}, LOOPING_IMAGE);
}