-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.js
292 lines (250 loc) · 5.66 KB
/
game.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
var leaders = [
'oliver',
'mie',
'naja',
'mikkel',
'mathias',
'david',
'znegl',
'morten'
];
var states = {
START: 0,
SELECT: 1,
PLAY: 2,
FINISH: 3
};
var activeKeys = [];
// State specific variables
var selectingUser = 1;
// Image array
var images;
// Players: {score: number, image: number}
var players = [];
var state = states.START;
var clockIntervalId;
var updateIntervalId;
var secondsLeft;
var interval = 1000;
var gameTime = 100;
function displayStartScreen() {
state = states.START;
// Clear all images
images.prop({
'src': '',
'data-id': 0
});
// Clear players
players = [];
selectingUser = 1;
updateScoreDisplay();
// Reset time
secondsLeft = gameTime + 1;
updateClock();
// Display type selector
$('#start').addClass('active');
$('#finish').removeClass('active');
}
function displayFinishScreen() {
clearInterval(clockIntervalId);
clearInterval(updateIntervalId);
state = states.FINISH;
$('#finish').addClass('active');
$('#imageWrapper').removeClass('active');
if (players.length > 1) {
if (players[0].score == players[1].score) {
$('#finishHeading').text('Ingen vandt!');
} else if (players[0].score < players[1].score) {
$('#finishHeading').text('Spiller 2 vandt!');
} else {
$('#finishHeading').text('Spiller 1 vandt!');
}
} else {
$('#finishHeading').text('Du fik ' + players[0].score + ' point!');
}
}
function startGame() {
console.log('start game', players);
state = states.PLAY;
$('#select').removeClass('active');
$('#imageWrapper').addClass('active');
images.prop({
'src': '',
'data-id': 0
});
updateIntervalId = setInterval(displayRandomLeader, interval);
clockIntervalId = setInterval(updateClock, 1000);
displayRandomLeader();
}
function displayRandomLeader() {
// Choose random field
var field = Math.ceil(Math.random() * 4);
// Choose random leader
var leader = Math.floor(Math.random() * leaders.length);
$('#image' + field + ' img').prop({
'src': 'images/' + leaders[leader] + '.jpg',
'data-index': leader + 1
});
}
function updateClock() {
secondsLeft--;
$('#clock').text(secondsLeft);
if (!secondsLeft && state == states.PLAY) {
displayFinishScreen();
}
}
function displayLeaderSelector() {
console.log('display leader selector', players);
state = states.SELECT;
$('#start').removeClass('active');
$('#select').addClass('active');
$('#imageWrapper').addClass('active');
if (players.length == 1) {
// Single player
$('#selectHeading').text('Vælg hvem du vil tæve');
} else if (players.length == 2) {
// Two player
$('#selectHeading').text('Vælg hvem du vil tæve (spiller 1)');
}
displayFirst4();
}
function displayFirst4() {
leaders.forEach(function(leader, index) {
if (index < 4) {
images[index].setAttribute('data-index', index + 1);
images[index].src = 'images/' + leader + '.jpg';
}
});
}
function displayLast4() {
leaders.forEach(function(leader, index) {
if (index > 3) {
images[index-4].setAttribute('data-index', index + 1);
images[index-4].src = 'images/' + leader + '.jpg';
}
});
}
function updateScoreDisplay() {
if (players.length == 0) {
$('.score').text(0);
return;
}
$('#score1').text(players[0].score);
if (players.length > 1) {
$('#score2').text(players[1].score);
}
}
function validateKeyPress(e) {
if (activeKeys.indexOf(e.keyCode) != -1) {
return;
}
activeKeys.push(e.keyCode);
var selectedImage = -1;
switch(e.keyCode) {
case 49: // 1
case 38: // Key up
selectedImage = 1;
break;
case 50: // 2
case 40: // Key down
selectedImage = 2;
break;
case 51: // 3
case 37: // Key left
selectedImage = 3;
break;
case 52: // 4
case 39: // Key right
selectedImage = 4;
break;
case 32: // Space
break;
default:
console.log('Keycode', e.keyCode);
break;
}
switch(state) {
case states.PLAY:
if (e.keyCode == 32) {
secondsLeft = 1;
return;
}
if (selectedImage == -1) {
return;
}
var image = $('#image' + selectedImage + ' img');
var correct = false;
// Count up if the pressed leader is one of the selected leaders
players.forEach(function(player) {
if (player.image == image.prop('data-index')) {
console.log('image selected', image.prop('data-index'));
player.score++;
image.prop({
'src': '',
'data-index': 0
});
correct = true;
}
});
// Count down if not
if (!correct) {
players.forEach(function(player) {
player.score--;
});
}
updateScoreDisplay();
break;
case states.START:
if (selectedImage == 2) {
// Add one player
players.push({score: 0, image: 0});
} else if (selectedImage == 3) {
// Add two players
players.push({score: 0, image: 0});
players.push({score: 0, image: 0});
} else {
return;
}
displayLeaderSelector();
break;
case states.SELECT:
if (selectedImage == -1) {
if (images[0].src.indexOf(leaders[0]) != -1) {
displayLast4();
} else {
displayFirst4()
}
} else {
players[selectingUser-1].image = $('#image' + selectedImage + ' img').attr('data-index');
if (players.length > 1) {
if (selectingUser == 2) {
startGame();
} else {
selectingUser++;
$('#selectHeading').text('Vælg hvem du vil tæve (spiller 2)');
}
} else {
startGame();
}
}
break;
case states.FINISH:
if (e.keyCode == 32) {
displayStartScreen();
}
break;
}
}
function validateKeyRelease(e) {
var index = activeKeys.indexOf(e.keyCode);
if (index != -1) {
activeKeys.splice(index, 1);
}
}
// Start the game
$(document).ready(function() {
images = $('.image img');
displayStartScreen();
$('body').keydown(validateKeyPress);
$('body').keyup(validateKeyRelease);
});