Project

General

Profile

« Previous | Next » 

Revision ebb64a1d

Added by Leszek Koltunski almost 5 years ago

RubikCube: add skeleton WinEffects (for now only one effect - 'Spin' copied from Solve)

View differences:

src/main/java/org/distorted/effect/BaseEffect.java
25 25
import org.distorted.effect.scramble.ScrambleEffect;
26 26
import org.distorted.effect.sizechange.SizeChangeEffect;
27 27
import org.distorted.effect.solve.SolveEffect;
28
import org.distorted.effect.win.WinEffect;
28 29
import org.distorted.magic.R;
29 30
import org.distorted.magic.RubikRenderer;
30 31

  
......
37 38
    SIZECHANGE  ( 20, 1, R.string.sizechange_effect , SizeChangeEffect.class),
38 39
    SOLVE       ( 20, 1, R.string.solve_effect      , SolveEffect.class     ),
39 40
    SCRAMBLE    ( 20, 1, R.string.scramble_effect   , ScrambleEffect.class  ),
41
    WIN         ( 20, 1, R.string.win_effect        , WinEffect.class       ),
40 42
    ;
41 43

  
42 44
    private final int mDefaultPos, mDefaultType;
43
    private final Class mClass;
45
    private final Class<? extends BaseEffect> mClass;
44 46
    private int mCurrentPos, mCurrentType;
45 47
    private int mText;
46 48

  
47
    Type(int dPos, int dType, int text, Class clazz )
49
    Type(int dPos, int dType, int text, Class<? extends BaseEffect> clazz )
48 50
      {
49 51
      mDefaultPos  = mCurrentPos = dPos;
50 52
      mDefaultType = mCurrentType= dType;
......
256 258
      return (pos/2)*100;
257 259
      }
258 260
    }
261

  
262
  // END ENUM ////////////////////////////////////////////////////////////////////
259 263
  }
src/main/java/org/distorted/effect/win/WinEffect.java
1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2019 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Distorted.                                                               //
5
//                                                                                               //
6
// Distorted 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
// Distorted 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 Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19

  
20
package org.distorted.effect.win;
21

  
22
import org.distorted.effect.BaseEffect;
23
import org.distorted.library.effect.Effect;
24
import org.distorted.library.main.DistortedEffects;
25
import org.distorted.library.main.DistortedScreen;
26
import org.distorted.library.message.EffectListener;
27
import org.distorted.magic.RubikCube;
28
import org.distorted.magic.RubikRenderer;
29

  
30
import java.lang.reflect.Method;
31

  
32
///////////////////////////////////////////////////////////////////////////////////////////////////
33

  
34
public abstract class WinEffect extends BaseEffect implements EffectListener
35
{
36
  public enum Type
37
    {
38
    NONE   (WinEffectNone.class),
39
    SPIN   (WinEffectSpin.class),
40
    ;
41

  
42
    final Class<? extends WinEffect> effect;
43

  
44
    Type(Class<? extends WinEffect> effect)
45
      {
46
      this.effect = effect;
47
      }
48
    }
49

  
50
  private static final int NUM_EFFECTS = Type.values().length;
51
  private static final int NUM_PHASES  = 2;
52
  private static final int FAKE_EFFECT_ID = -2;
53
  private static final Type[] types;
54

  
55
  static
56
    {
57
    int i=0;
58
    types = new Type[NUM_EFFECTS];
59

  
60
    for(Type type: Type.values())
61
      {
62
      types[i++] = type;
63
      }
64
    }
65

  
66
  private EffectListener mListener;
67
  private int mDuration;
68
  private int mEffectReturned;
69
  private int[] mCubeEffectNumber, mNodeEffectNumber;
70
  private int mPhase;
71

  
72
  RubikCube mCube;
73
  DistortedScreen mScreen;
74
  Effect[][] mCubeEffects;
75
  int[][] mCubeEffectPosition;
76
  Effect[][] mNodeEffects;
77
  int[][] mNodeEffectPosition;
78

  
79
///////////////////////////////////////////////////////////////////////////////////////////////////
80

  
81
  WinEffect()
82
    {
83
    mPhase        =  0;
84

  
85
    mCubeEffectNumber   = new int[NUM_PHASES];
86
    mNodeEffectNumber   = new int[NUM_PHASES];
87
    mCubeEffectPosition = new int[NUM_PHASES][];
88
    mNodeEffectPosition = new int[NUM_PHASES][];
89
    mCubeEffects        = new Effect[NUM_PHASES][];
90
    mNodeEffects        = new Effect[NUM_PHASES][];
91
    }
92

  
93
///////////////////////////////////////////////////////////////////////////////////////////////////
94

  
95
  public static String[] getNames()
96
    {
97
    String[] names = new String[NUM_EFFECTS];
98

  
99
    for( int i=0; i<NUM_EFFECTS; i++)
100
      {
101
      names[i] = types[i].name();
102
      }
103

  
104
    return names;
105
    }
106

  
107
///////////////////////////////////////////////////////////////////////////////////////////////////
108

  
109
  public static WinEffect create(int ordinal) throws InstantiationException, IllegalAccessException
110
    {
111
    return types[ordinal].effect.newInstance();
112
    }
113

  
114
///////////////////////////////////////////////////////////////////////////////////////////////////
115

  
116
  abstract void createEffectsPhase0(int duration);
117
  abstract void createEffectsPhase1(int duration);
118

  
119
///////////////////////////////////////////////////////////////////////////////////////////////////
120

  
121
  public void effectFinished(final long effectID)
122
    {
123
    int total = mCubeEffectNumber[mPhase]+mNodeEffectNumber[mPhase];
124

  
125
    for(int i=0; i<mCubeEffectNumber[mPhase]; i++)
126
      {
127
      long id = mCubeEffects[mPhase][i].getID();
128

  
129
      if( effectID == id )
130
        {
131
        if( ++mEffectReturned == total ) effectAction(mPhase);
132
        mCube.remove(id);
133
        return;
134
        }
135
      }
136
    for(int i=0; i<mNodeEffectNumber[mPhase]; i++)
137
      {
138
      long id = mNodeEffects[mPhase][i].getID();
139

  
140
      if( effectID == id )
141
        {
142
        if( ++mEffectReturned == total ) effectAction(mPhase);
143
        mCube.getEffects().abortById(id);
144
        return;
145
        }
146
      }
147
    }
148

  
149
///////////////////////////////////////////////////////////////////////////////////////////////////
150

  
151
  private void effectAction(int phase)
152
    {
153
    switch(phase)
154
      {
155
      case 0: mEffectReturned = 0;
156
              mPhase          = 1;
157
              mCube.solve();
158
              createEffectsPhase1(mDuration);
159
              assignEffects(mPhase);
160
              break;
161
      case 1: mListener.effectFinished(FAKE_EFFECT_ID);
162
              break;
163
      }
164
    }
165

  
166
///////////////////////////////////////////////////////////////////////////////////////////////////
167

  
168
  public long start(int duration, RubikRenderer renderer)
169
    {
170
    mScreen   = renderer.getScreen();
171
    mCube     = renderer.getCube();
172
    mListener = renderer;
173
    mDuration = duration;
174

  
175
    createEffectsPhase0(mDuration);
176
    assignEffects(mPhase);
177

  
178
    return FAKE_EFFECT_ID;
179
    }
180

  
181
///////////////////////////////////////////////////////////////////////////////////////////////////
182

  
183
  private void assignEffects(int phase)
184
    {
185
    mCubeEffectNumber[phase] = ( mCubeEffects[phase]!=null ) ? mCubeEffects[phase].length : 0;
186
    mNodeEffectNumber[phase] = ( mNodeEffects[phase]!=null ) ? mNodeEffects[phase].length : 0;
187

  
188
    if( mCubeEffectNumber[phase]==0 && mNodeEffectNumber[phase]==0 )
189
      {
190
      throw new RuntimeException("Cube and Node Effects ("+phase+" phase) both not created!");
191
      }
192

  
193
    for(int i=0; i<mCubeEffectNumber[phase]; i++)
194
      {
195
      mCube.apply(mCubeEffects[phase][i],mCubeEffectPosition[phase][i]);
196
      mCubeEffects[phase][i].notifyWhenFinished(this);
197
      }
198

  
199
    DistortedEffects nodeEffects = mCube.getEffects();
200

  
201
    for(int i=0; i<mNodeEffectNumber[phase]; i++)
202
      {
203
      nodeEffects.apply(mNodeEffects[phase][i],mNodeEffectPosition[phase][i]);
204
      mNodeEffects[phase][i].notifyWhenFinished(this);
205
      }
206
    }
207

  
208
///////////////////////////////////////////////////////////////////////////////////////////////////
209

  
210
  @SuppressWarnings("unused")
211
  public static void enableEffects()
212
    {
213
    Method method;
214

  
215
    for(Type type: Type.values())
216
      {
217
      try
218
        {
219
        method = type.effect.getDeclaredMethod("enable"); // enable not public, thus getDeclaredMethod
220
        }
221
      catch(NoSuchMethodException ex)
222
        {
223
        android.util.Log.e("SolveEffect", type.effect.getSimpleName()+": exception getting method: "+ex.getMessage());
224
        method = null;
225
        }
226

  
227
      try
228
        {
229
        if( method!=null ) method.invoke(null);
230
        }
231
      catch(Exception ex)
232
        {
233
        android.util.Log.e("SolveEffect", type.effect.getSimpleName()+": exception invoking method: "+ex.getMessage());
234
        }
235
      }
236
    }
237
}
src/main/java/org/distorted/effect/win/WinEffectNone.java
1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2019 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Distorted.                                                               //
5
//                                                                                               //
6
// Distorted 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
// Distorted 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 Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19

  
20
package org.distorted.effect.win;
21

  
22
///////////////////////////////////////////////////////////////////////////////////////////////////
23

  
24
import org.distorted.library.effect.Effect;
25
import org.distorted.library.effect.MatrixEffectMove;
26
import org.distorted.library.type.Dynamic3D;
27
import org.distorted.library.type.Static3D;
28

  
29
public class WinEffectNone extends WinEffect
30
  {
31
  public void createEffectsPhase0(int duration)
32
    {
33
    Dynamic3D d0 = new Dynamic3D(1,0.5f);
34
    d0.add(new Static3D(0,0,0));
35

  
36
    mCubeEffectPosition[0] = new int[] {-1};
37
    mCubeEffects[0]        = new Effect[mCubeEffectPosition[0].length];
38
    mCubeEffects[0][0]     = new MatrixEffectMove(d0);
39
    }
40

  
41
///////////////////////////////////////////////////////////////////////////////////////////////////
42

  
43
  public void createEffectsPhase1(int duration)
44
    {
45
    Dynamic3D d0 = new Dynamic3D(1,0.5f);
46
    d0.add(new Static3D(0,0,0));
47

  
48
    mCubeEffectPosition[1]  = new int[] {-1};
49
    mCubeEffects[1]         = new Effect[mCubeEffectPosition[1].length];
50
    mCubeEffects[1][0]      = new MatrixEffectMove(d0);
51
    }
52

  
53
///////////////////////////////////////////////////////////////////////////////////////////////////
54
// Enable all effects used in this Effect. Called by reflection from the parent class.
55

  
56
  @SuppressWarnings("unused")
57
  static void enable()
58
    {
59

  
60
    }
61
  }
src/main/java/org/distorted/effect/win/WinEffectSpin.java
1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2019 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Distorted.                                                               //
5
//                                                                                               //
6
// Distorted 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
// Distorted 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 Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19

  
20
package org.distorted.effect.win;
21

  
22
///////////////////////////////////////////////////////////////////////////////////////////////////
23

  
24
import org.distorted.library.effect.Effect;
25
import org.distorted.library.effect.MatrixEffectRotate;
26
import org.distorted.library.type.Dynamic;
27
import org.distorted.library.type.Dynamic1D;
28
import org.distorted.library.type.Static1D;
29
import org.distorted.library.type.Static3D;
30

  
31
public class WinEffectSpin extends WinEffect
32
  {
33
  public void createEffectsPhase0(int duration)
34
    {
35
    mCubeEffectPosition[0] = new int[] {3};
36
    mCubeEffects[0]        = new Effect[mCubeEffectPosition[0].length];
37

  
38
    Static3D axis  = new Static3D(1,0,0);
39
    Static3D center= new Static3D(0,0,0);
40

  
41
    Dynamic1D d0 = new Dynamic1D(duration/2, 1.0f);
42
    d0.setMode(Dynamic.MODE_JUMP);
43
    d0.setConvexity(0.0f);          // otherwise speed of the rotation would be strangely uneven
44
    d0.add(new Static1D( 0*36));
45
    d0.add(new Static1D( 1*36));
46
    d0.add(new Static1D( 3*36));
47
    d0.add(new Static1D( 6*36));
48
    d0.add(new Static1D(10*36));
49
    mCubeEffects[0][0] = new MatrixEffectRotate(d0,axis,center);
50
    }
51

  
52
///////////////////////////////////////////////////////////////////////////////////////////////////
53

  
54
  public void createEffectsPhase1(int duration)
55
    {
56
    mCubeEffectPosition[1] = new int[] {3};
57
    mCubeEffects[1]        = new Effect[mCubeEffectPosition[1].length];
58

  
59
    Static3D axis  = new Static3D(1,0,0);
60
    Static3D center= new Static3D(0,0,0);
61

  
62
    Dynamic1D d1 = new Dynamic1D(duration/2, 1.0f);
63
    d1.setMode(Dynamic.MODE_JUMP);
64
    d1.setConvexity(0.0f);
65
    d1.add(new Static1D( 0*36));
66
    d1.add(new Static1D( 4*36));
67
    d1.add(new Static1D( 7*36));
68
    d1.add(new Static1D( 9*36));
69
    d1.add(new Static1D(10*36));
70
    mCubeEffects[1][0] = new MatrixEffectRotate(d1,axis,center);
71
    }
72

  
73
///////////////////////////////////////////////////////////////////////////////////////////////////
74
// Enable all effects used in this Effect. Called by reflection from the parent class.
75

  
76
  @SuppressWarnings("unused")
77
  static void enable()
78
    {
79

  
80
    }
81
  }
src/main/java/org/distorted/magic/RubikActivity.java
30 30
import android.view.View;
31 31

  
32 32
import org.distorted.component.HorizontalNumberPicker;
33
import org.distorted.effect.win.WinEffect;
33 34
import org.distorted.library.main.DistortedLibrary;
34 35
import org.distorted.effect.BaseEffect;
35 36

  
src/main/java/org/distorted/magic/RubikCube.java
53 53
    private static final Static3D VectY = new Static3D(0,1,0);
54 54
    private static final Static3D VectZ = new Static3D(0,0,1);
55 55

  
56
    private static final float SQ2 = 0.5f*((float)Math.sqrt(2));
57
    private static final float[] LEGAL = { 0.0f , 0.5f , -0.5f , 1.0f , -1.0f , SQ2 , -SQ2 };
58

  
59 56
    public static final int VECTX = 0;  //
60 57
    public static final int VECTY = 1;  // don't change this
61 58
    public static final int VECTZ = 2;  //
......
548 545
// We also have to remember that the group of unit quaternions is a double-cover of rotations
549 546
// in 3D ( q represents the same rotation as -q ) - so invert if needed.
550 547

  
548
    private static final float SQ2 = 0.5f*((float)Math.sqrt(2));
549
    private static final float[] LEGAL = { 0.0f , 0.5f , -0.5f , 1.0f , -1.0f , SQ2 , -SQ2 };
550

  
551 551
    private void normalizeScrambleQuat(int i, int j, int k)
552 552
      {
553 553
      Static4D quat = mQuatScramble[i][j][k];
src/main/java/org/distorted/magic/RubikRenderer.java
109 109

  
110 110
      if( mFinishRotation )
111 111
        {
112
        mCanRotate = false;
113
        mFinishRotation=false;
112
        mFinishRotation = false;
113
        mCanRotate      = false;
114
        mCanUI          = false;
114 115
        mRotationFinishedID = mNewCube.finishRotationNow(this);
115 116
        }
116 117

  
......
121 122

  
122 123
        if( mNewCube.isSolved() )
123 124
          {
124
          android.util.Log.e("renderer", "CUBE IS SOLVED NOW");
125
          mCanDrag        = false;
126
          mCanRotate      = false;
127
          mCanUI          = false;
128
          doEffectNow( BaseEffect.Type.WIN );
129
          }
130
        else
131
          {
132
          mCanRotate = true;
133
          mCanUI     = true;
125 134
          }
126

  
127
        mCanRotate = true;
128 135
        }
129 136

  
130 137
      if( mSizeChangeCube )
......
132 139
        mSizeChangeCube = false;
133 140
        mCanDrag        = false;
134 141
        mCanRotate      = false;
142
        mCanUI          = false;
135 143
        createCubeNow(mNextCubeSize);
136 144
        doEffectNow( BaseEffect.Type.SIZECHANGE );
137 145
        }
138 146

  
139 147
      if( mSolveCube )
140 148
        {
141
        mSolveCube   = false;
142
        mCanDrag     = false;
143
        mCanRotate   = false;
144
        mCanUI       = false;
149
        mSolveCube      = false;
150
        mCanDrag        = false;
151
        mCanRotate      = false;
152
        mCanUI          = false;
145 153
        doEffectNow( BaseEffect.Type.SOLVE );
146 154
        }
147 155

  
src/main/res/values/strings.xml
11 11
    <string name="sizechange_effect">Size Change Effect:</string>
12 12
    <string name="solve_effect">Solve Effect:</string>
13 13
    <string name="scramble_effect">Scramble Effect:</string>
14
    <string name="win_effect">Win Effect:</string>
14 15
    <string name="duration">Duration:</string>
15 16
    <string name="type">Type:</string>
16 17
    <string name="credits1">Open Source app developed using the Distorted graphics library. </string>

Also available in: Unified diff