Project

General

Profile

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

distorted-objectlib / src / main / java / org / distorted / objectlib / effects / solve / SolveEffect.java @ a5a52e8d

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.solve;
21

    
22
import java.lang.reflect.Method;
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

    
29
import org.distorted.objectlib.main.TwistyObject;
30

    
31
import org.distorted.objectlib.effects.BaseEffect;
32
import org.distorted.objectlib.effects.EffectController;
33

    
34
///////////////////////////////////////////////////////////////////////////////////////////////////
35

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

    
44
    final Class<? extends SolveEffect> effect;
45

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

    
52
  private static final int NUM_EFFECTS = Type.values().length;
53
  private static final int NUM_PHASES  = 2;
54
  private static final int FAKE_EFFECT_ID = -2;
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 mDuration;
70
  private int mEffectReturned;
71
  private final int[] mCubeEffectNumber, mNodeEffectNumber;
72
  private int mPhase;
73

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

    
81
///////////////////////////////////////////////////////////////////////////////////////////////////
82

    
83
  SolveEffect()
84
    {
85
    mPhase= 0;
86

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

    
95
///////////////////////////////////////////////////////////////////////////////////////////////////
96

    
97
  abstract void createEffectsPhase0(int duration);
98
  abstract void createEffectsPhase1(int duration);
99

    
100
///////////////////////////////////////////////////////////////////////////////////////////////////
101

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

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

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

    
118
    DistortedEffects nodeEffects = mObject.getEffects();
119

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

    
127
///////////////////////////////////////////////////////////////////////////////////////////////////
128

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

    
144
///////////////////////////////////////////////////////////////////////////////////////////////////
145
// PUBLIC API
146
///////////////////////////////////////////////////////////////////////////////////////////////////
147

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

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

    
158
    return names;
159
    }
160

    
161
///////////////////////////////////////////////////////////////////////////////////////////////////
162

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

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

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

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

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

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

    
199
///////////////////////////////////////////////////////////////////////////////////////////////////
200

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

    
209
    createEffectsPhase0(mDuration);
210
    assignEffects(mPhase);
211

    
212
    return FAKE_EFFECT_ID;
213
    }
214

    
215
///////////////////////////////////////////////////////////////////////////////////////////////////
216

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

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

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