Project

General

Profile

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

magiccube / src / main / java / org / distorted / effects / solve / SolveEffect.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.solve;
21

    
22
import org.distorted.effects.BaseEffect;
23
import org.distorted.library.effect.Effect;
24
import org.distorted.library.main.DistortedEffects;
25
import org.distorted.library.main.DistortedScreen;
26
import org.distorted.library.message.EffectListener;
27
import org.distorted.effects.EffectController;
28
import org.distorted.objects.TwistyObject;
29

    
30
import java.lang.reflect.Method;
31

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

    
34
public abstract class SolveEffect extends BaseEffect implements EffectListener
35
{
36
  public enum Type
37
    {
38
    NONE   (SolveEffectNone.class),
39
    SPIN   (SolveEffectSpin.class),
40
    ;
41

    
42
    final Class<? extends SolveEffect> effect;
43

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

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

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

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

    
66
  private EffectController mController;
67
  private int mDuration;
68
  private int mEffectReturned;
69
  private final int[] mCubeEffectNumber, mNodeEffectNumber;
70
  private int mPhase;
71

    
72
  TwistyObject mObject;
73
  DistortedScreen mScreen;
74
  Effect[][] mCubeEffects;
75
  int[][] mCubeEffectPosition;
76
  Effect[][] mNodeEffects;
77
  int[][] mNodeEffectPosition;
78

    
79
///////////////////////////////////////////////////////////////////////////////////////////////////
80

    
81
  SolveEffect()
82
    {
83
    mPhase= 0;
84

    
85
    mCubeEffectNumber   = new int[NUM_PHASES];
86
    mNodeEffectNumber   = new int[NUM_PHASES];
87
    mCubeEffectPosition = new int[NUM_PHASES][];
88
    mNodeEffectPosition = new int[NUM_PHASES][];
89
    mCubeEffects        = new Effect[NUM_PHASES][];
90
    mNodeEffects        = new Effect[NUM_PHASES][];
91
    }
92

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

    
95
  abstract void createEffectsPhase0(int duration);
96
  abstract void createEffectsPhase1(int duration);
97

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

    
100
  private void assignEffects(int phase)
101
    {
102
    mCubeEffectNumber[phase] = ( mCubeEffects[phase]!=null ) ? mCubeEffects[phase].length : 0;
103
    mNodeEffectNumber[phase] = ( mNodeEffects[phase]!=null ) ? mNodeEffects[phase].length : 0;
104

    
105
    if( mCubeEffectNumber[phase]==0 && mNodeEffectNumber[phase]==0 )
106
      {
107
      throw new RuntimeException("Cube and Node Effects ("+phase+" phase) both not created!");
108
      }
109

    
110
    for(int i=0; i<mCubeEffectNumber[phase]; i++)
111
      {
112
      mObject.apply(mCubeEffects[phase][i],mCubeEffectPosition[phase][i]);
113
      mCubeEffects[phase][i].notifyWhenFinished(this);
114
      }
115

    
116
    DistortedEffects nodeEffects = mObject.getEffects();
117

    
118
    for(int i=0; i<mNodeEffectNumber[phase]; i++)
119
      {
120
      nodeEffects.apply(mNodeEffects[phase][i],mNodeEffectPosition[phase][i]);
121
      mNodeEffects[phase][i].notifyWhenFinished(this);
122
      }
123
    }
124

    
125
///////////////////////////////////////////////////////////////////////////////////////////////////
126

    
127
  private void effectAction(int phase)
128
    {
129
    switch(phase)
130
      {
131
      case 0: mEffectReturned = 0;
132
              mPhase          = 1;
133
              mController.solve();
134
              createEffectsPhase1(mDuration);
135
              assignEffects(mPhase);
136
              break;
137
      case 1: mController.effectFinished(FAKE_EFFECT_ID);
138
              break;
139
      }
140
    }
141

    
142
///////////////////////////////////////////////////////////////////////////////////////////////////
143
// PUBLIC API
144
///////////////////////////////////////////////////////////////////////////////////////////////////
145

    
146
  @SuppressWarnings("unused")
147
  public static String[] getNames()
148
    {
149
    String[] names = new String[NUM_EFFECTS];
150

    
151
    for( int i=0; i<NUM_EFFECTS; i++)
152
      {
153
      names[i] = types[i].name();
154
      }
155

    
156
    return names;
157
    }
158

    
159
///////////////////////////////////////////////////////////////////////////////////////////////////
160

    
161
  @SuppressWarnings("unused")
162
  public static SolveEffect create(int ordinal) throws InstantiationException, IllegalAccessException
163
    {
164
    return types[ordinal].effect.newInstance();
165
    }
166

    
167
///////////////////////////////////////////////////////////////////////////////////////////////////
168

    
169
  public void effectFinished(final long effectID)
170
    {
171
    int total = mCubeEffectNumber[mPhase]+mNodeEffectNumber[mPhase];
172

    
173
    for(int i=0; i<mCubeEffectNumber[mPhase]; i++)
174
      {
175
      long id = mCubeEffects[mPhase][i].getID();
176

    
177
      if( effectID == id )
178
        {
179
        if( ++mEffectReturned == total ) effectAction(mPhase);
180
        mObject.remove(id);
181
        return;
182
        }
183
      }
184
    for(int i=0; i<mNodeEffectNumber[mPhase]; i++)
185
      {
186
      long id = mNodeEffects[mPhase][i].getID();
187

    
188
      if( effectID == id )
189
        {
190
        if( ++mEffectReturned == total ) effectAction(mPhase);
191
        mObject.getEffects().abortById(id);
192
        return;
193
        }
194
      }
195
    }
196

    
197
///////////////////////////////////////////////////////////////////////////////////////////////////
198

    
199
  @SuppressWarnings("unused")
200
  public long start(int duration, DistortedScreen screen, EffectController cont)
201
    {
202
    mScreen    = screen;
203
    mObject    = cont.getObject();
204
    mController= cont;
205
    mDuration  = duration;
206

    
207
    createEffectsPhase0(mDuration);
208
    assignEffects(mPhase);
209

    
210
    return FAKE_EFFECT_ID;
211
    }
212

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

    
215
  @SuppressWarnings("unused")
216
  public static void enableEffects()
217
    {
218
    Method method;
219

    
220
    for(Type type: Type.values())
221
      {
222
      try
223
        {
224
        method = type.effect.getDeclaredMethod("enable"); // enable not public, thus getDeclaredMethod
225
        }
226
      catch(NoSuchMethodException ex)
227
        {
228
        android.util.Log.e("SolveEffect", type.effect.getSimpleName()+": exception getting method: "+ex.getMessage());
229
        method = null;
230
        }
231

    
232
      try
233
        {
234
        if( method!=null ) method.invoke(null);
235
        }
236
      catch(Exception ex)
237
        {
238
        android.util.Log.e("SolveEffect", type.effect.getSimpleName()+": exception invoking method: "+ex.getMessage());
239
        }
240
      }
241
    }
242
}
(1-1/3)