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 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.main.DistortedScreen;
|
28
|
import org.distorted.library.message.EffectListener;
|
29
|
|
30
|
import org.distorted.objectlib.main.ObjectType;
|
31
|
import org.distorted.objectlib.main.TwistyObject;
|
32
|
|
33
|
import org.distorted.effects.BaseEffect;
|
34
|
import org.distorted.effects.EffectController;
|
35
|
import org.distorted.objectlib.helpers.MovesFinished;
|
36
|
|
37
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
38
|
|
39
|
public abstract class ScrambleEffect extends BaseEffect implements EffectListener, MovesFinished
|
40
|
{
|
41
|
public enum Type
|
42
|
{
|
43
|
NONE (ScrambleEffectNone.class ),
|
44
|
ROTATIONS (ScrambleEffectRotations.class ),
|
45
|
;
|
46
|
|
47
|
final Class<? extends ScrambleEffect> effect;
|
48
|
|
49
|
Type(Class<? extends ScrambleEffect> effect)
|
50
|
{
|
51
|
this.effect= effect;
|
52
|
}
|
53
|
}
|
54
|
|
55
|
private static final int NUM_EFFECTS = Type.values().length;
|
56
|
private static final int FAKE_EFFECT_ID = -3;
|
57
|
private static final Type[] types;
|
58
|
|
59
|
static
|
60
|
{
|
61
|
int i=0;
|
62
|
types = new Type[NUM_EFFECTS];
|
63
|
|
64
|
for(Type type: Type.values())
|
65
|
{
|
66
|
types[i++] = type;
|
67
|
}
|
68
|
}
|
69
|
|
70
|
private EffectController mController;
|
71
|
private int mEffectReturned;
|
72
|
private int mNumScramblesLeft;
|
73
|
private long mDurationPerDegree;
|
74
|
private final Random mRnd;
|
75
|
private int[] mBasicAngle;
|
76
|
private boolean mRotReady, mPluginReady;
|
77
|
|
78
|
TwistyObject mObject;
|
79
|
Effect[] mNodeEffects;
|
80
|
int[] mNodeEffectPosition;
|
81
|
Effect[] mCubeEffects;
|
82
|
int[] mCubeEffectPosition;
|
83
|
int mCubeEffectNumber, mNodeEffectNumber;
|
84
|
int mNumScrambles;
|
85
|
int[][] mScrambles;
|
86
|
|
87
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
88
|
|
89
|
ScrambleEffect()
|
90
|
{
|
91
|
mRnd = new Random( System.currentTimeMillis() );
|
92
|
mScrambles = new int[ObjectType.MAX_SCRAMBLE][3];
|
93
|
}
|
94
|
|
95
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
96
|
|
97
|
abstract void createEffects(int duration, int numScrambles);
|
98
|
abstract void effectFinishedPlugin(final long effectID);
|
99
|
|
100
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
101
|
|
102
|
private void createBaseEffects(int duration, int numScrambles)
|
103
|
{
|
104
|
mNumScramblesLeft = numScrambles;
|
105
|
int absAngle, angle, axis, basicDegrees, totalDegrees = 0;
|
106
|
|
107
|
for(int scramble=0; scramble<mNumScramblesLeft; scramble++)
|
108
|
{
|
109
|
mObject.randomizeNewScramble(mScrambles, mRnd, scramble, numScrambles);
|
110
|
axis = mScrambles[scramble][0];
|
111
|
angle = mScrambles[scramble][2];
|
112
|
absAngle = (angle<0 ? -angle : angle);
|
113
|
basicDegrees = 360/mBasicAngle[axis];
|
114
|
totalDegrees += absAngle*basicDegrees;
|
115
|
}
|
116
|
|
117
|
mDurationPerDegree = duration/totalDegrees;
|
118
|
mNumScrambles = 0;
|
119
|
|
120
|
mRotReady = false;
|
121
|
mPluginReady = false;
|
122
|
|
123
|
addNewScramble();
|
124
|
}
|
125
|
|
126
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
127
|
|
128
|
private void addNewScramble()
|
129
|
{
|
130
|
if( mNumScramblesLeft>0 )
|
131
|
{
|
132
|
int axis = mScrambles[mNumScrambles][0];
|
133
|
int row = mScrambles[mNumScrambles][1];
|
134
|
int angle= mScrambles[mNumScrambles][2];
|
135
|
|
136
|
int rowBitmap= (1<<row);
|
137
|
int absAngle = (angle<0 ? -angle : angle);
|
138
|
int basicDegrees = 360/mBasicAngle[axis];
|
139
|
long durationMillis = absAngle*basicDegrees*mDurationPerDegree;
|
140
|
|
141
|
mNumScramblesLeft--;
|
142
|
mController.addRotation(this, axis, rowBitmap, angle*basicDegrees, durationMillis);
|
143
|
mNumScrambles++;
|
144
|
}
|
145
|
else
|
146
|
{
|
147
|
mRotReady = true;
|
148
|
if( mPluginReady ) mController.effectFinished(FAKE_EFFECT_ID);
|
149
|
}
|
150
|
}
|
151
|
|
152
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
153
|
|
154
|
private void assignEffects()
|
155
|
{
|
156
|
for(int i=0; i<mCubeEffectNumber; i++)
|
157
|
{
|
158
|
mObject.apply(mCubeEffects[i],mCubeEffectPosition[i]);
|
159
|
mCubeEffects[i].notifyWhenFinished(this);
|
160
|
}
|
161
|
|
162
|
DistortedEffects nodeEffects = mObject.getEffects();
|
163
|
|
164
|
for(int i=0; i<mNodeEffectNumber; i++)
|
165
|
{
|
166
|
nodeEffects.apply(mNodeEffects[i],mNodeEffectPosition[i]);
|
167
|
mNodeEffects[i].notifyWhenFinished(this);
|
168
|
}
|
169
|
}
|
170
|
|
171
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
172
|
|
173
|
private void disassignEffects()
|
174
|
{
|
175
|
for(int i=0; i<mCubeEffectNumber; i++)
|
176
|
{
|
177
|
mObject.remove(mCubeEffects[i].getID());
|
178
|
}
|
179
|
|
180
|
DistortedEffects nodeEffects = mObject.getEffects();
|
181
|
|
182
|
for(int i=0; i<mNodeEffectNumber; i++)
|
183
|
{
|
184
|
nodeEffects.abortById(mNodeEffects[i].getID());
|
185
|
}
|
186
|
}
|
187
|
|
188
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
189
|
// PUBLIC API
|
190
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
191
|
|
192
|
@SuppressWarnings("unused")
|
193
|
public static String[] getNames()
|
194
|
{
|
195
|
String[] names = new String[NUM_EFFECTS];
|
196
|
|
197
|
for( int i=0; i<NUM_EFFECTS; i++)
|
198
|
{
|
199
|
names[i] = types[i].name();
|
200
|
}
|
201
|
|
202
|
return names;
|
203
|
}
|
204
|
|
205
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
206
|
|
207
|
@SuppressWarnings("unused")
|
208
|
public static ScrambleEffect create(int ordinal) throws InstantiationException, IllegalAccessException
|
209
|
{
|
210
|
return types[ordinal].effect.newInstance();
|
211
|
}
|
212
|
|
213
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
214
|
|
215
|
public void onActionFinished(final long effectID)
|
216
|
{
|
217
|
addNewScramble();
|
218
|
}
|
219
|
|
220
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
221
|
|
222
|
private void effectFinishedAction(final long effectID, final long id)
|
223
|
{
|
224
|
mEffectReturned++;
|
225
|
effectFinishedPlugin(effectID);
|
226
|
|
227
|
if( mEffectReturned == mCubeEffectNumber+mNodeEffectNumber )
|
228
|
{
|
229
|
disassignEffects();
|
230
|
|
231
|
mPluginReady = true;
|
232
|
if( mRotReady ) mController.effectFinished(FAKE_EFFECT_ID);
|
233
|
}
|
234
|
}
|
235
|
|
236
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
237
|
|
238
|
public void effectFinished(final long effectID)
|
239
|
{
|
240
|
for(int i=0; i<mCubeEffectNumber; i++)
|
241
|
{
|
242
|
long id = mCubeEffects[i].getID();
|
243
|
|
244
|
if( effectID == id )
|
245
|
{
|
246
|
effectFinishedAction(effectID,id);
|
247
|
return;
|
248
|
}
|
249
|
}
|
250
|
|
251
|
for(int i=0; i<mNodeEffectNumber; i++)
|
252
|
{
|
253
|
long id = mNodeEffects[i].getID();
|
254
|
|
255
|
if( effectID == id )
|
256
|
{
|
257
|
effectFinishedAction(effectID,id);
|
258
|
return;
|
259
|
}
|
260
|
}
|
261
|
}
|
262
|
|
263
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
264
|
|
265
|
@SuppressWarnings("unused")
|
266
|
public long start(int duration, DistortedScreen screen, EffectController cont)
|
267
|
{
|
268
|
mObject = cont.getObject();
|
269
|
mController= cont;
|
270
|
|
271
|
// NOT mController.solve() !! This would be a very subtle bug. We need to do this immediately,
|
272
|
// because here we are already inside the mController.preRender() function (doing 'scrambleObjectNow')
|
273
|
// and doing a delayed 'solve()' here would mean we'd be sometimes first doing the first rotation,
|
274
|
// and only after it - the solve.
|
275
|
mObject.solve();
|
276
|
|
277
|
mBasicAngle = mObject.getBasicAngle();
|
278
|
|
279
|
int numScrambles = cont.getNumScrambles();
|
280
|
int dura = (int)(duration*Math.pow(numScrambles,0.66f));
|
281
|
createBaseEffects(dura,numScrambles);
|
282
|
createEffects (dura,numScrambles);
|
283
|
|
284
|
if( mCubeEffectNumber==0 && mNodeEffectNumber==0 )
|
285
|
{
|
286
|
throw new RuntimeException("Cube and Node Plugin Effects not created!");
|
287
|
}
|
288
|
|
289
|
assignEffects();
|
290
|
|
291
|
return FAKE_EFFECT_ID;
|
292
|
}
|
293
|
|
294
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
295
|
|
296
|
@SuppressWarnings("unused")
|
297
|
public static void enableEffects()
|
298
|
{
|
299
|
Method method;
|
300
|
|
301
|
for(Type type: Type.values())
|
302
|
{
|
303
|
try
|
304
|
{
|
305
|
method = type.effect.getDeclaredMethod("enable"); // enable not public, thus getDeclaredMethod
|
306
|
}
|
307
|
catch(NoSuchMethodException ex)
|
308
|
{
|
309
|
android.util.Log.e("ScrambleEffect", type.effect.getSimpleName()+": exception getting method: "+ex.getMessage());
|
310
|
method = null;
|
311
|
}
|
312
|
|
313
|
try
|
314
|
{
|
315
|
if( method!=null ) method.invoke(null);
|
316
|
}
|
317
|
catch(Exception ex)
|
318
|
{
|
319
|
android.util.Log.e("ScrambleEffect", type.effect.getSimpleName()+": exception invoking method: "+ex.getMessage());
|
320
|
}
|
321
|
}
|
322
|
}
|
323
|
}
|