Project

General

Profile

Download (10 KB) Statistics
| Branch: | Revision:

distorted-objectlib / src / main / java / org / distorted / objectlib / effects / scramble / ScrambleEffect.java @ 5cc61ae4

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

    
22
import java.lang.reflect.Method;
23
import java.util.Random;
24

    
25
import org.distorted.library.effect.Effect;
26
import org.distorted.library.main.DistortedEffects;
27
import org.distorted.library.message.EffectListener;
28

    
29
import org.distorted.objectlib.main.ObjectPreRender;
30
import org.distorted.objectlib.main.TwistyObject;
31
import org.distorted.objectlib.effects.BaseEffect;
32
import org.distorted.objectlib.helpers.MovesFinished;
33
import org.distorted.objectlib.main.TwistyObjectNode;
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 int mEffectReturned;
69
  private int mNumScramblesLeft;
70
  private int mDurationPerDegree;
71
  private final Random mRnd;
72
  private int[] mBasicAngle;
73
  private boolean mRotReady, mPluginReady;
74

    
75
  ObjectPreRender mPre;
76
  TwistyObject mObject;
77
  TwistyObjectNode mObjectNode;
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
    }
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
      mNumScramblesLeft--;
135
      mPre.addRotation(this, axis, (1<<row), angle, mDurationPerDegree);
136
      mNumScrambles++;
137
      }
138
    else
139
      {
140
      mRotReady = true;
141
      if( mPluginReady ) mPre.effectFinished(FAKE_EFFECT_ID);
142
      }
143
    }
144

    
145
///////////////////////////////////////////////////////////////////////////////////////////////////
146

    
147
  private void assignEffects()
148
    {
149
    for(int i=0; i<mCubeEffectNumber; i++)
150
      {
151
      mObject.apply(mCubeEffects[i],mCubeEffectPosition[i]);
152
      mCubeEffects[i].notifyWhenFinished(this);
153
      }
154

    
155
    DistortedEffects nodeEffects = mObjectNode.getEffects();
156

    
157
    for(int i=0; i<mNodeEffectNumber; i++)
158
      {
159
      nodeEffects.apply(mNodeEffects[i],mNodeEffectPosition[i]);
160
      mNodeEffects[i].notifyWhenFinished(this);
161
      }
162
    }
163

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

    
166
  private void disassignEffects()
167
    {
168
    for(int i=0; i<mCubeEffectNumber; i++)
169
      {
170
      mObject.remove(mCubeEffects[i].getID());
171
      }
172

    
173
    DistortedEffects nodeEffects = mObjectNode.getEffects();
174

    
175
    for(int i=0; i<mNodeEffectNumber; i++)
176
      {
177
      nodeEffects.abortById(mNodeEffects[i].getID());
178
      }
179
    }
180

    
181
///////////////////////////////////////////////////////////////////////////////////////////////////
182
// PUBLIC API
183
///////////////////////////////////////////////////////////////////////////////////////////////////
184

    
185
  @SuppressWarnings("unused")
186
  public static String[] getNames()
187
    {
188
    String[] names = new String[NUM_EFFECTS];
189

    
190
    for( int i=0; i<NUM_EFFECTS; i++)
191
      {
192
      names[i] = types[i].name();
193
      }
194

    
195
    return names;
196
    }
197

    
198
///////////////////////////////////////////////////////////////////////////////////////////////////
199

    
200
  @SuppressWarnings("unused")
201
  public static ScrambleEffect create(int ordinal) throws InstantiationException, IllegalAccessException
202
    {
203
    return types[ordinal].effect.newInstance();
204
    }
205

    
206
///////////////////////////////////////////////////////////////////////////////////////////////////
207

    
208
  public void onActionFinished(final long effectID)
209
    {
210
    addNewScramble();
211
    }
212

    
213
///////////////////////////////////////////////////////////////////////////////////////////////////
214

    
215
  private void effectFinishedAction(final long effectID)
216
    {
217
    mEffectReturned++;
218
    effectFinishedPlugin(effectID);
219

    
220
    if( mEffectReturned == mCubeEffectNumber+mNodeEffectNumber )
221
      {
222
      disassignEffects();
223

    
224
      mPluginReady = true;
225
      if( mRotReady ) mPre.effectFinished(FAKE_EFFECT_ID);
226
      }
227
    }
228

    
229
///////////////////////////////////////////////////////////////////////////////////////////////////
230

    
231
  public void effectFinished(final long effectID)
232
    {
233
    for(int i=0; i<mCubeEffectNumber; i++)
234
      {
235
      long id = mCubeEffects[i].getID();
236

    
237
      if( effectID == id )
238
        {
239
        effectFinishedAction(effectID);
240
        return;
241
        }
242
      }
243

    
244
    for(int i=0; i<mNodeEffectNumber; i++)
245
      {
246
      long id = mNodeEffects[i].getID();
247

    
248
      if( effectID == id )
249
        {
250
        effectFinishedAction(effectID);
251
        return;
252
        }
253
      }
254
    }
255

    
256
///////////////////////////////////////////////////////////////////////////////////////////////////
257

    
258
  @SuppressWarnings("unused")
259
  public long start(int duration, ObjectPreRender pre)
260
    {
261
    mObject     = pre.getObject();
262
    mObjectNode = pre.getObjectNode();
263
    mPre        = pre;
264

    
265
    // NOT mController.solve() !! This would be a very subtle bug. We need to do this immediately,
266
    // because here we are already inside the mController.preRender() function (doing 'scrambleObjectNow')
267
    // and doing a delayed 'solve()' here would mean we'd be sometimes first doing the first rotation,
268
    // and only after it - the solve.
269
    mObject.solve();
270

    
271
    mBasicAngle = mObject.getBasicAngle();
272

    
273
    int numScrambles = pre.getNumScrambles();
274
    mScrambles = new int[numScrambles][3];
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)