Project

General

Profile

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

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

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

    
30
import java.lang.reflect.Field;
31

    
32
///////////////////////////////////////////////////////////////////////////////////////////////////
33

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

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

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

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

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

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

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

    
93
    MAX_NUM_OBJECTS = maxNum;
94
    MAX_LEVEL       = maxLevel;
95
    MAX_OBJECT_SIZE = maxSize;
96
    }
97

    
98
///////////////////////////////////////////////////////////////////////////////////////////////////
99

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

    
105
///////////////////////////////////////////////////////////////////////////////////////////////////
106

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

    
112
    return ret+sizeIndex;
113
    }
114

    
115
///////////////////////////////////////////////////////////////////////////////////////////////////
116

    
117
  public static int unpackSizeIndex(int number)
118
    {
119
    int num;
120

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

    
128
    return -1;
129
    }
130

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

    
133
  public static int unpackObject(int number)
134
    {
135
    int num;
136

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

    
144
    return -1;
145
    }
146

    
147
///////////////////////////////////////////////////////////////////////////////////////////////////
148

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

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

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

    
169
    return -1;
170
    }
171

    
172
///////////////////////////////////////////////////////////////////////////////////////////////////
173

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

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

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

    
196
    return list.toString();
197
    }
198

    
199
///////////////////////////////////////////////////////////////////////////////////////////////////
200

    
201
  public static int getTotal()
202
    {
203
    return mNumAll;
204
    }
205

    
206
///////////////////////////////////////////////////////////////////////////////////////////////////
207

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

    
216
    return 0;
217
    }
218

    
219
///////////////////////////////////////////////////////////////////////////////////////////////////
220

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

    
228
    return -1;
229
    }
230

    
231
///////////////////////////////////////////////////////////////////////////////////////////////////
232

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

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

    
243
    return -1;
244
    }
245

    
246
///////////////////////////////////////////////////////////////////////////////////////////////////
247

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

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

    
269
    return faceColors;
270
    }
271

    
272
///////////////////////////////////////////////////////////////////////////////////////////////////
273

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

    
278
    mObjectSizes= new int[length];
279
    mMaxLevels  = new int[length];
280
    mIconIDs    = new int[length];
281
    mResourceIDs= new int[length];
282

    
283
    for(int i=0; i<length; i++)
284
      {
285
      mObjectSizes[i] = info[i][0];
286
      mMaxLevels[i]   = info[i][1];
287
      mIconIDs[i]     = info[i][2];
288
      mResourceIDs[i] = info[i][3];
289
      }
290

    
291
    mObjectClass         = object;
292
    mObjectMovementClass = movement;
293
    }
294

    
295
///////////////////////////////////////////////////////////////////////////////////////////////////
296

    
297
  public int[] getSizes()
298
    {
299
    return mObjectSizes;
300
    }
301

    
302
///////////////////////////////////////////////////////////////////////////////////////////////////
303

    
304
  public int[] getMaxLevels()
305
    {
306
    return mMaxLevels;
307
    }
308

    
309
///////////////////////////////////////////////////////////////////////////////////////////////////
310

    
311
  public int[] getIconIDs()
312
    {
313
    return mIconIDs;
314
    }
315

    
316
///////////////////////////////////////////////////////////////////////////////////////////////////
317

    
318
  public int[] getResourceIDs()
319
    {
320
    return mResourceIDs;
321
    }
322

    
323
///////////////////////////////////////////////////////////////////////////////////////////////////
324

    
325
  public RubikObject create(int size, Static4D quatCur, Static4D quatAcc, int[][] moves, Resources res)
326
    {
327
    DistortedTexture texture = new DistortedTexture();
328
    DistortedEffects effects = new DistortedEffects();
329
    MeshRectangles mesh      = new MeshRectangles(20,20);   // mesh of the node, not of the cubits
330

    
331
    switch(ordinal())
332
      {
333
      case 0: return new RubikCube    (size, quatCur, quatAcc, texture, mesh, effects, moves, res);
334
      case 1: return new RubikPyraminx(size, quatCur, quatAcc, texture, mesh, effects, moves, res);
335
      }
336

    
337
    return null;
338
    }
339

    
340
///////////////////////////////////////////////////////////////////////////////////////////////////
341

    
342
  public RubikObjectMovement getObjectMovementClass()
343
    {
344
    return mObjectMovementClass;
345
    }
346
  }
(5-5/8)