Project

General

Profile

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

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

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 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.dino, 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 RubikMovement mObjectMovementClass;
75
  private static final RubikObjectList[] objects;
76
  private static int mNumAll;
77
  private static int[] mIndices;
78

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

    
88
    mIndices = new int[] {0,1,2};
89

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

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

    
105
    MAX_NUM_OBJECTS = maxNum;
106
    MAX_LEVEL       = maxLevel;
107
    MAX_OBJECT_SIZE = maxSize;
108
    }
109

    
110
///////////////////////////////////////////////////////////////////////////////////////////////////
111
// TODO
112
  public static int getColumnCount()
113
    {
114
    return 3;
115
    }
116

    
117
///////////////////////////////////////////////////////////////////////////////////////////////////
118
// TODO
119
  public static int getRowCount()
120
    {
121
    return 4;
122
    }
123

    
124
///////////////////////////////////////////////////////////////////////////////////////////////////
125
// TODO
126
  public static int[] getIndices()
127
    {
128
    return mIndices;
129
    }
130

    
131
///////////////////////////////////////////////////////////////////////////////////////////////////
132

    
133
  public static RubikObjectList getObject(int ordinal)
134
    {
135
    return ordinal>=0 && ordinal<NUM_OBJECTS ? objects[ordinal] : CUBE;
136
    }
137

    
138
///////////////////////////////////////////////////////////////////////////////////////////////////
139

    
140
  public static int pack(int object, int sizeIndex)
141
    {
142
    int ret = 0;
143
    for(int i=0; i<object; i++) ret += objects[i].mObjectSizes.length;
144

    
145
    return ret+sizeIndex;
146
    }
147

    
148
///////////////////////////////////////////////////////////////////////////////////////////////////
149

    
150
  public static int unpackSizeIndex(int number)
151
    {
152
    int num;
153

    
154
    for(int i=0; i<NUM_OBJECTS; i++)
155
      {
156
      num = objects[i].mObjectSizes.length;
157
      if( number<num ) return number;
158
      number -= num;
159
      }
160

    
161
    return -1;
162
    }
163

    
164
///////////////////////////////////////////////////////////////////////////////////////////////////
165

    
166
  public static int unpackObject(int number)
167
    {
168
    int num;
169

    
170
    for(int i=0; i<NUM_OBJECTS; i++)
171
      {
172
      num = objects[i].mObjectSizes.length;
173
      if( number<num ) return i;
174
      number -= num;
175
      }
176

    
177
    return -1;
178
    }
179

    
180
///////////////////////////////////////////////////////////////////////////////////////////////////
181

    
182
  public static int unpackObjectFromString(String obj)
183
    {
184
    int u = obj.indexOf('_');
185
    int l = obj.length();
186

    
187
    if( u>0 )
188
      {
189
      String name = obj.substring(0,u);
190
      int size = Integer.parseInt( obj.substring(u+1,l) );
191

    
192
      for(int i=0; i<NUM_OBJECTS; i++)
193
        {
194
        if( objects[i].name().equals(name) )
195
          {
196
          int sizeIndex = getSizeIndex(i,size);
197
          return pack(i,sizeIndex);
198
          }
199
        }
200
      }
201

    
202
    return -1;
203
    }
204

    
205
///////////////////////////////////////////////////////////////////////////////////////////////////
206

    
207
  public static String getObjectList()
208
    {
209
    String name;
210
    StringBuilder list = new StringBuilder();
211
    int len;
212
    int[] sizes;
213

    
214
    for(int i=0; i<NUM_OBJECTS; i++)
215
      {
216
      sizes = objects[i].mObjectSizes;
217
      len   = sizes.length;
218
      name  = objects[i].name();
219

    
220
      for(int j=0; j<len; j++)
221
        {
222
        if( i>0 || j>0 ) list.append(',');
223
        list.append(name);
224
        list.append('_');
225
        list.append(sizes[j]);
226
        }
227
      }
228

    
229
    return list.toString();
230
    }
231

    
232
///////////////////////////////////////////////////////////////////////////////////////////////////
233

    
234
  public static int getTotal()
235
    {
236
    return mNumAll;
237
    }
238

    
239
///////////////////////////////////////////////////////////////////////////////////////////////////
240

    
241
  public static int getMaxLevel(int ordinal, int sizeIndex)
242
    {
243
    if( ordinal>=0 && ordinal<NUM_OBJECTS )
244
      {
245
      int num = objects[ordinal].mObjectSizes.length;
246
      return sizeIndex>=0 && sizeIndex<num ? objects[ordinal].mMaxLevels[sizeIndex] : 0;
247
      }
248

    
249
    return 0;
250
    }
251

    
252
///////////////////////////////////////////////////////////////////////////////////////////////////
253

    
254
  public static int getOrdinal(String name)
255
    {
256
    for(int i=0; i<NUM_OBJECTS; i++)
257
      {
258
      if(objects[i].name().equals(name)) return i;
259
      }
260

    
261
    return -1;
262
    }
263

    
264
///////////////////////////////////////////////////////////////////////////////////////////////////
265

    
266
  public static int getSizeIndex(int ordinal, int size)
267
    {
268
    if( ordinal>=0 && ordinal<NUM_OBJECTS )
269
      {
270
      int[] sizes = objects[ordinal].getSizes();
271
      int len = sizes.length;
272

    
273
      for(int i=0; i<len; i++)
274
        {
275
        if( sizes[i]==size ) return i;
276
        }
277
      }
278

    
279
    return -1;
280
    }
281

    
282
///////////////////////////////////////////////////////////////////////////////////////////////////
283

    
284
  public static int[] retFaceColors(RubikObjectList object)
285
    {
286
    Field field;
287
    int[] faceColors=null;
288

    
289
    try
290
      {
291
      field = object.mObjectClass.getDeclaredField("FACE_COLORS");
292
      field.setAccessible(true);
293
      Object obj = field.get(null);
294
      faceColors = (int[]) obj;
295
      }
296
    catch(NoSuchFieldException ex)
297
      {
298
      android.util.Log.e("RubikObjectList", object.mObjectClass.getSimpleName()+": no such field exception getting field: "+ex.getMessage());
299
      }
300
    catch(IllegalAccessException ex)
301
      {
302
      android.util.Log.e("RubikObjectList", object.mObjectClass.getSimpleName()+": illegal access exception getting field: "+ex.getMessage());
303
      }
304

    
305
    return faceColors;
306
    }
307

    
308
///////////////////////////////////////////////////////////////////////////////////////////////////
309

    
310
  RubikObjectList(int[][] info, Class<? extends RubikObject> object , RubikMovement movement)
311
    {
312
    int length = info.length;
313

    
314
    mObjectSizes  = new int[length];
315
    mMaxLevels    = new int[length];
316
    mResourceIDs  = new int[length];
317
    mSmallIconIDs = new int[length];
318
    mMediumIconIDs= new int[length];
319
    mBigIconIDs   = new int[length];
320
    mHugeIconIDs  = new int[length];
321

    
322
    for(int i=0; i<length; i++)
323
      {
324
      mObjectSizes[i]  = info[i][0];
325
      mMaxLevels[i]    = info[i][1];
326
      mResourceIDs[i]  = info[i][2];
327
      mSmallIconIDs[i] = info[i][3];
328
      mMediumIconIDs[i]= info[i][4];
329
      mBigIconIDs[i]   = info[i][5];
330
      mHugeIconIDs[i]  = info[i][6];
331
      }
332

    
333
    mObjectClass         = object;
334
    mObjectMovementClass = movement;
335
    }
336

    
337
///////////////////////////////////////////////////////////////////////////////////////////////////
338

    
339
  public int[] getSizes()
340
    {
341
    return mObjectSizes;
342
    }
343

    
344
///////////////////////////////////////////////////////////////////////////////////////////////////
345

    
346
  public int[] getMaxLevels()
347
    {
348
    return mMaxLevels;
349
    }
350

    
351
///////////////////////////////////////////////////////////////////////////////////////////////////
352

    
353
  public int[] getIconIDs()
354
    {
355
    int size = RubikActivity.getDrawableSize();
356

    
357
    switch(size)
358
      {
359
      case 0 : return mSmallIconIDs;
360
      case 1 : return mMediumIconIDs;
361
      case 2 : return mBigIconIDs;
362
      default: return mHugeIconIDs;
363
      }
364
    }
365

    
366
///////////////////////////////////////////////////////////////////////////////////////////////////
367

    
368
  public int[] getResourceIDs()
369
    {
370
    return mResourceIDs;
371
    }
372

    
373
///////////////////////////////////////////////////////////////////////////////////////////////////
374

    
375
  public int getNumVariants()
376
    {
377
    return mObjectSizes.length;
378
    }
379

    
380
///////////////////////////////////////////////////////////////////////////////////////////////////
381

    
382
  public RubikObject create(int size, Static4D quat, int[][] moves, Resources res, int scrWidth)
383
    {
384
    DistortedTexture texture = new DistortedTexture();
385
    DistortedEffects effects = new DistortedEffects();
386
    MeshSquare mesh      = new MeshSquare(20,20);   // mesh of the node, not of the cubits
387

    
388
    switch(ordinal())
389
      {
390
      case 0: return new RubikCube    (size, quat, texture, mesh, effects, moves, res, scrWidth);
391
      case 1: return new RubikPyraminx(size, quat, texture, mesh, effects, moves, res, scrWidth);
392
      case 2: return new RubikDino    (size, quat, texture, mesh, effects, moves, res, scrWidth);
393
      }
394

    
395
    return null;
396
    }
397

    
398
///////////////////////////////////////////////////////////////////////////////////////////////////
399

    
400
  public RubikMovement getObjectMovementClass()
401
    {
402
    return mObjectMovementClass;
403
    }
404
  }
(9-9/10)