Project

General

Profile

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

magiccube / src / main / java / org / distorted / object / RubikObjectList.java @ b498f3f6

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