Project

General

Profile

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

distorted-objectlib / src / main / java / org / distorted / objectlib / effects / objectchange / ObjectChangeEffect.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.objectchange;
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 ObjectChangeEffect extends BaseEffect implements EffectListener
37
{
38
  public enum Type
39
    {
40
    NONE         (ObjectChangeEffectNone.class        ),
41
    TRANSPARENCY (ObjectChangeEffectTransparency.class),
42
    MOVE         (ObjectChangeEffectMove.class        ),
43
    ROUND        (ObjectChangeEffectRound.class       ),
44
    SCALE        (ObjectChangeEffectScale.class       ),
45
    ;
46

    
47
    final Class<? extends ObjectChangeEffect> effect;
48

    
49
    Type(Class<? extends ObjectChangeEffect> effect)
50
      {
51
      this.effect= effect;
52
      }
53
    }
54

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

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

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

    
71
  private EffectController mController;
72
  private int mDuration;
73
  private final int[] mEffectReturned;
74
  private final int[] mCubeEffectNumber, mNodeEffectNumber;
75
  private final int[] mEffectFinished;
76
  private final boolean[] mPhaseActive;
77

    
78
  TwistyObject[] mObject;
79
  DistortedScreen mScreen;
80
  Effect[][] mCubeEffects;
81
  int[][] mCubeEffectPosition;
82
  Effect[][] mNodeEffects;
83
  int[][] mNodeEffectPosition;
84

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

    
87
  ObjectChangeEffect()
88
    {
89
    mPhaseActive        = new boolean[NUM_PHASES];
90
    mEffectReturned     = new int[NUM_PHASES];
91
    mCubeEffectNumber   = new int[NUM_PHASES];
92
    mNodeEffectNumber   = new int[NUM_PHASES];
93
    mEffectFinished     = new int[NUM_PHASES];
94
    mCubeEffectPosition = new int[NUM_PHASES][];
95
    mNodeEffectPosition = new int[NUM_PHASES][];
96
    mCubeEffects        = new Effect[NUM_PHASES][];
97
    mNodeEffects        = new Effect[NUM_PHASES][];
98
    mObject             = new TwistyObject[NUM_PHASES];
99
    }
100

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

    
103
  abstract int createEffectsPhase0(int duration);
104
  abstract int createEffectsPhase1(int duration);
105

    
106
///////////////////////////////////////////////////////////////////////////////////////////////////
107

    
108
  private void effectFinishedPhase(final long effectID, int phase)
109
    {
110
    for(int i=0; i<mCubeEffectNumber[phase]; i++)
111
      {
112
      long id = mCubeEffects[phase][i].getID();
113

    
114
      if( effectID == id )
115
        {
116
        effectReturned(phase);
117
        mObject[phase].remove(id);
118
        return;
119
        }
120
      }
121
    for(int i=0; i<mNodeEffectNumber[phase]; i++)
122
      {
123
      long id = mNodeEffects[phase][i].getID();
124

    
125
      if( effectID == id )
126
        {
127
        effectReturned(phase);
128
        mObject[phase].getEffects().abortById(id);
129
        return;
130
        }
131
      }
132
    }
133

    
134
///////////////////////////////////////////////////////////////////////////////////////////////////
135

    
136
  private void effectReturned(int phase)
137
    {
138
    mEffectReturned[phase]++;
139

    
140
    if( mEffectReturned[phase] == mEffectFinished[phase] )
141
      {
142
      switch(phase)
143
        {
144
        case 0: mPhaseActive[1] = true;
145
                mEffectFinished[1] = createEffectsPhase1(mDuration);
146
                assignEffects(1);
147
                mScreen.attach(mObject[1]);
148
                break;
149
        case 1: mController.effectFinished(FAKE_EFFECT_ID);
150
                break;
151
        }
152
      }
153
    if( mEffectReturned[phase] == mCubeEffectNumber[phase]+mNodeEffectNumber[phase] )
154
      {
155
      switch(phase)
156
        {
157
        case 0: mPhaseActive[0] = false;
158
                mScreen.detach(mObject[0]);
159
                break;
160
        case 1: mPhaseActive[1] = false;
161
                break;
162
        }
163
      }
164
    }
165

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

    
168
  private void assignEffects(int phase)
169
    {
170
    mCubeEffectNumber[phase] = ( mCubeEffects[phase]!=null ) ? mCubeEffects[phase].length : 0;
171
    mNodeEffectNumber[phase] = ( mNodeEffects[phase]!=null ) ? mNodeEffects[phase].length : 0;
172

    
173
    if( mCubeEffectNumber[phase]==0 && mNodeEffectNumber[phase]==0 )
174
      {
175
      throw new RuntimeException("Cube and Node Effects ("+phase+" phase) both not created!");
176
      }
177

    
178
    for(int i=0; i<mCubeEffectNumber[phase]; i++)
179
      {
180
      mObject[phase].apply(mCubeEffects[phase][i],mCubeEffectPosition[phase][i]);
181
      mCubeEffects[phase][i].notifyWhenFinished(this);
182
      }
183

    
184
    DistortedEffects nodeEffects = mObject[phase].getEffects();
185

    
186
    for(int i=0; i<mNodeEffectNumber[phase]; i++)
187
      {
188
      nodeEffects.apply(mNodeEffects[phase][i],mNodeEffectPosition[phase][i]);
189
      mNodeEffects[phase][i].notifyWhenFinished(this);
190
      }
191
    }
192

    
193
///////////////////////////////////////////////////////////////////////////////////////////////////
194
// PUBLIC API
195
///////////////////////////////////////////////////////////////////////////////////////////////////
196

    
197
  public void effectFinished(final long effectID)
198
    {
199
    if( mPhaseActive[0] ) effectFinishedPhase(effectID,0);
200
    if( mPhaseActive[1] ) effectFinishedPhase(effectID,1);
201
    }
202

    
203
///////////////////////////////////////////////////////////////////////////////////////////////////
204

    
205
  @SuppressWarnings("unused")
206
  public static String[] getNames()
207
    {
208
    String[] names = new String[NUM_EFFECTS];
209

    
210
    for( int i=0; i<NUM_EFFECTS; i++)
211
      {
212
      names[i] = types[i].name();
213
      }
214

    
215
    return names;
216
    }
217

    
218
///////////////////////////////////////////////////////////////////////////////////////////////////
219

    
220
  @SuppressWarnings("unused")
221
  public static ObjectChangeEffect create(int ordinal) throws InstantiationException, IllegalAccessException
222
    {
223
    return types[ordinal].effect.newInstance();
224
    }
225

    
226
///////////////////////////////////////////////////////////////////////////////////////////////////
227

    
228
  @SuppressWarnings("unused")
229
  public long start(int duration, DistortedScreen screen, EffectController cont)
230
    {
231
    mScreen    = screen;
232
    mObject[0] = cont.getOldObject();
233
    mObject[1] = cont.getObject();
234
    mController= cont;
235
    mDuration  = duration;
236

    
237
    if( mObject[0]!=null )
238
      {
239
      mPhaseActive[0] = true;
240
      mEffectFinished[0] = createEffectsPhase0(mDuration);
241
      assignEffects(0);
242
      }
243
    else
244
      {
245
      mPhaseActive[1] = true;
246
      mEffectFinished[1] = createEffectsPhase1(mDuration);
247
      assignEffects(1);
248
      mScreen.attach(mObject[1]);
249
      }
250

    
251
    return FAKE_EFFECT_ID;
252
    }
253

    
254
///////////////////////////////////////////////////////////////////////////////////////////////////
255

    
256
  @SuppressWarnings("unused")
257
  public static void enableEffects()
258
    {
259
    Method method;
260

    
261
    for(Type type: Type.values())
262
      {
263
      try
264
        {
265
        method = type.effect.getDeclaredMethod("enable");  // enable not public, thus getDeclaredMethod
266
        }
267
      catch(NoSuchMethodException ex)
268
        {
269
        android.util.Log.e("SizeChangeEffect", type.effect.getSimpleName()+": exception getting method: "+ex.getMessage());
270
        method = null;
271
        }
272

    
273
      try
274
        {
275
        if( method!=null ) method.invoke(null);
276
        }
277
      catch(Exception ex)
278
        {
279
        android.util.Log.e("SizeChangeEffect", type.effect.getSimpleName()+": exception invoking method: "+ex.getMessage());
280
        }
281
      }
282
    }
283
}
(1-1/6)