Project

General

Profile

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

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

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2019 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.helpers.ObjectShape;
25
import org.distorted.helpers.ObjectSticker;
26
import org.distorted.helpers.ScrambleState;
27
import org.distorted.library.main.DistortedEffects;
28
import org.distorted.library.main.DistortedTexture;
29
import org.distorted.library.mesh.MeshSquare;
30
import org.distorted.library.type.Static3D;
31
import org.distorted.library.type.Static4D;
32
import org.distorted.main.R;
33

    
34
///////////////////////////////////////////////////////////////////////////////////////////////////
35

    
36
class TwistyCube extends TwistyObject
37
{
38
  static final Static3D[] ROT_AXIS = new Static3D[]
39
         {
40
           new Static3D(1,0,0),
41
           new Static3D(0,1,0),
42
           new Static3D(0,0,1)
43
         };
44

    
45
  private static final int[] FACE_COLORS = new int[]
46
         {
47
           COLOR_YELLOW, COLOR_WHITE,
48
           COLOR_BLUE  , COLOR_GREEN,
49
           COLOR_RED   , COLOR_ORANGE
50
         };
51

    
52
  private ScrambleState[] mStates;
53
  private Static4D[] mQuats;
54
  private int[] mBasicAngle;
55
  private ObjectSticker[] mStickers;
56

    
57
///////////////////////////////////////////////////////////////////////////////////////////////////
58

    
59
  TwistyCube(int size, Static4D quat, DistortedTexture texture, MeshSquare mesh,
60
             DistortedEffects effects, int[][] moves, Resources res, int scrWidth)
61
    {
62
    super(size, size, quat, texture, mesh, effects, moves, ObjectList.CUBE, res, scrWidth);
63
    }
64

    
65
///////////////////////////////////////////////////////////////////////////////////////////////////
66

    
67
  ScrambleState[] getScrambleStates()
68
    {
69
    if( mStates==null )
70
      {
71
      int size = getNumLayers();
72
      int[][] m = new int[16][];
73
      for(int i=1; i<16; i++) m[i] = createEdges(size,i);
74

    
75
      mStates = new ScrambleState[]
76
        {
77
        new ScrambleState( new int[][] { m[ 1], m[ 2], m[ 3] } ),  // 0
78
        new ScrambleState( new int[][] {  null, m[ 4], m[ 5] } ),  // x
79
        new ScrambleState( new int[][] { m[ 6],  null, m[ 7] } ),  // y
80
        new ScrambleState( new int[][] { m[ 8], m[ 8],  null } ),  // z
81
        new ScrambleState( new int[][] { m[10],  null, m[ 7] } ),  // xy
82
        new ScrambleState( new int[][] { m[11], m[ 9],  null } ),  // xz
83
        new ScrambleState( new int[][] {  null, m[12], m[ 5] } ),  // yx
84
        new ScrambleState( new int[][] { m[ 8], m[13],  null } ),  // yz
85
        new ScrambleState( new int[][] {  null, m[ 4], m[14] } ),  // zx
86
        new ScrambleState( new int[][] { m[ 6],  null, m[15] } ),  // zy
87
        new ScrambleState( new int[][] {  null,  null, m[ 5] } ),  // xyx
88
        new ScrambleState( new int[][] {  null, m[ 4],  null } ),  // xzx
89
        new ScrambleState( new int[][] {  null,  null, m[ 7] } ),  // yxy
90
        new ScrambleState( new int[][] { m[ 6],  null,  null } ),  // yzy
91
        new ScrambleState( new int[][] {  null, m[ 9],  null } ),  // zxz
92
        new ScrambleState( new int[][] { m[ 8],  null,  null } ),  // zyz
93
        };
94
      }
95

    
96
    return mStates;
97
    }
98

    
99
///////////////////////////////////////////////////////////////////////////////////////////////////
100

    
101
  private int[] createEdges(int size, int vertex)
102
    {
103
    int[] ret = new int[9*size];
104

    
105
    for(int l=0; l<size; l++)
106
      {
107
      ret[9*l  ] = l;
108
      ret[9*l+1] =-1;
109
      ret[9*l+2] = vertex;
110
      ret[9*l+3] = l;
111
      ret[9*l+4] = 1;
112
      ret[9*l+5] = vertex;
113
      ret[9*l+6] = l;
114
      ret[9*l+7] = 2;
115
      ret[9*l+8] = vertex;
116
      }
117

    
118
    return ret;
119
    }
120

    
121
///////////////////////////////////////////////////////////////////////////////////////////////////
122

    
123
  private void initializeQuats()
124
    {
125
    mQuats = new Static4D[]
126
         {
127
         new Static4D(  0.0f,   0.0f,   0.0f,   1.0f),
128
         new Static4D(  1.0f,   0.0f,   0.0f,   0.0f),
129
         new Static4D(  0.0f,   1.0f,   0.0f,   0.0f),
130
         new Static4D(  0.0f,   0.0f,   1.0f,   0.0f),
131

    
132
         new Static4D( SQ2/2,  SQ2/2,  0.0f ,   0.0f),
133
         new Static4D( SQ2/2, -SQ2/2,  0.0f ,   0.0f),
134
         new Static4D( SQ2/2,   0.0f,  SQ2/2,   0.0f),
135
         new Static4D(-SQ2/2,   0.0f,  SQ2/2,   0.0f),
136
         new Static4D( SQ2/2,   0.0f,   0.0f,  SQ2/2),
137
         new Static4D( SQ2/2,   0.0f,   0.0f, -SQ2/2),
138
         new Static4D(  0.0f,  SQ2/2,  SQ2/2,   0.0f),
139
         new Static4D(  0.0f,  SQ2/2, -SQ2/2,   0.0f),
140
         new Static4D(  0.0f,  SQ2/2,   0.0f,  SQ2/2),
141
         new Static4D(  0.0f,  SQ2/2,   0.0f, -SQ2/2),
142
         new Static4D(  0.0f,   0.0f,  SQ2/2,  SQ2/2),
143
         new Static4D(  0.0f,   0.0f,  SQ2/2, -SQ2/2),
144

    
145
         new Static4D(  0.5f,   0.5f,   0.5f,   0.5f),
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
         };
154
    }
155

    
156
///////////////////////////////////////////////////////////////////////////////////////////////////
157

    
158
  int[] getSolvedQuats(int cubit, int numLayers)
159
    {
160
    if( mQuats ==null ) initializeQuats();
161
    int status = retCubitSolvedStatus(cubit,numLayers);
162
    return status<0 ? null : buildSolvedQuats(MovementCube.FACE_AXIS[status], mQuats);
163
    }
164

    
165
///////////////////////////////////////////////////////////////////////////////////////////////////
166

    
167
  ObjectShape getObjectShape(int cubit, int numLayers)
168
    {
169
    int extraI, extraV, num;
170
    float height;
171

    
172
    switch(numLayers)
173
      {
174
      case 2 : num = 6; extraI = 2; extraV = 2; height = 0.045f; break;
175
      case 3 : num = 5; extraI = 2; extraV = 2; height = 0.045f; break;
176
      case 4 : num = 5; extraI = 1; extraV = 1; height = 0.045f; break;
177
      default: num = 5; extraI = 0; extraV = 0; height = 0.045f; break;
178
      }
179

    
180
    double[][] vertices = new double[][]
181
          {
182
              { 0.5, 0.5, 0.5 },
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
          };
191

    
192
    int[][] vert_indices = new int[][]
193
          {
194
              {2,3,1,0},
195
              {7,6,4,5},
196
              {4,0,1,5},
197
              {7,3,2,6},
198
              {6,2,0,4},
199
              {3,7,5,1}
200
          };
201

    
202
    float[][] bands     = new float[][] { {height,35,0.5f,0.7f,num,extraI,extraV} };
203
    int[] bandIndices   = new int[] { 0,0,0,0,0,0};
204
    float[][] corners   = new float[][] { {0.036f,0.12f} };
205
    int[] cornerIndices = new int[] { 0,0,0,0,0,0,0,0 };
206
    float[][] centers   = new float[][] { {0.0f, 0.0f, 0.0f} };
207
    int[] centerIndices = new int[] { 0,0,0,0,0,0,0,0 };
208

    
209
    return new ObjectShape(vertices,vert_indices,bands,bandIndices,corners,cornerIndices,centers,centerIndices,getNumCubitFaces(), null);
210
    }
211

    
212
///////////////////////////////////////////////////////////////////////////////////////////////////
213

    
214
  Static4D getQuat(int cubit, int numLayers)
215
    {
216
    if( mQuats ==null ) initializeQuats();
217
    return mQuats[0];
218
    }
219

    
220
///////////////////////////////////////////////////////////////////////////////////////////////////
221

    
222
  int getNumCubitVariants(int numLayers)
223
    {
224
    return 1;
225
    }
226

    
227
///////////////////////////////////////////////////////////////////////////////////////////////////
228

    
229
  int getCubitVariant(int cubit, int numLayers)
230
    {
231
    return 0;
232
    }
233

    
234
///////////////////////////////////////////////////////////////////////////////////////////////////
235

    
236
  int getColor(int face)
237
    {
238
    return FACE_COLORS[face];
239
    }
240

    
241
///////////////////////////////////////////////////////////////////////////////////////////////////
242

    
243
  ObjectSticker retSticker(int face)
244
    {
245
    if( mStickers==null )
246
      {
247
      final float[][] STICKERS = new float[][]  { { -0.5f, -0.5f, 0.5f, -0.5f, 0.5f, 0.5f, -0.5f, 0.5f } };
248
      final float radius = 0.10f;
249
      final float stroke = 0.08f;
250
      final float[] radii = {radius,radius,radius,radius};
251
      mStickers = new ObjectSticker[STICKERS.length];
252
      mStickers[0] = new ObjectSticker(STICKERS[0],null,radii,stroke );
253
      }
254

    
255
    return mStickers[face/NUM_FACE_COLORS];
256
    }
257

    
258
///////////////////////////////////////////////////////////////////////////////////////////////////
259

    
260
  float[][] getCubitPositions(int numLayers)
261
    {
262
    int numCubits = numLayers>1 ? 6*numLayers*numLayers - 12*numLayers + 8 : 1;
263
    float[][] tmp = new float[numCubits][];
264

    
265
    float diff = 0.5f*(numLayers-1);
266
    int currentPosition = 0;
267

    
268
    for(int x = 0; x<numLayers; x++)
269
      for(int y = 0; y<numLayers; y++)
270
        for(int z = 0; z<numLayers; z++)
271
          if( x==0 || x==numLayers-1 || y==0 || y==numLayers-1 || z==0 || z==numLayers-1 )
272
            {
273
            tmp[currentPosition++] = new float[] {x-diff,y-diff,z-diff};
274
            }
275

    
276
    return tmp;
277
    }
278

    
279
///////////////////////////////////////////////////////////////////////////////////////////////////
280

    
281
  Static4D[] getQuats()
282
    {
283
    if( mQuats ==null ) initializeQuats();
284
    return mQuats;
285
    }
286

    
287
///////////////////////////////////////////////////////////////////////////////////////////////////
288

    
289
  boolean shouldResetTextureMaps()
290
    {
291
    return false;
292
    }
293

    
294
///////////////////////////////////////////////////////////////////////////////////////////////////
295

    
296
  int getNumFaceColors()
297
    {
298
    return FACE_COLORS.length;
299
    }
300

    
301
///////////////////////////////////////////////////////////////////////////////////////////////////
302

    
303
  float[][] getCuts(int numLayers)
304
    {
305
    float[][] cuts = new float[3][numLayers-1];
306

    
307
    for(int i=0; i<numLayers-1; i++)
308
      {
309
      float cut = (2-numLayers)*0.5f + i;
310
      cuts[0][i] = cut;
311
      cuts[1][i] = cut;
312
      cuts[2][i] = cut;
313
      }
314

    
315
    return cuts;
316
    }
317

    
318
///////////////////////////////////////////////////////////////////////////////////////////////////
319

    
320
  int getSolvedFunctionIndex()
321
    {
322
    return 0;
323
    }
324

    
325
///////////////////////////////////////////////////////////////////////////////////////////////////
326

    
327
  int getNumStickerTypes(int numLayers)
328
    {
329
    return 1;
330
    }
331

    
332
///////////////////////////////////////////////////////////////////////////////////////////////////
333

    
334
  int getNumCubitFaces()
335
    {
336
    return FACE_COLORS.length;
337
    }
338

    
339
///////////////////////////////////////////////////////////////////////////////////////////////////
340

    
341
  float getScreenRatio()
342
    {
343
    return 0.5f;
344
    }
345

    
346
///////////////////////////////////////////////////////////////////////////////////////////////////
347

    
348
  int getFaceColor(int cubit, int cubitface, int numLayers)
349
    {
350
    return CUBITS[cubit].mRotationRow[cubitface/2] == (cubitface%2==0 ? (1<<(numLayers-1)):1) ? cubitface : NUM_TEXTURES;
351
    }
352

    
353
///////////////////////////////////////////////////////////////////////////////////////////////////
354

    
355
  float returnMultiplier()
356
    {
357
    return getNumLayers();
358
    }
359

    
360
///////////////////////////////////////////////////////////////////////////////////////////////////
361
// PUBLIC API
362

    
363
  public Static3D[] getRotationAxis()
364
    {
365
    return ROT_AXIS;
366
    }
367

    
368
///////////////////////////////////////////////////////////////////////////////////////////////////
369

    
370
  public int[] getBasicAngle()
371
    {
372
    if( mBasicAngle==null ) mBasicAngle = new int[] { 4,4,4 };
373
    return mBasicAngle;
374
    }
375

    
376
///////////////////////////////////////////////////////////////////////////////////////////////////
377

    
378
  public int getObjectName(int numLayers)
379
    {
380
    switch(numLayers)
381
      {
382
      case 2: return R.string.cube2;
383
      case 3: return R.string.cube3;
384
      case 4: return R.string.cube4;
385
      case 5: return R.string.cube5;
386
      }
387
    return R.string.cube3;
388
    }
389

    
390
///////////////////////////////////////////////////////////////////////////////////////////////////
391

    
392
  public int getInventor(int numLayers)
393
    {
394
    switch(numLayers)
395
      {
396
      case 2: return R.string.cube2_inventor;
397
      case 3: return R.string.cube3_inventor;
398
      case 4: return R.string.cube4_inventor;
399
      case 5: return R.string.cube5_inventor;
400
      }
401
    return R.string.cube3_inventor;
402
    }
403

    
404
///////////////////////////////////////////////////////////////////////////////////////////////////
405

    
406
  public int getComplexity(int numLayers)
407
    {
408
    switch(numLayers)
409
      {
410
      case 2: return 4;
411
      case 3: return 6;
412
      case 4: return 8;
413
      case 5: return 10;
414
      }
415
    return 6;
416
    }
417
}
(22-22/43)