-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathgroups.js
More file actions
executable file
·395 lines (268 loc) · 8.28 KB
/
groups.js
File metadata and controls
executable file
·395 lines (268 loc) · 8.28 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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/*
GROUPS
Groups are collections of an arbitrary number of Cubelets.
They have no concept of Cubelet location or orientation
and therefore are not capable of rotation around any axis.
*/
function Group(){
this.cubelets = []
this.add( Array.prototype.slice.call( arguments ))
}
setupTasks = window.setupTasks || []
setupTasks.push( function(){
augment( Group, {
inspect: function( face ){
this.cubelets.forEach( function( cubelet ){
cubelet.inspect( face )
})
return this
},
add: function(){
var
cubeletsToAdd = Array.prototype.slice.call( arguments ),
that = this
cubeletsToAdd.forEach( function( cubelet ){
if( cubelet instanceof Group ) cubelet = cubelet.cubelets
if( cubelet instanceof Array ) that.add.apply( that, cubelet )
else that.cubelets.push( cubelet )
})
return this
},
remove: function( cubeletToRemove ){
if( cubeletToRemove instanceof Group ) cubeletToRemove = cubeletToRemove.cubelets
if( cubeletToRemove instanceof Array ){
var that = this
cubeletToRemove.forEach( function( c ){
that.remove( c )
})
}
for( var i = this.cubelets.length - 1; i >= 0; i -- ){
if( this.cubelets[ i ] === cubeletToRemove )
this.cubelets.splice( i, 1 )
}
return this
},
// Boolean checker.
// Are any Cubelets in this group tweening?
// Engaged on the Z axis? Etc.
isFlagged: function( property ){
var count = 0
this.cubelets.forEach( function( cubelet ){
count += cubelet[ property ] ? 1 : 0
})
return count
},
isTweening: function(){
return this.isFlagged( 'isTweening' )
},
isEngagedX: function(){
return this.isFlagged( 'isEngagedX' )
},
isEngagedY: function(){
return this.isFlagged( 'isEngagedY' )
},
isEngagedZ: function(){
return this.isFlagged( 'isEngagedZ' )
},
isEngaged: function(){
return this.isEngagedX() + this.isEngagedY() + this.isEngagedZ()
},
// Search functions.
// What Cubelets in this Group have a particular color?
// How about all of these three colors?
// And index? address? Solver uses these a lot.
hasProperty: function( property, value ){
var
results = new Group()
this.cubelets.forEach( function( cubelet ){
if( cubelet[ property ] === value ) results.add( cubelet )
})
return results
},
hasId: function( id ){
return this.hasProperty( 'id', id ).cubelets[ 0 ]// expecting a single return!
},
hasAddress: function( address ){
return this.hasProperty( 'address', address ).cubelets[ 0 ]// expecting a single return!
},
hasType: function( type ){
return this.hasProperty( 'type', type )
},
hasColor: function( color ){
var
results = new Group()
this.cubelets.forEach( function( cubelet ){
if( cubelet.hasColor( color )) results.add( cubelet )
})
return results
},
hasColors: function(){// this function implies AND rather than OR, XOR, etc.
var
results = new Group(),
colors = Array.prototype.slice.call( arguments )
this.cubelets.forEach( function( cubelet ){
if( cubelet.hasColors.apply( cubelet, colors )) results.add( cubelet )
})
return results
},
// We needed this business in order to deal with partial rotations.
// A little janky perhaps (grabbing the average, that is)
// but gets the job done and allows us to do getDistanceToPeg() below.
getAverageRotation: function( axis ){
var sum = 0
this.cubelets.forEach( function( cubelet ){
sum += cubelet[ axis.toLowerCase() ]
})
return sum / this.cubelets.length
},
getAverageRotationX: function(){
return this.getAverageRotation( 'x' )
},
getAverageRotationY: function(){
return this.getAverageRotation( 'y' )
},
getAverageRotationZ: function(){
return this.getAverageRotation( 'z' )
},
// What rotation degree are we on right now?
// What direction are we spinning from here? (Clockwise or anti?)
// Let's go in that direction until we hit degree % 90 === 0.
// What's the distance from here to there?
// Not the prettiest code I've ever written... Sorry.
getDistanceToPeg: function( axis ){
var
current = this.getAverageRotation( axis ),
direction = axis.toUpperCase() === axis ? 'clockwise' : 'anticlockwise',
distance = current
.add( 90 )
.divide( 90 )
.roundDown()
.multiply( 90 )
.subtract( current ),
target = current + distance
if( direction === 'anticlockwise' ){
distance -= 90
if( distance === 0 ) distance -= 90
target = current + distance
}
if( erno.verbosity >= 0.9 ) console.log(
'Average rotation for this group about the '+ axis.toUpperCase() +' axis:', current,
'\nRotation direction:', direction,
'\nDistance to next peg:', distance,
'\nTarget rotation:', target
)
// We need to return the absolute() value of the distance
// because the vector (direction) of the twist will be taken into account
// by cube.twist() or whatever else is calling this function.
return distance.absolute()
},
// cube.front.isSolved( 'front' )
// cube.front.up.isSolved( 'up' )
isSolved: function( face ){
if( face ){
var
faceColors = {},
numberOfColors = 0
if( face instanceof Direction ) face = face.name
this.cubelets.forEach( function( cubelet ){
var color = cubelet[ face ].color.name
if( faceColors[ color ] === undefined ){
faceColors[ color ] = 1
numberOfColors ++
}
else faceColors[ color ] ++
})
return numberOfColors === 1 ? true : false
}
else {
console.warn( 'A face [String or Direction] argument must be specified when using Group.isSolved().' )
return false
}
},
// Visual switches.
// Take this group and hide all the stickers,
// turn on wireframe mode, etc.
show: function(){
this.cubelets.forEach( function( cubelet ){ cubelet.show() })
return this
},
hide: function(){
this.cubelets.forEach( function( cubelet ){ cubelet.hide() })
return this
},
showPlastics: function(){
this.cubelets.forEach( function( cubelet ){ cubelet.showPlastics() })
return this
},
hidePlastics: function(){
this.cubelets.forEach( function( cubelet ){ cubelet.hidePlastics() })
return this
},
showExtroverts: function(){
this.cubelets.forEach( function( cubelet ){ cubelet.showExtroverts() })
return this
},
hideExtroverts: function(){
this.cubelets.forEach( function( cubelet ){ cubelet.hideExtroverts() })
return this
},
showIntroverts: function(){
this.cubelets.forEach( function( cubelet ){ cubelet.showIntroverts() })
return this
},
hideIntroverts: function(){
this.cubelets.forEach( function( cubelet ){ cubelet.hideIntroverts() })
return this
},
showStickers: function(){
this.cubelets.forEach( function( cubelet ){ cubelet.showStickers() })
return this
},
hideStickers: function(){
this.cubelets.forEach( function( cubelet ){ cubelet.hideStickers() })
return this
},
showWireframes: function(){
this.cubelets.forEach( function( cubelet ){ cubelet.showWireframes() })
return this
},
hideWireframes: function(){
this.cubelets.forEach( function( cubelet ){ cubelet.hideWireframes() })
return this
},
showIds: function(){
this.cubelets.forEach( function( cubelet ){ cubelet.showIds() })
return this
},
hideIds: function(){
this.cubelets.forEach( function( cubelet ){ cubelet.hideIds() })
return this
},
showTexts: function(){
this.cubelets.forEach( function( cubelet ){ cubelet.showTexts() })
return this
},
hideTexts: function(){
this.cubelets.forEach( function( cubelet ){ cubelet.hideTexts() })
return this
},
getOpacity: function(){
var avg = 0
this.cubelets.forEach( function( cubelet ){ avg += cubelet.getOpacity() })
return avg / this.cubelets.length
},
setOpacity: function( opacity, onComplete ){
this.cubelets.forEach( function( cubelet ){ cubelet.setOpacity( opacity, onComplete ) })
return this
},
getRadius: function(){
var avg = 0
this.cubelets.forEach( function( cubelet ){ avg += cubelet.getRadius() })
return avg / this.cubelets.length
},
setRadius: function( radius, onComplete ){
this.cubelets.forEach( function( cubelet ){ cubelet.setRadius( radius, onComplete ) })
return this
}
})
})