-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
92 lines (81 loc) · 1.91 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
import { analyseAudioTick } from "./input.js";
import {
gameLoop,
gameTouchStartHandler,
gameTouchStopHandler,
} from "./game.js";
import { drawMenu, menuTouchHandler } from "./menu.js";
import { preMenuClickHandler, preMenuLoop } from "./premenu.js";
const canvas = document.getElementById("screen");
const ctx = canvas.getContext("2d");
const backgroundImage = new Image();
backgroundImage.src = 'img/bg.png';
// Global variables, not ideal
window.state = 'enter';
window.score = 0;
window.difficulty = 'easy';
requestAnimationFrame(loop);
function loop() {
analyseAudioTick();
drawBackground();
if (state !== 'playing') drawOverlay();
switch (state) {
case "playing":
gameLoop();
break;
case "menu":
drawMenu();
break;
case "enter":
preMenuLoop(true);
break;
case "endgame":
preMenuLoop();
break;
}
requestAnimationFrame(loop);
}
document.addEventListener('mousedown', (e) => {
const rect = canvas.getBoundingClientRect();
const x = e.clientX - rect.left;
const y = e.clientY - rect.top;
switch (state) {
case 'menu':
menuTouchHandler({ x, y });
break;
case 'playing':
gameTouchStartHandler();
break;
case 'enter':
case 'endgame':
preMenuClickHandler({ x, y });
break;
}
});
document.addEventListener('mouseup', () => {
switch (state) {
case 'playing':
gameTouchStopHandler();
break;
}
});
let bgOffset = 0;
function drawBackground() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.drawImage(backgroundImage, -bgOffset, 0);
if (bgOffset > 400) {
ctx.drawImage(backgroundImage, -bgOffset + 1200, 0);
}
if (bgOffset > 1200) {
bgOffset = 0;
}
bgOffset++;
}
function drawOverlay() {
ctx.fillStyle = "rgba(30, 30, 30, 0.9)";
ctx.fillRect(0, 0, canvas.width, canvas.height);
}
// PWA
document.ondblclick = function (e) {
e.preventDefault();
};