Project

General

Profile

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

magiccube / src / main / java / org / distorted / effect / scramble / ScrambleEffect.java @ efef689c

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.effect.scramble;
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.message.EffectListener;
26
import org.distorted.magic.RubikRenderer;
27
import org.distorted.object.RubikObject;
28

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

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

    
34
public abstract class ScrambleEffect extends BaseEffect implements EffectListener
35
{
36
  public enum Type
37
    {
38
    NONE         (ScrambleEffectNone.class        ),
39
    ROTATIONS    (ScrambleEffectRotations.class   ),
40
    ;
41

    
42
    final Class<? extends ScrambleEffect> effect;
43

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

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

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

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

    
65
  private EffectListener mListener;
66
  private int mEffectReturned;
67
  private long mCurrentBaseEffectID;
68
  private int mNumDoubleScramblesLeft, mNumScramblesLeft;
69
  private int mLastVector;
70
  private long mDurationSingleTurn;
71
  private Random mRnd;
72
  private RubikObject mObject;
73

    
74
  Effect[] mNodeEffects;
75
  int[] mNodeEffectPosition;
76
  Effect[] mCubeEffects;
77
  int[] mCubeEffectPosition;
78
  int mCubeEffectNumber, mNodeEffectNumber;
79

    
80
///////////////////////////////////////////////////////////////////////////////////////////////////
81

    
82
  ScrambleEffect()
83
    {
84
    mRnd = new Random( System.currentTimeMillis() );
85
    mLastVector = 3;
86
    }
87

    
88
///////////////////////////////////////////////////////////////////////////////////////////////////
89

    
90
  abstract void createEffects(int duration, int numScrambles);
91
  abstract void effectFinishedPlugin(final long effectID);
92

    
93
///////////////////////////////////////////////////////////////////////////////////////////////////
94
// first compute how many out of 'numScrambles' are double turns (this will matter when we compute
95
// the time a single quarter-turn takes!
96

    
97
  private void createBaseEffects(int duration, int numScrambles)
98
    {
99
    mNumScramblesLeft = numScrambles;
100

    
101
    mNumDoubleScramblesLeft=0;
102

    
103
    for(int i=0; i<numScrambles; i++)
104
      {
105
      if( (mRnd.nextInt() % 3) == 0 )
106
        {
107
        mNumDoubleScramblesLeft++;
108
        }
109
      }
110

    
111
    mDurationSingleTurn = duration/(mNumScramblesLeft+mNumDoubleScramblesLeft);
112

    
113
    addNewScramble();
114
    }
115

    
116
///////////////////////////////////////////////////////////////////////////////////////////////////
117

    
118
  private void addNewScramble()
119
    {
120
    if( mNumScramblesLeft>0 )
121
      {
122
      if( mLastVector == 3 )
123
        {
124
        mLastVector = mRnd.nextInt(3);
125
        }
126
      else
127
        {
128
        int newVector = mRnd.nextInt(2);
129

    
130
        switch(mLastVector)
131
          {
132
          case 0: mLastVector = (newVector==0 ? 1:2); break;
133
          case 1: mLastVector = (newVector==0 ? 0:2); break;
134
          case 2: mLastVector = (newVector==0 ? 0:1); break;
135
          }
136
        }
137

    
138
      int row  = mRnd.nextInt(mObject.getSize());
139
      int angle= randomizeAngle();
140
      int absAngle = (angle<0 ? -angle : angle);
141
      long durationMillis =  absAngle*mDurationSingleTurn;
142

    
143
      mNumScramblesLeft--;
144
      if( absAngle==2 ) mNumDoubleScramblesLeft--;
145

    
146
      if( mNumScramblesLeft==0 && mNumDoubleScramblesLeft!=0 )
147
        {
148
        android.util.Log.e("effect", "ERROR: "+mNumDoubleScramblesLeft);
149
        }
150

    
151
      mCurrentBaseEffectID = mObject.addNewRotation(mLastVector, row, angle*90, durationMillis, this );
152
      }
153
    else
154
      {
155
      mLastVector = -1;
156

    
157
      if( mEffectReturned == mCubeEffectNumber+mNodeEffectNumber )
158
        {
159
        mListener.effectFinished(FAKE_EFFECT_ID);
160
        }
161
      }
162
    }
163

    
164
///////////////////////////////////////////////////////////////////////////////////////////////////
165

    
166
  private int randomizeAngle()
167
    {
168
    int random = mRnd.nextInt(mNumScramblesLeft);
169
    int result = random<mNumDoubleScramblesLeft ? 2:1;
170
    int sign   = mRnd.nextInt(2);
171

    
172
    return sign==0 ? result : -result;
173
    }
174

    
175
///////////////////////////////////////////////////////////////////////////////////////////////////
176

    
177
  private void assignEffects()
178
    {
179
    for(int i=0; i<mCubeEffectNumber; i++)
180
      {
181
      mObject.apply(mCubeEffects[i],mCubeEffectPosition[i]);
182
      mCubeEffects[i].notifyWhenFinished(this);
183
      }
184

    
185
    DistortedEffects nodeEffects = mObject.getEffects();
186

    
187
    for(int i=0; i<mNodeEffectNumber; i++)
188
      {
189
      nodeEffects.apply(mNodeEffects[i],mNodeEffectPosition[i]);
190
      mNodeEffects[i].notifyWhenFinished(this);
191
      }
192
    }
193

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

    
196
  private void disassignEffects()
197
    {
198
    for(int i=0; i<mCubeEffectNumber; i++)
199
      {
200
      mObject.remove(mCubeEffects[i].getID());
201
      }
202

    
203
    DistortedEffects nodeEffects = mObject.getEffects();
204

    
205
    for(int i=0; i<mNodeEffectNumber; i++)
206
      {
207
      nodeEffects.abortById(mNodeEffects[i].getID());
208
      }
209
    }
210

    
211
///////////////////////////////////////////////////////////////////////////////////////////////////
212
// PUBLIC API
213
///////////////////////////////////////////////////////////////////////////////////////////////////
214

    
215
  @SuppressWarnings("unused")
216
  public static String[] getNames()
217
    {
218
    String[] names = new String[NUM_EFFECTS];
219

    
220
    for( int i=0; i<NUM_EFFECTS; i++)
221
      {
222
      names[i] = types[i].name();
223
      }
224

    
225
    return names;
226
    }
227

    
228
///////////////////////////////////////////////////////////////////////////////////////////////////
229

    
230
  @SuppressWarnings("unused")
231
  public static ScrambleEffect create(int ordinal) throws InstantiationException, IllegalAccessException
232
    {
233
    return types[ordinal].effect.newInstance();
234
    }
235

    
236
///////////////////////////////////////////////////////////////////////////////////////////////////
237

    
238
  public void effectFinished(final long effectID)
239
    {
240
    if( effectID == mCurrentBaseEffectID )
241
      {
242
      mObject.removeRotationNow();
243
      addNewScramble();
244
      return;
245
      }
246

    
247
    for(int i=0; i<mCubeEffectNumber; i++)
248
      {
249
      long id = mCubeEffects[i].getID();
250

    
251
      if( effectID == id )
252
        {
253
        mEffectReturned++;
254
        effectFinishedPlugin(effectID);
255

    
256
        if( mEffectReturned == mCubeEffectNumber+mNodeEffectNumber )
257
          {
258
          disassignEffects();
259

    
260
          if( mNumScramblesLeft==0 )
261
            {
262
            mListener.effectFinished(FAKE_EFFECT_ID);
263
            }
264
          }
265

    
266
        return;
267
        }
268
      }
269

    
270
    for(int i=0; i<mNodeEffectNumber; i++)
271
      {
272
      long id = mNodeEffects[i].getID();
273

    
274
      if( effectID == id )
275
        {
276
        mEffectReturned++;
277
        effectFinishedPlugin(effectID);
278

    
279
        if( mEffectReturned == mCubeEffectNumber+mNodeEffectNumber )
280
          {
281
          disassignEffects();
282

    
283
          if( mNumScramblesLeft==0 )
284
            {
285
            mListener.effectFinished(FAKE_EFFECT_ID);
286
            }
287
          }
288

    
289
        return;
290
        }
291
      }
292
    }
293

    
294
///////////////////////////////////////////////////////////////////////////////////////////////////
295

    
296
  @SuppressWarnings("unused")
297
  public long start(int duration, RubikRenderer renderer)
298
    {
299
    mObject   = renderer.getObject();
300
    mListener = renderer;
301

    
302
    mObject.solve();
303

    
304
    int numScrambles = renderer.getNumScrambles();
305
    int dura = (int)(duration*Math.pow(numScrambles,0.6f));
306
    createBaseEffects(dura,numScrambles);
307
    createEffects    (dura,numScrambles);
308

    
309
    if( mCubeEffectNumber==0 && mNodeEffectNumber==0 )
310
      {
311
      throw new RuntimeException("Cube and Node Plugin Effects not created!");
312
      }
313

    
314
    assignEffects();
315

    
316
    return FAKE_EFFECT_ID;
317
    }
318

    
319
///////////////////////////////////////////////////////////////////////////////////////////////////
320

    
321
  @SuppressWarnings("unused")
322
  public static void enableEffects()
323
    {
324
    Method method;
325

    
326
    for(Type type: Type.values())
327
      {
328
      try
329
        {
330
        method = type.effect.getDeclaredMethod("enable"); // enable not public, thus getDeclaredMethod
331
        }
332
      catch(NoSuchMethodException ex)
333
        {
334
        android.util.Log.e("ScrambleEffect", type.effect.getSimpleName()+": exception getting method: "+ex.getMessage());
335
        method = null;
336
        }
337

    
338
      try
339
        {
340
        if( method!=null ) method.invoke(null);
341
        }
342
      catch(Exception ex)
343
        {
344
        android.util.Log.e("ScrambleEffect", type.effect.getSimpleName()+": exception invoking method: "+ex.getMessage());
345
        }
346
      }
347
    }
348
}
(1-1/3)