Project

General

Profile

« Previous | Next » 

Revision 3c4a326c

Added by Leszek Koltunski about 4 years ago

More work on making new types of RubikObjects easily creatable.

View differences:

src/main/java/org/distorted/effect/scramble/ScrambleEffect.java
23 23
import org.distorted.library.effect.Effect;
24 24
import org.distorted.library.main.DistortedEffects;
25 25
import org.distorted.library.message.EffectListener;
26
import org.distorted.library.type.Static3D;
26 27
import org.distorted.magic.RubikRenderer;
27 28
import org.distorted.object.RubikObject;
28 29

  
29 30
import java.lang.reflect.Method;
30 31
import java.util.Random;
31 32

  
32
import static org.distorted.object.RubikObjectList.VECTX;
33
import static org.distorted.object.RubikObjectList.VECTY;
34
import static org.distorted.object.RubikObjectList.VECTZ;
35

  
36 33
///////////////////////////////////////////////////////////////////////////////////////////////////
37 34

  
38 35
public abstract class ScrambleEffect extends BaseEffect implements EffectListener
......
55 52
  private static final int FAKE_EFFECT_ID  = -3;
56 53
  private static final Type[] types;
57 54

  
55
  private static final Static3D VECTX = new Static3D(1,0,0);
56
  private static final Static3D VECTY = new Static3D(0,1,0);
57
  private static final Static3D VECTZ = new Static3D(0,0,1);
58
  private static final Static3D VECTU = new Static3D(0,0,0);
59

  
60
  private static final Static3D[] VECTORS = { VECTX, VECTY, VECTZ, VECTZ };
61

  
58 62
  static
59 63
    {
60 64
    int i=0;
......
86 90
  ScrambleEffect()
87 91
    {
88 92
    mRnd = new Random( System.currentTimeMillis() );
89
    mLastVector = -1;
93
    mLastVector = 3;
90 94
    }
91 95

  
92 96
///////////////////////////////////////////////////////////////////////////////////////////////////
......
123 127
    {
124 128
    if( mNumScramblesLeft>0 )
125 129
      {
126
      if( mLastVector == -1 )
130
      if( mLastVector == 3 )
127 131
        {
128
        switch(mRnd.nextInt(3))
129
          {
130
          case 0: mLastVector = VECTX; break;
131
          case 1: mLastVector = VECTY; break;
132
          case 2: mLastVector = VECTZ; break;
133
          }
132
        mLastVector = mRnd.nextInt(3);
134 133
        }
135 134
      else
136 135
        {
......
138 137

  
139 138
        switch(mLastVector)
140 139
          {
141
          case VECTX: mLastVector = (newVector==0 ? VECTY: VECTZ); break;
142
          case VECTY: mLastVector = (newVector==0 ? VECTX: VECTZ); break;
143
          case VECTZ: mLastVector = (newVector==0 ? VECTX: VECTY); break;
140
          case 0: mLastVector = (newVector==0 ? 1:2); break;
141
          case 1: mLastVector = (newVector==0 ? 0:2); break;
142
          case 2: mLastVector = (newVector==0 ? 0:1); break;
144 143
          }
145 144
        }
146 145

  
......
157 156
        android.util.Log.e("effect", "ERROR: "+mNumDoubleScramblesLeft);
158 157
        }
159 158

  
160
      mCurrentBaseEffectID = mObject.addNewRotation(mLastVector, row, angle*90, durationMillis, this );
159
      mCurrentBaseEffectID = mObject.addNewRotation(VECTORS[mLastVector], row, angle*90, durationMillis, this );
161 160
      }
162 161
    else
163 162
      {
src/main/java/org/distorted/magic/RubikRenderer.java
132 132

  
133 133
   void savePreferences(SharedPreferences.Editor editor)
134 134
     {
135
     mNewObject.savePreferences(editor);
135
     if( mNewObject!=null )
136
       {
137
       mNewObject.savePreferences(editor);
138
       }
136 139
     }
137 140

  
138 141
///////////////////////////////////////////////////////////////////////////////////////////////////
src/main/java/org/distorted/magic/RubikSurfaceView.java
27 27
import android.view.MotionEvent;
28 28

  
29 29
import org.distorted.library.type.Static2D;
30
import org.distorted.library.type.Static3D;
30 31
import org.distorted.library.type.Static4D;
31 32
import org.distorted.object.RubikObject;
32 33
import org.distorted.object.RubikObjectMovement;
......
264 265
                                           Static4D touchPoint2 = new Static4D(x, y, 0, 0);
265 266
                                           Static4D rotatedTouchPoint2= rotateVectorByInvertedQuat(touchPoint2, mQuatAccumulated);
266 267

  
267
                                           Static2D rot = mMovement.newRotation(rotatedTouchPoint2);
268
                                           Static4D rot = mMovement.newRotation(rotatedTouchPoint2);
268 269
                                           RubikObject object = mRenderer.getObject();
269 270

  
270
                                           object.beginNewRotation( (int)rot.get0(), (int)(object.getSize()*rot.get1()) );
271
                                           object.beginNewRotation( new Static3D(rot.get0(),rot.get1(),rot.get2()), (int)(object.getSize()*rot.get3()) );
271 272

  
272 273
                                           if( RubikState.getCurrentState()==RubikState.SOLV )
273 274
                                             {
src/main/java/org/distorted/object/Cubit.java
233 233

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

  
236
  Static4D returnRotationQuat(float qx,float qy,float qz)
236
  Static4D returnRotationQuat( Static3D axis)
237 237
    {
238 238
    int pointNum = mRotationAngle.getNumPoints();
239 239

  
......
244 244
      double nearestAngleInRadians = nearestAngleInDegrees*Math.PI/180;
245 245
      float sinA =-(float)Math.sin(nearestAngleInRadians*0.5);
246 246
      float cosA = (float)Math.cos(nearestAngleInRadians*0.5);
247
      return new Static4D(qx*sinA, qy*sinA, qz*sinA, cosA);
247
      return new Static4D( axis.get0()*sinA, axis.get1()*sinA, axis.get2()*sinA, cosA);
248 248
      }
249 249

  
250 250
    return null;
src/main/java/org/distorted/object/RubikCube.java
30 30
import org.distorted.library.type.Static3D;
31 31
import org.distorted.library.type.Static4D;
32 32

  
33
import static org.distorted.object.RubikObjectList.VECTX;
34
import static org.distorted.object.RubikObjectList.VECTY;
35
import static org.distorted.object.RubikObjectList.VECTZ;
36

  
37 33
///////////////////////////////////////////////////////////////////////////////////////////////////
38 34

  
39 35
class RubikCube extends RubikObject
......
64 60
    }
65 61

  
66 62
///////////////////////////////////////////////////////////////////////////////////////////////////
67
// i.e. size^3 - (size-2)^3 - number of cubits in the outside wall of the Cube (we dont create or
68
// render the inside0
63
// i.e. size^3 - (size-2)^3 - number of cubits in the outside wall of the Cube (we don't create or
64
// render the inside)
69 65

  
70 66
  int getNumCubits(int size)
71 67
    {
72
    return 6*size*size - 12*size + 8;
68
    return size>1 ? 6*size*size - 12*size + 8 : 1;
73 69
    }
74 70

  
75 71
///////////////////////////////////////////////////////////////////////////////////////////////////
......
109 105

  
110 106
///////////////////////////////////////////////////////////////////////////////////////////////////
111 107

  
112
  boolean belongsToRotation( Static3D currentPosition, int vector, int row)
108
  boolean belongsToRotation( Static3D currentPosition, Static3D axis, int row)
113 109
    {
114
    switch(vector)
115
      {
116
      case VECTX: return currentPosition.get0()==row;
117
      case VECTY: return currentPosition.get1()==row;
118
      case VECTZ: return currentPosition.get2()==row;
119
      }
110
    if( axis.get0()!=0 ) return currentPosition.get0()==row;
111
    if( axis.get1()!=0 ) return currentPosition.get1()==row;
112
    if( axis.get2()!=0 ) return currentPosition.get2()==row;
120 113

  
121 114
    return false;
122 115
    }
src/main/java/org/distorted/object/RubikCubeMovement.java
19 19

  
20 20
package org.distorted.object;
21 21

  
22
import org.distorted.library.type.Static2D;
23 22
import org.distorted.library.type.Static4D;
24 23

  
25
import static org.distorted.object.RubikObjectList.VECTX;
26
import static org.distorted.object.RubikObjectList.VECTY;
27
import static org.distorted.object.RubikObjectList.VECTZ;
28

  
29 24
///////////////////////////////////////////////////////////////////////////////////////////////////
30 25

  
31 26
class RubikCubeMovement extends RubikObjectMovement
32 27
{
33 28
    private final static int NONE   =-1;
34
    private final static int FRONT  = 0;  // has to be 6 consecutive ints
35
    private final static int BACK   = 1;  // FRONT ... BOTTOM
36
    private final static int LEFT   = 2;  //
37
    private final static int RIGHT  = 3;  //
29
    private final static int FRONT  = 0;  //
30
    private final static int BACK   = 1;  //
31
    private final static int LEFT   = 2;  // has to be 6 consecutive ints
32
    private final static int RIGHT  = 3;  // FRONT ... BOTTOM
38 33
    private final static int TOP    = 4;  //
39 34
    private final static int BOTTOM = 5;  //
40 35

  
36
    private static final int VECTX  = 0;  //
37
    private static final int VECTY  = 1;  // don't change this
38
    private static final int VECTZ  = 2;  //
39

  
41 40
    private static final int[] VECT = {VECTX,VECTY,VECTZ};
42 41

  
43 42
    private float[] mPoint, mCamera, mDiff, mTouch;
......
192 191

  
193 192
///////////////////////////////////////////////////////////////////////////////////////////////////
194 193

  
195
    public Static2D newRotation(Static4D rotatedTouchPoint)
194
    public Static4D newRotation(Static4D rotatedTouchPoint)
196 195
      {
197 196
      float cubeHalfSize= RubikObject.OBJECT_SCREEN_RATIO*0.5f;
198 197

  
......
215 214
      mTouch[1] = mPoint[1];
216 215
      mTouch[2] = mPoint[2];
217 216

  
218
      return new Static2D(mRotationVect,offset);
217
      Static4D result=null;
218

  
219
      switch(mRotationVect)
220
        {
221
        case VECTX: result = new Static4D(1,0,0,offset); break;
222
        case VECTY: result = new Static4D(0,1,0,offset); break;
223
        case VECTZ: result = new Static4D(0,0,1,offset); break;
224
        }
225

  
226
      return result;
219 227
      }
220 228

  
221 229
///////////////////////////////////////////////////////////////////////////////////////////////////
src/main/java/org/distorted/object/RubikObject.java
37 37
import org.distorted.library.type.Static3D;
38 38
import org.distorted.library.type.Static4D;
39 39

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

  
42 40
///////////////////////////////////////////////////////////////////////////////////////////////////
43 41

  
44 42
public abstract class RubikObject extends DistortedNode
......
50 48
  private static final int POST_ROTATION_MILLISEC = 500;
51 49
  private final int NUM_CUBITS;
52 50
  private final int[][] CUBIT_POSITIONS;
53
  private int mRotAxis, mRotRow;
54

  
51
  private int mRotRow;
52
  private Static3D mRotAxis;
55 53
  private Static3D mMove, mScale, mNodeMove, mNodeScale;
56 54
  private Static4D mQuatAccumulated;
57 55
  private DistortedTexture mNodeTexture;
......
292 290

  
293 291
///////////////////////////////////////////////////////////////////////////////////////////////////
294 292

  
295
  public void beginNewRotation(int vector, int row )
293
  public void beginNewRotation(Static3D axis, int row )
296 294
    {
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;
295
    mRotAxis = axis;
307 296
    mRotRow  = row;
308 297

  
309 298
    mRotationAngleStatic.set0(0.0f);
310 299

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

  
318 307
///////////////////////////////////////////////////////////////////////////////////////////////////
319 308

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

  
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;
314
     mRotAxis = axis;
334 315
     mRotRow  = row;
335 316

  
336 317
     mRotationAngleStatic.set0(0.0f);
337 318

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

  
......
354 335

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

  
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 341
     for(int i=0; i<NUM_CUBITS; i++)
369 342
       if( belongsToRotation(mCubits[i].mCurrentPosition,mRotAxis,mRotRow) )
370 343
         {
371 344
         if( first )
372 345
           {
373 346
           first = false;
374
           quat = mCubits[i].returnRotationQuat(qx,qy,qz);
347
           quat = mCubits[i].returnRotationQuat(mRotAxis);
375 348
           }
376 349

  
377 350
         mCubits[i].removeRotationNow(quat);
......
385 358
  abstract int getNumCubits(int size);
386 359
  abstract int[][] getCubitPositions(int size);
387 360
  abstract float[] getLegalQuats();
388
  abstract boolean belongsToRotation(Static3D position, int axis, int row);
361
  abstract boolean belongsToRotation(Static3D position, Static3D axis, int row);
389 362
  abstract MeshCubes createMesh(int vertices,int x, int y, int z);
390 363

  
391 364
  public abstract void createTexture();
src/main/java/org/distorted/object/RubikObjectList.java
22 22
import org.distorted.library.main.DistortedEffects;
23 23
import org.distorted.library.main.DistortedTexture;
24 24
import org.distorted.library.mesh.MeshRectangles;
25
import org.distorted.library.type.Static3D;
26 25
import org.distorted.library.type.Static4D;
27 26
import org.distorted.magic.R;
28 27

  
......
38 37
  CUBE5 ( 5, R.drawable.button5 , RubikCube.class, RubikCubeMovement.class),
39 38
  ;
40 39

  
41
  public static final int VECTX = 0;  //
42
  public static final int VECTY = 1;  // don't change this
43
  public static final int VECTZ = 2;  //
44
  static final Static3D VectX = new Static3D(1,0,0);
45
  static final Static3D VectY = new Static3D(0,1,0);
46
  static final Static3D VectZ = new Static3D(0,0,1);
47

  
48 40
  public static final int LENGTH = values().length;
49 41
  private final int mObjectSize, mIconID;
50 42
  final Class<? extends RubikObject> mObjectClass;
src/main/java/org/distorted/object/RubikObjectMovement.java
19 19

  
20 20
package org.distorted.object;
21 21

  
22
import org.distorted.library.type.Static2D;
23 22
import org.distorted.library.type.Static4D;
24 23

  
25 24
///////////////////////////////////////////////////////////////////////////////////////////////////
......
27 26
public abstract class RubikObjectMovement
28 27
  {
29 28
  public abstract boolean faceTouched(Static4D rotatedTouchPoint, Static4D rotatedCamera);
30
  public abstract Static2D newRotation(Static4D rotatedTouchPoint);
29
  public abstract Static4D newRotation(Static4D rotatedTouchPoint);
31 30
  public abstract float continueRotation(Static4D rotatedTouchPoint);
32 31
  }

Also available in: Unified diff