Project

General

Profile

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

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

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.DistortedFramebuffer;
27
import org.distorted.library.message.EffectListener;
28

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

    
32
import org.distorted.objectlib.effects.BaseEffect;
33
import org.distorted.objectlib.main.TwistyObjectNode;
34

    
35
///////////////////////////////////////////////////////////////////////////////////////////////////
36

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

    
45
    final Class<? extends SolveEffect> effect;
46

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

    
53
  private static final int NUM_EFFECTS = Type.values().length;
54
  private static final int NUM_PHASES  = 2;
55
  private static final int FAKE_EFFECT_ID = -2;
56
  private static final Type[] types;
57

    
58
  static
59
    {
60
    int i=0;
61
    types = new Type[NUM_EFFECTS];
62

    
63
    for(Type type: Type.values())
64
      {
65
      types[i++] = type;
66
      }
67
    }
68

    
69
  private int mDuration;
70
  private int mEffectReturned;
71
  private final int[] mCubeEffectNumber, mNodeEffectNumber;
72
  private int mPhase;
73

    
74
  ObjectPreRender mPre;
75
  TwistyObject mObject;
76
  TwistyObjectNode mObjectNode;
77
  Effect[][] mCubeEffects;
78
  int[][] mCubeEffectPosition;
79
  Effect[][] mNodeEffects;
80
  int[][] mNodeEffectPosition;
81

    
82
///////////////////////////////////////////////////////////////////////////////////////////////////
83

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

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

    
96
///////////////////////////////////////////////////////////////////////////////////////////////////
97

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

    
101
///////////////////////////////////////////////////////////////////////////////////////////////////
102

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

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

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

    
119
    DistortedEffects nodeEffects = mObjectNode.getEffects();
120

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

    
128
///////////////////////////////////////////////////////////////////////////////////////////////////
129

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

    
145
///////////////////////////////////////////////////////////////////////////////////////////////////
146
// PUBLIC API
147
///////////////////////////////////////////////////////////////////////////////////////////////////
148

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

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

    
159
    return names;
160
    }
161

    
162
///////////////////////////////////////////////////////////////////////////////////////////////////
163

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

    
170
///////////////////////////////////////////////////////////////////////////////////////////////////
171

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

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

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

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

    
200
///////////////////////////////////////////////////////////////////////////////////////////////////
201

    
202
  @SuppressWarnings("unused")
203
  public long start(int duration, ObjectPreRender pre)
204
    {
205
    mObject     = pre.getObject();
206
    mObjectNode = pre.getObjectNode();
207
    mPre        = pre;
208
    mDuration   = duration;
209

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

    
213
    return FAKE_EFFECT_ID;
214
    }
215

    
216
///////////////////////////////////////////////////////////////////////////////////////////////////
217

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

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

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