-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
292 lines (288 loc) · 10.1 KB
/
script.js
File metadata and controls
292 lines (288 loc) · 10.1 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
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 intervalId;
var allColorSwitch = ["blue", "red", "green", "yellow", "black", "white"];
var endGame = false;
var switchColorEnable = true;
var actualFlagId = allFlag[levelCounter.getValue()].getCountry();
var saveOn = false;
var music = new Audio("mainMusique.mp3");
music.loop = true;
var inGame = false;
var pointsPerFlag = 0;
var stopChrono = false;
// function call on refesh or exit
window.onbeforeunload = function (event) {
if (levelCounter.getValue() < allFlag.length) {
saveData();
}
}
// load only when the page is totally loaded
$(document).ready(function () {
initialize();
if (saveOn == false) {
popUp();
} else {
$("#endGame").hide();
$("#introduction").remove();
nextFlag(levelCounter.getValue());
$("body").removeClass("popup");
$("#scoreBoard").show();
$("#time").show();
$("#joker").show();
$('#skip').show();
inGame = true;
// music.play();
}
})
// function to simulate starting and ending popUp
// hiding most of the componant and apply background effect on body to make it "shadowed"
function popUp() {
$("body").addClass("popup");
$('#time').hide();
$("#scoreBoard").hide();
$('#valider').hide();
$('#joker').hide();
$('#skip').hide();
if (!endGame) {
$("#endGame").hide();
if (inGame) {
$("#points").show();
animateScript();
$("#points p").html("Bien joué ! <br> Vous avez réalisé : " + pointsPerFlag + " points avec " + clicPerFlagCounter.getValue() + " clics");
clicPerFlagCounter.decrease(clicPerFlagCounter.getValue());
$("#points button").click(function () {
clearInterval(intervalId);
nextFlag(levelCounter.getValue());
$("#points").hide();
$("body").removeClass("popup");
$("#scoreBoard").show();
$("#time").show();
$("#joker").fadeIn();
$('#skip').fadeIn();
})
}
} else {
music.pause();
$("#points").show();
animateScript();
$("#points button").remove();
$("#points p").html("Bien joué ! <br> Vous avez réalisé un total de : " + pointsPerFlag + " points avec " + clicPerFlagCounter.getValue() + " clics").append('<br><br><button onClick="window.location.reload();">Recommencer</button>');
localStorage.clear();
}
}
// initialisation of the game, starting color, clickable div, joker activation, import save
function initialize() {
$("#points").hide();
$("#joker").click(function () {
joker()
});
// parse du JSON to recover saved data
const save = JSON.parse(localStorage.getItem("save"));
if (save == null) {
saveData();
} else {
scoreCounter.increase(save.score);
clicCounter.increase(save.clicNumber);
levelCounter.increase(save.level);
start = new Date() - new Date(save.time);
saveOn = true;
music.play();
}
updateLevel();
updateClic();
updateScore();
validateButton();
giveUp();
}
// add click function on the validate Button
function validateButton() {
$("#valider").hide().click(function () {
var optimalClic = allFlag[levelCounter.getValue()].getOptmimalClics();
calculPoints(optimalClic, clicPerFlagCounter.getValue());
$('#joker button').prop("disabled", false);
levelCounter.increase(1)
updateScore()
$(this).hide();
$(".drapeau").children().remove();
switchColorEnable = true;
// if it was the last flag, validate the game and display the final score
if (levelCounter.getValue() == allFlag.length) {
endGame = true;
popUp();
$("#points p").html("Bien joué ! <br> Vous avez réalisé un total de : " + scoreCounter.getValue() + " points avec " + clicCounter.getValue() + " clics")
} else {
popUp();
}
})
}
// set div initial color at start
function initialColor(flagDiv, color) {
$("#" + flagDiv).children().filter("div").children().each(function () {
$(this).addClass(color);
});
}
// enable clic to switch the color on the color's list and check if the displayed color are valid for the solution
function switchColor(flagDiv) {
$("#" + flagDiv).children().filter("div").children().click(function () {
if (switchColorEnable && !$(this).hasClass("joker")) {
clicCounter.increase(1);
clicPerFlagCounter.increase(1);
updateClic();
let color = $(this).attr("class");
let newColorIndex = allColorSwitch.indexOf(color) + 1;
if (newColorIndex == 6) {
newColorIndex = 0;
}
let newColor = allColorSwitch[newColorIndex];
$(this).removeClass(color).addClass(newColor);
checkSolution(flagDiv);
}
saveData();
})
}
// check if the color actually displayed are the same than the one in the flag data
function checkSolution(flagDiv) {
var soluce = new Array(allFlag[levelCounter.getValue()].getColors().length);
for (let i = 0; i < soluce.length; i++) {
soluce[i] = allFlag[levelCounter.getValue()].getColor(i);
}
var reponse = new Array(soluce.length);
$("#" + flagDiv).children().last().children().each(function (i) {
reponse[i] = "" + $(this).attr('class').replace("joker ", "");
})
// case for czech republic with one fake div
if (levelCounter.getValue() == 5) {
reponse.pop();
soluce.pop();
}
if (JSON.stringify(soluce) === JSON.stringify(reponse)) {
clearTimeout(timer);
timer = null;
stopChrono = true;
$("#valider").fadeIn();
$("#skip").fadeOut();
$("#joker").fadeOut();
// can't switch color once everything is good
switchColorEnable = false;
$('#joker button').prop("disabled", true);
}
}
// switch the game to the next flag and start new chrono
function nextFlag(index) {
if (index != allFlag.length) {
$(".drapeau").children().each(function () {
$(this).remove();
});
createFlag(allFlag[index]);
updateLevel();
if (!saveOn) {
start = new Date();
}
saveOn = false;
stopChrono = false;
chrono();
} else {
$('#time').hide();
}
}
// calcul of the points winning when correct answer is guessed, based on data of the flag
function calculPoints(optimalClicNumber, actualClicNumber) {
let clicScore = actualClicNumber - optimalClicNumber;
var score = 0
if (clicScore <= 0 && diff.getSeconds() <= optimalClicNumber - 2) {
score = 3 - jokerCounterUse.getValue();
} else if (clicScore == 0) {
score = 2 - jokerCounterUse.getValue();
} else {
score = 1 - jokerCounterUse.getValue();
}
if (score < 0) {
score = 0;
}
scoreCounter.increase(score);
pointsPerFlag = score;
jokerCounterUse.decrease(jokerCounterUse.getValue());
updateScore();
}
// update displayed level to the actual one
function updateLevel() {
$('#level').html("Niveau " + (levelCounter.getValue() + 1) + "/" + allFlag.length + "!");
}
// update number of clicks displayed
function updateClic() {
$('#clic').html("Nombre de clics : " + clicCounter.getValue());
}
// update score displayed
function updateScore() {
$('#score').html("Score : " + scoreCounter.getValue());
if (scoreCounter.getValue() == 0) {
$('#joker button').prop("disabled", true);
} else {
$('#joker button').prop("disabled", false);
}
}
// save data into JSON for easy manipulation
function saveData() {
const save = {
"level": levelCounter.getValue(),
"score": scoreCounter.getValue(),
"clicNumber": clicCounter.getValue(),
"time": diff
};
localStorage.setItem("save", JSON.stringify(save));
return false;
}
// joker button function, activate only if the div isn't a joker already and not everything is guessed, lock the div on which the joker was use
function joker() {
jokerCounterUse.increase(1);
var actualFlag = $(".drapeau");
var numberOfDiv = actualFlag.children().last().children().length;
if (numberOfDiv == 4) {
numberOfDiv = 3;
}
var randomNumber = new Array();
for (let i = 0; i < numberOfDiv; i++) {
randomNumber[i] = i;
}
var randomNthChild = randomNumber.splice(Math.floor(Math.random() * randomNumber.length), 1);
var divToLock = actualFlag.children().last().children().eq(randomNthChild);
while (divToLock.attr("class").indexOf("joker") >= 0) {
randomNthChild = randomNumber.splice(Math.floor(Math.random() * randomNumber.length), 1);
divToLock = actualFlag.children().last().children().eq(randomNthChild);
}
scoreCounter.decrease(1);
updateScore();
divToLock.removeClass().addClass("joker").addClass(allFlag[levelCounter.getValue()].getColor(randomNthChild));
checkSolution($(".drapeau").attr("id"));
}
// give up button with no points
function giveUp() {
$("#skip button").click(function () {
$('.drapeau').children().remove();
levelCounter.increase(1);
nextFlag(levelCounter.getValue());
if (levelCounter.getValue() == allFlag.length) {
endGame = true;
popUp();
$("#endGame").html("Bien joué ! <br> Vous avez réalisé : " + scoreCounter.getValue() + " points avec " + clicCounter.getValue() + " clics");
}
})
}
// animation script for the firework effect, import the sprite and show part of the image one by one on a short interval
function animateScript() {
var positionX = 192;
var positionY = 0;
var interval = 50;
intervalId = setInterval(function () {
document.getElementById("image").style.backgroundPosition =
`-${positionX}px -${positionY}px`;
if (positionX < 768) {
positionX += 192
} else if (positionY < 1344) {
positionX = 192;
positionY += 192;
} else {
positionX = 192;
positionY = 0;
}
}, interval)
}