Project

General

Profile

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

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

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.MeshRectangles;
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 RubikObjectList
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
         RubikCube.class,
45
         new RubikCubeMovement()
46
       ),
47

    
48
  PYRA (
49
         new int[][] {
50
                       {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
                     },
54
         RubikPyraminx.class,
55
         new RubikPyraminxMovement()
56
       ),
57

    
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
  ;
66

    
67
  public static final int NUM_OBJECTS = values().length;
68
  public static final int MAX_NUM_OBJECTS;
69
  public static final int MAX_LEVEL;
70
  public static final int MAX_OBJECT_SIZE;
71

    
72
  private final int[] mObjectSizes, mMaxLevels, mSmallIconIDs, mMediumIconIDs, mBigIconIDs, mHugeIconIDs, mResourceIDs;
73
  private final Class<? extends RubikObject> mObjectClass;
74
  private final RubikObjectMovement mObjectMovementClass;
75
  private static final RubikObjectList[] objects;
76
  private static int mNumAll;
77

    
78
  static
79
    {
80
    mNumAll = 0;
81
    int num, i = 0;
82
    objects = new RubikObjectList[NUM_OBJECTS];
83
    int maxNum  = Integer.MIN_VALUE;
84
    int maxLevel= Integer.MIN_VALUE;
85
    int maxSize = Integer.MIN_VALUE;
86

    
87
    for(RubikObjectList object: RubikObjectList.values())
88
      {
89
      objects[i] = object;
90
      i++;
91
      num = object.mObjectSizes.length;
92
      mNumAll += num;
93
      if( num> maxNum ) maxNum = num;
94

    
95
      for(int j=0; j<num; j++)
96
        {
97
        if( object.mMaxLevels[j] > maxLevel ) maxLevel = object.mMaxLevels[j];
98
        if( object.mObjectSizes[j] > maxSize) maxSize  = object.mObjectSizes[j];
99
        }
100
      }
101

    
102
    MAX_NUM_OBJECTS = maxNum;
103
    MAX_LEVEL       = maxLevel;
104
    MAX_OBJECT_SIZE = maxSize;
105
    }
106

    
107
///////////////////////////////////////////////////////////////////////////////////////////////////
108

    
109
  public static RubikObjectList getObject(int ordinal)
110
    {
111
    return objects[ordinal];
112
    }
113

    
114
///////////////////////////////////////////////////////////////////////////////////////////////////
115

    
116
  public static int pack(int object, int sizeIndex)
117
    {
118
    int ret = 0;
119
    for(int i=0; i<object; i++) ret += objects[i].mObjectSizes.length;
120

    
121
    return ret+sizeIndex;
122
    }
123

    
124
///////////////////////////////////////////////////////////////////////////////////////////////////
125

    
126
  public static int unpackSizeIndex(int number)
127
    {
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
    {
144
    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
///////////////////////////////////////////////////////////////////////////////////////////////////
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
          int sizeIndex = getSizeIndex(i,size);
173
          return pack(i,sizeIndex);
174
          }
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
///////////////////////////////////////////////////////////////////////////////////////////////////
209

    
210
  public static int getTotal()
211
    {
212
    return mNumAll;
213
    }
214

    
215
///////////////////////////////////////////////////////////////////////////////////////////////////
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
///////////////////////////////////////////////////////////////////////////////////////////////////
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
  public static int getSizeIndex(int ordinal, int size)
243
    {
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
///////////////////////////////////////////////////////////////////////////////////////////////////
256

    
257
  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
    {
285
    int length = info.length;
286

    
287
    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
    mHugeIconIDs  = new int[length];
294

    
295
    for(int i=0; i<length; i++)
296
      {
297
      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
      mHugeIconIDs[i]  = info[i][6];
304
      }
305

    
306
    mObjectClass         = object;
307
    mObjectMovementClass = movement;
308
    }
309

    
310
///////////////////////////////////////////////////////////////////////////////////////////////////
311

    
312
  public int[] getSizes()
313
    {
314
    return mObjectSizes;
315
    }
316

    
317
///////////////////////////////////////////////////////////////////////////////////////////////////
318

    
319
  public int[] getMaxLevels()
320
    {
321
    return mMaxLevels;
322
    }
323

    
324
///////////////////////////////////////////////////////////////////////////////////////////////////
325

    
326
  public int[] getIconIDs()
327
    {
328
    int size = RubikActivity.getDrawableSize();
329

    
330
    switch(size)
331
      {
332
      case 0 : return mSmallIconIDs;
333
      case 1 : return mMediumIconIDs;
334
      case 2 : return mBigIconIDs;
335
      default: return mHugeIconIDs;
336
      }
337
    }
338

    
339
///////////////////////////////////////////////////////////////////////////////////////////////////
340

    
341
  public int[] getResourceIDs()
342
    {
343
    return mResourceIDs;
344
    }
345

    
346
///////////////////////////////////////////////////////////////////////////////////////////////////
347

    
348
  public int getNumVariants()
349
    {
350
    return mObjectSizes.length;
351
    }
352

    
353
///////////////////////////////////////////////////////////////////////////////////////////////////
354

    
355
  public RubikObject create(int size, Static4D quat, int[][] moves, Resources res, int scrWidth)
356
    {
357
    DistortedTexture texture = new DistortedTexture();
358
    DistortedEffects effects = new DistortedEffects();
359
    MeshRectangles mesh      = new MeshRectangles(20,20);   // mesh of the node, not of the cubits
360

    
361
    switch(ordinal())
362
      {
363
      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
      case 2: return new RubikDino    (size, quat, texture, mesh, effects, moves, res, scrWidth);
366
      }
367

    
368
    return null;
369
    }
370

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

    
373
  public RubikObjectMovement getObjectMovementClass()
374
    {
375
    return mObjectMovementClass;
376
    }
377
  }
(7-7/10)