Project

General

Profile

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

magiccube / src / main / java / org / distorted / effects / scramble / ScrambleEffect.java @ 55e6be1d

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.effects.scramble;
21

    
22
import org.distorted.effects.BaseEffect;
23
import org.distorted.helpers.MovesFinished;
24
import org.distorted.library.effect.Effect;
25
import org.distorted.library.main.DistortedEffects;
26
import org.distorted.library.main.DistortedScreen;
27
import org.distorted.library.message.EffectListener;
28
import org.distorted.main.RubikPreRender;
29
import org.distorted.effects.EffectController;
30
import org.distorted.objects.ObjectList;
31
import org.distorted.objects.TwistyObject;
32

    
33
import java.lang.reflect.Method;
34
import java.util.Random;
35

    
36
///////////////////////////////////////////////////////////////////////////////////////////////////
37

    
38
public abstract class ScrambleEffect extends BaseEffect implements EffectListener, MovesFinished
39
{
40
  public enum Type
41
    {
42
    NONE         (ScrambleEffectNone.class        ),
43
    ROTATIONS    (ScrambleEffectRotations.class   ),
44
    ;
45

    
46
    final Class<? extends ScrambleEffect> effect;
47

    
48
    Type(Class<? extends ScrambleEffect> effect)
49
      {
50
      this.effect= effect;
51
      }
52
    }
53

    
54
  private static final int NUM_EFFECTS = Type.values().length;
55
  private static final int FAKE_EFFECT_ID  = -3;
56
  private static final Type[] types;
57

    
58
  static
59
    {
60
    int i=0;
61
    types = new Type[NUM_EFFECTS];
62

    
63
    for(Type type: Type.values())
64
      {
65
      types[i++] = type;
66
      }
67
    }
68

    
69
  private EffectController mController;
70
  private int mEffectReturned;
71
  private int mNumScramblesLeft;
72
  private long mDurationPerDegree;
73
  private final Random mRnd;
74
  private int[] mBasicAngle;
75
  private boolean mRotReady, mPluginReady;
76

    
77
  TwistyObject mObject;
78
  Effect[] mNodeEffects;
79
  int[] mNodeEffectPosition;
80
  Effect[] mCubeEffects;
81
  int[] mCubeEffectPosition;
82
  int mCubeEffectNumber, mNodeEffectNumber;
83
  int mNumScrambles;
84
  int[][] mScrambles;
85

    
86
///////////////////////////////////////////////////////////////////////////////////////////////////
87

    
88
  ScrambleEffect()
89
    {
90
    mRnd = new Random( System.currentTimeMillis() );
91
    mScrambles = new int[ObjectList.MAX_SCRAMBLE][3];
92
    }
93

    
94
///////////////////////////////////////////////////////////////////////////////////////////////////
95

    
96
  abstract void createEffects(int duration, int numScrambles);
97
  abstract void effectFinishedPlugin(final long effectID);
98

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

    
101
  private void createBaseEffects(int duration, int numScrambles)
102
    {
103
    mNumScramblesLeft = numScrambles;
104
    int absAngle, angle, axis, basicDegrees, totalDegrees = 0;
105

    
106
    for(int scramble=0; scramble<mNumScramblesLeft; scramble++)
107
      {
108
      mObject.randomizeNewScramble(mScrambles, mRnd, scramble, numScrambles);
109
      axis  = mScrambles[scramble][0];
110
      angle = mScrambles[scramble][2];
111
      absAngle = (angle<0 ? -angle : angle);
112
      basicDegrees = 360/mBasicAngle[axis];
113
      totalDegrees += absAngle*basicDegrees;
114
      }
115

    
116
    mDurationPerDegree = duration/totalDegrees;
117
    mNumScrambles = 0;
118

    
119
    mRotReady    = false;
120
    mPluginReady = false;
121

    
122
    addNewScramble();
123
    }
124

    
125
///////////////////////////////////////////////////////////////////////////////////////////////////
126

    
127
  private void addNewScramble()
128
    {
129
    if( mNumScramblesLeft>0 )
130
      {
131
      int axis = mScrambles[mNumScrambles][0];
132
      int row  = mScrambles[mNumScrambles][1];
133
      int angle= mScrambles[mNumScrambles][2];
134

    
135
      int rowBitmap= (1<<row);
136
      int absAngle = (angle<0 ? -angle : angle);
137
      int basicDegrees  = 360/mBasicAngle[axis];
138
      long durationMillis = absAngle*basicDegrees*mDurationPerDegree;
139

    
140
      mNumScramblesLeft--;
141
      mController.addRotation(this, axis, rowBitmap, angle*basicDegrees, durationMillis);
142
      mNumScrambles++;
143
      }
144
    else
145
      {
146
      mRotReady = true;
147
      if( mPluginReady ) mController.effectFinished(FAKE_EFFECT_ID);
148
      }
149
    }
150

    
151
///////////////////////////////////////////////////////////////////////////////////////////////////
152

    
153
  private void assignEffects()
154
    {
155
    for(int i=0; i<mCubeEffectNumber; i++)
156
      {
157
      mObject.apply(mCubeEffects[i],mCubeEffectPosition[i]);
158
      mCubeEffects[i].notifyWhenFinished(this);
159
      }
160

    
161
    DistortedEffects nodeEffects = mObject.getEffects();
162

    
163
    for(int i=0; i<mNodeEffectNumber; i++)
164
      {
165
      nodeEffects.apply(mNodeEffects[i],mNodeEffectPosition[i]);
166
      mNodeEffects[i].notifyWhenFinished(this);
167
      }
168
    }
169

    
170
///////////////////////////////////////////////////////////////////////////////////////////////////
171

    
172
  private void disassignEffects()
173
    {
174
    for(int i=0; i<mCubeEffectNumber; i++)
175
      {
176
      mObject.remove(mCubeEffects[i].getID());
177
      }
178

    
179
    DistortedEffects nodeEffects = mObject.getEffects();
180

    
181
    for(int i=0; i<mNodeEffectNumber; i++)
182
      {
183
      nodeEffects.abortById(mNodeEffects[i].getID());
184
      }
185
    }
186

    
187
///////////////////////////////////////////////////////////////////////////////////////////////////
188
// PUBLIC API
189
///////////////////////////////////////////////////////////////////////////////////////////////////
190

    
191
  @SuppressWarnings("unused")
192
  public static String[] getNames()
193
    {
194
    String[] names = new String[NUM_EFFECTS];
195

    
196
    for( int i=0; i<NUM_EFFECTS; i++)
197
      {
198
      names[i] = types[i].name();
199
      }
200

    
201
    return names;
202
    }
203

    
204
///////////////////////////////////////////////////////////////////////////////////////////////////
205

    
206
  @SuppressWarnings("unused")
207
  public static ScrambleEffect create(int ordinal) throws InstantiationException, IllegalAccessException
208
    {
209
    return types[ordinal].effect.newInstance();
210
    }
211

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

    
214
  public void onActionFinished(final long effectID)
215
    {
216
    addNewScramble();
217
    }
218

    
219
///////////////////////////////////////////////////////////////////////////////////////////////////
220

    
221
  private void effectFinishedAction(final long effectID, final long id)
222
    {
223
    mEffectReturned++;
224
    effectFinishedPlugin(effectID);
225

    
226
    if( mEffectReturned == mCubeEffectNumber+mNodeEffectNumber )
227
      {
228
      disassignEffects();
229

    
230
      mPluginReady = true;
231
      if( mRotReady ) mController.effectFinished(FAKE_EFFECT_ID);
232
      }
233
    }
234

    
235
///////////////////////////////////////////////////////////////////////////////////////////////////
236

    
237
  public void effectFinished(final long effectID)
238
    {
239
    for(int i=0; i<mCubeEffectNumber; i++)
240
      {
241
      long id = mCubeEffects[i].getID();
242

    
243
      if( effectID == id )
244
        {
245
        effectFinishedAction(effectID,id);
246
        return;
247
        }
248
      }
249

    
250
    for(int i=0; i<mNodeEffectNumber; i++)
251
      {
252
      long id = mNodeEffects[i].getID();
253

    
254
      if( effectID == id )
255
        {
256
        effectFinishedAction(effectID,id);
257
        return;
258
        }
259
      }
260
    }
261

    
262
///////////////////////////////////////////////////////////////////////////////////////////////////
263

    
264
  @SuppressWarnings("unused")
265
  public long start(int duration, DistortedScreen screen, EffectController cont)
266
    {
267
    mObject    = cont.getObject();
268
    mController= cont;
269

    
270
    mObject.solve();
271

    
272
    mBasicAngle = mObject.getBasicAngle();
273

    
274
    int numScrambles = cont.getNumScrambles();
275
    int dura = (int)(duration*Math.pow(numScrambles,0.66f));
276
    createBaseEffects(dura,numScrambles);
277
    createEffects    (dura,numScrambles);
278

    
279
    if( mCubeEffectNumber==0 && mNodeEffectNumber==0 )
280
      {
281
      throw new RuntimeException("Cube and Node Plugin Effects not created!");
282
      }
283

    
284
    assignEffects();
285

    
286
    return FAKE_EFFECT_ID;
287
    }
288

    
289
///////////////////////////////////////////////////////////////////////////////////////////////////
290

    
291
  @SuppressWarnings("unused")
292
  public static void enableEffects()
293
    {
294
    Method method;
295

    
296
    for(Type type: Type.values())
297
      {
298
      try
299
        {
300
        method = type.effect.getDeclaredMethod("enable"); // enable not public, thus getDeclaredMethod
301
        }
302
      catch(NoSuchMethodException ex)
303
        {
304
        android.util.Log.e("ScrambleEffect", type.effect.getSimpleName()+": exception getting method: "+ex.getMessage());
305
        method = null;
306
        }
307

    
308
      try
309
        {
310
        if( method!=null ) method.invoke(null);
311
        }
312
      catch(Exception ex)
313
        {
314
        android.util.Log.e("ScrambleEffect", type.effect.getSimpleName()+": exception invoking method: "+ex.getMessage());
315
        }
316
      }
317
    }
318
}
(1-1/3)