1
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2
|
// Copyright 2019 Leszek Koltunski //
|
3
|
// //
|
4
|
// This file is part of Distorted. //
|
5
|
// //
|
6
|
// Distorted 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
|
// Distorted 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 Distorted. If not, see <http://www.gnu.org/licenses/>. //
|
18
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
19
|
|
20
|
package org.distorted.effect;
|
21
|
|
22
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
23
|
|
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.magic.RubikCube;
|
29
|
|
30
|
import java.lang.reflect.Method;
|
31
|
|
32
|
public abstract class SolveEffect implements EffectListener
|
33
|
{
|
34
|
public enum Type
|
35
|
{
|
36
|
NONE (SolveEffectNone.class),
|
37
|
SPIN (SolveEffectSpin.class),
|
38
|
;
|
39
|
|
40
|
final Class<? extends SolveEffect> effect;
|
41
|
|
42
|
Type(Class<? extends SolveEffect> effect)
|
43
|
{
|
44
|
this.effect = effect;
|
45
|
}
|
46
|
}
|
47
|
|
48
|
private static final int NUM_EFFECTS = Type.values().length;
|
49
|
private static final int NUM_PHASES = 2;
|
50
|
private static final int FAKE_EFFECT_ID = -2;
|
51
|
private static final Type[] types;
|
52
|
|
53
|
static
|
54
|
{
|
55
|
int i=0;
|
56
|
types = new Type[NUM_EFFECTS];
|
57
|
|
58
|
for(Type type: Type.values())
|
59
|
{
|
60
|
types[i++] = type;
|
61
|
}
|
62
|
}
|
63
|
|
64
|
private EffectListener mListener;
|
65
|
private int mDuration;
|
66
|
private int mEffectReturned;
|
67
|
private int[] mCubeEffectNumber, mNodeEffectNumber;
|
68
|
private int mPhase;
|
69
|
|
70
|
RubikCube mCube;
|
71
|
DistortedScreen mScreen;
|
72
|
Effect[][] mCubeEffects;
|
73
|
int[][] mCubeEffectPosition;
|
74
|
Effect[][] mNodeEffects;
|
75
|
int[][] mNodeEffectPosition;
|
76
|
|
77
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
78
|
|
79
|
SolveEffect()
|
80
|
{
|
81
|
mPhase = 0;
|
82
|
|
83
|
mCubeEffectNumber = new int[NUM_PHASES];
|
84
|
mNodeEffectNumber = new int[NUM_PHASES];
|
85
|
mCubeEffectPosition = new int[NUM_PHASES][];
|
86
|
mNodeEffectPosition = new int[NUM_PHASES][];
|
87
|
mCubeEffects = new Effect[NUM_PHASES][];
|
88
|
mNodeEffects = new Effect[NUM_PHASES][];
|
89
|
}
|
90
|
|
91
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
92
|
|
93
|
public static String[] getNames()
|
94
|
{
|
95
|
String[] names = new String[NUM_EFFECTS];
|
96
|
|
97
|
for( int i=0; i<NUM_EFFECTS; i++)
|
98
|
{
|
99
|
names[i] = types[i].name();
|
100
|
}
|
101
|
|
102
|
return names;
|
103
|
}
|
104
|
|
105
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
106
|
|
107
|
public static SolveEffect create(int ordinal) throws InstantiationException, IllegalAccessException
|
108
|
{
|
109
|
return types[ordinal].effect.newInstance();
|
110
|
}
|
111
|
|
112
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
113
|
|
114
|
abstract void createEffectsPhase0(int duration);
|
115
|
abstract void createEffectsPhase1(int duration);
|
116
|
|
117
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
118
|
|
119
|
public void effectFinished(final long effectID)
|
120
|
{
|
121
|
int total = mCubeEffectNumber[mPhase]+mNodeEffectNumber[mPhase];
|
122
|
|
123
|
for(int i=0; i<mCubeEffectNumber[mPhase]; i++)
|
124
|
{
|
125
|
long id = mCubeEffects[mPhase][i].getID();
|
126
|
|
127
|
if( effectID == id )
|
128
|
{
|
129
|
if( ++mEffectReturned == total ) effectAction(mPhase);
|
130
|
mCube.remove(id);
|
131
|
return;
|
132
|
}
|
133
|
}
|
134
|
for(int i=0; i<mNodeEffectNumber[mPhase]; i++)
|
135
|
{
|
136
|
long id = mNodeEffects[mPhase][i].getID();
|
137
|
|
138
|
if( effectID == id )
|
139
|
{
|
140
|
if( ++mEffectReturned == total ) effectAction(mPhase);
|
141
|
mCube.getEffects().abortById(id);
|
142
|
return;
|
143
|
}
|
144
|
}
|
145
|
}
|
146
|
|
147
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
148
|
|
149
|
private void effectAction(int phase)
|
150
|
{
|
151
|
switch(phase)
|
152
|
{
|
153
|
case 0: mEffectReturned = 0;
|
154
|
mPhase = 1;
|
155
|
mCube.solve();
|
156
|
createEffectsPhase1(mDuration);
|
157
|
assignEffects(mPhase);
|
158
|
break;
|
159
|
case 1: mListener.effectFinished(FAKE_EFFECT_ID);
|
160
|
break;
|
161
|
}
|
162
|
}
|
163
|
|
164
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
165
|
|
166
|
public long start(int duration, DistortedScreen screen, RubikCube cube, EffectListener listener)
|
167
|
{
|
168
|
mScreen = screen;
|
169
|
mCube = cube;
|
170
|
mListener = listener;
|
171
|
mDuration = duration;
|
172
|
|
173
|
createEffectsPhase0(mDuration);
|
174
|
assignEffects(mPhase);
|
175
|
|
176
|
return FAKE_EFFECT_ID;
|
177
|
}
|
178
|
|
179
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
180
|
|
181
|
private void assignEffects(int phase)
|
182
|
{
|
183
|
mCubeEffectNumber[phase] = ( mCubeEffects[phase]!=null ) ? mCubeEffects[phase].length : 0;
|
184
|
mNodeEffectNumber[phase] = ( mNodeEffects[phase]!=null ) ? mNodeEffects[phase].length : 0;
|
185
|
|
186
|
if( mCubeEffectNumber[phase]==0 && mNodeEffectNumber[phase]==0 )
|
187
|
{
|
188
|
throw new RuntimeException("Cube and Node Effects ("+phase+" phase) both not created!");
|
189
|
}
|
190
|
|
191
|
for(int i=0; i<mCubeEffectNumber[phase]; i++)
|
192
|
{
|
193
|
mCube.apply(mCubeEffects[phase][i],mCubeEffectPosition[phase][i]);
|
194
|
mCubeEffects[phase][i].notifyWhenFinished(this);
|
195
|
}
|
196
|
|
197
|
DistortedEffects nodeEffects = mCube.getEffects();
|
198
|
|
199
|
for(int i=0; i<mNodeEffectNumber[phase]; i++)
|
200
|
{
|
201
|
nodeEffects.apply(mNodeEffects[phase][i],mNodeEffectPosition[phase][i]);
|
202
|
mNodeEffects[phase][i].notifyWhenFinished(this);
|
203
|
}
|
204
|
}
|
205
|
|
206
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
207
|
|
208
|
@SuppressWarnings("unused")
|
209
|
public static void enableEffects()
|
210
|
{
|
211
|
Method method;
|
212
|
|
213
|
for(Type type: Type.values())
|
214
|
{
|
215
|
try
|
216
|
{
|
217
|
method = type.effect.getDeclaredMethod("enable"); // enable not public, thus getDeclaredMethod
|
218
|
}
|
219
|
catch(NoSuchMethodException ex)
|
220
|
{
|
221
|
android.util.Log.e("SolveEffect", type.effect.getSimpleName()+": exception getting method: "+ex.getMessage());
|
222
|
method = null;
|
223
|
}
|
224
|
|
225
|
try
|
226
|
{
|
227
|
if( method!=null ) method.invoke(null);
|
228
|
}
|
229
|
catch(Exception ex)
|
230
|
{
|
231
|
android.util.Log.e("SolveEffect", type.effect.getSimpleName()+": exception invoking method: "+ex.getMessage());
|
232
|
}
|
233
|
}
|
234
|
}
|
235
|
}
|