forked from Kaelinator/AGAD
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimon.js
More file actions
194 lines (139 loc) · 3.54 KB
/
Simon.js
File metadata and controls
194 lines (139 loc) · 3.54 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
const SIZE = 175; // size of Tiles
var pattern = []; // order of Tiles
var tiles = [];
var input = []; // order of user's input Tiles
var time;
var dIndex; // index of Tile which is being focused on during demonstration
var score; // "level"
var demonstrating; // whether or not Simon is demonstrating
var playing; // whether the game is still going or over
function setup() {
createCanvas(500, 500);
for (var i = 0; i < 2; i++) {
for (var j = 0; j < 2; j++) {
var col = randomTileColor(i, j);
var brightCol = brightenCol(col);
var x = i * SIZE + SIZE;
var y = j * SIZE + SIZE;
tiles.push(new Tile(x, y, SIZE, col, brightCol));
}
}
playing = true;
score = -1;
newTile();
textAlign(CENTER);
}
function draw() {
background(51);
handleTiles();
handleDemonstration();
drawScore();
if (!playing)
drawGameEnd();
}
/**
* handle user input
* checks for mistakes
* handles new Tile
*/
function mousePressed() {
if (demonstrating) // user may not click during demonstration
return;
var t = getTile(tiles, mouseX, mouseY); // find the tile clicked
if (t != -1) // if it exists, push it to the input array
input.push(t);
/* check for mistake */
for (var i = 0; i < input.length; i++) {
if (input[i] != pattern[i])
endGame();
}
/* push a new tile */
if (input.length === pattern.length) // if user reached the end of pattern
newTile();
}
/**
* selects random tile to push to the pattern array
* resets user input
* handles states, time, dIndex, and score
*/
function newTile() {
pattern.push(Math.floor(random() * tiles.length));
demonstrating = true; // set state
var negativeInput = (-input[input.length - 1]) ? -input[input.length - 1] : 1; // find 1's complement
dIndex = negativeInput - 1; // set demonstration to 2's complement index of last input
time = 0; // reset time
input = []; // reset user input
score++;
}
/**
* ends the game
*/
function endGame() {
playing = false;
noLoop();
}
/**
* draws game over message
*/
function drawGameEnd() {
textSize(50);
fill(255);
text("Game Over!", width / 2, height - 20);
}
/**
* draws score at the bottom center
*/
function drawScore() {
textSize(30);
fill(255);
text(score, width / 2, height - 50);
}
/**
* handles drawing of tiles
*/
function handleTiles() {
for (var i = 0; i < tiles.length; i++) {
if (demonstrating && dIndex > -1) {
tiles[i].draw(i === pattern[dIndex]); // draw the dIndex Tile bigger
} else if (demonstrating) {
tiles[i].draw(i === (-dIndex - 1)); // draw
} else {
tiles[i].draw(i === input[input.length - 1]); // draw latest selected Tile bigger
}
}
}
/**
* tracks time
* handles dIndex and state
*/
function handleDemonstration() {
time++;
if (demonstrating && time % 60 === 0) { // demonstrate next Tile
if (dIndex > -1) {
dIndex++; // continue demonstration
} else if (dIndex === (-tiles.length - 1)) {
dIndex = 0; // begin demonstration
} else {
dIndex = (-tiles.length - 1); // pause in between last input and beggining demonstration
}
}
if (dIndex >= pattern.length) {
// entire pattern has been demonstrated
demonstrating = false;
}
}
/**
* returns random color slightly darkened
*/
function randomTileColor(i, j) {
var r = (i % 2 === 0) ? 0 : random(155) + 50;
var g = (j % 2 === 0) ? 0 : random(155) + 50;
var b = random(155) + 50;
return color(r, g, b);
}
/**
* returns brightened version of col
*/
function brightenCol(col) {
return color(red(col) + 50, green(col) + 50, blue(col) + 50);
}