Project

General

Profile

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

magiccube / src / main / java / org / distorted / objects / ObjectList.java @ 03aa05d5

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

    
134
  public static final int NUM_OBJECTS = values().length;
135
  public static final int MAX_NUM_OBJECTS;
136
  public static final int MAX_LEVEL;
137
  public static final int MAX_OBJECT_SIZE;
138

    
139
  private final int[] mObjectSizes, mMaxLevels, mSmallIconIDs, mMediumIconIDs, mBigIconIDs, mHugeIconIDs, mResourceIDs;
140
  private final Class<? extends TwistyObject> mObjectClass;
141
  private final Movement mObjectMovementClass;
142
  private final int mColumn, mNumSizes;
143

    
144
  private static final ObjectList[] objects;
145
  private static int mNumAll;
146
  private static int[] mIndices;
147
  private static int mColCount, mRowCount;
148

    
149
  static
150
    {
151
    mNumAll = 0;
152
    int num, i = 0;
153
    objects = new ObjectList[NUM_OBJECTS];
154
    int maxNum  = Integer.MIN_VALUE;
155
    int maxLevel= Integer.MIN_VALUE;
156
    int maxSize = Integer.MIN_VALUE;
157

    
158
    for(ObjectList object: ObjectList.values())
159
      {
160
      objects[i] = object;
161
      i++;
162
      num = object.mObjectSizes.length;
163
      mNumAll += num;
164
      if( num> maxNum ) maxNum = num;
165

    
166
      for(int j=0; j<num; j++)
167
        {
168
        if( object.mMaxLevels[j] > maxLevel ) maxLevel = object.mMaxLevels[j];
169
        if( object.mObjectSizes[j] > maxSize) maxSize  = object.mObjectSizes[j];
170
        }
171
      }
172

    
173
    MAX_NUM_OBJECTS = maxNum;
174
    MAX_LEVEL       = maxLevel;
175
    MAX_OBJECT_SIZE = maxSize;
176
    }
177

    
178
///////////////////////////////////////////////////////////////////////////////////////////////////
179

    
180
  private static void setUpColAndRow()
181
    {
182
    mIndices = new int[NUM_OBJECTS];
183
    mColCount= 0;
184

    
185
    for(int obj=0; obj<NUM_OBJECTS; obj++)
186
      {
187
      mIndices[obj] = objects[obj].mColumn;
188
      if( mIndices[obj]>=mColCount ) mColCount = mIndices[obj]+1;
189
      }
190

    
191
    mRowCount = 0;
192

    
193
    for(int col=0; col<mColCount; col++)
194
      {
195
      int numObjects = computeNumObjectsInColumn(col);
196
      if( numObjects>mRowCount ) mRowCount = numObjects;
197
      }
198
    }
199

    
200
///////////////////////////////////////////////////////////////////////////////////////////////////
201

    
202
  private static int computeNumObjectsInColumn(int column)
203
    {
204
    int num=0;
205

    
206
    for(int object=0; object<NUM_OBJECTS; object++)
207
      {
208
      if( objects[object].mColumn == column )
209
        {
210
        num += objects[object].mNumSizes;
211
        }
212
      }
213

    
214
    return num;
215
    }
216

    
217
///////////////////////////////////////////////////////////////////////////////////////////////////
218

    
219
  public static int getColumnCount()
220
    {
221
    if( mIndices==null ) setUpColAndRow();
222

    
223
    return mColCount;
224
    }
225

    
226
///////////////////////////////////////////////////////////////////////////////////////////////////
227

    
228
  public static int getRowCount()
229
    {
230
    if( mIndices==null ) setUpColAndRow();
231

    
232
    return mRowCount;
233
    }
234

    
235
///////////////////////////////////////////////////////////////////////////////////////////////////
236

    
237
  public static int[] getIndices()
238
    {
239
    if( mIndices==null ) setUpColAndRow();
240

    
241
    return mIndices;
242
    }
243

    
244
///////////////////////////////////////////////////////////////////////////////////////////////////
245

    
246
  public static ObjectList getObject(int ordinal)
247
    {
248
    return ordinal>=0 && ordinal<NUM_OBJECTS ? objects[ordinal] : CUBE;
249
    }
250

    
251
///////////////////////////////////////////////////////////////////////////////////////////////////
252

    
253
  public static int pack(int object, int sizeIndex)
254
    {
255
    int ret = 0;
256
    for(int i=0; i<object; i++) ret += objects[i].mObjectSizes.length;
257

    
258
    return ret+sizeIndex;
259
    }
260

    
261
///////////////////////////////////////////////////////////////////////////////////////////////////
262

    
263
  public static int unpackSizeIndex(int number)
264
    {
265
    int num;
266

    
267
    for(int i=0; i<NUM_OBJECTS; i++)
268
      {
269
      num = objects[i].mObjectSizes.length;
270
      if( number<num ) return number;
271
      number -= num;
272
      }
273

    
274
    return -1;
275
    }
276

    
277
///////////////////////////////////////////////////////////////////////////////////////////////////
278

    
279
  public static int unpackObject(int number)
280
    {
281
    int num;
282

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

    
290
    return -1;
291
    }
292

    
293
///////////////////////////////////////////////////////////////////////////////////////////////////
294

    
295
  public static int unpackObjectFromString(String obj)
296
    {
297
    int u = obj.indexOf('_');
298
    int l = obj.length();
299

    
300
    if( u>0 )
301
      {
302
      String name = obj.substring(0,u);
303
      int size = Integer.parseInt( obj.substring(u+1,l) );
304

    
305
      for(int i=0; i<NUM_OBJECTS; i++)
306
        {
307
        if( objects[i].name().equals(name) )
308
          {
309
          int sizeIndex = getSizeIndex(i,size);
310
          return pack(i,sizeIndex);
311
          }
312
        }
313
      }
314

    
315
    return -1;
316
    }
317

    
318
///////////////////////////////////////////////////////////////////////////////////////////////////
319

    
320
  public static String getObjectList()
321
    {
322
    String name;
323
    StringBuilder list = new StringBuilder();
324
    int len;
325
    int[] sizes;
326

    
327
    for(int i=0; i<NUM_OBJECTS; i++)
328
      {
329
      sizes = objects[i].mObjectSizes;
330
      len   = sizes.length;
331
      name  = objects[i].name();
332

    
333
      for(int j=0; j<len; j++)
334
        {
335
        if( i>0 || j>0 ) list.append(',');
336
        list.append(name);
337
        list.append('_');
338
        list.append(sizes[j]);
339
        }
340
      }
341

    
342
    return list.toString();
343
    }
344

    
345
///////////////////////////////////////////////////////////////////////////////////////////////////
346

    
347
  public static int getTotal()
348
    {
349
    return mNumAll;
350
    }
351

    
352
///////////////////////////////////////////////////////////////////////////////////////////////////
353

    
354
  public static int getMaxLevel(int ordinal, int sizeIndex)
355
    {
356
    if( ordinal>=0 && ordinal<NUM_OBJECTS )
357
      {
358
      int num = objects[ordinal].mObjectSizes.length;
359
      return sizeIndex>=0 && sizeIndex<num ? objects[ordinal].mMaxLevels[sizeIndex] : 0;
360
      }
361

    
362
    return 0;
363
    }
364

    
365
///////////////////////////////////////////////////////////////////////////////////////////////////
366

    
367
  public static int getOrdinal(String name)
368
    {
369
    for(int i=0; i<NUM_OBJECTS; i++)
370
      {
371
      if(objects[i].name().equals(name)) return i;
372
      }
373

    
374
    return -1;
375
    }
376

    
377
///////////////////////////////////////////////////////////////////////////////////////////////////
378

    
379
  public static int getSizeIndex(int ordinal, int size)
380
    {
381
    if( ordinal>=0 && ordinal<NUM_OBJECTS )
382
      {
383
      int[] sizes = objects[ordinal].getSizes();
384
      int len = sizes.length;
385

    
386
      for(int i=0; i<len; i++)
387
        {
388
        if( sizes[i]==size ) return i;
389
        }
390
      }
391

    
392
    return -1;
393
    }
394

    
395
///////////////////////////////////////////////////////////////////////////////////////////////////
396

    
397
  public static int[] retFaceColors(ObjectList object)
398
    {
399
    Field field;
400
    int[] faceColors=null;
401

    
402
    try
403
      {
404
      field = object.mObjectClass.getDeclaredField("FACE_COLORS");
405
      field.setAccessible(true);
406
      Object obj = field.get(null);
407
      faceColors = (int[]) obj;
408
      }
409
    catch(NoSuchFieldException ex)
410
      {
411
      android.util.Log.e("RubikObjectList", object.mObjectClass.getSimpleName()+": no such field exception getting field: "+ex.getMessage());
412
      }
413
    catch(IllegalAccessException ex)
414
      {
415
      android.util.Log.e("RubikObjectList", object.mObjectClass.getSimpleName()+": illegal access exception getting field: "+ex.getMessage());
416
      }
417

    
418
    return faceColors;
419
    }
420

    
421
///////////////////////////////////////////////////////////////////////////////////////////////////
422

    
423
  ObjectList(int[][] info, Class<? extends TwistyObject> object , Movement movement, int column)
424
    {
425
    mNumSizes = info.length;
426

    
427
    mObjectSizes  = new int[mNumSizes];
428
    mMaxLevels    = new int[mNumSizes];
429
    mResourceIDs  = new int[mNumSizes];
430
    mSmallIconIDs = new int[mNumSizes];
431
    mMediumIconIDs= new int[mNumSizes];
432
    mBigIconIDs   = new int[mNumSizes];
433
    mHugeIconIDs  = new int[mNumSizes];
434

    
435
    for(int i=0; i<mNumSizes; i++)
436
      {
437
      mObjectSizes[i]  = info[i][0];
438
      mMaxLevels[i]    = info[i][1];
439
      mResourceIDs[i]  = info[i][2];
440
      mSmallIconIDs[i] = info[i][3];
441
      mMediumIconIDs[i]= info[i][4];
442
      mBigIconIDs[i]   = info[i][5];
443
      mHugeIconIDs[i]  = info[i][6];
444
      }
445

    
446
    mObjectClass         = object;
447
    mObjectMovementClass = movement;
448
    mColumn              = column;
449
    }
450

    
451
///////////////////////////////////////////////////////////////////////////////////////////////////
452

    
453
  public int[] getSizes()
454
    {
455
    return mObjectSizes;
456
    }
457

    
458
///////////////////////////////////////////////////////////////////////////////////////////////////
459

    
460
  public int[] getMaxLevels()
461
    {
462
    return mMaxLevels;
463
    }
464

    
465
///////////////////////////////////////////////////////////////////////////////////////////////////
466

    
467
  public int[] getIconIDs()
468
    {
469
    int size = RubikActivity.getDrawableSize();
470

    
471
    switch(size)
472
      {
473
      case 0 : return mSmallIconIDs;
474
      case 1 : return mMediumIconIDs;
475
      case 2 : return mBigIconIDs;
476
      default: return mHugeIconIDs;
477
      }
478
    }
479

    
480
///////////////////////////////////////////////////////////////////////////////////////////////////
481

    
482
  public int[] getResourceIDs()
483
    {
484
    return mResourceIDs;
485
    }
486

    
487
///////////////////////////////////////////////////////////////////////////////////////////////////
488

    
489
  public int getNumVariants()
490
    {
491
    return mObjectSizes.length;
492
    }
493

    
494
///////////////////////////////////////////////////////////////////////////////////////////////////
495

    
496
  public TwistyObject create(int size, Static4D quat, int[][] moves, Resources res, int scrWidth)
497
    {
498
    DistortedTexture texture = new DistortedTexture();
499
    DistortedEffects effects = new DistortedEffects();
500
    MeshSquare mesh          = new MeshSquare(20,20);   // mesh of the node, not of the cubits
501

    
502
    switch(ordinal())
503
      {
504
      case 0: return new TwistyCube      (size, quat, texture, mesh, effects, moves, res, scrWidth);
505
      case 1: return new TwistyPyraminx  (size, quat, texture, mesh, effects, moves, res, scrWidth);
506
      case 2: return new TwistyDiamond   (size, quat, texture, mesh, effects, moves, res, scrWidth);
507
      case 3: return new TwistyDino6     (size, quat, texture, mesh, effects, moves, res, scrWidth);
508
      case 4: return new TwistyDino4     (size, quat, texture, mesh, effects, moves, res, scrWidth);
509
      case 5: return new TwistyRedi      (size, quat, texture, mesh, effects, moves, res, scrWidth);
510
      case 6: return new TwistyHelicopter(size, quat, texture, mesh, effects, moves, res, scrWidth);
511
      case 7: return new TwistySkewb     (size, quat, texture, mesh, effects, moves, res, scrWidth);
512
      case 8: return new TwistyIvy       (size, quat, texture, mesh, effects, moves, res, scrWidth);
513
      case 9: return new TwistyRex       (size, quat, texture, mesh, effects, moves, res, scrWidth);
514
      }
515

    
516
    return null;
517
    }
518

    
519
///////////////////////////////////////////////////////////////////////////////////////////////////
520

    
521
  public Movement getObjectMovementClass()
522
    {
523
    return mObjectMovementClass;
524
    }
525
  }
(14-14/26)