-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
435 lines (329 loc) · 14.5 KB
/
script.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
import { elemid, getRandomInt, lerp, formatNumber } from './helper.js'
/*======================================================================================================================
INSTANCE VARIABLES
======================================================================================================================*/
let repeating_request;
// Marks
var marks_displayed = 0;
// Timing
var counter = 0;
var time_limit = 10;
// Question
var bottom_value = 1;
var top_value = 10;
var seconds = 0;
// Debugging
var debug_mode = true;
var solved = 0;
var paused = false;
// Upgrades
var upgrade_div = null;
var upgrade_image = null;
var upgrade_text = null;
var upgrade_cost = null;
const upgrade_purchaseable_color = 'chartreuse';
const upgrade_non_purchaseable_color = 'red';
const game_data = {
marks: 0,
question_value: 1,
mps: 0,
upgrades : [
{name: 'pencil', cost: 2, value: 1},
{name: 'mathematician', cost: 3, value: 1},
{name: 'trigonometry', cost: 4, value: 4},
{name: 'amphetamine', cost: 3000, value: 10},
{name: 'artificial_intelligence', cost: 10_000, value: 40},
{name: 'quantum_computing', cost: 40_000, value: 100},
{name: 'space_travel', cost: 200_000, value: 400},
{name: 'time_travel', cost: 1_500_000, value: 6666},
{name: 'animal_sacrifice', cost: 123_666_444, value: 98_765},
{name: 'undead_experiments', cost: 3_999_999_999, value: 999_999},
{name: 'nuclear_warfare', cost: 75_000_000_000, value: 10_000_000}
]
};
// Loop for upgrades to initialise default values
game_data.upgrades.forEach(function(upgrade) {
upgrade.originalCost = upgrade.cost;
upgrade.numberOfPurchases = 0;
});
/*======================================================================================================================
GAME LOOP
========================================================================================================================*/
//window.onload = init;
window.addEventListener('load', init);
function init() {
// Run game loop before next repaint
window.requestAnimationFrame(gameLoop);
// Create the upgrades
createUpgrades();
}
function gameLoop(timeStamp) {
// * Checking and purchasing upgrades
purchaseUpgrades();
// * Displayed marks interpolation
// Check if marks displayed is not equal to the marks
if (marks_displayed !== game_data.marks) {
// Determine whether to round up or down based on whether marks_displayed is less than marks
let roundingFunction = marks_displayed < game_data.marks ? Math.ceil : Math.floor;
// Lerp the marks, round as determined, and set the value
marks_displayed = roundingFunction(lerp(marks_displayed, game_data.marks, 0.3));
// Update the 'marks' element and log the value
elemid('marks').innerHTML = formatNumber(marks_displayed);
console.log(marks_displayed);
}
// * Marks per second
counter++;
if (counter >= 60) {
// Increase the marks by the MPS
game_data.marks += game_data.mps;
// Increment the seconds timer
seconds++;
elemid('timer').innerHTML = (time_limit - seconds) + 1;
console.log((time_limit - seconds) + 1);
// Reset question
if (seconds > time_limit) {
// Randomise values
value1 = getRandomInt(1, 10);
value2 = getRandomInt(1, 10);
elemid('value1').innerHTML = value1;
elemid('value2').innerHTML = value2;
// Reset seconds
seconds = 0;
elemid('timer').innerHTML = "OUT OF TIME!";
}
// Reset the counter
counter = 0;
}
// * Request again
repeating_request = window.requestAnimationFrame(gameLoop);
}
/*======================================================================================================================
RENDERING UPGRADES
======================================================================================================================*/
function createUpgrades() {
// Store the upgrades container
let upgrade_container = elemid("upgrades");
// Upgrades loop
for (let upgrade of game_data.upgrades) {
//? === UPGRADE ===
upgrade_div = document.createElement("div");
upgrade_div.classList.add("upgrade");
// Image
upgrade_image = document.createElement("img");
upgrade_image.src = `images/${upgrade.name}.png`;
upgrade_image.classList.add("upgrade-image");
// Text
upgrade_text = document.createElement("div");
upgrade_text.className = "upgrade-text";
let capital_name = upgrade.name.split('_').map(word => word.charAt(0).toUpperCase() + word.slice(1)).join(' ');
upgrade_text.innerHTML = `${capital_name}<br><br><br>`;
// Cost
upgrade_cost = document.createElement("span");
upgrade_cost.className = "cost";
upgrade_cost.style.color = upgrade_non_purchaseable_color;
upgrade_cost.textContent = `Price: ${upgrade.cost}`;
upgrade_text.appendChild(upgrade_cost);
upgrade_div.appendChild(upgrade_image);
upgrade_div.appendChild(upgrade_text);
upgrade_container.appendChild(upgrade_div);
// Store the upgrade div in the upgrade
upgrade.div = upgrade_div;
upgrade.image = upgrade_image;
upgrade.text = upgrade_text;
upgrade.cost_span = upgrade_cost;
// Create a dark overlay div
let dark_overlay = document.createElement('div');
dark_overlay.className = 'overlay';
upgrade.div.appendChild(dark_overlay);
upgrade.dark_overlay = dark_overlay;
//? === MOUSE OVER ===
upgrade_div.addEventListener('mouseover', (event) => {
// Get the tooltip element
const tooltip = elemid('tooltip');
// Set the tooltip content
if (upgrade.name == 'pencil') {
tooltip.innerHTML = `${upgrade.name.toUpperCase()}<br><br>+${upgrade.value} qv<br><br>${upgrade.numberOfPurchases} ${upgrade.name}'s giving ${upgrade.value * upgrade.numberOfPurchases} question value`;
} else {
tooltip.innerHTML = `${upgrade.name.toUpperCase()}<br><br>+${upgrade.value} mps<br><br>${upgrade.numberOfPurchases} ${upgrade.name}'s getting ${upgrade.value * upgrade.numberOfPurchases} marks per second`;
}
// Get the upgrade element's dimensions and position
const upgradeRect = upgrade_div.getBoundingClientRect();
const upgradeX = upgradeRect.left;
const upgradeY = upgradeRect.top;
// Calculate the tooltip position based on the mouse position
const tooltipWidth = tooltip.offsetWidth;
const tooltipX = upgradeX - upgradeRect.width - 60;
const tooltipY = event.clientY;
tooltip.style.width = `${upgradeRect.width}px`;
// Position and show the tooltip
tooltip.style.left = `${tooltipX}px`;
tooltip.style.top = `${tooltipY}px`;
tooltip.style.display = 'block';
});
//? === MOUSE OUT ===
upgrade_div.addEventListener('mouseout', () => {
// Hide the tooltip
const tooltip = elemid('tooltip');
tooltip.style.display = 'none';
});
}
}
function purchaseUpgrades() {
function upgradeAdjust(color, overlay_opacity, upgrade) {
if (upgrade.cost_span.style.color != color) {
upgrade.dark_overlay.style.opacity = overlay_opacity;
upgrade.cost_span.style.color = color;
}
}
// Loop through the upgrades
for (let upgrade of game_data.upgrades) {
//* SUFFICIENT FUNDS
if (game_data.marks >= upgrade.cost) {
// Change the colour
upgradeAdjust(upgrade_purchaseable_color, 0, upgrade);
//? === PURCHASE UPGRADE ===
upgrade.div.onclick = function() {
if (game_data.marks >= upgrade.cost) {
// Deduct cost from marks
game_data.marks -= upgrade.cost;
// Increment no. of purchases
upgrade.numberOfPurchases++;
// Debugging
console.log(`Purchased Upgrade: ${upgrade.name} for ${upgrade.cost} marks`);
// Increase the cost
upgrade.cost = upgrade.originalCost * (1 + 2 * upgrade.numberOfPurchases);
upgrade.cost_span.textContent = `Price: ${upgrade.cost}`;
// Reap the benefits
if (upgrade.name == 'pencil') {
game_data.question_value++;
elemid('question-value').innerHTML = formatNumber(game_data.question_value);
} else {
game_data.mps += upgrade.value;
elemid('marks-per-second').innerHTML = formatNumber(game_data.mps);
}
// Flash bang effect on upgrade div
let flash = document.createElement('div');
flash.className = 'flash';
this.appendChild(flash);
// After a delay, remove the flash overlay
setTimeout(function() { flash.parentNode.removeChild(flash); }, 250);
}
}
}
//! INSUFFICIENT FUNDS
else if (game_data.marks < upgrade.cost) {
// Change the colour
upgradeAdjust(upgrade_non_purchaseable_color, 0.5, upgrade);
}
}
}
/*======================================================================================================================
SUBMISSION OF ANSWER
======================================================================================================================*/
document.addEventListener("keydown", (event) => {
// If the user submits the answer
if (event.key === 'Enter') {
// Get values
var value1 = parseInt(elemid('value1').innerHTML);
var value2 = parseInt(elemid('value2').innerHTML);
var answer = elemid('answer').value;
// Check if the input was correct
if (value1 + value2 == answer) {
// Randomise values
value1 = getRandomInt(bottom_value, top_value);
value2 = getRandomInt(bottom_value, top_value);
elemid('value1').innerHTML = value1;
elemid('value2').innerHTML = value2;
// Add Marks
game_data.marks += game_data.question_value;
// Clear the input
elemid('answer').value = "";
// Increment 'solved' count
solved++;
// Reset the seconds
seconds = 0;
elemid("timer").innerHTML = time_limit;
// Debugging
console.log(`Correct, Marks: ${game_data.marks}`);
// Confetti
confetti({
particleCount: 100,
spread: 360,
startVelocity: 30,
origin: {
x: Math.random(),
y: Math.random()
}
});
} else {
// Debugging
console.log('Wrong.');
}
}
});
/*======================================================================================================================
KEY UP
========================================================================================================================*/
document.addEventListener("keyup", (event) => {
if (event.key === 'Escape') {
if (paused) {
// Un-paused
paused = false;
// Restart the animation frame
window.requestAnimationFrame(gameLoop);
console.log("Un-Pause");
} else {
// Pause
paused = true;
// Cancel the animation frame
window.cancelAnimationFrame(repeating_request);
console.log("Paused");
}
}
if (event.key === 's') {
// Save game
localStorage.setItem('solvGameData', JSON.stringify(game_data));
console.log("SAVED GAME");
}
if (event.key === 'l') {
// Load game
const saved_data = localStorage.getItem('solvGameData');
// IF saved data is found, then proceed to apply
if (saved_data) {
// Parse and store the loaded game data
const loaded_game_data = JSON.parse(saved_data);
// Update the loaded values
game_data.marks = loaded_game_data.marks;
game_data.question_value = loaded_game_data.question_value;
game_data.mps = loaded_game_data.mps;
game_data.upgrades = loaded_game_data.upgrades;
elemid("marks-per-second").innerHTML = game_data.mps;
elemid("question-value").innerHTML = game_data.question_value;
// Clear the old upgrades
const upgrades_container = elemid("upgrades");
while (upgrades_container.firstChild) {
upgrades_container.removeChild(upgrades_container.firstChild);
}
// Create new upgrades
createUpgrades();
// Debugging message
console.log("LOADED GAME");
}
}
});
/*======================================================================================================================
DEBUG MODE
======================================================================================================================*/
document.addEventListener("keydown", (event) => {
if (event.key == '=' && debug_mode === true) {
game_data.marks += 100_000;
}
else if (event.key == '-' && debug_mode === true) {
game_data.marks -= 100_000;
}
});
/*======================================================================================================================
REFERENCES:
- Game loop concept is adapted from: https://spicyyoghurt.com/tutorials/html5-javascript-game-development/create-a-proper-game-loop-with-requestanimationframe
- Some game concepts are inspired from Cookie Clicker by Orteil: https://orteil.dashnet.org/cookieclicker/
======================================================================================================================*/