-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch.js
247 lines (185 loc) · 4.76 KB
/
sketch.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
var padding = 30;
var speed = 0.2; // 0.5 second
var w_width = 800;
const rows = 6;
const cols = 6;
var spacing = w_width / cols;
var deltaSpeed = (spacing + padding / 2) / (60 * speed)
let grid;
var scrambleInterval;
const scrambleMoves = [];
let scrambling = false;
/** INTERACTION **/
let locked = false;
let selectedRow, selectedCol;
let lockedX, lockedY;
const COLORS = {
white: '#F2F2F2CC',
yellow: '#F5FF20CC',
green: '#3ACE1CCC',
blue: '#1889E3CC',
red: '#E20E00CC',
orange: '#F69D09CC',
}
function setup() {
const w_height = w_width * rows / cols;
createCanvas(w_width, w_height);
grid = [];
for (let i = 0; i < cols; i++) {
grid.push([]);
for (let j = 0; j < rows; j++) {
const color = getColor(i, j);
grid[i].push(new Cube(i, j, color));
}
}
scramble();
}
function draw() {
background(30);
for (let i = 0; i < cols; i++) {
for (let j = 0; j < rows; j++) {
if (typeof grid[i][j].show !== 'function') {
console.log(JSON.parse(JSON.stringify(grid)));
console.log(i, j);
noLoop();
break;
}
grid[i][j].show(i, j);
}
}
}
function mousePressed() {
if (scrambling) return
// Get the row and col selected
setSelectedRowCol()
if (selectedRow !== null && selectedCol !== null) {
locked = true;
} else {
locked = false;
}
}
function mouseReleased() {
if (locked) {
const minDist = spacing / 2;
let dir = null;
if (mouseY < lockedY - minDist) {
dir = 'UP'
} else if (mouseY > lockedY + minDist) {
dir = 'DOWN'
} else if (mouseX > lockedX + minDist) {
dir = 'RIGHT'
} else if (mouseX < lockedX - minDist) {
dir = 'LEFT'
}
if (dir) {
moveGridLine(dir);
}
}
locked = false;
selectedRow = null;
selectedCol = null;
}
function scramble() {
const dirs = ['UP', 'DOWN', 'RIGHT', 'LEFT']
const nbMoves = Math.floor(random(15, 20));
console.log(nbMoves)
for (let i = 0; i < nbMoves; i++) {
scrambleMoves.push({
dir: random(dirs),
row: Math.floor(random(0, rows)),
col: Math.floor(random(0, cols)),
})
}
scrambling = true
scrambleInterval = setInterval(scrambleMove, speed * 1200);
}
function scrambleMove() {
if (scrambleMoves.length === 0) {
scrambling = false;
clearInterval(scrambleInterval);
return
}
const sMove = scrambleMoves.shift();
selectedRow = sMove.row;
selectedCol = sMove.col;
moveGridLine(sMove.dir);
}
function setSelectedRowCol() {
selectedRow = null;
selectedCol = null;
lockedY = null;
lockedX = null;
for (let i = 0; i < rows; i++) {
const boundUp = i * spacing;
const boundDown = (i + 1) * spacing;
if (mouseY > boundUp && mouseY < boundDown) {
selectedRow = i;
lockedY = mouseY;
break;
}
}
if (selectedRow === null) return
for (let j = 0; j < cols; j++) {
const boundLeft = j * spacing;
const boundRight = (j + 1) * spacing;
if (mouseX > boundLeft && mouseX < boundRight) {
selectedCol = j;
lockedX = mouseX;
break;
}
}
}
function moveGridLine(dir) {
let cubeToInsert;
switch(dir) {
case 'UP':
for(const cube of grid[selectedCol]) {
cube.move(dir);
}
const firstCube = grid[selectedCol].shift();
grid[selectedCol].push(firstCube);
break;
case 'DOWN':
for(const cube of grid[selectedCol]) {
cube.move(dir);
}
const lastCube = grid[selectedCol].pop();
grid[selectedCol].splice(0, 0, lastCube);
break;
case 'RIGHT':
for(let i = 0; i < cols; i++) {
const cube = grid[i][selectedRow];
cube.move(dir);
}
// Intialize with last cube of row
cubeToInsert = grid[cols - 1][selectedRow];
for (let i = 0; i < cols; i++) {
grid[i].splice(selectedRow, 0, cubeToInsert);
cubeToInsert = grid[i].splice(selectedRow + 1, 1)[0];
}
break;
case 'LEFT':
for(let i = 0; i < cols; i++) {
const cube = grid[i][selectedRow];
cube.move(dir);
}
// Intialize with first cube of row
cubeToInsert = grid[0][selectedRow];
for (let i = cols - 1; i >= 0; i--) {
grid[i].splice(selectedRow, 0, cubeToInsert);
cubeToInsert = grid[i].splice(selectedRow + 1, 1)[0];
}
break;
}
}
function getColor(i, j) {
if (i < 3) {
return j < 3 ? COLORS.white : COLORS.yellow;
} else if (i < 6) {
return j < 3 ? COLORS.green : COLORS.blue;
} else {
return j < 3 ? COLORS.red : COLORS.orange;
}
const key = random(Object.keys(COLORS));
return COLORS[key];
}