Project

General

Profile

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

magiccube / src / main / java / org / distorted / effect / solve / SolveEffect.java @ 64975793

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

    
22
import org.distorted.effect.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.magic.RubikCube;
28
import org.distorted.magic.RubikRenderer;
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 EffectListener mListener;
67
  private int mDuration;
68
  private int mEffectReturned;
69
  private int[] mCubeEffectNumber, mNodeEffectNumber;
70
  private int mPhase;
71

    
72
  RubikCube mCube;
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
  public static String[] getNames()
96
    {
97
    String[] names = new String[NUM_EFFECTS];
98

    
99
    for( int i=0; i<NUM_EFFECTS; i++)
100
      {
101
      names[i] = types[i].name();
102
      }
103

    
104
    return names;
105
    }
106

    
107
///////////////////////////////////////////////////////////////////////////////////////////////////
108

    
109
  public static SolveEffect create(int ordinal) throws InstantiationException, IllegalAccessException
110
    {
111
    return types[ordinal].effect.newInstance();
112
    }
113

    
114
///////////////////////////////////////////////////////////////////////////////////////////////////
115

    
116
  abstract void createEffectsPhase0(int duration);
117
  abstract void createEffectsPhase1(int duration);
118

    
119
///////////////////////////////////////////////////////////////////////////////////////////////////
120

    
121
  public void effectFinished(final long effectID)
122
    {
123
    int total = mCubeEffectNumber[mPhase]+mNodeEffectNumber[mPhase];
124

    
125
    for(int i=0; i<mCubeEffectNumber[mPhase]; i++)
126
      {
127
      long id = mCubeEffects[mPhase][i].getID();
128

    
129
      if( effectID == id )
130
        {
131
        if( ++mEffectReturned == total ) effectAction(mPhase);
132
        mCube.remove(id);
133
        return;
134
        }
135
      }
136
    for(int i=0; i<mNodeEffectNumber[mPhase]; i++)
137
      {
138
      long id = mNodeEffects[mPhase][i].getID();
139

    
140
      if( effectID == id )
141
        {
142
        if( ++mEffectReturned == total ) effectAction(mPhase);
143
        mCube.getEffects().abortById(id);
144
        return;
145
        }
146
      }
147
    }
148

    
149
///////////////////////////////////////////////////////////////////////////////////////////////////
150

    
151
  private void effectAction(int phase)
152
    {
153
    switch(phase)
154
      {
155
      case 0: mEffectReturned = 0;
156
              mPhase          = 1;
157
              mCube.solve();
158
              createEffectsPhase1(mDuration);
159
              assignEffects(mPhase);
160
              break;
161
      case 1: mListener.effectFinished(FAKE_EFFECT_ID);
162
              break;
163
      }
164
    }
165

    
166
///////////////////////////////////////////////////////////////////////////////////////////////////
167

    
168
  public long start(int duration, RubikRenderer renderer)
169
    {
170
    mScreen   = renderer.getScreen();
171
    mCube     = renderer.getCube();
172
    mListener = renderer;
173
    mDuration = duration;
174

    
175
    createEffectsPhase0(mDuration);
176
    assignEffects(mPhase);
177

    
178
    return FAKE_EFFECT_ID;
179
    }
180

    
181
///////////////////////////////////////////////////////////////////////////////////////////////////
182

    
183
  private void assignEffects(int phase)
184
    {
185
    mCubeEffectNumber[phase] = ( mCubeEffects[phase]!=null ) ? mCubeEffects[phase].length : 0;
186
    mNodeEffectNumber[phase] = ( mNodeEffects[phase]!=null ) ? mNodeEffects[phase].length : 0;
187

    
188
    if( mCubeEffectNumber[phase]==0 && mNodeEffectNumber[phase]==0 )
189
      {
190
      throw new RuntimeException("Cube and Node Effects ("+phase+" phase) both not created!");
191
      }
192

    
193
    for(int i=0; i<mCubeEffectNumber[phase]; i++)
194
      {
195
      mCube.apply(mCubeEffects[phase][i],mCubeEffectPosition[phase][i]);
196
      mCubeEffects[phase][i].notifyWhenFinished(this);
197
      }
198

    
199
    DistortedEffects nodeEffects = mCube.getEffects();
200

    
201
    for(int i=0; i<mNodeEffectNumber[phase]; i++)
202
      {
203
      nodeEffects.apply(mNodeEffects[phase][i],mNodeEffectPosition[phase][i]);
204
      mNodeEffects[phase][i].notifyWhenFinished(this);
205
      }
206
    }
207

    
208
///////////////////////////////////////////////////////////////////////////////////////////////////
209

    
210
  @SuppressWarnings("unused")
211
  public static void enableEffects()
212
    {
213
    Method method;
214

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

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