Project

General

Profile

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

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

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2019 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// Magic Cube is proprietary software licensed under an EULA which you should have received      //
7
// along with the code. If not, check https://distorted.org/magic/License-Magic-Cube.html        //
8
///////////////////////////////////////////////////////////////////////////////////////////////////
9

    
10
package org.distorted.objectlib.effects.solve;
11

    
12
import java.lang.reflect.Method;
13

    
14
import org.distorted.library.effect.Effect;
15
import org.distorted.library.main.DistortedEffects;
16
import org.distorted.library.message.EffectListener;
17

    
18
import org.distorted.objectlib.main.ObjectPreRender;
19
import org.distorted.objectlib.main.TwistyObject;
20

    
21
import org.distorted.objectlib.effects.BaseEffect;
22
import org.distorted.objectlib.main.TwistyObjectNode;
23

    
24
///////////////////////////////////////////////////////////////////////////////////////////////////
25

    
26
public abstract class SolveEffect extends BaseEffect implements EffectListener
27
{
28
  public enum Type
29
    {
30
    NONE   (SolveEffectNone.class),
31
    SPIN   (SolveEffectSpin.class),
32
    ;
33

    
34
    final Class<? extends SolveEffect> effect;
35

    
36
    Type(Class<? extends SolveEffect> effect)
37
      {
38
      this.effect = effect;
39
      }
40
    }
41

    
42
  private static final int NUM_EFFECTS = Type.values().length;
43
  private static final int NUM_PHASES  = 2;
44
  private static final int FAKE_EFFECT_ID = -2;
45
  private static final Type[] types;
46

    
47
  static
48
    {
49
    int i=0;
50
    types = new Type[NUM_EFFECTS];
51

    
52
    for(Type type: Type.values())
53
      {
54
      types[i++] = type;
55
      }
56
    }
57

    
58
  private int mDuration;
59
  private int mEffectReturned;
60
  private final int[] mCubeEffectNumber, mNodeEffectNumber;
61
  private int mPhase;
62

    
63
  ObjectPreRender mPre;
64
  TwistyObject mObject;
65
  TwistyObjectNode mObjectNode;
66
  Effect[][] mCubeEffects;
67
  int[][] mCubeEffectPosition;
68
  Effect[][] mNodeEffects;
69
  int[][] mNodeEffectPosition;
70

    
71
///////////////////////////////////////////////////////////////////////////////////////////////////
72

    
73
  SolveEffect()
74
    {
75
    mPhase= 0;
76

    
77
    mCubeEffectNumber   = new int[NUM_PHASES];
78
    mNodeEffectNumber   = new int[NUM_PHASES];
79
    mCubeEffectPosition = new int[NUM_PHASES][];
80
    mNodeEffectPosition = new int[NUM_PHASES][];
81
    mCubeEffects        = new Effect[NUM_PHASES][];
82
    mNodeEffects        = new Effect[NUM_PHASES][];
83
    }
84

    
85
///////////////////////////////////////////////////////////////////////////////////////////////////
86

    
87
  abstract void createEffectsPhase0(int duration);
88
  abstract void createEffectsPhase1(int duration);
89

    
90
///////////////////////////////////////////////////////////////////////////////////////////////////
91

    
92
  private void assignEffects(int phase)
93
    {
94
    mCubeEffectNumber[phase] = ( mCubeEffects[phase]!=null ) ? mCubeEffects[phase].length : 0;
95
    mNodeEffectNumber[phase] = ( mNodeEffects[phase]!=null ) ? mNodeEffects[phase].length : 0;
96

    
97
    if( mCubeEffectNumber[phase]==0 && mNodeEffectNumber[phase]==0 )
98
      {
99
      throw new RuntimeException("Cube and Node Effects ("+phase+" phase) both not created!");
100
      }
101

    
102
    for(int i=0; i<mCubeEffectNumber[phase]; i++)
103
      {
104
      mObject.applyEffect(mCubeEffects[phase][i],mCubeEffectPosition[phase][i]);
105
      mCubeEffects[phase][i].notifyWhenFinished(this);
106
      }
107

    
108
    DistortedEffects nodeEffects = mObjectNode.getEffects();
109

    
110
    for(int i=0; i<mNodeEffectNumber[phase]; i++)
111
      {
112
      nodeEffects.apply(mNodeEffects[phase][i],mNodeEffectPosition[phase][i]);
113
      mNodeEffects[phase][i].notifyWhenFinished(this);
114
      }
115
    }
116

    
117
///////////////////////////////////////////////////////////////////////////////////////////////////
118

    
119
  private void effectAction(int phase)
120
    {
121
    switch(phase)
122
      {
123
      case 0: mEffectReturned = 0;
124
              mPhase          = 1;
125
              mPre.solveOnly();
126
              createEffectsPhase1(mDuration);
127
              assignEffects(mPhase);
128
              break;
129
      case 1: mPre.effectFinished(FAKE_EFFECT_ID);
130
              break;
131
      }
132
    }
133

    
134
///////////////////////////////////////////////////////////////////////////////////////////////////
135
// PUBLIC API
136
///////////////////////////////////////////////////////////////////////////////////////////////////
137

    
138
  @SuppressWarnings("unused")
139
  public static String[] getNames()
140
    {
141
    String[] names = new String[NUM_EFFECTS];
142

    
143
    for( int i=0; i<NUM_EFFECTS; i++)
144
      {
145
      names[i] = types[i].name();
146
      }
147

    
148
    return names;
149
    }
150

    
151
///////////////////////////////////////////////////////////////////////////////////////////////////
152

    
153
  @SuppressWarnings("unused")
154
  public static SolveEffect create(int ordinal) throws InstantiationException, IllegalAccessException
155
    {
156
    return types[ordinal].effect.newInstance();
157
    }
158

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

    
161
  public void effectFinished(final long effectID)
162
    {
163
    int total = mCubeEffectNumber[mPhase]+mNodeEffectNumber[mPhase];
164

    
165
    for(int i=0; i<mCubeEffectNumber[mPhase]; i++)
166
      {
167
      long id = mCubeEffects[mPhase][i].getID();
168

    
169
      if( effectID == id )
170
        {
171
        if( ++mEffectReturned == total ) effectAction(mPhase);
172
        mObject.removeEffect(id);
173
        return;
174
        }
175
      }
176
    for(int i=0; i<mNodeEffectNumber[mPhase]; i++)
177
      {
178
      long id = mNodeEffects[mPhase][i].getID();
179

    
180
      if( effectID == id )
181
        {
182
        if( ++mEffectReturned == total ) effectAction(mPhase);
183
        mObjectNode.getEffects().abortById(id);
184
        return;
185
        }
186
      }
187
    }
188

    
189
///////////////////////////////////////////////////////////////////////////////////////////////////
190

    
191
  @SuppressWarnings("unused")
192
  public long start(int duration, ObjectPreRender pre)
193
    {
194
    mObject     = pre.getObject();
195
    mObjectNode = pre.getObjectNode();
196
    mPre        = pre;
197
    mDuration   = duration;
198

    
199
    createEffectsPhase0(mDuration);
200
    assignEffects(mPhase);
201

    
202
    return FAKE_EFFECT_ID;
203
    }
204

    
205
///////////////////////////////////////////////////////////////////////////////////////////////////
206

    
207
  @SuppressWarnings("unused")
208
  public static void enableEffects()
209
    {
210
    Method method;
211

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

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