Project

General

Profile

« Previous | Next » 

Revision a10ada2a

Added by Leszek Koltunski about 4 years ago

More work on Cubit.

View differences:

src/main/java/org/distorted/object/RubikObject.java
29 29
import org.distorted.library.main.DistortedEffects;
30 30
import org.distorted.library.main.DistortedNode;
31 31
import org.distorted.library.main.DistortedTexture;
32
import org.distorted.library.mesh.MeshCubes;
32 33
import org.distorted.library.mesh.MeshRectangles;
33 34
import org.distorted.library.message.EffectListener;
34 35
import org.distorted.library.type.Dynamic1D;
......
36 37
import org.distorted.library.type.Static3D;
37 38
import org.distorted.library.type.Static4D;
38 39

  
40
import static org.distorted.object.RubikObjectList.*;
41

  
39 42
///////////////////////////////////////////////////////////////////////////////////////////////////
40 43

  
41 44
public abstract class RubikObject extends DistortedNode
42 45
  {
43 46
  static final float OBJECT_SCREEN_RATIO = 0.5f;
44 47
  static final int TEXTURE_SIZE = 100;
48
  final float[] LEGAL_QUATS;
45 49

  
46 50
  private static final int POST_ROTATION_MILLISEC = 500;
47
  final float[] LEGAL_QUATS;
51
  private final int NUM_CUBITS;
52
  private final int[][] CUBIT_POSITIONS;
53
  private int mRotAxis, mRotRow;
48 54

  
49 55
  private Static3D mMove, mScale, mNodeMove, mNodeScale;
50 56
  private Static4D mQuatAccumulated;
51 57
  private DistortedTexture mNodeTexture;
52 58

  
53
  int mSize, mRotAxis, mRotRow;
59
  int mSize;
54 60

  
55 61
  Static1D mRotationAngleStatic, mRotationAngleMiddle, mRotationAngleFinal;
56 62
  DistortedTexture mTexture;
......
61 67
  MatrixEffectQuaternion mQuatCEffect;
62 68
  MatrixEffectQuaternion mQuatAEffect;
63 69

  
70
  private Cubit[] mCubits;
71

  
64 72
///////////////////////////////////////////////////////////////////////////////////////////////////
65 73

  
66 74
  RubikObject(int size, Static4D quatCur, Static4D quatAcc, DistortedTexture texture, MeshRectangles mesh, DistortedEffects effects)
......
68 76
    super(texture,effects,mesh);
69 77

  
70 78
    LEGAL_QUATS = getLegalQuats();
79
    NUM_CUBITS  = getNumCubits(size);
80
    CUBIT_POSITIONS = getCubitPositions(size);
81

  
71 82
    mNodeTexture = texture;
72 83
    mSize = size;
73 84

  
......
97 108

  
98 109
    effects.apply(nodeScaleEffect);
99 110
    effects.apply(nodeMoveEffect);
111

  
112

  
113
    mCubits = new Cubit[NUM_CUBITS];
114

  
115
    mTexture = new DistortedTexture(TEXTURE_SIZE,TEXTURE_SIZE);
116

  
117
    int vertices = (int)(24.0f/mSize + 2.0f);
118

  
119
    for(int i=0; i<NUM_CUBITS; i++)
120
      {
121
      int x = CUBIT_POSITIONS[i][0];
122
      int y = CUBIT_POSITIONS[i][1];
123
      int z = CUBIT_POSITIONS[i][2];
124

  
125
      mCubits[i] = new Cubit( this ,createMesh(vertices,x,y,z), new Static3D(x,y,z) );
126
      attach(mCubits[i].mNode);
127
      }
100 128
    }
101 129

  
102 130
///////////////////////////////////////////////////////////////////////////////////////////////////
103 131

  
104
  void resetRotationAngle(Dynamic1D rotationAngle)
132
  private void resetRotationAngle(Dynamic1D rotationAngle)
105 133
    {
106 134
    rotationAngle.setDuration(POST_ROTATION_MILLISEC);
107 135
    rotationAngle.resetToBeginning();
......
176 204

  
177 205
///////////////////////////////////////////////////////////////////////////////////////////////////
178 206

  
179
  abstract float[] getLegalQuats();
207
  public void savePreferences(SharedPreferences.Editor editor)
208
    {
209
    for(int i=0; i<NUM_CUBITS; i++) mCubits[i].savePreferences(editor);
210
    }
180 211

  
181
  public abstract void savePreferences(SharedPreferences.Editor editor);
182
  public abstract void restorePreferences(SharedPreferences preferences);
212
///////////////////////////////////////////////////////////////////////////////////////////////////
183 213

  
184
  public abstract void beginNewRotation(int vector, int row );
185
  public abstract long addNewRotation(int vector, int row, int angle, long durationMillis, EffectListener listener );
186
  public abstract long finishRotationNow(EffectListener listener);
187
  public abstract void removeRotationNow();
214
  public void restorePreferences(SharedPreferences preferences)
215
    {
216
    for(int i=0; i<NUM_CUBITS; i++) mCubits[i].restorePreferences(preferences);
217
    }
188 218

  
189
  public abstract void apply(Effect effect, int position);
190
  public abstract void remove(long effectID);
219
///////////////////////////////////////////////////////////////////////////////////////////////////
191 220

  
192
  public abstract void releaseResources();
193
  public abstract void createTexture();
221
  public long finishRotationNow(EffectListener listener)
222
    {
223
    boolean first = true;
224
    long effectID=0;
225

  
226
    for(int i=0; i<NUM_CUBITS; i++)
227
      {
228
      if( belongsToRotation(mCubits[i].mCurrentPosition,mRotAxis,mRotRow) )
229
        {
230
        if( first )
231
          {
232
          first=false;
233
          effectID = mCubits[i].finishRotationNow(listener);
234
          }
235
        resetRotationAngle(mCubits[i].mRotationAngle);
236
        }
237
      }
238

  
239
    return effectID;
240
    }
241

  
242
///////////////////////////////////////////////////////////////////////////////////////////////////
243

  
244
  public void releaseResources()
245
    {
246
    mTexture.markForDeletion();
247

  
248
    for(int i=0; i<NUM_CUBITS; i++) mCubits[i].releaseResources();
249
    }
250

  
251
///////////////////////////////////////////////////////////////////////////////////////////////////
252

  
253
  public void apply(Effect effect, int position)
254
    {
255
    for(int i=0; i<NUM_CUBITS; i++) mCubits[i].mEffect.apply(effect, position);
256
    }
257

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

  
260
  public void remove(long effectID)
261
    {
262
    for(int i=0; i<NUM_CUBITS; i++) mCubits[i].mEffect.abortById(effectID);
263
    }
194 264

  
195
  public abstract void solve();
196
  public abstract boolean isSolved();
265
///////////////////////////////////////////////////////////////////////////////////////////////////
266

  
267
  public void solve()
268
    {
269
    for(int i=0; i<NUM_CUBITS; i++) mCubits[i].solve();
270
    }
271

  
272
///////////////////////////////////////////////////////////////////////////////////////////////////
273

  
274
  public boolean isSolved()
275
    {
276
    Static4D q = mCubits[0].mQuatScramble;
277

  
278
    float x = q.get0();
279
    float y = q.get1();
280
    float z = q.get2();
281
    float w = q.get3();
282

  
283
    for(int i=0; i<NUM_CUBITS; i++)
284
      {
285
      q = mCubits[i].mQuatScramble;
286

  
287
      if( q.get0()!=x || q.get1()!=y || q.get2()!=z || q.get3()!=w ) return false;
288
      }
289

  
290
    return true;
291
    }
292

  
293
///////////////////////////////////////////////////////////////////////////////////////////////////
294

  
295
  public void beginNewRotation(int vector, int row )
296
    {
297
    Static3D axis = VectX;
298

  
299
    switch(vector)
300
      {
301
      case VECTX: axis = VectX; break;
302
      case VECTY: axis = VectY; break;
303
      case VECTZ: axis = VectZ; break;
304
      }
305

  
306
    mRotAxis = vector;
307
    mRotRow  = row;
308

  
309
    mRotationAngleStatic.set0(0.0f);
310

  
311
    for(int i=0; i<NUM_CUBITS; i++)
312
      if( belongsToRotation( mCubits[i].mCurrentPosition,vector,mRotRow) )
313
        {
314
        mCubits[i].beginNewRotation(axis);
315
        }
316
     }
317

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

  
320
  public long addNewRotation(int vector, int row, int angle, long durationMillis, EffectListener listener )
321
     {
322
     Static3D axis = VectX;
323
     long effectID=0;
324
     boolean first = true;
325

  
326
     switch(vector)
327
       {
328
       case VECTX: axis = VectX; break;
329
       case VECTY: axis = VectY; break;
330
       case VECTZ: axis = VectZ; break;
331
       }
332

  
333
     mRotAxis = vector;
334
     mRotRow  = row;
335

  
336
     mRotationAngleStatic.set0(0.0f);
337

  
338
     for(int i=0; i<NUM_CUBITS; i++)
339
       if( belongsToRotation(mCubits[i].mCurrentPosition,vector,mRotRow) )
340
         {
341
         mCubits[i].addNewRotation(axis,durationMillis,angle);
342

  
343
         if( first )
344
           {
345
           first = false;
346
           effectID = mCubits[i].setUpCallback(listener);
347
           }
348
         }
349

  
350
     return effectID;
351
     }
352

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

  
355
  public void removeRotationNow()
356
     {
357
     float qx=0,qy=0,qz=0;
358
     boolean first = true;
359
     Static4D quat = null;
360

  
361
     switch(mRotAxis)
362
       {
363
       case VECTX: qx=1; break;
364
       case VECTY: qy=1; break;
365
       case VECTZ: qz=1; break;
366
       }
367

  
368
     for(int i=0; i<NUM_CUBITS; i++)
369
       if( belongsToRotation(mCubits[i].mCurrentPosition,mRotAxis,mRotRow) )
370
         {
371
         if( first )
372
           {
373
           first = false;
374
           quat = mCubits[i].returnRotationQuat(qx,qy,qz);
375
           }
376

  
377
         mCubits[i].removeRotationNow(quat);
378
         }
379

  
380
     mRotationAngleStatic.set0(0);
381
     }
382

  
383
///////////////////////////////////////////////////////////////////////////////////////////////////
384

  
385
  abstract int getNumCubits(int size);
386
  abstract int[][] getCubitPositions(int size);
387
  abstract float[] getLegalQuats();
388
  abstract boolean belongsToRotation(Static3D position, int axis, int row);
389
  abstract MeshCubes createMesh(int vertices,int x, int y, int z);
390

  
391
  public abstract void createTexture();
197 392
  }

Also available in: Unified diff