|
@@ -1,8 +1,12 @@
|
1
|
1
|
const $ = (s) => document.querySelector(s);
|
2
|
|
-const $$ = (s) => Array.from(document.querySelectorAll(s));
|
|
2
|
+
|
3
|
3
|
const ROOT = $('#root');
|
|
4
|
+const LIVES_LABEL = $('#lives');
|
|
5
|
+const SOUND_BUTTON = $('#sound');
|
4
|
6
|
const CANVAS = $('#canvas');
|
5
|
7
|
const CTX = CANVAS.getContext('2d');
|
|
8
|
+
|
|
9
|
+
|
6
|
10
|
const X_BOUND = 0.05;
|
7
|
11
|
const GAME_OVER_BOUND = 0.9;
|
8
|
12
|
const MAX_RAINDROPS = 100;
|
|
@@ -11,8 +15,9 @@ const clouds = new Set();
|
11
|
15
|
const bullets = new Set();
|
12
|
16
|
const raindrops = new Set();
|
13
|
17
|
let shootingPad = new ShootingPad();
|
14
|
|
-let running = true;
|
|
18
|
+let running = false;
|
15
|
19
|
let direction = 1;
|
|
20
|
+let soundEnabled = localStorage.soundEnabled !== 'false';
|
16
|
21
|
|
17
|
22
|
const initCloudField = () => {
|
18
|
23
|
for (let y = 0; y < 6; y++) {
|
|
@@ -36,6 +41,11 @@ const initRainEffect = () => {
|
36
|
41
|
}
|
37
|
42
|
}
|
38
|
43
|
|
|
44
|
+const updateSoundButton = () => {
|
|
45
|
+ SOUND_BUTTON.textContent = soundEnabled ? '🔊' : '🔈';
|
|
46
|
+ SOUND_BUTTON.title = `Sound ${soundEnabled ? 'enabled' : 'disabled'}`;
|
|
47
|
+}
|
|
48
|
+
|
39
|
49
|
const start = () => {
|
40
|
50
|
$('.result')?.remove();
|
41
|
51
|
|
|
@@ -51,7 +61,6 @@ const start = () => {
|
51
|
61
|
initCloudField();
|
52
|
62
|
|
53
|
63
|
running = true;
|
54
|
|
- loop();
|
55
|
64
|
}
|
56
|
65
|
|
57
|
66
|
const gameOver = (result) => {
|
|
@@ -63,58 +72,63 @@ const gameOver = (result) => {
|
63
|
72
|
}
|
64
|
73
|
|
65
|
74
|
const loop = () => {
|
66
|
|
- if (!running) return;
|
67
|
75
|
document.title = `${fps()} FPS | CloudInvaders | OLC Codejam 2022`;
|
68
|
76
|
CTX.clearRect(0, 0, CANVAS.width, CANVAS.height);
|
69
|
77
|
|
70
|
|
- let directionUpdated = false;
|
71
|
|
- let isOver = false;
|
72
|
|
- Array.from(clouds).forEach(c => {
|
73
|
|
- c.update(direction);
|
74
|
|
- c.draw();
|
75
|
|
- if (c.x < X_BOUND || c.x > 1 - X_BOUND) {
|
76
|
|
- directionUpdated = true;
|
77
|
|
- }
|
78
|
|
- if (c.y >= GAME_OVER_BOUND) {
|
79
|
|
- isOver = true;
|
|
78
|
+ raindrops.forEach(d => {
|
|
79
|
+ d.update();
|
|
80
|
+ d.draw();
|
|
81
|
+ });
|
|
82
|
+ if (running) {
|
|
83
|
+ let directionUpdated = false;
|
|
84
|
+ let isOver = false;
|
|
85
|
+ Array.from(clouds).forEach(c => {
|
|
86
|
+ c.update(direction);
|
|
87
|
+ c.draw();
|
|
88
|
+ if (c.x < X_BOUND || c.x > 1 - X_BOUND) {
|
|
89
|
+ directionUpdated = true;
|
|
90
|
+ }
|
|
91
|
+ if (c.y >= GAME_OVER_BOUND) {
|
|
92
|
+ isOver = true;
|
|
93
|
+ }
|
|
94
|
+ if (!c.alive) {
|
|
95
|
+ clouds.delete(c);
|
|
96
|
+ }
|
|
97
|
+ });
|
|
98
|
+
|
|
99
|
+ if (directionUpdated) {
|
|
100
|
+ direction = -direction;
|
80
|
101
|
}
|
81
|
|
- if (!c.alive) {
|
82
|
|
- clouds.delete(c);
|
|
102
|
+
|
|
103
|
+ if (isOver) {
|
|
104
|
+ gameOver('Game over');
|
83
|
105
|
}
|
84
|
|
- });
|
85
|
106
|
|
86
|
|
- if (directionUpdated) {
|
87
|
|
- direction = -direction;
|
88
|
|
- }
|
|
107
|
+ Array.from(bullets).forEach(b => {
|
|
108
|
+ b.update();
|
|
109
|
+ b.draw();
|
89
|
110
|
|
90
|
|
- if (isOver) {
|
91
|
|
- gameOver('Game over');
|
92
|
|
- }
|
|
111
|
+ if (!b.alive) {
|
|
112
|
+ bullets.delete(b);
|
|
113
|
+ }
|
|
114
|
+ });
|
93
|
115
|
|
94
|
|
- Array.from(bullets).forEach(b => {
|
95
|
|
- b.update();
|
96
|
|
- b.draw();
|
|
116
|
+ shootingPad.update();
|
|
117
|
+ shootingPad.draw();
|
97
|
118
|
|
98
|
|
- if (!b.alive) {
|
99
|
|
- bullets.delete(b);
|
|
119
|
+ if (clouds.size === 0) {
|
|
120
|
+ gameOver('WIN');
|
100
|
121
|
}
|
101
|
|
- });
|
102
|
|
-
|
103
|
|
- raindrops.forEach(d => {
|
104
|
|
- d.update();
|
105
|
|
- d.draw();
|
106
|
|
- });
|
107
|
|
-
|
108
|
|
- shootingPad.update();
|
109
|
|
- shootingPad.draw();
|
110
|
|
-
|
111
|
|
- if (clouds.size === 0) {
|
112
|
|
- gameOver('WIN');
|
113
|
122
|
}
|
114
|
|
-
|
115
|
123
|
requestAnimationFrame(loop);
|
116
|
124
|
};
|
117
|
125
|
|
|
126
|
+SOUND_BUTTON.addEventListener('click', () => {
|
|
127
|
+ soundEnabled = !soundEnabled;
|
|
128
|
+ localStorage.soundEnabled = soundEnabled;
|
|
129
|
+ updateSoundButton();
|
|
130
|
+});
|
|
131
|
+
|
118
|
132
|
document.addEventListener('keypress', () => {
|
119
|
133
|
if (!running) {
|
120
|
134
|
start();
|
|
@@ -122,4 +136,6 @@ document.addEventListener('keypress', () => {
|
122
|
136
|
});
|
123
|
137
|
|
124
|
138
|
initRainEffect();
|
125
|
|
-start();
|
|
139
|
+updateSoundButton();
|
|
140
|
+loop();
|
|
141
|
+gameOver('🌩 Cloud Invaders 🌩');
|