Project

General

Profile

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

magiccube / src / main / java / org / distorted / objects / RubikObjectList.java @ 1576f41c

1 27a70eae Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 1f9772f3 Leszek Koltunski
package org.distorted.objects;
21 27a70eae Leszek Koltunski
22 ccf9fec5 Leszek Koltunski
import android.content.res.Resources;
23
24 27a70eae Leszek Koltunski
import org.distorted.library.main.DistortedEffects;
25
import org.distorted.library.main.DistortedTexture;
26 efa8aa48 Leszek Koltunski
import org.distorted.library.mesh.MeshSquare;
27 27a70eae Leszek Koltunski
import org.distorted.library.type.Static4D;
28 1f9772f3 Leszek Koltunski
import org.distorted.main.R;
29 f6a9c538 Leszek Koltunski
import org.distorted.main.RubikActivity;
30 27a70eae Leszek Koltunski
31 20931cf6 Leszek Koltunski
import java.lang.reflect.Field;
32
33 27a70eae Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
34
35
public enum RubikObjectList
36
  {
37 4c0cd600 Leszek Koltunski
  CUBE (
38
         new int[][] {
39 31a9f38d Leszek Koltunski
                       {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 4c0cd600 Leszek Koltunski
                     },
44 20931cf6 Leszek Koltunski
         RubikCube.class,
45 fa679111 Leszek Koltunski
         new RubikMovementCube(),
46
         0
47 4c0cd600 Leszek Koltunski
       ),
48
49
  PYRA (
50
         new int[][] {
51 31a9f38d Leszek Koltunski
                       {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 4c0cd600 Leszek Koltunski
                     },
55 20931cf6 Leszek Koltunski
         RubikPyraminx.class,
56 fa679111 Leszek Koltunski
         new RubikMovementPyraminx(),
57
         1
58 4c0cd600 Leszek Koltunski
       ),
59 418aa554 Leszek Koltunski
60
  DINO (
61
         new int[][] {
62 f05807f7 Leszek Koltunski
                       {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} ,
63 418aa554 Leszek Koltunski
                     },
64
         RubikDino.class,
65 fa679111 Leszek Koltunski
         new RubikMovementDino(),
66
         2
67 418aa554 Leszek Koltunski
       ),
68 27a70eae Leszek Koltunski
  ;
69
70 4888e97c Leszek Koltunski
  public static final int NUM_OBJECTS = values().length;
71 8cccfb10 Leszek Koltunski
  public static final int MAX_NUM_OBJECTS;
72 8e3898c8 Leszek Koltunski
  public static final int MAX_LEVEL;
73 8cccfb10 Leszek Koltunski
  public static final int MAX_OBJECT_SIZE;
74 4888e97c Leszek Koltunski
75 31a9f38d Leszek Koltunski
  private final int[] mObjectSizes, mMaxLevels, mSmallIconIDs, mMediumIconIDs, mBigIconIDs, mHugeIconIDs, mResourceIDs;
76 20931cf6 Leszek Koltunski
  private final Class<? extends RubikObject> mObjectClass;
77 faa3aed0 Leszek Koltunski
  private final RubikMovement mObjectMovementClass;
78 fa679111 Leszek Koltunski
  private final int mColumn, mNumSizes;
79
80 27a70eae Leszek Koltunski
  private static final RubikObjectList[] objects;
81 4888e97c Leszek Koltunski
  private static int mNumAll;
82 e07c48a2 Leszek Koltunski
  private static int[] mIndices;
83 fa679111 Leszek Koltunski
  private static int mColCount, mRowCount;
84 27a70eae Leszek Koltunski
85
  static
86
    {
87 4888e97c Leszek Koltunski
    mNumAll = 0;
88 8cccfb10 Leszek Koltunski
    int num, i = 0;
89 4888e97c Leszek Koltunski
    objects = new RubikObjectList[NUM_OBJECTS];
90 8cccfb10 Leszek Koltunski
    int maxNum  = Integer.MIN_VALUE;
91
    int maxLevel= Integer.MIN_VALUE;
92
    int maxSize = Integer.MIN_VALUE;
93 27a70eae Leszek Koltunski
94 4888e97c Leszek Koltunski
    for(RubikObjectList object: RubikObjectList.values())
95 27a70eae Leszek Koltunski
      {
96 4888e97c Leszek Koltunski
      objects[i] = object;
97 27a70eae Leszek Koltunski
      i++;
98 8cccfb10 Leszek Koltunski
      num = object.mObjectSizes.length;
99
      mNumAll += num;
100
      if( num> maxNum ) maxNum = num;
101 8e3898c8 Leszek Koltunski
102 8cccfb10 Leszek Koltunski
      for(int j=0; j<num; j++)
103 8e3898c8 Leszek Koltunski
        {
104
        if( object.mMaxLevels[j] > maxLevel ) maxLevel = object.mMaxLevels[j];
105 8cccfb10 Leszek Koltunski
        if( object.mObjectSizes[j] > maxSize) maxSize  = object.mObjectSizes[j];
106 8e3898c8 Leszek Koltunski
        }
107 27a70eae Leszek Koltunski
      }
108 4888e97c Leszek Koltunski
109 8cccfb10 Leszek Koltunski
    MAX_NUM_OBJECTS = maxNum;
110
    MAX_LEVEL       = maxLevel;
111
    MAX_OBJECT_SIZE = maxSize;
112 27a70eae Leszek Koltunski
    }
113
114 e07c48a2 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
115 fa679111 Leszek Koltunski
116
  private static void setUpColAndRow()
117
    {
118
    mIndices = new int[NUM_OBJECTS];
119
    mColCount= 0;
120
121
    for(int obj=0; obj<NUM_OBJECTS; obj++)
122
      {
123
      mIndices[obj] = objects[obj].mColumn;
124
      if( mIndices[obj]>=mColCount ) mColCount = mIndices[obj]+1;
125
      }
126
127
    mRowCount = 0;
128
129
    for(int col=0; col<mColCount; col++)
130
      {
131
      int numObjects = computeNumObjectsInColumn(col);
132
      if( numObjects>mRowCount ) mRowCount = numObjects;
133
      }
134
    }
135
136
///////////////////////////////////////////////////////////////////////////////////////////////////
137
138
  private static int computeNumObjectsInColumn(int column)
139
    {
140
    int num=0;
141
142
    for(int object=0; object<NUM_OBJECTS; object++)
143
      {
144
      if( objects[object].mColumn == column )
145
        {
146
        num += objects[object].mNumSizes;
147
        }
148
      }
149
150
    return num;
151
    }
152
153
///////////////////////////////////////////////////////////////////////////////////////////////////
154
155 e07c48a2 Leszek Koltunski
  public static int getColumnCount()
156
    {
157 fa679111 Leszek Koltunski
    if( mIndices==null ) setUpColAndRow();
158
159
    return mColCount;
160 e07c48a2 Leszek Koltunski
    }
161
162
///////////////////////////////////////////////////////////////////////////////////////////////////
163 fa679111 Leszek Koltunski
164 e07c48a2 Leszek Koltunski
  public static int getRowCount()
165
    {
166 fa679111 Leszek Koltunski
    if( mIndices==null ) setUpColAndRow();
167
168
    return mRowCount;
169 e07c48a2 Leszek Koltunski
    }
170
171
///////////////////////////////////////////////////////////////////////////////////////////////////
172 fa679111 Leszek Koltunski
173 e07c48a2 Leszek Koltunski
  public static int[] getIndices()
174
    {
175 fa679111 Leszek Koltunski
    if( mIndices==null ) setUpColAndRow();
176
177 e07c48a2 Leszek Koltunski
    return mIndices;
178
    }
179
180 27a70eae Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
181
182
  public static RubikObjectList getObject(int ordinal)
183
    {
184 7ec268b1 Leszek Koltunski
    return ordinal>=0 && ordinal<NUM_OBJECTS ? objects[ordinal] : CUBE;
185 27a70eae Leszek Koltunski
    }
186
187
///////////////////////////////////////////////////////////////////////////////////////////////////
188
189 b498f3f6 Leszek Koltunski
  public static int pack(int object, int sizeIndex)
190 4888e97c Leszek Koltunski
    {
191
    int ret = 0;
192
    for(int i=0; i<object; i++) ret += objects[i].mObjectSizes.length;
193
194 b498f3f6 Leszek Koltunski
    return ret+sizeIndex;
195 4888e97c Leszek Koltunski
    }
196
197
///////////////////////////////////////////////////////////////////////////////////////////////////
198
199 8e3898c8 Leszek Koltunski
  public static int unpackSizeIndex(int number)
200 4888e97c Leszek Koltunski
    {
201
    int num;
202
203
    for(int i=0; i<NUM_OBJECTS; i++)
204
      {
205
      num = objects[i].mObjectSizes.length;
206
      if( number<num ) return number;
207
      number -= num;
208
      }
209
210
    return -1;
211
    }
212
213
///////////////////////////////////////////////////////////////////////////////////////////////////
214
215
  public static int unpackObject(int number)
216 27a70eae Leszek Koltunski
    {
217 4888e97c Leszek Koltunski
    int num;
218
219
    for(int i=0; i<NUM_OBJECTS; i++)
220
      {
221
      num = objects[i].mObjectSizes.length;
222
      if( number<num ) return i;
223
      number -= num;
224
      }
225
226
    return -1;
227
    }
228
229 286d73ae Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
230
231
  public static int unpackObjectFromString(String obj)
232
    {
233
    int u = obj.indexOf('_');
234
    int l = obj.length();
235
236
    if( u>0 )
237
      {
238
      String name = obj.substring(0,u);
239
      int size = Integer.parseInt( obj.substring(u+1,l) );
240
241
      for(int i=0; i<NUM_OBJECTS; i++)
242
        {
243
        if( objects[i].name().equals(name) )
244
          {
245 53f23b64 Leszek Koltunski
          int sizeIndex = getSizeIndex(i,size);
246
          return pack(i,sizeIndex);
247 286d73ae Leszek Koltunski
          }
248
        }
249
      }
250
251
    return -1;
252
    }
253
254
///////////////////////////////////////////////////////////////////////////////////////////////////
255
256
  public static String getObjectList()
257
    {
258
    String name;
259
    StringBuilder list = new StringBuilder();
260
    int len;
261
    int[] sizes;
262
263
    for(int i=0; i<NUM_OBJECTS; i++)
264
      {
265
      sizes = objects[i].mObjectSizes;
266
      len   = sizes.length;
267
      name  = objects[i].name();
268
269
      for(int j=0; j<len; j++)
270
        {
271
        if( i>0 || j>0 ) list.append(',');
272
        list.append(name);
273
        list.append('_');
274
        list.append(sizes[j]);
275
        }
276
      }
277
278
    return list.toString();
279
    }
280
281 4888e97c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
282
283
  public static int getTotal()
284
    {
285
    return mNumAll;
286
    }
287
288 8e3898c8 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
289
290
  public static int getMaxLevel(int ordinal, int sizeIndex)
291
    {
292
    if( ordinal>=0 && ordinal<NUM_OBJECTS )
293
      {
294
      int num = objects[ordinal].mObjectSizes.length;
295
      return sizeIndex>=0 && sizeIndex<num ? objects[ordinal].mMaxLevels[sizeIndex] : 0;
296
      }
297
298
    return 0;
299
    }
300
301 c86f9f1f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
302
303
  public static int getOrdinal(String name)
304
    {
305
    for(int i=0; i<NUM_OBJECTS; i++)
306
      {
307
      if(objects[i].name().equals(name)) return i;
308
      }
309
310
    return -1;
311
    }
312
313
///////////////////////////////////////////////////////////////////////////////////////////////////
314
315 53f23b64 Leszek Koltunski
  public static int getSizeIndex(int ordinal, int size)
316 c86f9f1f Leszek Koltunski
    {
317 7ec268b1 Leszek Koltunski
    if( ordinal>=0 && ordinal<NUM_OBJECTS )
318 c86f9f1f Leszek Koltunski
      {
319 7ec268b1 Leszek Koltunski
      int[] sizes = objects[ordinal].getSizes();
320
      int len = sizes.length;
321
322
      for(int i=0; i<len; i++)
323
        {
324
        if( sizes[i]==size ) return i;
325
        }
326 c86f9f1f Leszek Koltunski
      }
327
328
    return -1;
329
    }
330
331 4888e97c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
332
333 20931cf6 Leszek Koltunski
  public static int[] retFaceColors(RubikObjectList object)
334
    {
335
    Field field;
336
    int[] faceColors=null;
337
338
    try
339
      {
340
      field = object.mObjectClass.getDeclaredField("FACE_COLORS");
341
      field.setAccessible(true);
342
      Object obj = field.get(null);
343
      faceColors = (int[]) obj;
344
      }
345
    catch(NoSuchFieldException ex)
346
      {
347
      android.util.Log.e("RubikObjectList", object.mObjectClass.getSimpleName()+": no such field exception getting field: "+ex.getMessage());
348
      }
349
    catch(IllegalAccessException ex)
350
      {
351
      android.util.Log.e("RubikObjectList", object.mObjectClass.getSimpleName()+": illegal access exception getting field: "+ex.getMessage());
352
      }
353
354
    return faceColors;
355
    }
356
357
///////////////////////////////////////////////////////////////////////////////////////////////////
358
359 fa679111 Leszek Koltunski
  RubikObjectList(int[][] info, Class<? extends RubikObject> object , RubikMovement movement, int column)
360 4888e97c Leszek Koltunski
    {
361 fa679111 Leszek Koltunski
    mNumSizes = info.length;
362 ea6ee91b Leszek Koltunski
363 fa679111 Leszek Koltunski
    mObjectSizes  = new int[mNumSizes];
364
    mMaxLevels    = new int[mNumSizes];
365
    mResourceIDs  = new int[mNumSizes];
366
    mSmallIconIDs = new int[mNumSizes];
367
    mMediumIconIDs= new int[mNumSizes];
368
    mBigIconIDs   = new int[mNumSizes];
369
    mHugeIconIDs  = new int[mNumSizes];
370 ea6ee91b Leszek Koltunski
371 fa679111 Leszek Koltunski
    for(int i=0; i<mNumSizes; i++)
372 ea6ee91b Leszek Koltunski
      {
373 f6a9c538 Leszek Koltunski
      mObjectSizes[i]  = info[i][0];
374
      mMaxLevels[i]    = info[i][1];
375
      mResourceIDs[i]  = info[i][2];
376
      mSmallIconIDs[i] = info[i][3];
377
      mMediumIconIDs[i]= info[i][4];
378
      mBigIconIDs[i]   = info[i][5];
379 31a9f38d Leszek Koltunski
      mHugeIconIDs[i]  = info[i][6];
380 ea6ee91b Leszek Koltunski
      }
381
382 20931cf6 Leszek Koltunski
    mObjectClass         = object;
383 27a70eae Leszek Koltunski
    mObjectMovementClass = movement;
384 fa679111 Leszek Koltunski
    mColumn              = column;
385 27a70eae Leszek Koltunski
    }
386
387
///////////////////////////////////////////////////////////////////////////////////////////////////
388
389 8e3898c8 Leszek Koltunski
  public int[] getSizes()
390 27a70eae Leszek Koltunski
    {
391 8e3898c8 Leszek Koltunski
    return mObjectSizes;
392 27a70eae Leszek Koltunski
    }
393
394
///////////////////////////////////////////////////////////////////////////////////////////////////
395
396 8e3898c8 Leszek Koltunski
  public int[] getMaxLevels()
397 4888e97c Leszek Koltunski
    {
398 8e3898c8 Leszek Koltunski
    return mMaxLevels;
399
    }
400
401
///////////////////////////////////////////////////////////////////////////////////////////////////
402
403
  public int[] getIconIDs()
404
    {
405 f6a9c538 Leszek Koltunski
    int size = RubikActivity.getDrawableSize();
406
407
    switch(size)
408
      {
409
      case 0 : return mSmallIconIDs;
410
      case 1 : return mMediumIconIDs;
411 31a9f38d Leszek Koltunski
      case 2 : return mBigIconIDs;
412
      default: return mHugeIconIDs;
413 f6a9c538 Leszek Koltunski
      }
414 4888e97c Leszek Koltunski
    }
415
416
///////////////////////////////////////////////////////////////////////////////////////////////////
417
418 ccf9fec5 Leszek Koltunski
  public int[] getResourceIDs()
419
    {
420
    return mResourceIDs;
421
    }
422
423 fbca0033 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
424
425
  public int getNumVariants()
426
    {
427
    return mObjectSizes.length;
428
    }
429
430 ccf9fec5 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
431
432 5b893eee Leszek Koltunski
  public RubikObject create(int size, Static4D quat, int[][] moves, Resources res, int scrWidth)
433 27a70eae Leszek Koltunski
    {
434 5974d2ae Leszek Koltunski
    DistortedTexture texture = new DistortedTexture();
435 36273130 Leszek Koltunski
    DistortedEffects effects = new DistortedEffects();
436 efa8aa48 Leszek Koltunski
    MeshSquare mesh      = new MeshSquare(20,20);   // mesh of the node, not of the cubits
437 74686c71 Leszek Koltunski
438 4888e97c Leszek Koltunski
    switch(ordinal())
439
      {
440 5b893eee Leszek Koltunski
      case 0: return new RubikCube    (size, quat, texture, mesh, effects, moves, res, scrWidth);
441
      case 1: return new RubikPyraminx(size, quat, texture, mesh, effects, moves, res, scrWidth);
442 418aa554 Leszek Koltunski
      case 2: return new RubikDino    (size, quat, texture, mesh, effects, moves, res, scrWidth);
443 4888e97c Leszek Koltunski
      }
444
445
    return null;
446 27a70eae Leszek Koltunski
    }
447
448
///////////////////////////////////////////////////////////////////////////////////////////////////
449
450 faa3aed0 Leszek Koltunski
  public RubikMovement getObjectMovementClass()
451 27a70eae Leszek Koltunski
    {
452 4888e97c Leszek Koltunski
    return mObjectMovementClass;
453 27a70eae Leszek Koltunski
    }
454
  }