-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjs.js
169 lines (126 loc) · 4.58 KB
/
js.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
"use strict";
(function (){
const movesRange = 20;
const generateArray = function(movesRange) { //func generates random array for the game move sequence
return Array(movesRange)
.fill()
.map(() => Math.floor(Math.random()*4));
};
const gameSettings = {
gameSounds: [...Array(7).keys()].map((i) => $(`#sound${i}`)),
userMessages: { ongoing: 'Good luck!', defeat: "You lose!", victory: "Victory!", wrongField: "Wrong field!", correct: "Correct!" },
speedLvls: [900, 750, 475],
value: 1,
strictMode: false,
movesCounter: 0,
movesOrder: generateArray(movesRange)
}; const gameController = {
counter: $('#counter'),
gameField: $('.gameField'),
strict: $('#strict'),
infobox: $('#infobox'),
incrementValue: function() {
gameSettings.value < movesRange ? gameSettings.value++ : gameSettings.value;
gameController.counter.html(gameSettings.value);
},
decrementValue: function() {
gameSettings.value > 1 ? gameSettings.value-- : gameSettings.value;
gameController.counter.html(gameSettings.value);
},
incMovesCount: function() {
gameSettings.movesCounter += 1;
},
enableFields: function() {
gameController.gameField.removeClass('unclickable');
},
disableFields: function() {
gameController.gameField.addClass('unclickable');
},
reset: function () {
var that = this;
gameSettings.value = 1;
gameController.counter.html(gameSettings.value);
gameSettings.movesCounter = 0;
gameSettings.movesOrder = generateArray(movesRange);
gameController.runGame();
},
setStrictMode: function() {
gameSettings.strictMode = !gameSettings.strictMode;
gameController.strict.toggleClass('strictModeOn');
gameSettings.gameSounds[4][0].play();
},
lastMove: function () { // function checks if the last user move was correct, if so, proceeds to next level
this.disableFields();
gameController.infobox.html(gameSettings.userMessages.correct);
gameSettings.movesCounter = 0;
this.incrementValue();
setTimeout(this.runGame, 1500);
},
setGameSpeed: function() {
if (gameSettings.value < 8) {
return gameSettings.speedLvls[0];
} else if (gameSettings.value < 15) {
return gameSettings.speedLvls[1];
} else {
return gameSettings.speedLvls[2];
}
},
highlightBtn: function(fieldNumber) { // function is reponsible for toggling classes to mark buttons chosen by comp
let field = "#field" + fieldNumber,
classReplacement = `field${fieldNumber}a`;
$(field).toggleClass(classReplacement);
setTimeout(() => $(field).toggleClass(classReplacement), 400);
},
runGame: function() {
let gameSpeed = gameController.setGameSpeed(),
i = 0;
gameController.disableFields();
gameController.infobox.html(gameSettings.userMessages.ongoing);
function loop() {
setTimeout(function () {
gameController.highlightBtn(gameSettings.movesOrder[i]);
gameSettings.gameSounds[gameSettings.movesOrder[i]][0].play();
i++;
i < gameSettings.value ? loop() : gameController.enableFields();
}, gameSpeed)
};
loop();
}
};
const Btn0 = new Button(0);
const Btn1 = new Button(1);
const Btn2 = new Button(2);
const Btn3 = new Button(3);
function Button (index) { //constructor function for making buttons
this.field = "#field" + index,
this.btnValue = index,
this.checkMove = function () {
if (this.btnValue === gameSettings.movesOrder[gameSettings.movesCounter]) {
this.noise();
gameController.incMovesCount();
if (gameSettings.movesCounter === gameSettings.value) {
gameSettings.movesCounter === movesRange ? gameController.infobox.html(gameSettings.userMessages.victory) : gameController.lastMove();
}
} else if (!gameSettings.strictMode) {
gameController.disableFields();
gameController.infobox.html(gameSettings.userMessages.wrongField);
gameSettings.movesCounter = 0;
setTimeout(() => gameController.runGame(), 1000);
} else if (gameSettings.strictMode) {
gameController.infobox.html(gameSettings.userMessages.defeat);
gameSettings.gameSounds[6][0].play();
setTimeout(() => gameController.reset(), 3000);
}
}
this.noise = function () {
return gameSettings.gameSounds[index][0].play();
};
$(this.field).on('click', () => this.checkMove());
}
$('#incCounter').on('click', gameController.incrementValue);
$('#decCounter').on('click', gameController.decrementValue);
$('#start').on('click', gameController.runGame);
$('#reset').on('click', gameController.reset);
gameController.strict.on('click', gameController.setStrictMode);
gameController.strict.hover(() => $('.cloudInfo').toggleClass('invisible'));
}());