Project

General

Profile

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

magiccube / src / main / java / org / distorted / objects / RubikObjectList.java @ 20931cf6

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