Project

General

Profile

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

magiccube / src / main / java / org / distorted / object / RubikCube.java @ 70b76549

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.object;
21

    
22
import android.content.SharedPreferences;
23
import android.graphics.Bitmap;
24
import android.graphics.Canvas;
25
import android.graphics.Paint;
26

    
27
import org.distorted.library.effect.Effect;
28
import org.distorted.library.main.DistortedEffects;
29
import org.distorted.library.main.DistortedTexture;
30
import org.distorted.library.mesh.MeshCubes;
31
import org.distorted.library.mesh.MeshRectangles;
32
import org.distorted.library.message.EffectListener;
33
import org.distorted.library.type.Static3D;
34
import org.distorted.library.type.Static4D;
35

    
36
import static org.distorted.object.RubikObjectList.VECTX;
37
import static org.distorted.object.RubikObjectList.VECTY;
38
import static org.distorted.object.RubikObjectList.VECTZ;
39

    
40
///////////////////////////////////////////////////////////////////////////////////////////////////
41

    
42
class RubikCube extends RubikObject
43
{
44
    private static final Static3D VectX = new Static3D(1,0,0);
45
    private static final Static3D VectY = new Static3D(0,1,0);
46
    private static final Static3D VectZ = new Static3D(0,0,1);
47
    private static final Static4D mapFront, mapBack, mapLeft, mapRight, mapTop, mapBottom, mapBlack;
48

    
49
    private Cubit[][][] mCubits;
50

    
51
    static
52
      {
53
      // 3x2 bitmap = 6 squares:
54
      //
55
      // RED     GREEN   BLUE
56
      // YELLOW  WHITE   BROWN
57

    
58
      final float ze = 0.0f;
59
      final float ot = 1.0f/3.0f;
60
      final float tt = 2.0f/3.0f;
61
      final float oh = 1.0f/2.0f;
62
      final float of = 1.0f/40.0f;
63

    
64
      mapFront = new Static4D(ze,oh, ze+ot,oh+oh);
65
      mapBack  = new Static4D(tt,ze, tt+ot,ze+oh);
66
      mapLeft  = new Static4D(ot,ze, ot+ot,ze+oh);
67
      mapRight = new Static4D(ze,ze, ze+ot,ze+oh);
68
      mapTop   = new Static4D(tt,oh, tt+ot,oh+oh);
69
      mapBottom= new Static4D(ot,oh, ot+ot,oh+oh);
70

    
71
      mapBlack = new Static4D(ze,ze, ze+of,ze+of);
72
      }
73

    
74
///////////////////////////////////////////////////////////////////////////////////////////////////
75
// All legal rotation quats of a RubikCube of any size must have all four of their components
76
// equal to either 0, 1, -1, 0.5, -0.5 or +-sqrt(2)/2.
77

    
78
    float[] getLegalQuats()
79
      {
80
      final float SQ2 = 0.5f*((float)Math.sqrt(2));
81
      return new float[] { 0.0f , 0.5f , -0.5f , 1.0f , -1.0f , SQ2 , -SQ2 };
82
      }
83

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

    
86
    private boolean belongsToRotation( Static3D currentPosition, int vector, int row)
87
      {
88
      switch(vector)
89
        {
90
        case VECTX: return currentPosition.get0()==row;
91
        case VECTY: return currentPosition.get1()==row;
92
        case VECTZ: return currentPosition.get2()==row;
93
        }
94

    
95
      return false;
96
      }
97

    
98
///////////////////////////////////////////////////////////////////////////////////////////////////
99

    
100
    RubikCube(int size, Static4D quatCur, Static4D quatAcc, DistortedTexture texture, MeshRectangles mesh, DistortedEffects effects)
101
      {
102
      super(size,quatCur,quatAcc,texture,mesh,effects);
103

    
104
      mTexture = new DistortedTexture(TEXTURE_SIZE,TEXTURE_SIZE);
105
      mCubits  = new Cubit[mSize][mSize][mSize];
106

    
107
      int vertices = (int)(24.0f/mSize + 2.0f);
108

    
109
      for(int x = 0; x< mSize; x++)
110
        for(int y = 0; y< mSize; y++)
111
          for(int z = 0; z< mSize; z++)
112
            {
113
            if( x==0 || x==mSize-1 || y==0 || y==mSize-1 || z==0 || z==mSize-1 )
114
              {
115
              mCubits[x][y][z] = new Cubit( this,createMesh(vertices,x,y,z),new Static3D(x,y,z) );
116
              attach(mCubits[x][y][z].mNode);
117
              }
118
            }
119
      }
120

    
121
///////////////////////////////////////////////////////////////////////////////////////////////////
122
// PUBLIC API
123
///////////////////////////////////////////////////////////////////////////////////////////////////
124
// mSize already saved as RubikStatePlay.mButton
125

    
126
   public void savePreferences(SharedPreferences.Editor editor)
127
     {
128
     for(int x=0; x<mSize; x++)
129
        for(int y=0; y<mSize; y++)
130
          for(int z=0; z<mSize; z++)
131
            if( x==0 || x==mSize-1 || y==0 || y==mSize-1 || z==0 || z==mSize-1 )
132
              {
133
              mCubits[x][y][z].savePreferences(editor);
134
              }
135
     }
136

    
137
///////////////////////////////////////////////////////////////////////////////////////////////////
138

    
139
   public void restorePreferences(SharedPreferences preferences)
140
     {
141
     for(int x=0; x<mSize; x++)
142
        for(int y=0; y<mSize; y++)
143
          for(int z=0; z<mSize; z++)
144
            if( x==0 || x==mSize-1 || y==0 || y==mSize-1 || z==0 || z==mSize-1 )
145
              {
146
              mCubits[x][y][z].restorePreferences(preferences);
147
              }
148
     }
149

    
150
///////////////////////////////////////////////////////////////////////////////////////////////////
151

    
152
   public long finishRotationNow(EffectListener listener)
153
     {
154
     boolean first = true;
155
     long effectID=0;
156

    
157
     for(int x=0; x<mSize; x++)
158
       for(int y=0; y<mSize; y++)
159
         for(int z=0; z<mSize; z++)
160
           if( x==0 || x==mSize-1 || y==0 || y==mSize-1 || z==0 || z==mSize-1 )
161
             {
162
             if( belongsToRotation(mCubits[x][y][z].mCurrentPosition,mRotAxis,mRotRow) )
163
               {
164
               if( first )
165
                 {
166
                 first = false;
167
                 effectID = mCubits[x][y][z].finishRotationNow(listener);
168
                 }
169

    
170
               resetRotationAngle(mCubits[x][y][z].mRotationAngle);
171
               }
172
             }
173

    
174
     return effectID;
175
     }
176

    
177
///////////////////////////////////////////////////////////////////////////////////////////////////
178

    
179
   public void releaseResources()
180
     {
181
     mTexture.markForDeletion();
182

    
183
     for(int x=0; x<mSize; x++)
184
       for(int y=0; y<mSize; y++)
185
         for(int z=0; z<mSize; z++)
186
           {
187
           if( x==0 || x==mSize-1 || y==0 || y==mSize-1 || z==0 || z==mSize-1 )
188
             {
189
             mCubits[x][y][z].releaseResources();
190
             }
191
           }
192
     }
193

    
194
///////////////////////////////////////////////////////////////////////////////////////////////////
195

    
196
   public void apply(Effect effect, int position)
197
     {
198
     for(int x=0; x<mSize; x++)
199
       for(int y=0; y<mSize; y++)
200
         for(int z=0; z<mSize; z++)
201
           {
202
           if( x==0 || x==mSize-1 || y==0 || y==mSize-1 || z==0 || z==mSize-1 )
203
             {
204
             mCubits[x][y][z].mEffect.apply(effect, position);
205
             }
206
           }
207
      }
208

    
209
///////////////////////////////////////////////////////////////////////////////////////////////////
210

    
211
   public void remove(long effectID)
212
     {
213
     for(int x=0; x<mSize; x++)
214
       for(int y=0; y<mSize; y++)
215
         for(int z=0; z<mSize; z++)
216
           {
217
           if( x==0 || x==mSize-1 || y==0 || y==mSize-1 || z==0 || z==mSize-1 )
218
             {
219
             mCubits[x][y][z].mEffect.abortById(effectID);
220
             }
221
           }
222
      }
223

    
224
///////////////////////////////////////////////////////////////////////////////////////////////////
225

    
226
   public void solve()
227
     {
228
     for(int x=0; x<mSize; x++)
229
       for(int y=0; y<mSize; y++)
230
         for(int z=0; z<mSize; z++)
231
           if( x==0 || x==mSize-1 || y==0 || y==mSize-1 || z==0 || z==mSize-1 )
232
             {
233
             mCubits[x][y][z].solve();
234
             }
235
      }
236

    
237
///////////////////////////////////////////////////////////////////////////////////////////////////
238

    
239
   public boolean isSolved()
240
     {
241
     Static4D q = mCubits[0][0][0].mQuatScramble;
242

    
243
     float x = q.get0();
244
     float y = q.get1();
245
     float z = q.get2();
246
     float w = q.get3();
247

    
248
     for(int i = 0; i< mSize; i++)
249
       for(int j = 0; j< mSize; j++)
250
         for(int k = 0; k< mSize; k++)
251
           {
252
           if( i==0 || i==mSize-1 || j==0 || j==mSize-1 || k==0 || k==mSize-1 )
253
             {
254
             q = mCubits[i][j][k].mQuatScramble;
255

    
256
             if( q.get0()!=x || q.get1()!=y || q.get2()!=z || q.get3()!=w )
257
               {
258
               return false;
259
               }
260
             }
261
           }
262

    
263
     return true;
264
     }
265

    
266
///////////////////////////////////////////////////////////////////////////////////////////////////
267

    
268
   public void beginNewRotation(int vector, int row )
269
     {
270
     Static3D axis = VectX;
271

    
272
     switch(vector)
273
       {
274
       case VECTX: axis = VectX; break;
275
       case VECTY: axis = VectY; break;
276
       case VECTZ: axis = VectZ; break;
277
       }
278

    
279
     mRotAxis = vector;
280
     mRotRow  = row;
281

    
282
     mRotationAngleStatic.set0(0.0f);
283

    
284
     for(int x=0; x<mSize; x++)
285
       for(int y=0; y<mSize; y++)
286
         for(int z=0; z<mSize; z++)
287
           if( x==0 || x==mSize-1 || y==0 || y==mSize-1 || z==0 || z==mSize-1 )
288
             {
289
             if( belongsToRotation( mCubits[x][y][z].mCurrentPosition,vector,mRotRow) )
290
               {
291
               mCubits[x][y][z].beginNewRotation(axis);
292
               }
293
             }
294
     }
295

    
296
///////////////////////////////////////////////////////////////////////////////////////////////////
297

    
298
   public long addNewRotation(int vector, int row, int angle, long durationMillis, EffectListener listener )
299
      {
300
      Static3D axis = VectX;
301
      long effectID=0;
302
      boolean first = true;
303

    
304
      switch(vector)
305
        {
306
        case VECTX: axis = VectX; break;
307
        case VECTY: axis = VectY; break;
308
        case VECTZ: axis = VectZ; break;
309
        }
310

    
311
      mRotAxis = vector;
312
      mRotRow  = row;
313

    
314
      mRotationAngleStatic.set0(0.0f);
315

    
316
      for(int x=0; x<mSize; x++)
317
        for(int y=0; y<mSize; y++)
318
          for(int z=0; z<mSize; z++)
319
            if( x==0 || x==mSize-1 || y==0 || y==mSize-1 || z==0 || z==mSize-1 )
320
              {
321
              if( belongsToRotation(mCubits[x][y][z].mCurrentPosition,vector,mRotRow) )
322
                {
323
                mCubits[x][y][z].addNewRotation(axis,durationMillis,angle);
324

    
325
                if( first )
326
                  {
327
                  first = false;
328
                  effectID = mCubits[x][y][z].setUpCallback(listener);
329
                  }
330
                }
331
              }
332

    
333
      return effectID;
334
      }
335

    
336
///////////////////////////////////////////////////////////////////////////////////////////////////
337

    
338
   public void removeRotationNow()
339
      {
340
      float qx=0,qy=0,qz=0;
341
      boolean first = true;
342
      Static4D quat = null;
343

    
344
      switch(mRotAxis)
345
        {
346
        case VECTX: qx=1; break;
347
        case VECTY: qy=1; break;
348
        case VECTZ: qz=1; break;
349
        }
350

    
351
      for(int x=0; x<mSize; x++)
352
        for(int y=0; y<mSize; y++)
353
          for(int z=0; z<mSize; z++)
354
            if( x==0 || x==mSize-1 || y==0 || y==mSize-1 || z==0 || z==mSize-1 )
355
              {
356
              if( belongsToRotation(mCubits[x][y][z].mCurrentPosition,mRotAxis,mRotRow) )
357
                {
358
                if( first )
359
                  {
360
                  first = false;
361
                  quat = mCubits[x][y][z].returnRotationQuat(qx,qy,qz);
362
                  }
363

    
364
                mCubits[x][y][z].removeRotationNow(quat);
365
                }
366
              }
367

    
368
      mRotationAngleStatic.set0(0);
369
      }
370

    
371
///////////////////////////////////////////////////////////////////////////////////////////////////
372

    
373
   public void createTexture()
374
     {
375
     Bitmap bitmap;
376

    
377
     final int S = 128;
378
     final int W = 3*S;
379
     final int H = 2*S;
380
     final int R = S/10;
381
     final int M = S/20;
382

    
383
     Paint paint = new Paint();
384
     bitmap = Bitmap.createBitmap(W,H, Bitmap.Config.ARGB_8888);
385
     Canvas canvas = new Canvas(bitmap);
386

    
387
     paint.setAntiAlias(true);
388
     paint.setTextAlign(Paint.Align.CENTER);
389
     paint.setStyle(Paint.Style.FILL);
390

    
391
     // 3x2 bitmap = 6 squares:
392
     //
393
     // RED     GREEN   BLUE
394
     // YELLOW  WHITE   BROWN
395

    
396
     paint.setColor(0xff000000);                                  // BLACK BACKGROUND
397
     canvas.drawRect(0, 0, W, H, paint);                          //
398

    
399
     paint.setColor(0xffff0000);                                  // RED
400
     canvas.drawRoundRect(    M,   M,   S-M,   S-M, R, R, paint); //
401
     paint.setColor(0xff00ff00);                                  // GREEN
402
     canvas.drawRoundRect(  S+M,   M, 2*S-M,   S-M, R, R, paint); //
403
     paint.setColor(0xff0000ff);                                  // BLUE
404
     canvas.drawRoundRect(2*S+M,   M, 3*S-M,   S-M, R, R, paint); //
405
     paint.setColor(0xffffff00);                                  // YELLOW
406
     canvas.drawRoundRect(    M, S+M,   S-M, 2*S-M, R, R, paint); //
407
     paint.setColor(0xffffffff);                                  // WHITE
408
     canvas.drawRoundRect(  S+M, S+M, 2*S-M, 2*S-M, R, R, paint); //
409
     paint.setColor(0xffb5651d);                                  // BROWN
410
     canvas.drawRoundRect(2*S+M, S+M, 3*S-M, 2*S-M, R, R, paint); //
411

    
412
     mTexture.setTexture(bitmap);
413
     }
414

    
415
///////////////////////////////////////////////////////////////////////////////////////////////////
416

    
417
   private MeshCubes createMesh(int vertices,int x, int y, int z)
418
     {
419
     Static4D tmpLeft  = (x==       0 ? mapLeft  :mapBlack);
420
     Static4D tmpRight = (x== mSize-1 ? mapRight :mapBlack);
421
     Static4D tmpFront = (z== mSize-1 ? mapFront :mapBlack);
422
     Static4D tmpBack  = (z==       0 ? mapBack  :mapBlack);
423
     Static4D tmpTop   = (y== mSize-1 ? mapTop   :mapBlack);
424
     Static4D tmpBottom= (y==       0 ? mapBottom:mapBlack);
425

    
426
     return new MeshCubes(vertices,vertices,vertices, tmpFront, tmpBack, tmpLeft, tmpRight, tmpTop, tmpBottom);
427
     }
428
}
(2-2/6)