Project

General

Profile

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

magiccube / src / main / java / org / distorted / objects / TwistyCube.java @ e9a87113

1 0c52af30 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2019 Leszek Koltunski                                                               //
3
//                                                                                               //
4 fdec60a3 Leszek Koltunski
// This file is part of Magic Cube.                                                              //
5 0c52af30 Leszek Koltunski
//                                                                                               //
6 fdec60a3 Leszek Koltunski
// Magic Cube is free software: you can redistribute it and/or modify                            //
7 0c52af30 Leszek Koltunski
// 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 fdec60a3 Leszek Koltunski
// Magic Cube is distributed in the hope that it will be useful,                                 //
12 0c52af30 Leszek Koltunski
// 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 fdec60a3 Leszek Koltunski
// along with Magic Cube.  If not, see <http://www.gnu.org/licenses/>.                           //
18 0c52af30 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
19
20 1f9772f3 Leszek Koltunski
package org.distorted.objects;
21 0c52af30 Leszek Koltunski
22 ccf9fec5 Leszek Koltunski
import android.content.res.Resources;
23 0c52af30 Leszek Koltunski
24 f10a88a8 Leszek Koltunski
import org.distorted.helpers.ObjectShape;
25 9c06394a Leszek Koltunski
import org.distorted.helpers.ObjectSticker;
26 6cf89a3e Leszek Koltunski
import org.distorted.helpers.ScrambleState;
27 0c52af30 Leszek Koltunski
import org.distorted.library.main.DistortedEffects;
28
import org.distorted.library.main.DistortedTexture;
29 efa8aa48 Leszek Koltunski
import org.distorted.library.mesh.MeshSquare;
30 0c52af30 Leszek Koltunski
import org.distorted.library.type.Static3D;
31
import org.distorted.library.type.Static4D;
32 6fd4a72c Leszek Koltunski
import org.distorted.main.R;
33 0c52af30 Leszek Koltunski
34
///////////////////////////////////////////////////////////////////////////////////////////////////
35
36 9c2f0c91 Leszek Koltunski
class TwistyCube extends TwistyObject
37 0c52af30 Leszek Koltunski
{
38 ad38d800 Leszek Koltunski
  static final Static3D[] ROT_AXIS = new Static3D[]
39 efef689c Leszek Koltunski
         {
40
           new Static3D(1,0,0),
41
           new Static3D(0,1,0),
42
           new Static3D(0,0,1)
43
         };
44
45 37a25788 Leszek Koltunski
  private static final int[] FACE_COLORS = new int[]
46 efef689c Leszek Koltunski
         {
47 ece1b58d Leszek Koltunski
           COLOR_YELLOW, COLOR_WHITE,
48
           COLOR_BLUE  , COLOR_GREEN,
49 323b217c Leszek Koltunski
           COLOR_RED   , COLOR_ORANGE
50 efef689c Leszek Koltunski
         };
51
52 91792184 Leszek Koltunski
  private ScrambleState[] mStates;
53 7ff38997 Leszek Koltunski
  private Static4D[] mQuats;
54
  private int[] mBasicAngle;
55
  private ObjectSticker[] mStickers;
56 e9a87113 Leszek Koltunski
  private Movement mMovement;
57 caccea6e Leszek Koltunski
58 411c6285 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
59
60 d90c55cc Leszek Koltunski
  TwistyCube(int size, Static4D quat, DistortedTexture texture, MeshSquare mesh,
61
             DistortedEffects effects, int[][] moves, Resources res, int scrWidth)
62 411c6285 Leszek Koltunski
    {
63 db875721 Leszek Koltunski
    super(size, size, quat, texture, mesh, effects, moves, ObjectList.CUBE, res, scrWidth);
64 91792184 Leszek Koltunski
    }
65 caccea6e Leszek Koltunski
66 91792184 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
67 caccea6e Leszek Koltunski
68 91792184 Leszek Koltunski
  ScrambleState[] getScrambleStates()
69
    {
70
    if( mStates==null )
71 caccea6e Leszek Koltunski
      {
72 91792184 Leszek Koltunski
      int size = getNumLayers();
73
      int[][] m = new int[16][];
74
      for(int i=1; i<16; i++) m[i] = createEdges(size,i);
75
76
      mStates = new ScrambleState[]
77
        {
78
        new ScrambleState( new int[][] { m[ 1], m[ 2], m[ 3] } ),  // 0
79
        new ScrambleState( new int[][] {  null, m[ 4], m[ 5] } ),  // x
80
        new ScrambleState( new int[][] { m[ 6],  null, m[ 7] } ),  // y
81
        new ScrambleState( new int[][] { m[ 8], m[ 8],  null } ),  // z
82
        new ScrambleState( new int[][] { m[10],  null, m[ 7] } ),  // xy
83
        new ScrambleState( new int[][] { m[11], m[ 9],  null } ),  // xz
84
        new ScrambleState( new int[][] {  null, m[12], m[ 5] } ),  // yx
85
        new ScrambleState( new int[][] { m[ 8], m[13],  null } ),  // yz
86
        new ScrambleState( new int[][] {  null, m[ 4], m[14] } ),  // zx
87
        new ScrambleState( new int[][] { m[ 6],  null, m[15] } ),  // zy
88
        new ScrambleState( new int[][] {  null,  null, m[ 5] } ),  // xyx
89
        new ScrambleState( new int[][] {  null, m[ 4],  null } ),  // xzx
90
        new ScrambleState( new int[][] {  null,  null, m[ 7] } ),  // yxy
91
        new ScrambleState( new int[][] { m[ 6],  null,  null } ),  // yzy
92
        new ScrambleState( new int[][] {  null, m[ 9],  null } ),  // zxz
93
        new ScrambleState( new int[][] { m[ 8],  null,  null } ),  // zyz
94
        };
95
      }
96
97
    return mStates;
98 caccea6e Leszek Koltunski
    }
99
100
///////////////////////////////////////////////////////////////////////////////////////////////////
101
102
  private int[] createEdges(int size, int vertex)
103
    {
104
    int[] ret = new int[9*size];
105
106
    for(int l=0; l<size; l++)
107
      {
108
      ret[9*l  ] = l;
109
      ret[9*l+1] =-1;
110
      ret[9*l+2] = vertex;
111
      ret[9*l+3] = l;
112
      ret[9*l+4] = 1;
113
      ret[9*l+5] = vertex;
114
      ret[9*l+6] = l;
115
      ret[9*l+7] = 2;
116
      ret[9*l+8] = vertex;
117
      }
118
119
    return ret;
120 411c6285 Leszek Koltunski
    }
121
122 7ff38997 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
123
124
  private void initializeQuats()
125
    {
126
    mQuats = new Static4D[]
127
         {
128
         new Static4D(  0.0f,   0.0f,   0.0f,   1.0f),
129
         new Static4D(  1.0f,   0.0f,   0.0f,   0.0f),
130
         new Static4D(  0.0f,   1.0f,   0.0f,   0.0f),
131
         new Static4D(  0.0f,   0.0f,   1.0f,   0.0f),
132
133
         new Static4D( SQ2/2,  SQ2/2,  0.0f ,   0.0f),
134
         new Static4D( SQ2/2, -SQ2/2,  0.0f ,   0.0f),
135
         new Static4D( SQ2/2,   0.0f,  SQ2/2,   0.0f),
136
         new Static4D(-SQ2/2,   0.0f,  SQ2/2,   0.0f),
137
         new Static4D( SQ2/2,   0.0f,   0.0f,  SQ2/2),
138
         new Static4D( SQ2/2,   0.0f,   0.0f, -SQ2/2),
139
         new Static4D(  0.0f,  SQ2/2,  SQ2/2,   0.0f),
140
         new Static4D(  0.0f,  SQ2/2, -SQ2/2,   0.0f),
141
         new Static4D(  0.0f,  SQ2/2,   0.0f,  SQ2/2),
142
         new Static4D(  0.0f,  SQ2/2,   0.0f, -SQ2/2),
143
         new Static4D(  0.0f,   0.0f,  SQ2/2,  SQ2/2),
144
         new Static4D(  0.0f,   0.0f,  SQ2/2, -SQ2/2),
145
146
         new Static4D(  0.5f,   0.5f,   0.5f,   0.5f),
147
         new Static4D(  0.5f,   0.5f,  -0.5f,   0.5f),
148
         new Static4D(  0.5f,   0.5f,  -0.5f,  -0.5f),
149
         new Static4D(  0.5f,  -0.5f,   0.5f,  -0.5f),
150
         new Static4D( -0.5f,  -0.5f,  -0.5f,   0.5f),
151
         new Static4D( -0.5f,   0.5f,  -0.5f,  -0.5f),
152
         new Static4D( -0.5f,   0.5f,   0.5f,  -0.5f),
153
         new Static4D( -0.5f,   0.5f,   0.5f,   0.5f)
154
         };
155
    }
156
157 a480ee80 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
158
159
  int[] getSolvedQuats(int cubit, int numLayers)
160
    {
161 7ff38997 Leszek Koltunski
    if( mQuats ==null ) initializeQuats();
162 a480ee80 Leszek Koltunski
    int status = retCubitSolvedStatus(cubit,numLayers);
163 7ff38997 Leszek Koltunski
    return status<0 ? null : buildSolvedQuats(MovementCube.FACE_AXIS[status], mQuats);
164 a480ee80 Leszek Koltunski
    }
165
166 f10a88a8 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
167
168
  ObjectShape getObjectShape(int cubit, int numLayers)
169
    {
170
    int extraI, extraV, num;
171
    float height;
172
173
    switch(numLayers)
174
      {
175
      case 2 : num = 6; extraI = 2; extraV = 2; height = 0.045f; break;
176
      case 3 : num = 5; extraI = 2; extraV = 2; height = 0.045f; break;
177
      case 4 : num = 5; extraI = 1; extraV = 1; height = 0.045f; break;
178
      default: num = 5; extraI = 0; extraV = 0; height = 0.045f; break;
179
      }
180
181 7ff38997 Leszek Koltunski
    double[][] vertices = new double[][]
182
          {
183
              { 0.5, 0.5, 0.5 },
184
              { 0.5, 0.5,-0.5 },
185
              { 0.5,-0.5, 0.5 },
186
              { 0.5,-0.5,-0.5 },
187
              {-0.5, 0.5, 0.5 },
188
              {-0.5, 0.5,-0.5 },
189
              {-0.5,-0.5, 0.5 },
190
              {-0.5,-0.5,-0.5 },
191
          };
192
193
    int[][] vert_indices = new int[][]
194
          {
195 d90c55cc Leszek Koltunski
              {2,3,1,0},
196 7ff38997 Leszek Koltunski
              {7,6,4,5},
197
              {4,0,1,5},
198
              {7,3,2,6},
199
              {6,2,0,4},
200
              {3,7,5,1}
201
          };
202
203 f10a88a8 Leszek Koltunski
    float[][] bands     = new float[][] { {height,35,0.5f,0.7f,num,extraI,extraV} };
204
    int[] bandIndices   = new int[] { 0,0,0,0,0,0};
205
    float[][] corners   = new float[][] { {0.036f,0.12f} };
206
    int[] cornerIndices = new int[] { 0,0,0,0,0,0,0,0 };
207
    float[][] centers   = new float[][] { {0.0f, 0.0f, 0.0f} };
208
    int[] centerIndices = new int[] { 0,0,0,0,0,0,0,0 };
209
210 7ff38997 Leszek Koltunski
    return new ObjectShape(vertices,vert_indices,bands,bandIndices,corners,cornerIndices,centers,centerIndices,getNumCubitFaces(), null);
211 f10a88a8 Leszek Koltunski
    }
212
213 ac940e24 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
214
215 3e605536 Leszek Koltunski
  Static4D getQuat(int cubit, int numLayers)
216 ac940e24 Leszek Koltunski
    {
217 7ff38997 Leszek Koltunski
    if( mQuats ==null ) initializeQuats();
218
    return mQuats[0];
219 3e605536 Leszek Koltunski
    }
220 f10a88a8 Leszek Koltunski
221 3e605536 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
222 ac940e24 Leszek Koltunski
223 3e605536 Leszek Koltunski
  int getNumCubitVariants(int numLayers)
224
    {
225
    return 1;
226
    }
227 ac940e24 Leszek Koltunski
228 3e605536 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
229 f10a88a8 Leszek Koltunski
230 3e605536 Leszek Koltunski
  int getCubitVariant(int cubit, int numLayers)
231
    {
232
    return 0;
233 ac940e24 Leszek Koltunski
    }
234
235 7289fd6c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
236
237 9c06394a Leszek Koltunski
  int getColor(int face)
238 7289fd6c Leszek Koltunski
    {
239 9c06394a Leszek Koltunski
    return FACE_COLORS[face];
240
    }
241
242
///////////////////////////////////////////////////////////////////////////////////////////////////
243 7289fd6c Leszek Koltunski
244 9c06394a Leszek Koltunski
  ObjectSticker retSticker(int face)
245
    {
246 7ff38997 Leszek Koltunski
    if( mStickers==null )
247
      {
248
      final float[][] STICKERS = new float[][]  { { -0.5f, -0.5f, 0.5f, -0.5f, 0.5f, 0.5f, -0.5f, 0.5f } };
249
      final float radius = 0.10f;
250
      final float stroke = 0.08f;
251
      final float[] radii = {radius,radius,radius,radius};
252
      mStickers = new ObjectSticker[STICKERS.length];
253
      mStickers[0] = new ObjectSticker(STICKERS[0],null,radii,stroke );
254
      }
255
256 abf36986 Leszek Koltunski
    return mStickers[face/NUM_FACE_COLORS];
257 7289fd6c Leszek Koltunski
    }
258
259 411c6285 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
260
261 b1f2ccf5 Leszek Koltunski
  float[][] getCubitPositions(int numLayers)
262 a10ada2a Leszek Koltunski
    {
263 b1f2ccf5 Leszek Koltunski
    int numCubits = numLayers>1 ? 6*numLayers*numLayers - 12*numLayers + 8 : 1;
264 e6cf7283 Leszek Koltunski
    float[][] tmp = new float[numCubits][];
265 beb325a0 Leszek Koltunski
266 b1f2ccf5 Leszek Koltunski
    float diff = 0.5f*(numLayers-1);
267 a10ada2a Leszek Koltunski
    int currentPosition = 0;
268 beb325a0 Leszek Koltunski
269 b1f2ccf5 Leszek Koltunski
    for(int x = 0; x<numLayers; x++)
270
      for(int y = 0; y<numLayers; y++)
271
        for(int z = 0; z<numLayers; z++)
272
          if( x==0 || x==numLayers-1 || y==0 || y==numLayers-1 || z==0 || z==numLayers-1 )
273 beb325a0 Leszek Koltunski
            {
274 e6cf7283 Leszek Koltunski
            tmp[currentPosition++] = new float[] {x-diff,y-diff,z-diff};
275 a10ada2a Leszek Koltunski
            }
276 47ba5ddc Leszek Koltunski
277 a10ada2a Leszek Koltunski
    return tmp;
278
    }
279 47ba5ddc Leszek Koltunski
280 beb325a0 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
281
282 10585385 Leszek Koltunski
  Static4D[] getQuats()
283 a10ada2a Leszek Koltunski
    {
284 7ff38997 Leszek Koltunski
    if( mQuats ==null ) initializeQuats();
285
    return mQuats;
286 a10ada2a Leszek Koltunski
    }
287 47ba5ddc Leszek Koltunski
288 eaee1ddc Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
289
290
  boolean shouldResetTextureMaps()
291
    {
292
    return false;
293
    }
294
295 47ba5ddc Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
296
297 abf36986 Leszek Koltunski
  int getNumFaceColors()
298 a10ada2a Leszek Koltunski
    {
299 411c6285 Leszek Koltunski
    return FACE_COLORS.length;
300 8f53e513 Leszek Koltunski
    }
301
302 7403cdfa Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
303
304 e6734aa9 Leszek Koltunski
  float[][] getCuts(int numLayers)
305 7403cdfa Leszek Koltunski
    {
306 e6734aa9 Leszek Koltunski
    float[][] cuts = new float[3][numLayers-1];
307 a97e02b7 Leszek Koltunski
308 a64e07d0 Leszek Koltunski
    for(int i=0; i<numLayers-1; i++)
309 a97e02b7 Leszek Koltunski
      {
310 e6734aa9 Leszek Koltunski
      float cut = (2-numLayers)*0.5f + i;
311
      cuts[0][i] = cut;
312
      cuts[1][i] = cut;
313
      cuts[2][i] = cut;
314 a97e02b7 Leszek Koltunski
      }
315
316
    return cuts;
317 7403cdfa Leszek Koltunski
    }
318
319 169219a7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
320
321
  int getSolvedFunctionIndex()
322
    {
323
    return 0;
324
    }
325
326 eab9d8f8 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
327
328 a64e07d0 Leszek Koltunski
  int getNumStickerTypes(int numLayers)
329 eab9d8f8 Leszek Koltunski
    {
330 7ff38997 Leszek Koltunski
    return 1;
331 eab9d8f8 Leszek Koltunski
    }
332
333 8f53e513 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
334
335
  int getNumCubitFaces()
336
    {
337
    return FACE_COLORS.length;
338 411c6285 Leszek Koltunski
    }
339 47ba5ddc Leszek Koltunski
340 f0fa83ae Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
341
342
  float getScreenRatio()
343
    {
344
    return 0.5f;
345
    }
346
347 f6d06256 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
348
349 f10a88a8 Leszek Koltunski
  int getFaceColor(int cubit, int cubitface, int numLayers)
350 f6d06256 Leszek Koltunski
    {
351 d464f54f Leszek Koltunski
    return CUBITS[cubit].mRotationRow[cubitface/2] == (cubitface%2==0 ? (1<<(numLayers-1)):1) ? cubitface : NUM_TEXTURES;
352 f6d06256 Leszek Koltunski
    }
353
354 fb377dae Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
355
356
  float returnMultiplier()
357
    {
358 d99f3a48 Leszek Koltunski
    return getNumLayers();
359 fb377dae Leszek Koltunski
    }
360
361 e844c116 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
362
// PUBLIC API
363
364 12ad3fca Leszek Koltunski
  public Static3D[] getRotationAxis()
365
    {
366 ad38d800 Leszek Koltunski
    return ROT_AXIS;
367 12ad3fca Leszek Koltunski
    }
368
369 e9a87113 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
370
371
  public Movement getMovement()
372
    {
373
    if( mMovement==null ) mMovement = new MovementCube();
374
    return mMovement;
375
    }
376
377 12ad3fca Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
378
379 925ed78f Leszek Koltunski
  public int[] getBasicAngle()
380 e844c116 Leszek Koltunski
    {
381 d90c55cc Leszek Koltunski
    if( mBasicAngle==null ) mBasicAngle = new int[] { 4,4,4 };
382 7ff38997 Leszek Koltunski
    return mBasicAngle;
383 e844c116 Leszek Koltunski
    }
384 39e74052 Leszek Koltunski
385 f0336037 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
386 6fd4a72c Leszek Koltunski
387
  public int getObjectName(int numLayers)
388
    {
389
    switch(numLayers)
390
      {
391
      case 2: return R.string.cube2;
392
      case 3: return R.string.cube3;
393
      case 4: return R.string.cube4;
394
      case 5: return R.string.cube5;
395
      }
396
    return R.string.cube3;
397
    }
398
399
///////////////////////////////////////////////////////////////////////////////////////////////////
400
401
  public int getInventor(int numLayers)
402
    {
403
    switch(numLayers)
404
      {
405
      case 2: return R.string.cube2_inventor;
406
      case 3: return R.string.cube3_inventor;
407
      case 4: return R.string.cube4_inventor;
408
      case 5: return R.string.cube5_inventor;
409
      }
410
    return R.string.cube3_inventor;
411
    }
412
413
///////////////////////////////////////////////////////////////////////////////////////////////////
414
415
  public int getComplexity(int numLayers)
416
    {
417
    switch(numLayers)
418
      {
419
      case 2: return 4;
420
      case 3: return 6;
421
      case 4: return 8;
422
      case 5: return 10;
423
      }
424
    return 6;
425
    }
426 0c52af30 Leszek Koltunski
}