-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
214 lines (182 loc) · 5.98 KB
/
Copy pathmain.js
File metadata and controls
214 lines (182 loc) · 5.98 KB
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
'use strict';
function buildDOM(html) {
var div = document.createElement('div');
div.innerHTML = html;
return div.children[0];
};
function main() {
var splashScreen;
var gameScreen;
var lostGameScreen;
var wonGameScreen;
var brainstormButton;
var rethinkButton;
var backToSplashButton;
var scoreElement;
var timerElementMinute;
var timerElementSecond;
var canvasElement;
var game;
function buildSplash() {
destroyLostGameScreen();
destroyWonGameScreen();
splashScreen = buildDOM(`
<main class="relative">
<div class="title">
<h1>Aha!</h1>
<h2>A Brainstorming Game</h2>
</div>
<div class="content">
<h3>Instructions</h3>
<p class="instructions inline">You're this guy:</p>
<img src="./visuals/guy.svg" class="instructions guy inline"/>
<br>
<p class="instructions inline">You need to come up with </p>
<h5 class="rainbow inline"> THE GREATEST IDEA EVER </h5>
<p class="instructions inline"> for your next project, so it's time to brainstorm!</p>
<br>
<p class="instructions inline">Use the mouse to move and catch a bunch of good ideas</p>
<img src="./visuals/lightbulb.svg" class="instructions lightbulb inline"/>
<p class="instructions inline">But avoid the shitty ones, or you'll end up with a piece of crap!</p>
<img src="./visuals/squiggle.svg" class="instructions squiggle inline"/>
</div>
<div class="takeaction">
<button class="brainstorm">brainstorm!</button>
</div>
</main>
`);
document.body.prepend(splashScreen);
brainstormButton = document.querySelector('button');
brainstormButton.addEventListener('click', destroySplash);
};
function destroySplash() {
splashScreen.remove();
brainstormButton.removeEventListener('click', destroySplash);
buildGameScreen();
};
function buildGameScreen() {
destroyLostGameScreen();
destroyWonGameScreen();
gameScreen = buildDOM(`
<main class="relative">
<div class="status">
<p class="inline gamescreen">Idea: </p><p class="score inline gamescreen">0</p>
<p class="inline gamescreen">       </p>
<p class="timer inline gamescreen">
Time spent brainstorming:
<span class="minutes">00</span>
<span>:</span>
<span class="seconds">00</span>
</p>
</div>
<div class="moveme-esta">
<canvas></canvas>
</div>
</main>
`);
document.body.prepend(gameScreen);
canvasElement = document.querySelector('canvas');
scoreElement = document.querySelector('p.score');
timerElementMinute = document.querySelector('span.minutes');
timerElementSecond = document.querySelector('span.seconds');
game = new Game(canvasElement);
game.start();
game.gameIsOverCallback(function() {
gameOverTransition(game.player.score, game.minutes, game.seconds)
});
game.checkScore(updateScore);
game.updateTimerCallback(updateTimer);
};
function gameOverTransition(score, minutes, seconds, intervalIdDifficulty) {
destroyGameScreen();
clearInterval(intervalIdDifficulty);
if (game.player.score < 0 ) {
buildLostGameScreen(score, minutes, seconds);
} else {
buildWonGameScreen(score, minutes, seconds);
};
}
function updateScore(score) {
scoreElement.innerText = score;
}
function updateTimer(minutes, seconds) {
timerElementMinute.innerText = minutes;
timerElementSecond.innerText = seconds;
}
function destroyGameScreen() {
if (gameScreen) {
gameScreen.remove();
};
};
function buildLostGameScreen(minutes, seconds) {
lostGameScreen = buildDOM(`
<main class="relative">
<h1>Done!</h1>
<p class="timer inline endtime">
You brainstormed for
<span class="minutes">00</span>
<span>minutes and</span>
<span class="seconds">00</span>
seconds,
</p>
<img src="./visuals/pieceofcrap.svg" class="crap inline" />
<h2>and came up with a Piece of Crap.</h2>
<button class="rethink">rethink!</button>
<button class="back">back to start</button>
</main>
`);
document.body.prepend(lostGameScreen);
timerElementMinute = document.querySelector('span.minutes');
timerElementSecond = document.querySelector('span.seconds');
timerElementMinute.innerText = minutes;
timerElementSecond.innerText = seconds;
rethinkButton = document.querySelector('.rethink');
rethinkButton.addEventListener('click', buildGameScreen);
backToSplashButton = document.querySelector('.back');
backToSplashButton.addEventListener('click', buildSplash);
};
function destroyLostGameScreen() {
if (lostGameScreen) {
lostGameScreen.remove();
rethinkButton.removeEventListener('click', buildGameScreen);
backToSplashButton.removeEventListener('click', buildSplash);
};
};
function buildWonGameScreen(minutes, seconds) {
wonGameScreen = buildDOM(`
<main class="relative">
<h1>Done!</h1>
<p class="timer inline endtime">
You brainstormed for
<span class="minutes">00</span>
<span>minutes and</span>
<span class="seconds">00</span>
seconds,
</p>
<img src="./visuals/masterpiece.svg" class="masterpiece inline" />
<h2>and came up with a Masterpiece!</h2>
<h3>You smart butt. Who you gonna sell it to?</h3>
<button class="rethink">brainstorm a new one!</button>
<button class="back">back to start</button>
</main>
`);
document.body.prepend(wonGameScreen);
timerElementMinute = document.querySelector('span.minutes');
timerElementSecond = document.querySelector('span.seconds');
timerElementMinute.innerText = minutes;
timerElementSecond.innerText = seconds;
rethinkButton = document.querySelector('.rethink');
rethinkButton.addEventListener('click', buildGameScreen);
backToSplashButton = document.querySelector('.back');
backToSplashButton.addEventListener('click', buildSplash);
};
function destroyWonGameScreen() {
if (wonGameScreen) {
wonGameScreen.remove();
rethinkButton.removeEventListener('click', buildGameScreen);
backToSplashButton.removeEventListener('click', buildSplash);
};
};
buildSplash();
};
window.addEventListener('load', main);