Project

General

Profile

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

magiccube / src / main / java / org / distorted / objects / RubikObjectList.java @ 418aa554

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 97c012ae Leszek Koltunski
import org.distorted.library.mesh.MeshRectangles;
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 4c0cd600 Leszek Koltunski
         new RubikCubeMovement()
46
       ),
47
48
  PYRA (
49
         new int[][] {
50 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} ,
51
                       {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} ,
52
                       {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}
53 4c0cd600 Leszek Koltunski
                     },
54 20931cf6 Leszek Koltunski
         RubikPyraminx.class,
55 4c0cd600 Leszek Koltunski
         new RubikPyraminxMovement()
56
       ),
57 418aa554 Leszek Koltunski
58
  DINO (
59
         new int[][] {
60
                       {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} ,
61
                     },
62
         RubikDino.class,
63
         new RubikDinoMovement()
64
       ),
65 27a70eae Leszek Koltunski
  ;
66
67 4888e97c Leszek Koltunski
  public static final int NUM_OBJECTS = values().length;
68 8cccfb10 Leszek Koltunski
  public static final int MAX_NUM_OBJECTS;
69 8e3898c8 Leszek Koltunski
  public static final int MAX_LEVEL;
70 8cccfb10 Leszek Koltunski
  public static final int MAX_OBJECT_SIZE;
71 4888e97c Leszek Koltunski
72 31a9f38d Leszek Koltunski
  private final int[] mObjectSizes, mMaxLevels, mSmallIconIDs, mMediumIconIDs, mBigIconIDs, mHugeIconIDs, mResourceIDs;
73 20931cf6 Leszek Koltunski
  private final Class<? extends RubikObject> mObjectClass;
74
  private final RubikObjectMovement mObjectMovementClass;
75 27a70eae Leszek Koltunski
  private static final RubikObjectList[] objects;
76 4888e97c Leszek Koltunski
  private static int mNumAll;
77 27a70eae Leszek Koltunski
78
  static
79
    {
80 4888e97c Leszek Koltunski
    mNumAll = 0;
81 8cccfb10 Leszek Koltunski
    int num, i = 0;
82 4888e97c Leszek Koltunski
    objects = new RubikObjectList[NUM_OBJECTS];
83 8cccfb10 Leszek Koltunski
    int maxNum  = Integer.MIN_VALUE;
84
    int maxLevel= Integer.MIN_VALUE;
85
    int maxSize = Integer.MIN_VALUE;
86 27a70eae Leszek Koltunski
87 4888e97c Leszek Koltunski
    for(RubikObjectList object: RubikObjectList.values())
88 27a70eae Leszek Koltunski
      {
89 4888e97c Leszek Koltunski
      objects[i] = object;
90 27a70eae Leszek Koltunski
      i++;
91 8cccfb10 Leszek Koltunski
      num = object.mObjectSizes.length;
92
      mNumAll += num;
93
      if( num> maxNum ) maxNum = num;
94 8e3898c8 Leszek Koltunski
95 8cccfb10 Leszek Koltunski
      for(int j=0; j<num; j++)
96 8e3898c8 Leszek Koltunski
        {
97
        if( object.mMaxLevels[j] > maxLevel ) maxLevel = object.mMaxLevels[j];
98 8cccfb10 Leszek Koltunski
        if( object.mObjectSizes[j] > maxSize) maxSize  = object.mObjectSizes[j];
99 8e3898c8 Leszek Koltunski
        }
100 27a70eae Leszek Koltunski
      }
101 4888e97c Leszek Koltunski
102 8cccfb10 Leszek Koltunski
    MAX_NUM_OBJECTS = maxNum;
103
    MAX_LEVEL       = maxLevel;
104
    MAX_OBJECT_SIZE = maxSize;
105 27a70eae Leszek Koltunski
    }
106
107
///////////////////////////////////////////////////////////////////////////////////////////////////
108
109
  public static RubikObjectList getObject(int ordinal)
110
    {
111
    return objects[ordinal];
112
    }
113
114
///////////////////////////////////////////////////////////////////////////////////////////////////
115
116 b498f3f6 Leszek Koltunski
  public static int pack(int object, int sizeIndex)
117 4888e97c Leszek Koltunski
    {
118
    int ret = 0;
119
    for(int i=0; i<object; i++) ret += objects[i].mObjectSizes.length;
120
121 b498f3f6 Leszek Koltunski
    return ret+sizeIndex;
122 4888e97c Leszek Koltunski
    }
123
124
///////////////////////////////////////////////////////////////////////////////////////////////////
125
126 8e3898c8 Leszek Koltunski
  public static int unpackSizeIndex(int number)
127 4888e97c Leszek Koltunski
    {
128
    int num;
129
130
    for(int i=0; i<NUM_OBJECTS; i++)
131
      {
132
      num = objects[i].mObjectSizes.length;
133
      if( number<num ) return number;
134
      number -= num;
135
      }
136
137
    return -1;
138
    }
139
140
///////////////////////////////////////////////////////////////////////////////////////////////////
141
142
  public static int unpackObject(int number)
143 27a70eae Leszek Koltunski
    {
144 4888e97c Leszek Koltunski
    int num;
145
146
    for(int i=0; i<NUM_OBJECTS; i++)
147
      {
148
      num = objects[i].mObjectSizes.length;
149
      if( number<num ) return i;
150
      number -= num;
151
      }
152
153
    return -1;
154
    }
155
156 286d73ae Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
157
158
  public static int unpackObjectFromString(String obj)
159
    {
160
    int u = obj.indexOf('_');
161
    int l = obj.length();
162
163
    if( u>0 )
164
      {
165
      String name = obj.substring(0,u);
166
      int size = Integer.parseInt( obj.substring(u+1,l) );
167
168
      for(int i=0; i<NUM_OBJECTS; i++)
169
        {
170
        if( objects[i].name().equals(name) )
171
          {
172 53f23b64 Leszek Koltunski
          int sizeIndex = getSizeIndex(i,size);
173
          return pack(i,sizeIndex);
174 286d73ae Leszek Koltunski
          }
175
        }
176
      }
177
178
    return -1;
179
    }
180
181
///////////////////////////////////////////////////////////////////////////////////////////////////
182
183
  public static String getObjectList()
184
    {
185
    String name;
186
    StringBuilder list = new StringBuilder();
187
    int len;
188
    int[] sizes;
189
190
    for(int i=0; i<NUM_OBJECTS; i++)
191
      {
192
      sizes = objects[i].mObjectSizes;
193
      len   = sizes.length;
194
      name  = objects[i].name();
195
196
      for(int j=0; j<len; j++)
197
        {
198
        if( i>0 || j>0 ) list.append(',');
199
        list.append(name);
200
        list.append('_');
201
        list.append(sizes[j]);
202
        }
203
      }
204
205
    return list.toString();
206
    }
207
208 4888e97c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
209
210
  public static int getTotal()
211
    {
212
    return mNumAll;
213
    }
214
215 8e3898c8 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
216
217
  public static int getMaxLevel(int ordinal, int sizeIndex)
218
    {
219
    if( ordinal>=0 && ordinal<NUM_OBJECTS )
220
      {
221
      int num = objects[ordinal].mObjectSizes.length;
222
      return sizeIndex>=0 && sizeIndex<num ? objects[ordinal].mMaxLevels[sizeIndex] : 0;
223
      }
224
225
    return 0;
226
    }
227
228 c86f9f1f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
229
230
  public static int getOrdinal(String name)
231
    {
232
    for(int i=0; i<NUM_OBJECTS; i++)
233
      {
234
      if(objects[i].name().equals(name)) return i;
235
      }
236
237
    return -1;
238
    }
239
240
///////////////////////////////////////////////////////////////////////////////////////////////////
241
242 53f23b64 Leszek Koltunski
  public static int getSizeIndex(int ordinal, int size)
243 c86f9f1f Leszek Koltunski
    {
244
    int[] sizes = objects[ordinal].getSizes();
245
    int len = sizes.length;
246
247
    for(int i=0; i<len; i++)
248
      {
249
      if( sizes[i]==size ) return i;
250
      }
251
252
    return -1;
253
    }
254
255 4888e97c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
256
257 20931cf6 Leszek Koltunski
  public static int[] retFaceColors(RubikObjectList object)
258
    {
259
    Field field;
260
    int[] faceColors=null;
261
262
    try
263
      {
264
      field = object.mObjectClass.getDeclaredField("FACE_COLORS");
265
      field.setAccessible(true);
266
      Object obj = field.get(null);
267
      faceColors = (int[]) obj;
268
      }
269
    catch(NoSuchFieldException ex)
270
      {
271
      android.util.Log.e("RubikObjectList", object.mObjectClass.getSimpleName()+": no such field exception getting field: "+ex.getMessage());
272
      }
273
    catch(IllegalAccessException ex)
274
      {
275
      android.util.Log.e("RubikObjectList", object.mObjectClass.getSimpleName()+": illegal access exception getting field: "+ex.getMessage());
276
      }
277
278
    return faceColors;
279
    }
280
281
///////////////////////////////////////////////////////////////////////////////////////////////////
282
283
  RubikObjectList(int[][] info, Class<? extends RubikObject> object , RubikObjectMovement movement)
284 4888e97c Leszek Koltunski
    {
285 ea6ee91b Leszek Koltunski
    int length = info.length;
286
287 f6a9c538 Leszek Koltunski
    mObjectSizes  = new int[length];
288
    mMaxLevels    = new int[length];
289
    mResourceIDs  = new int[length];
290
    mSmallIconIDs = new int[length];
291
    mMediumIconIDs= new int[length];
292
    mBigIconIDs   = new int[length];
293 31a9f38d Leszek Koltunski
    mHugeIconIDs  = new int[length];
294 ea6ee91b Leszek Koltunski
295
    for(int i=0; i<length; i++)
296
      {
297 f6a9c538 Leszek Koltunski
      mObjectSizes[i]  = info[i][0];
298
      mMaxLevels[i]    = info[i][1];
299
      mResourceIDs[i]  = info[i][2];
300
      mSmallIconIDs[i] = info[i][3];
301
      mMediumIconIDs[i]= info[i][4];
302
      mBigIconIDs[i]   = info[i][5];
303 31a9f38d Leszek Koltunski
      mHugeIconIDs[i]  = info[i][6];
304 ea6ee91b Leszek Koltunski
      }
305
306 20931cf6 Leszek Koltunski
    mObjectClass         = object;
307 27a70eae Leszek Koltunski
    mObjectMovementClass = movement;
308
    }
309
310
///////////////////////////////////////////////////////////////////////////////////////////////////
311
312 8e3898c8 Leszek Koltunski
  public int[] getSizes()
313 27a70eae Leszek Koltunski
    {
314 8e3898c8 Leszek Koltunski
    return mObjectSizes;
315 27a70eae Leszek Koltunski
    }
316
317
///////////////////////////////////////////////////////////////////////////////////////////////////
318
319 8e3898c8 Leszek Koltunski
  public int[] getMaxLevels()
320 4888e97c Leszek Koltunski
    {
321 8e3898c8 Leszek Koltunski
    return mMaxLevels;
322
    }
323
324
///////////////////////////////////////////////////////////////////////////////////////////////////
325
326
  public int[] getIconIDs()
327
    {
328 f6a9c538 Leszek Koltunski
    int size = RubikActivity.getDrawableSize();
329
330
    switch(size)
331
      {
332
      case 0 : return mSmallIconIDs;
333
      case 1 : return mMediumIconIDs;
334 31a9f38d Leszek Koltunski
      case 2 : return mBigIconIDs;
335
      default: return mHugeIconIDs;
336 f6a9c538 Leszek Koltunski
      }
337 4888e97c Leszek Koltunski
    }
338
339
///////////////////////////////////////////////////////////////////////////////////////////////////
340
341 ccf9fec5 Leszek Koltunski
  public int[] getResourceIDs()
342
    {
343
    return mResourceIDs;
344
    }
345
346 fbca0033 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
347
348
  public int getNumVariants()
349
    {
350
    return mObjectSizes.length;
351
    }
352
353 ccf9fec5 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
354
355 5b893eee Leszek Koltunski
  public RubikObject create(int size, Static4D quat, int[][] moves, Resources res, int scrWidth)
356 27a70eae Leszek Koltunski
    {
357 5974d2ae Leszek Koltunski
    DistortedTexture texture = new DistortedTexture();
358 36273130 Leszek Koltunski
    DistortedEffects effects = new DistortedEffects();
359 b32444ee Leszek Koltunski
    MeshRectangles mesh      = new MeshRectangles(20,20);   // mesh of the node, not of the cubits
360 74686c71 Leszek Koltunski
361 4888e97c Leszek Koltunski
    switch(ordinal())
362
      {
363 5b893eee Leszek Koltunski
      case 0: return new RubikCube    (size, quat, texture, mesh, effects, moves, res, scrWidth);
364
      case 1: return new RubikPyraminx(size, quat, texture, mesh, effects, moves, res, scrWidth);
365 418aa554 Leszek Koltunski
      case 2: return new RubikDino    (size, quat, texture, mesh, effects, moves, res, scrWidth);
366 4888e97c Leszek Koltunski
      }
367
368
    return null;
369 27a70eae Leszek Koltunski
    }
370
371
///////////////////////////////////////////////////////////////////////////////////////////////////
372
373
  public RubikObjectMovement getObjectMovementClass()
374
    {
375 4888e97c Leszek Koltunski
    return mObjectMovementClass;
376 27a70eae Leszek Koltunski
    }
377
  }