Project

General

Profile

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

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

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 , R.drawable.cube2} ,
37
                       {3 , R.drawable.cube3} ,
38
                       {4 , R.drawable.cube4} ,
39
                       {5 , R.drawable.cube5}
40
                     },
41
         RubikCube.class,
42
         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
         RubikPyraminx.class,
52
         new RubikPyraminxMovement()
53
       ),
54
  ;
55

    
56
  public static final int NUM_OBJECTS = values().length;
57
  public static final int MAX_SIZE;
58

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

    
65
  static
66
    {
67
    mNumAll = 0;
68
    int size, i = 0;
69
    objects = new RubikObjectList[NUM_OBJECTS];
70
    int maxsize = Integer.MIN_VALUE;
71

    
72
    for(RubikObjectList object: RubikObjectList.values())
73
      {
74
      objects[i] = object;
75
      i++;
76
      size = object.mObjectSizes.length;
77
      mNumAll += size;
78
      if( size> maxsize ) maxsize = size;
79
      }
80

    
81
    MAX_SIZE = maxsize;
82
    }
83

    
84
///////////////////////////////////////////////////////////////////////////////////////////////////
85

    
86
  public static RubikObjectList getObject(int ordinal)
87
    {
88
    return objects[ordinal];
89
    }
90

    
91
///////////////////////////////////////////////////////////////////////////////////////////////////
92

    
93
  public static int pack(int object, int sizeIndex)
94
    {
95
    int ret = 0;
96
    for(int i=0; i<object; i++) ret += objects[i].mObjectSizes.length;
97

    
98
    return ret+sizeIndex;
99
    }
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
    {
121
    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
///////////////////////////////////////////////////////////////////////////////////////////////////
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
          int sizeIndex = getSizeIndex(i,size);
150
          return pack(i,sizeIndex);
151
          }
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
///////////////////////////////////////////////////////////////////////////////////////////////////
186

    
187
  public static int getTotal()
188
    {
189
    return mNumAll;
190
    }
191

    
192
///////////////////////////////////////////////////////////////////////////////////////////////////
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
  public static int getSizeIndex(int ordinal, int size)
207
    {
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
///////////////////////////////////////////////////////////////////////////////////////////////////
220

    
221
  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
    {
249
    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
    mObjectClass         = object;
261
    mObjectMovementClass = movement;
262
    }
263

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

    
266
  public int[] getIconIDs()
267
    {
268
    return mIconIDs;
269
    }
270

    
271
///////////////////////////////////////////////////////////////////////////////////////////////////
272

    
273
  public int[] getSizes()
274
    {
275
    return mObjectSizes;
276
    }
277

    
278
///////////////////////////////////////////////////////////////////////////////////////////////////
279

    
280
  public RubikObject create(int size, Static4D quatCur, Static4D quatAcc, int[][] moves)
281
    {
282
    DistortedTexture texture = new DistortedTexture();
283
    DistortedEffects effects = new DistortedEffects();
284
    MeshRectangles mesh      = new MeshRectangles(20,20);   // mesh of the node, not of the cubits
285

    
286
    switch(ordinal())
287
      {
288
      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
      }
291

    
292
    return null;
293
    }
294

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

    
297
  public RubikObjectMovement getObjectMovementClass()
298
    {
299
    return mObjectMovementClass;
300
    }
301
  }
(5-5/8)