Project

General

Profile

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

magiccube / src / main / java / org / distorted / objects / RubikObjectList.java @ ad38d800

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 RubikMovementCube()
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 RubikMovementPyraminx()
56
       ),
57

    
58
  DINO (
59
         new int[][] {
60
                       {3 , 10, R.raw.pyra3, R.drawable.ui_small_dino, R.drawable.ui_medium_dino, R.drawable.ui_big_dino, R.drawable.ui_huge_dino} ,
61
                     },
62
         RubikDino.class,
63
         new RubikMovementDino()
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 RubikMovementObject 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 ordinal>=0 && ordinal<NUM_OBJECTS ? objects[ordinal] : CUBE;
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
    if( ordinal>=0 && ordinal<NUM_OBJECTS )
245
      {
246
      int[] sizes = objects[ordinal].getSizes();
247
      int len = sizes.length;
248

    
249
      for(int i=0; i<len; i++)
250
        {
251
        if( sizes[i]==size ) return i;
252
        }
253
      }
254

    
255
    return -1;
256
    }
257

    
258
///////////////////////////////////////////////////////////////////////////////////////////////////
259

    
260
  public static int[] retFaceColors(RubikObjectList object)
261
    {
262
    Field field;
263
    int[] faceColors=null;
264

    
265
    try
266
      {
267
      field = object.mObjectClass.getDeclaredField("FACE_COLORS");
268
      field.setAccessible(true);
269
      Object obj = field.get(null);
270
      faceColors = (int[]) obj;
271
      }
272
    catch(NoSuchFieldException ex)
273
      {
274
      android.util.Log.e("RubikObjectList", object.mObjectClass.getSimpleName()+": no such field exception getting field: "+ex.getMessage());
275
      }
276
    catch(IllegalAccessException ex)
277
      {
278
      android.util.Log.e("RubikObjectList", object.mObjectClass.getSimpleName()+": illegal access exception getting field: "+ex.getMessage());
279
      }
280

    
281
    return faceColors;
282
    }
283

    
284
///////////////////////////////////////////////////////////////////////////////////////////////////
285

    
286
  RubikObjectList(int[][] info, Class<? extends RubikObject> object , RubikMovementObject movement)
287
    {
288
    int length = info.length;
289

    
290
    mObjectSizes  = new int[length];
291
    mMaxLevels    = new int[length];
292
    mResourceIDs  = new int[length];
293
    mSmallIconIDs = new int[length];
294
    mMediumIconIDs= new int[length];
295
    mBigIconIDs   = new int[length];
296
    mHugeIconIDs  = new int[length];
297

    
298
    for(int i=0; i<length; i++)
299
      {
300
      mObjectSizes[i]  = info[i][0];
301
      mMaxLevels[i]    = info[i][1];
302
      mResourceIDs[i]  = info[i][2];
303
      mSmallIconIDs[i] = info[i][3];
304
      mMediumIconIDs[i]= info[i][4];
305
      mBigIconIDs[i]   = info[i][5];
306
      mHugeIconIDs[i]  = info[i][6];
307
      }
308

    
309
    mObjectClass         = object;
310
    mObjectMovementClass = movement;
311
    }
312

    
313
///////////////////////////////////////////////////////////////////////////////////////////////////
314

    
315
  public int[] getSizes()
316
    {
317
    return mObjectSizes;
318
    }
319

    
320
///////////////////////////////////////////////////////////////////////////////////////////////////
321

    
322
  public int[] getMaxLevels()
323
    {
324
    return mMaxLevels;
325
    }
326

    
327
///////////////////////////////////////////////////////////////////////////////////////////////////
328

    
329
  public int[] getIconIDs()
330
    {
331
    int size = RubikActivity.getDrawableSize();
332

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

    
342
///////////////////////////////////////////////////////////////////////////////////////////////////
343

    
344
  public int[] getResourceIDs()
345
    {
346
    return mResourceIDs;
347
    }
348

    
349
///////////////////////////////////////////////////////////////////////////////////////////////////
350

    
351
  public int getNumVariants()
352
    {
353
    return mObjectSizes.length;
354
    }
355

    
356
///////////////////////////////////////////////////////////////////////////////////////////////////
357

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

    
364
    switch(ordinal())
365
      {
366
      case 0: return new RubikCube    (size, quat, texture, mesh, effects, moves, res, scrWidth);
367
      case 1: return new RubikPyraminx(size, quat, texture, mesh, effects, moves, res, scrWidth);
368
      case 2: return new RubikDino    (size, quat, texture, mesh, effects, moves, res, scrWidth);
369
      }
370

    
371
    return null;
372
    }
373

    
374
///////////////////////////////////////////////////////////////////////////////////////////////////
375

    
376
  public RubikMovementObject getObjectMovementClass()
377
    {
378
    return mObjectMovementClass;
379
    }
380
  }
(9-9/10)