Project

General

Profile

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

magiccube / src / main / java / org / distorted / effects / scramble / ScrambleEffect.java @ 771f6dfa

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.effects.EffectController;
29
import org.distorted.objects.ObjectList;
30
import org.distorted.objects.TwistyObject;
31

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

    
35
///////////////////////////////////////////////////////////////////////////////////////////////////
36

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

    
45
    final Class<? extends ScrambleEffect> effect;
46

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

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

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

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

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

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

    
85
///////////////////////////////////////////////////////////////////////////////////////////////////
86

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

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

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

    
98
///////////////////////////////////////////////////////////////////////////////////////////////////
99

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

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

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

    
118
    mRotReady    = false;
119
    mPluginReady = false;
120

    
121
    addNewScramble();
122
    }
123

    
124
///////////////////////////////////////////////////////////////////////////////////////////////////
125

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

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

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

    
150
///////////////////////////////////////////////////////////////////////////////////////////////////
151

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

    
160
    DistortedEffects nodeEffects = mObject.getEffects();
161

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

    
169
///////////////////////////////////////////////////////////////////////////////////////////////////
170

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

    
178
    DistortedEffects nodeEffects = mObject.getEffects();
179

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

    
186
///////////////////////////////////////////////////////////////////////////////////////////////////
187
// PUBLIC API
188
///////////////////////////////////////////////////////////////////////////////////////////////////
189

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

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

    
200
    return names;
201
    }
202

    
203
///////////////////////////////////////////////////////////////////////////////////////////////////
204

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

    
211
///////////////////////////////////////////////////////////////////////////////////////////////////
212

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

    
218
///////////////////////////////////////////////////////////////////////////////////////////////////
219

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

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

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

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

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

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

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

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

    
261
///////////////////////////////////////////////////////////////////////////////////////////////////
262

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

    
269
    mController.solve();
270

    
271
    mBasicAngle = mObject.getBasicAngle();
272

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

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

    
283
    assignEffects();
284

    
285
    return FAKE_EFFECT_ID;
286
    }
287

    
288
///////////////////////////////////////////////////////////////////////////////////////////////////
289

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

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

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