-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscripts.js
156 lines (138 loc) · 3.75 KB
/
scripts.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
// All colors must be represented in character form
// global Game object to hold all variables
var game = {
level: 0,
checkLevel: 0,
currentPattern: [],
colors: ['r', 'b', 'g', 'y'],
userPattern: [],
presses: 0,
blinks: 0,
advanceLevel: function() {
this.level++;
if (this.level === 21) {
alert('You win!');
this.initialize();
return;
}
this.checkLevel = 0;
var randomColor = this.chooseRandomColor();
this.currentPattern.push(randomColor);
this.userPattern = [];
$('.play').addClass('no-touch');
this.blinkNext(0);
// append generated clear user-pressed
},
checkPattern: function() {
// checks pattern only to currentLevel
for (var i = 0; i < this.checkLevel; i++) {
if (this.userPattern[i] !== this.currentPattern[i]) {
this.userMistake();
return false;
}
}
if (this.checkLevel === this.level) {
var state = this;
setTimeout(function() {
state.advanceLevel();
}, 1000);
}
return true;
},
chooseRandomColor: function() {
return this.colors[Math.floor(Math.random() * 4)];
},
userChoice: function(event) {
this.checkLevel++;
this.userPattern.push($(event.target).data().color);
if (this.checkPattern()) {
document.getElementById('audio-' + $(event.target).data().color).play();
}
},
userMistake: function() {
this.checkLevel = 0;
this.userPattern = [];
for (var i = 0; i < this.colors.length; i++) {
$('.play[data-color="' + this.colors[i] + '"').addClass("blink-" + this.colors[i]);
document.getElementById('audio-' + this.colors[i]).play();
}
if ($("#strict").prop("checked")) {
this.initialize(true);
} else {
setTimeout(this.blinkNext.bind(this), 1000, 0);
}
},
blinkNext: function(i) {
if (i === this.currentPattern.length) {
$('.play').removeClass('no-touch');
return false;
}
var animClass = "";
switch(this.currentPattern[i]) {
case 'r':
animClass = "blink-r";
break;
case 'b':
animClass = "blink-b";
break;
case 'g':
animClass = "blink-g";
break;
case 'y':
animClass = "blink-y";
break;
default:
break;
}
$('.play[data-color="' + this.currentPattern[i] + '"').addClass(animClass);
document.getElementById('audio-' + this.currentPattern[i]).play();
setTimeout(this.blinkNext.bind(this), 750, i+1);
},
initialize: function(strict) {
// strict is true when user makes a mistake when strict mode is on.
this.level = 0;
this.currentPattern = [];
if (strict) {
setTimeout(this.advanceLevel.bind(this), 1000);
} else {
this.advanceLevel();
}
}
}
// button state functions
function setActive(e) {
if ($(this).hasClass('no-touch') || game.checkLevel === game.level) return false;
$(this).addClass('active');
game.userChoice(e);
}
function setInactive(e) {
$(this).removeClass('active');
}
$(document).ready(function() {
// Pre-load operations
// FCC's provided audio files are annoyingly at different volumes
var settings = {
'audio-r': 0.3,
'audio-b': 0.3,
'audio-g': 0.8,
'audio-y': 1.0
}
Object.keys(settings).forEach(function(id) {
document.getElementById(id).volume = settings[id];
});
// Event Listeners
$('.start').on('click', function(e) {
game.initialize();
$(this).attr('disabled', true);
});
$('.play').on("mousedown", setActive);
$('.play').on("mouseout mouseup", setInactive);
$('.play').on('animationend', function() {
game.blinks++;
$(this).attr('class', 'play no-touch');
if (game.blinks === game.currentPattern.length) {
game.blinks = 0;
$(this).attr('class', 'play')
}
});
});