Project

General

Profile

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

magiccube / src / main / java / org / distorted / objects / RubikObjectList.java @ 8e3898c8

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
import org.distorted.library.main.DistortedEffects;
23
import org.distorted.library.main.DistortedTexture;
24 97c012ae Leszek Koltunski
import org.distorted.library.mesh.MeshRectangles;
25 27a70eae Leszek Koltunski
import org.distorted.library.type.Static4D;
26 1f9772f3 Leszek Koltunski
import org.distorted.main.R;
27 27a70eae Leszek Koltunski
28 20931cf6 Leszek Koltunski
import java.lang.reflect.Field;
29
30 27a70eae Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
31
32
public enum RubikObjectList
33
  {
34 4c0cd600 Leszek Koltunski
  CUBE (
35
         new int[][] {
36 8e3898c8 Leszek Koltunski
                       {2 , 12, R.drawable.cube2} ,
37
                       {3 , 16, R.drawable.cube3} ,
38
                       {4 , 20, R.drawable.cube4} ,
39
                       {5 , 24, R.drawable.cube5}
40 4c0cd600 Leszek Koltunski
                     },
41 20931cf6 Leszek Koltunski
         RubikCube.class,
42 4c0cd600 Leszek Koltunski
         new RubikCubeMovement()
43
       ),
44
45
  PYRA (
46
         new int[][] {
47 8e3898c8 Leszek Koltunski
                       {3 , 10, R.drawable.pyra3} ,
48
                       {4 , 15, R.drawable.pyra4} ,
49
                       {5 , 20, R.drawable.pyra5}
50 4c0cd600 Leszek Koltunski
                     },
51 20931cf6 Leszek Koltunski
         RubikPyraminx.class,
52 4c0cd600 Leszek Koltunski
         new RubikPyraminxMovement()
53
       ),
54 27a70eae Leszek Koltunski
  ;
55
56 4888e97c Leszek Koltunski
  public static final int NUM_OBJECTS = values().length;
57
  public static final int MAX_SIZE;
58 8e3898c8 Leszek Koltunski
  public static final int MAX_LEVEL;
59 4888e97c Leszek Koltunski
60 8e3898c8 Leszek Koltunski
  private final int[] mObjectSizes, mMaxLevels, mIconIDs;
61 20931cf6 Leszek Koltunski
  private final Class<? extends RubikObject> mObjectClass;
62
  private final RubikObjectMovement mObjectMovementClass;
63 27a70eae Leszek Koltunski
  private static final RubikObjectList[] objects;
64 4888e97c Leszek Koltunski
  private static int mNumAll;
65 27a70eae Leszek Koltunski
66
  static
67
    {
68 4888e97c Leszek Koltunski
    mNumAll = 0;
69
    int size, i = 0;
70
    objects = new RubikObjectList[NUM_OBJECTS];
71 8e3898c8 Leszek Koltunski
    int maxSize  = Integer.MIN_VALUE;
72
    int maxLevel = Integer.MIN_VALUE;
73 27a70eae Leszek Koltunski
74 4888e97c Leszek Koltunski
    for(RubikObjectList object: RubikObjectList.values())
75 27a70eae Leszek Koltunski
      {
76 4888e97c Leszek Koltunski
      objects[i] = object;
77 27a70eae Leszek Koltunski
      i++;
78 4888e97c Leszek Koltunski
      size = object.mObjectSizes.length;
79
      mNumAll += size;
80 8e3898c8 Leszek Koltunski
      if( size> maxSize ) maxSize = size;
81
82
      for(int j=0; j<size; j++)
83
        {
84
        if( object.mMaxLevels[j] > maxLevel ) maxLevel = object.mMaxLevels[j];
85
        }
86 27a70eae Leszek Koltunski
      }
87 4888e97c Leszek Koltunski
88 8e3898c8 Leszek Koltunski
    MAX_SIZE = maxSize;
89
    MAX_LEVEL= maxLevel;
90 27a70eae Leszek Koltunski
    }
91
92
///////////////////////////////////////////////////////////////////////////////////////////////////
93
94
  public static RubikObjectList getObject(int ordinal)
95
    {
96
    return objects[ordinal];
97
    }
98
99
///////////////////////////////////////////////////////////////////////////////////////////////////
100
101 b498f3f6 Leszek Koltunski
  public static int pack(int object, int sizeIndex)
102 4888e97c Leszek Koltunski
    {
103
    int ret = 0;
104
    for(int i=0; i<object; i++) ret += objects[i].mObjectSizes.length;
105
106 b498f3f6 Leszek Koltunski
    return ret+sizeIndex;
107 4888e97c Leszek Koltunski
    }
108
109
///////////////////////////////////////////////////////////////////////////////////////////////////
110
111 8e3898c8 Leszek Koltunski
  public static int unpackSizeIndex(int number)
112 4888e97c Leszek Koltunski
    {
113
    int num;
114
115
    for(int i=0; i<NUM_OBJECTS; i++)
116
      {
117
      num = objects[i].mObjectSizes.length;
118
      if( number<num ) return number;
119
      number -= num;
120
      }
121
122
    return -1;
123
    }
124
125
///////////////////////////////////////////////////////////////////////////////////////////////////
126
127
  public static int unpackObject(int number)
128 27a70eae Leszek Koltunski
    {
129 4888e97c Leszek Koltunski
    int num;
130
131
    for(int i=0; i<NUM_OBJECTS; i++)
132
      {
133
      num = objects[i].mObjectSizes.length;
134
      if( number<num ) return i;
135
      number -= num;
136
      }
137
138
    return -1;
139
    }
140
141 286d73ae Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
142
143
  public static int unpackObjectFromString(String obj)
144
    {
145
    int u = obj.indexOf('_');
146
    int l = obj.length();
147
148
    if( u>0 )
149
      {
150
      String name = obj.substring(0,u);
151
      int size = Integer.parseInt( obj.substring(u+1,l) );
152
153
      for(int i=0; i<NUM_OBJECTS; i++)
154
        {
155
        if( objects[i].name().equals(name) )
156
          {
157 53f23b64 Leszek Koltunski
          int sizeIndex = getSizeIndex(i,size);
158
          return pack(i,sizeIndex);
159 286d73ae Leszek Koltunski
          }
160
        }
161
      }
162
163
    return -1;
164
    }
165
166
///////////////////////////////////////////////////////////////////////////////////////////////////
167
168
  public static String getObjectList()
169
    {
170
    String name;
171
    StringBuilder list = new StringBuilder();
172
    int len;
173
    int[] sizes;
174
175
    for(int i=0; i<NUM_OBJECTS; i++)
176
      {
177
      sizes = objects[i].mObjectSizes;
178
      len   = sizes.length;
179
      name  = objects[i].name();
180
181
      for(int j=0; j<len; j++)
182
        {
183
        if( i>0 || j>0 ) list.append(',');
184
        list.append(name);
185
        list.append('_');
186
        list.append(sizes[j]);
187
        }
188
      }
189
190
    return list.toString();
191
    }
192
193 4888e97c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
194
195
  public static int getTotal()
196
    {
197
    return mNumAll;
198
    }
199
200 8e3898c8 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
201
202
  public static int getMaxLevel(int ordinal, int sizeIndex)
203
    {
204
    if( ordinal>=0 && ordinal<NUM_OBJECTS )
205
      {
206
      int num = objects[ordinal].mObjectSizes.length;
207
      return sizeIndex>=0 && sizeIndex<num ? objects[ordinal].mMaxLevels[sizeIndex] : 0;
208
      }
209
210
    return 0;
211
    }
212
213 c86f9f1f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
214
215
  public static int getOrdinal(String name)
216
    {
217
    for(int i=0; i<NUM_OBJECTS; i++)
218
      {
219
      if(objects[i].name().equals(name)) return i;
220
      }
221
222
    return -1;
223
    }
224
225
///////////////////////////////////////////////////////////////////////////////////////////////////
226
227 53f23b64 Leszek Koltunski
  public static int getSizeIndex(int ordinal, int size)
228 c86f9f1f Leszek Koltunski
    {
229
    int[] sizes = objects[ordinal].getSizes();
230
    int len = sizes.length;
231
232
    for(int i=0; i<len; i++)
233
      {
234
      if( sizes[i]==size ) return i;
235
      }
236
237
    return -1;
238
    }
239
240 4888e97c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
241
242 20931cf6 Leszek Koltunski
  public static int[] retFaceColors(RubikObjectList object)
243
    {
244
    Field field;
245
    int[] faceColors=null;
246
247
    try
248
      {
249
      field = object.mObjectClass.getDeclaredField("FACE_COLORS");
250
      field.setAccessible(true);
251
      Object obj = field.get(null);
252
      faceColors = (int[]) obj;
253
      }
254
    catch(NoSuchFieldException ex)
255
      {
256
      android.util.Log.e("RubikObjectList", object.mObjectClass.getSimpleName()+": no such field exception getting field: "+ex.getMessage());
257
      }
258
    catch(IllegalAccessException ex)
259
      {
260
      android.util.Log.e("RubikObjectList", object.mObjectClass.getSimpleName()+": illegal access exception getting field: "+ex.getMessage());
261
      }
262
263
    return faceColors;
264
    }
265
266
///////////////////////////////////////////////////////////////////////////////////////////////////
267
268
  RubikObjectList(int[][] info, Class<? extends RubikObject> object , RubikObjectMovement movement)
269 4888e97c Leszek Koltunski
    {
270 ea6ee91b Leszek Koltunski
    int length = info.length;
271
272
    mObjectSizes= new int[length];
273 8e3898c8 Leszek Koltunski
    mMaxLevels  = new int[length];
274 ea6ee91b Leszek Koltunski
    mIconIDs    = new int[length];
275
276
    for(int i=0; i<length; i++)
277
      {
278
      mObjectSizes[i] = info[i][0];
279 8e3898c8 Leszek Koltunski
      mMaxLevels[i]   = info[i][1];
280
      mIconIDs[i]     = info[i][2];
281 ea6ee91b Leszek Koltunski
      }
282
283 20931cf6 Leszek Koltunski
    mObjectClass         = object;
284 27a70eae Leszek Koltunski
    mObjectMovementClass = movement;
285
    }
286
287
///////////////////////////////////////////////////////////////////////////////////////////////////
288
289 8e3898c8 Leszek Koltunski
  public int[] getSizes()
290 27a70eae Leszek Koltunski
    {
291 8e3898c8 Leszek Koltunski
    return mObjectSizes;
292 27a70eae Leszek Koltunski
    }
293
294
///////////////////////////////////////////////////////////////////////////////////////////////////
295
296 8e3898c8 Leszek Koltunski
  public int[] getMaxLevels()
297 4888e97c Leszek Koltunski
    {
298 8e3898c8 Leszek Koltunski
    return mMaxLevels;
299
    }
300
301
///////////////////////////////////////////////////////////////////////////////////////////////////
302
303
  public int[] getIconIDs()
304
    {
305
    return mIconIDs;
306 4888e97c Leszek Koltunski
    }
307
308
///////////////////////////////////////////////////////////////////////////////////////////////////
309
310 a31d25de Leszek Koltunski
  public RubikObject create(int size, Static4D quatCur, Static4D quatAcc, int[][] moves)
311 27a70eae Leszek Koltunski
    {
312 5974d2ae Leszek Koltunski
    DistortedTexture texture = new DistortedTexture();
313 36273130 Leszek Koltunski
    DistortedEffects effects = new DistortedEffects();
314 b32444ee Leszek Koltunski
    MeshRectangles mesh      = new MeshRectangles(20,20);   // mesh of the node, not of the cubits
315 74686c71 Leszek Koltunski
316 4888e97c Leszek Koltunski
    switch(ordinal())
317
      {
318 aa171dee Leszek Koltunski
      case 0: return new RubikCube    (size, quatCur, quatAcc, texture, mesh, effects, moves);
319
      case 1: return new RubikPyraminx(size, quatCur, quatAcc, texture, mesh, effects, moves);
320 4888e97c Leszek Koltunski
      }
321
322
    return null;
323 27a70eae Leszek Koltunski
    }
324
325
///////////////////////////////////////////////////////////////////////////////////////////////////
326
327
  public RubikObjectMovement getObjectMovementClass()
328
    {
329 4888e97c Leszek Koltunski
    return mObjectMovementClass;
330 27a70eae Leszek Koltunski
    }
331
  }