Project

General

Profile

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

magiccube / src / main / java / org / distorted / objects / RubikObjectList.java @ 82d6e038

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 org.distorted.library.main.DistortedEffects;
23
import org.distorted.library.main.DistortedTexture;
24
import org.distorted.library.mesh.MeshRectangles;
25
import org.distorted.library.type.Static4D;
26
import org.distorted.main.R;
27

    
28
import java.lang.reflect.Field;
29

    
30
///////////////////////////////////////////////////////////////////////////////////////////////////
31

    
32
public enum RubikObjectList
33
  {
34
  CUBE (
35
         new int[][] {
36
                       {2 , 12, R.drawable.cube2} ,
37
                       {3 , 16, R.drawable.cube3} ,
38
                       {4 , 20, R.drawable.cube4} ,
39
                       {5 , 24, R.drawable.cube5}
40
                     },
41
         RubikCube.class,
42
         new RubikCubeMovement()
43
       ),
44

    
45
  PYRA (
46
         new int[][] {
47
                       {3 , 10, R.drawable.pyra3} ,
48
                       {4 , 15, R.drawable.pyra4} ,
49
                       {5 , 20, R.drawable.pyra5}
50
                     },
51
         RubikPyraminx.class,
52
         new RubikPyraminxMovement()
53
       ),
54
  ;
55

    
56
  public static final int NUM_OBJECTS = values().length;
57
  public static final int MAX_NUM_OBJECTS;
58
  public static final int MAX_LEVEL;
59
  public static final int MAX_OBJECT_SIZE;
60

    
61
  private final int[] mObjectSizes, mMaxLevels, mIconIDs;
62
  private final Class<? extends RubikObject> mObjectClass;
63
  private final RubikObjectMovement mObjectMovementClass;
64
  private static final RubikObjectList[] objects;
65
  private static int mNumAll;
66

    
67
  static
68
    {
69
    mNumAll = 0;
70
    int num, i = 0;
71
    objects = new RubikObjectList[NUM_OBJECTS];
72
    int maxNum  = Integer.MIN_VALUE;
73
    int maxLevel= Integer.MIN_VALUE;
74
    int maxSize = Integer.MIN_VALUE;
75

    
76
    for(RubikObjectList object: RubikObjectList.values())
77
      {
78
      objects[i] = object;
79
      i++;
80
      num = object.mObjectSizes.length;
81
      mNumAll += num;
82
      if( num> maxNum ) maxNum = num;
83

    
84
      for(int j=0; j<num; j++)
85
        {
86
        if( object.mMaxLevels[j] > maxLevel ) maxLevel = object.mMaxLevels[j];
87
        if( object.mObjectSizes[j] > maxSize) maxSize  = object.mObjectSizes[j];
88
        }
89
      }
90

    
91
    MAX_NUM_OBJECTS = maxNum;
92
    MAX_LEVEL       = maxLevel;
93
    MAX_OBJECT_SIZE = maxSize;
94
    }
95

    
96
///////////////////////////////////////////////////////////////////////////////////////////////////
97

    
98
  public static RubikObjectList getObject(int ordinal)
99
    {
100
    return objects[ordinal];
101
    }
102

    
103
///////////////////////////////////////////////////////////////////////////////////////////////////
104

    
105
  public static int pack(int object, int sizeIndex)
106
    {
107
    int ret = 0;
108
    for(int i=0; i<object; i++) ret += objects[i].mObjectSizes.length;
109

    
110
    return ret+sizeIndex;
111
    }
112

    
113
///////////////////////////////////////////////////////////////////////////////////////////////////
114

    
115
  public static int unpackSizeIndex(int number)
116
    {
117
    int num;
118

    
119
    for(int i=0; i<NUM_OBJECTS; i++)
120
      {
121
      num = objects[i].mObjectSizes.length;
122
      if( number<num ) return number;
123
      number -= num;
124
      }
125

    
126
    return -1;
127
    }
128

    
129
///////////////////////////////////////////////////////////////////////////////////////////////////
130

    
131
  public static int unpackObject(int number)
132
    {
133
    int num;
134

    
135
    for(int i=0; i<NUM_OBJECTS; i++)
136
      {
137
      num = objects[i].mObjectSizes.length;
138
      if( number<num ) return i;
139
      number -= num;
140
      }
141

    
142
    return -1;
143
    }
144

    
145
///////////////////////////////////////////////////////////////////////////////////////////////////
146

    
147
  public static int unpackObjectFromString(String obj)
148
    {
149
    int u = obj.indexOf('_');
150
    int l = obj.length();
151

    
152
    if( u>0 )
153
      {
154
      String name = obj.substring(0,u);
155
      int size = Integer.parseInt( obj.substring(u+1,l) );
156

    
157
      for(int i=0; i<NUM_OBJECTS; i++)
158
        {
159
        if( objects[i].name().equals(name) )
160
          {
161
          int sizeIndex = getSizeIndex(i,size);
162
          return pack(i,sizeIndex);
163
          }
164
        }
165
      }
166

    
167
    return -1;
168
    }
169

    
170
///////////////////////////////////////////////////////////////////////////////////////////////////
171

    
172
  public static String getObjectList()
173
    {
174
    String name;
175
    StringBuilder list = new StringBuilder();
176
    int len;
177
    int[] sizes;
178

    
179
    for(int i=0; i<NUM_OBJECTS; i++)
180
      {
181
      sizes = objects[i].mObjectSizes;
182
      len   = sizes.length;
183
      name  = objects[i].name();
184

    
185
      for(int j=0; j<len; j++)
186
        {
187
        if( i>0 || j>0 ) list.append(',');
188
        list.append(name);
189
        list.append('_');
190
        list.append(sizes[j]);
191
        }
192
      }
193

    
194
    return list.toString();
195
    }
196

    
197
///////////////////////////////////////////////////////////////////////////////////////////////////
198

    
199
  public static int getTotal()
200
    {
201
    return mNumAll;
202
    }
203

    
204
///////////////////////////////////////////////////////////////////////////////////////////////////
205

    
206
  public static int getMaxLevel(int ordinal, int sizeIndex)
207
    {
208
    if( ordinal>=0 && ordinal<NUM_OBJECTS )
209
      {
210
      int num = objects[ordinal].mObjectSizes.length;
211
      return sizeIndex>=0 && sizeIndex<num ? objects[ordinal].mMaxLevels[sizeIndex] : 0;
212
      }
213

    
214
    return 0;
215
    }
216

    
217
///////////////////////////////////////////////////////////////////////////////////////////////////
218

    
219
  public static int getOrdinal(String name)
220
    {
221
    for(int i=0; i<NUM_OBJECTS; i++)
222
      {
223
      if(objects[i].name().equals(name)) return i;
224
      }
225

    
226
    return -1;
227
    }
228

    
229
///////////////////////////////////////////////////////////////////////////////////////////////////
230

    
231
  public static int getSizeIndex(int ordinal, int size)
232
    {
233
    int[] sizes = objects[ordinal].getSizes();
234
    int len = sizes.length;
235

    
236
    for(int i=0; i<len; i++)
237
      {
238
      if( sizes[i]==size ) return i;
239
      }
240

    
241
    return -1;
242
    }
243

    
244
///////////////////////////////////////////////////////////////////////////////////////////////////
245

    
246
  public static int[] retFaceColors(RubikObjectList object)
247
    {
248
    Field field;
249
    int[] faceColors=null;
250

    
251
    try
252
      {
253
      field = object.mObjectClass.getDeclaredField("FACE_COLORS");
254
      field.setAccessible(true);
255
      Object obj = field.get(null);
256
      faceColors = (int[]) obj;
257
      }
258
    catch(NoSuchFieldException ex)
259
      {
260
      android.util.Log.e("RubikObjectList", object.mObjectClass.getSimpleName()+": no such field exception getting field: "+ex.getMessage());
261
      }
262
    catch(IllegalAccessException ex)
263
      {
264
      android.util.Log.e("RubikObjectList", object.mObjectClass.getSimpleName()+": illegal access exception getting field: "+ex.getMessage());
265
      }
266

    
267
    return faceColors;
268
    }
269

    
270
///////////////////////////////////////////////////////////////////////////////////////////////////
271

    
272
  RubikObjectList(int[][] info, Class<? extends RubikObject> object , RubikObjectMovement movement)
273
    {
274
    int length = info.length;
275

    
276
    mObjectSizes= new int[length];
277
    mMaxLevels  = new int[length];
278
    mIconIDs    = new int[length];
279

    
280
    for(int i=0; i<length; i++)
281
      {
282
      mObjectSizes[i] = info[i][0];
283
      mMaxLevels[i]   = info[i][1];
284
      mIconIDs[i]     = info[i][2];
285
      }
286

    
287
    mObjectClass         = object;
288
    mObjectMovementClass = movement;
289
    }
290

    
291
///////////////////////////////////////////////////////////////////////////////////////////////////
292

    
293
  public int[] getSizes()
294
    {
295
    return mObjectSizes;
296
    }
297

    
298
///////////////////////////////////////////////////////////////////////////////////////////////////
299

    
300
  public int[] getMaxLevels()
301
    {
302
    return mMaxLevels;
303
    }
304

    
305
///////////////////////////////////////////////////////////////////////////////////////////////////
306

    
307
  public int[] getIconIDs()
308
    {
309
    return mIconIDs;
310
    }
311

    
312
///////////////////////////////////////////////////////////////////////////////////////////////////
313

    
314
  public RubikObject create(int size, Static4D quatCur, Static4D quatAcc, int[][] moves)
315
    {
316
    DistortedTexture texture = new DistortedTexture();
317
    DistortedEffects effects = new DistortedEffects();
318
    MeshRectangles mesh      = new MeshRectangles(20,20);   // mesh of the node, not of the cubits
319

    
320
    switch(ordinal())
321
      {
322
      case 0: return new RubikCube    (size, quatCur, quatAcc, texture, mesh, effects, moves);
323
      case 1: return new RubikPyraminx(size, quatCur, quatAcc, texture, mesh, effects, moves);
324
      }
325

    
326
    return null;
327
    }
328

    
329
///////////////////////////////////////////////////////////////////////////////////////////////////
330

    
331
  public RubikObjectMovement getObjectMovementClass()
332
    {
333
    return mObjectMovementClass;
334
    }
335
  }
(5-5/8)