Project

General

Profile

Download (17.6 KB) Statistics
| Branch: | Tag: | Revision:

magiccube / src / main / java / org / distorted / objects / ObjectList.java @ 1e6fb034

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2020 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// Magic Cube is free software: you can redistribute it and/or modify                            //
7
// it under the terms of the GNU General Public License as published by                          //
8
// the Free Software Foundation, either version 2 of the License, or                             //
9
// (at your option) any later version.                                                           //
10
//                                                                                               //
11
// Magic Cube is distributed in the hope that it will be useful,                                 //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                 //
14
// GNU General Public License for more details.                                                  //
15
//                                                                                               //
16
// You should have received a copy of the GNU General Public License                             //
17
// along with Magic Cube.  If not, see <http://www.gnu.org/licenses/>.                           //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19

    
20
package org.distorted.objects;
21

    
22
import android.content.res.Resources;
23

    
24
import org.distorted.library.main.DistortedEffects;
25
import org.distorted.library.main.DistortedTexture;
26
import org.distorted.library.mesh.MeshSquare;
27
import org.distorted.library.type.Static4D;
28
import org.distorted.main.R;
29
import org.distorted.main.RubikActivity;
30

    
31
import java.lang.reflect.Field;
32

    
33
///////////////////////////////////////////////////////////////////////////////////////////////////
34

    
35
public enum ObjectList
36
  {
37
  CUBE (
38
         new int[][] {
39
                       {2 , 12, R.raw.cube2, R.drawable.ui_small_cube2, R.drawable.ui_medium_cube2, R.drawable.ui_big_cube2, R.drawable.ui_huge_cube2} ,
40
                       {3 , 16, R.raw.cube3, R.drawable.ui_small_cube3, R.drawable.ui_medium_cube3, R.drawable.ui_big_cube3, R.drawable.ui_huge_cube3} ,
41
                       {4 , 20, R.raw.cube4, R.drawable.ui_small_cube4, R.drawable.ui_medium_cube4, R.drawable.ui_big_cube4, R.drawable.ui_huge_cube4} ,
42
                       {5 , 24, R.raw.cube5, R.drawable.ui_small_cube5, R.drawable.ui_medium_cube5, R.drawable.ui_big_cube5, R.drawable.ui_huge_cube5}
43
                     },
44
         TwistyCube.class,
45
         new MovementCube(),
46
         0
47
       ),
48

    
49
  PYRA (
50
         new int[][] {
51
                       {3 , 10, R.raw.pyra3, R.drawable.ui_small_pyra3, R.drawable.ui_medium_pyra3, R.drawable.ui_big_pyra3, R.drawable.ui_huge_pyra3} ,
52
                       {4 , 15, R.raw.pyra4, R.drawable.ui_small_pyra4, R.drawable.ui_medium_pyra4, R.drawable.ui_big_pyra4, R.drawable.ui_huge_pyra4} ,
53
                       {5 , 20, R.raw.pyra5, R.drawable.ui_small_pyra5, R.drawable.ui_medium_pyra5, R.drawable.ui_big_pyra5, R.drawable.ui_huge_pyra5}
54
                     },
55
         TwistyPyraminx.class,
56
         new MovementPyraminx(),
57
         1
58
       ),
59

    
60
  DIAM (
61
         new int[][] {
62
                       {2 , 10, R.raw.diamond, R.drawable.ui_small_diam, R.drawable.ui_medium_diam, R.drawable.ui_big_diam, R.drawable.ui_huge_diam} ,
63
                     },
64
         TwistyDiamond.class,
65
         new MovementDiamond(),
66
         1
67
       ),
68

    
69
  DINO (
70
         new int[][] {
71
                       {3 , 10, R.raw.dino, R.drawable.ui_small_dino, R.drawable.ui_medium_dino, R.drawable.ui_big_dino, R.drawable.ui_huge_dino} ,
72
                     },
73
         TwistyDino6.class,
74
         new MovementDino(),
75
         2
76
       ),
77

    
78
  DIN4 (
79
         new int[][] {
80
                       {3 ,  7, R.raw.dino, R.drawable.ui_small_din4, R.drawable.ui_medium_din4, R.drawable.ui_big_din4, R.drawable.ui_huge_din4} ,
81
                     },
82
         TwistyDino4.class,
83
         new MovementDino(),
84
         2
85
       ),
86

    
87
  REDI (
88
         new int[][] {
89
                       {3 , 14, R.raw.redi, R.drawable.ui_small_redi, R.drawable.ui_medium_redi, R.drawable.ui_big_redi, R.drawable.ui_huge_redi} ,
90
                     },
91
         TwistyRedi.class,
92
         new MovementRedi(),
93
         2
94
       ),
95

    
96
  HELI (
97
         new int[][] {
98
                       {3 , 18, R.raw.heli, R.drawable.ui_small_heli, R.drawable.ui_medium_heli, R.drawable.ui_big_heli, R.drawable.ui_huge_heli} ,
99
                     },
100
         TwistyHelicopter.class,
101
         new MovementHelicopter(),
102
         2
103
       ),
104

    
105
  SKEW (
106
         new int[][] {
107
                       {2 , 11, R.raw.skewb, R.drawable.ui_small_skewb, R.drawable.ui_medium_skewb, R.drawable.ui_big_skewb, R.drawable.ui_huge_skewb} ,
108
                       {3 , 17, R.raw.skewm, R.drawable.ui_small_skewm, R.drawable.ui_medium_skewm, R.drawable.ui_big_skewm, R.drawable.ui_huge_skewm} ,
109
                     },
110
         TwistySkewb.class,
111
         new MovementSkewb(),
112
         3
113
       ),
114

    
115
  IVY  (
116
         new int[][] {
117
                       {2 , 8, R.raw.ivy, R.drawable.ui_small_ivy, R.drawable.ui_medium_ivy, R.drawable.ui_big_ivy, R.drawable.ui_huge_ivy} ,
118
                     },
119
         TwistyIvy.class,
120
         new MovementIvy(),
121
         3
122
       ),
123

    
124
  REX  (
125
         new int[][] {
126
                       {3 , 16, R.raw.rex, R.drawable.ui_small_rex, R.drawable.ui_medium_rex, R.drawable.ui_big_rex, R.drawable.ui_huge_rex} ,
127
                     },
128
         TwistyRex.class,
129
         new MovementRex(),
130
         3
131
       ),
132

    
133
  KILO (
134
         new int[][] {
135
                       {3 , 18, R.raw.kilo3, R.drawable.ui_small_kilo3, R.drawable.ui_medium_kilo3, R.drawable.ui_big_kilo3, R.drawable.ui_huge_kilo3} ,
136
                     },
137
         TwistyKilominx.class,
138
         new MovementMinx(),
139
         4
140
       ),
141

    
142
  MEGA (
143
         new int[][] {
144
                       {3 , 21, R.raw.mega3, R.drawable.ui_small_mega3, R.drawable.ui_medium_mega3, R.drawable.ui_big_mega3, R.drawable.ui_huge_mega3} ,
145
              //       {5 , 25, R.raw.kilo3, R.drawable.ui_small_mega5, R.drawable.ui_medium_mega5, R.drawable.ui_big_mega5, R.drawable.ui_huge_mega5} ,
146
                     },
147
         TwistyMegaminx.class,
148
         new MovementMinx(),
149
         4
150
       ),
151
  ;
152

    
153
  public static final int NUM_OBJECTS = values().length;
154
  public static final int MAX_NUM_OBJECTS;
155
  public static final int MAX_LEVEL;
156
  public static final int MAX_OBJECT_SIZE;
157

    
158
  private final int[] mObjectSizes, mMaxLevels, mSmallIconIDs, mMediumIconIDs, mBigIconIDs, mHugeIconIDs, mResourceIDs;
159
  private final Class<? extends TwistyObject> mObjectClass;
160
  private final Movement mObjectMovementClass;
161
  private final int mColumn, mNumSizes;
162

    
163
  private static final ObjectList[] objects;
164
  private static int mNumAll;
165
  private static int[] mIndices;
166
  private static int mColCount, mRowCount;
167

    
168
  static
169
    {
170
    mNumAll = 0;
171
    int num, i = 0;
172
    objects = new ObjectList[NUM_OBJECTS];
173
    int maxNum  = Integer.MIN_VALUE;
174
    int maxLevel= Integer.MIN_VALUE;
175
    int maxSize = Integer.MIN_VALUE;
176

    
177
    for(ObjectList object: ObjectList.values())
178
      {
179
      objects[i] = object;
180
      i++;
181
      num = object.mObjectSizes.length;
182
      mNumAll += num;
183
      if( num> maxNum ) maxNum = num;
184

    
185
      for(int j=0; j<num; j++)
186
        {
187
        if( object.mMaxLevels[j] > maxLevel ) maxLevel = object.mMaxLevels[j];
188
        if( object.mObjectSizes[j] > maxSize) maxSize  = object.mObjectSizes[j];
189
        }
190
      }
191

    
192
    MAX_NUM_OBJECTS = maxNum;
193
    MAX_LEVEL       = maxLevel;
194
    MAX_OBJECT_SIZE = maxSize;
195
    }
196

    
197
///////////////////////////////////////////////////////////////////////////////////////////////////
198

    
199
  private static void setUpColAndRow()
200
    {
201
    mIndices = new int[NUM_OBJECTS];
202
    mColCount= 0;
203

    
204
    for(int obj=0; obj<NUM_OBJECTS; obj++)
205
      {
206
      mIndices[obj] = objects[obj].mColumn;
207
      if( mIndices[obj]>=mColCount ) mColCount = mIndices[obj]+1;
208
      }
209

    
210
    mRowCount = 0;
211

    
212
    for(int col=0; col<mColCount; col++)
213
      {
214
      int numObjects = computeNumObjectsInColumn(col);
215
      if( numObjects>mRowCount ) mRowCount = numObjects;
216
      }
217
    }
218

    
219
///////////////////////////////////////////////////////////////////////////////////////////////////
220

    
221
  private static int computeNumObjectsInColumn(int column)
222
    {
223
    int num=0;
224

    
225
    for(int object=0; object<NUM_OBJECTS; object++)
226
      {
227
      if( objects[object].mColumn == column )
228
        {
229
        num += objects[object].mNumSizes;
230
        }
231
      }
232

    
233
    return num;
234
    }
235

    
236
///////////////////////////////////////////////////////////////////////////////////////////////////
237

    
238
  public static int getColumnCount()
239
    {
240
    if( mIndices==null ) setUpColAndRow();
241

    
242
    return mColCount;
243
    }
244

    
245
///////////////////////////////////////////////////////////////////////////////////////////////////
246

    
247
  public static int getRowCount()
248
    {
249
    if( mIndices==null ) setUpColAndRow();
250

    
251
    return mRowCount;
252
    }
253

    
254
///////////////////////////////////////////////////////////////////////////////////////////////////
255

    
256
  public static int[] getIndices()
257
    {
258
    if( mIndices==null ) setUpColAndRow();
259

    
260
    return mIndices;
261
    }
262

    
263
///////////////////////////////////////////////////////////////////////////////////////////////////
264

    
265
  public static ObjectList getObject(int ordinal)
266
    {
267
    return ordinal>=0 && ordinal<NUM_OBJECTS ? objects[ordinal] : CUBE;
268
    }
269

    
270
///////////////////////////////////////////////////////////////////////////////////////////////////
271

    
272
  public static int pack(int object, int sizeIndex)
273
    {
274
    int ret = 0;
275
    for(int i=0; i<object; i++) ret += objects[i].mObjectSizes.length;
276

    
277
    return ret+sizeIndex;
278
    }
279

    
280
///////////////////////////////////////////////////////////////////////////////////////////////////
281

    
282
  public static int unpackSizeIndex(int number)
283
    {
284
    int num;
285

    
286
    for(int i=0; i<NUM_OBJECTS; i++)
287
      {
288
      num = objects[i].mObjectSizes.length;
289
      if( number<num ) return number;
290
      number -= num;
291
      }
292

    
293
    return -1;
294
    }
295

    
296
///////////////////////////////////////////////////////////////////////////////////////////////////
297

    
298
  public static int unpackObject(int number)
299
    {
300
    int num;
301

    
302
    for(int i=0; i<NUM_OBJECTS; i++)
303
      {
304
      num = objects[i].mObjectSizes.length;
305
      if( number<num ) return i;
306
      number -= num;
307
      }
308

    
309
    return -1;
310
    }
311

    
312
///////////////////////////////////////////////////////////////////////////////////////////////////
313

    
314
  public static int unpackObjectFromString(String obj)
315
    {
316
    int u = obj.indexOf('_');
317
    int l = obj.length();
318

    
319
    if( u>0 )
320
      {
321
      String name = obj.substring(0,u);
322
      int size = Integer.parseInt( obj.substring(u+1,l) );
323

    
324
      for(int i=0; i<NUM_OBJECTS; i++)
325
        {
326
        if( objects[i].name().equals(name) )
327
          {
328
          int sizeIndex = getSizeIndex(i,size);
329
          return pack(i,sizeIndex);
330
          }
331
        }
332
      }
333

    
334
    return -1;
335
    }
336

    
337
///////////////////////////////////////////////////////////////////////////////////////////////////
338

    
339
  public static String getObjectList()
340
    {
341
    String name;
342
    StringBuilder list = new StringBuilder();
343
    int len;
344
    int[] sizes;
345

    
346
    for(int i=0; i<NUM_OBJECTS; i++)
347
      {
348
      sizes = objects[i].mObjectSizes;
349
      len   = sizes.length;
350
      name  = objects[i].name();
351

    
352
      for(int j=0; j<len; j++)
353
        {
354
        if( i>0 || j>0 ) list.append(',');
355
        list.append(name);
356
        list.append('_');
357
        list.append(sizes[j]);
358
        }
359
      }
360

    
361
    return list.toString();
362
    }
363

    
364
///////////////////////////////////////////////////////////////////////////////////////////////////
365

    
366
  public static int getTotal()
367
    {
368
    return mNumAll;
369
    }
370

    
371
///////////////////////////////////////////////////////////////////////////////////////////////////
372

    
373
  public static int getMaxLevel(int ordinal, int sizeIndex)
374
    {
375
    if( ordinal>=0 && ordinal<NUM_OBJECTS )
376
      {
377
      int num = objects[ordinal].mObjectSizes.length;
378
      return sizeIndex>=0 && sizeIndex<num ? objects[ordinal].mMaxLevels[sizeIndex] : 0;
379
      }
380

    
381
    return 0;
382
    }
383

    
384
///////////////////////////////////////////////////////////////////////////////////////////////////
385

    
386
  public static int getOrdinal(String name)
387
    {
388
    for(int i=0; i<NUM_OBJECTS; i++)
389
      {
390
      if(objects[i].name().equals(name)) return i;
391
      }
392

    
393
    return -1;
394
    }
395

    
396
///////////////////////////////////////////////////////////////////////////////////////////////////
397

    
398
  public static int getSizeIndex(int ordinal, int size)
399
    {
400
    if( ordinal>=0 && ordinal<NUM_OBJECTS )
401
      {
402
      int[] sizes = objects[ordinal].getSizes();
403
      int len = sizes.length;
404

    
405
      for(int i=0; i<len; i++)
406
        {
407
        if( sizes[i]==size ) return i;
408
        }
409
      }
410

    
411
    return -1;
412
    }
413

    
414
///////////////////////////////////////////////////////////////////////////////////////////////////
415

    
416
  public static int[] retFaceColors(ObjectList object)
417
    {
418
    Field field;
419
    int[] faceColors=null;
420

    
421
    try
422
      {
423
      field = object.mObjectClass.getDeclaredField("FACE_COLORS");
424
      field.setAccessible(true);
425
      Object obj = field.get(null);
426
      faceColors = (int[]) obj;
427
      }
428
    catch(NoSuchFieldException ex)
429
      {
430
      android.util.Log.e("RubikObjectList", object.mObjectClass.getSimpleName()+": no such field exception getting field: "+ex.getMessage());
431
      }
432
    catch(IllegalAccessException ex)
433
      {
434
      android.util.Log.e("RubikObjectList", object.mObjectClass.getSimpleName()+": illegal access exception getting field: "+ex.getMessage());
435
      }
436

    
437
    return faceColors;
438
    }
439

    
440
///////////////////////////////////////////////////////////////////////////////////////////////////
441

    
442
  ObjectList(int[][] info, Class<? extends TwistyObject> object , Movement movement, int column)
443
    {
444
    mNumSizes = info.length;
445

    
446
    mObjectSizes  = new int[mNumSizes];
447
    mMaxLevels    = new int[mNumSizes];
448
    mResourceIDs  = new int[mNumSizes];
449
    mSmallIconIDs = new int[mNumSizes];
450
    mMediumIconIDs= new int[mNumSizes];
451
    mBigIconIDs   = new int[mNumSizes];
452
    mHugeIconIDs  = new int[mNumSizes];
453

    
454
    for(int i=0; i<mNumSizes; i++)
455
      {
456
      mObjectSizes[i]  = info[i][0];
457
      mMaxLevels[i]    = info[i][1];
458
      mResourceIDs[i]  = info[i][2];
459
      mSmallIconIDs[i] = info[i][3];
460
      mMediumIconIDs[i]= info[i][4];
461
      mBigIconIDs[i]   = info[i][5];
462
      mHugeIconIDs[i]  = info[i][6];
463
      }
464

    
465
    mObjectClass         = object;
466
    mObjectMovementClass = movement;
467
    mColumn              = column;
468
    }
469

    
470
///////////////////////////////////////////////////////////////////////////////////////////////////
471

    
472
  public int[] getSizes()
473
    {
474
    return mObjectSizes;
475
    }
476

    
477
///////////////////////////////////////////////////////////////////////////////////////////////////
478

    
479
  public int[] getMaxLevels()
480
    {
481
    return mMaxLevels;
482
    }
483

    
484
///////////////////////////////////////////////////////////////////////////////////////////////////
485

    
486
  public int[] getIconIDs()
487
    {
488
    int size = RubikActivity.getDrawableSize();
489

    
490
    switch(size)
491
      {
492
      case 0 : return mSmallIconIDs;
493
      case 1 : return mMediumIconIDs;
494
      case 2 : return mBigIconIDs;
495
      default: return mHugeIconIDs;
496
      }
497
    }
498

    
499
///////////////////////////////////////////////////////////////////////////////////////////////////
500

    
501
  public int[] getResourceIDs()
502
    {
503
    return mResourceIDs;
504
    }
505

    
506
///////////////////////////////////////////////////////////////////////////////////////////////////
507

    
508
  public int getNumVariants()
509
    {
510
    return mObjectSizes.length;
511
    }
512

    
513
///////////////////////////////////////////////////////////////////////////////////////////////////
514

    
515
  public TwistyObject create(int size, Static4D quat, int[][] moves, Resources res, int scrWidth)
516
    {
517
    DistortedTexture texture = new DistortedTexture();
518
    DistortedEffects effects = new DistortedEffects();
519
    MeshSquare mesh          = new MeshSquare(20,20);   // mesh of the node, not of the cubits
520

    
521
    switch(ordinal())
522
      {
523
      case  0: return new TwistyCube      (size, quat, texture, mesh, effects, moves, res, scrWidth);
524
      case  1: return new TwistyPyraminx  (size, quat, texture, mesh, effects, moves, res, scrWidth);
525
      case  2: return new TwistyDiamond   (size, quat, texture, mesh, effects, moves, res, scrWidth);
526
      case  3: return new TwistyDino6     (size, quat, texture, mesh, effects, moves, res, scrWidth);
527
      case  4: return new TwistyDino4     (size, quat, texture, mesh, effects, moves, res, scrWidth);
528
      case  5: return new TwistyRedi      (size, quat, texture, mesh, effects, moves, res, scrWidth);
529
      case  6: return new TwistyHelicopter(size, quat, texture, mesh, effects, moves, res, scrWidth);
530
      case  7: return new TwistySkewb     (size, quat, texture, mesh, effects, moves, res, scrWidth);
531
      case  8: return new TwistyIvy       (size, quat, texture, mesh, effects, moves, res, scrWidth);
532
      case  9: return new TwistyRex       (size, quat, texture, mesh, effects, moves, res, scrWidth);
533
      case 10: return new TwistyKilominx  (size, quat, texture, mesh, effects, moves, res, scrWidth);
534
      case 11: return new TwistyMegaminx  (size, quat, texture, mesh, effects, moves, res, scrWidth);
535
      }
536

    
537
    return null;
538
    }
539

    
540
///////////////////////////////////////////////////////////////////////////////////////////////////
541

    
542
  public Movement getObjectMovementClass()
543
    {
544
    return mObjectMovementClass;
545
    }
546
  }
(15-15/30)