Project

General

Profile

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

distorted-objectlib / src / main / java / org / distorted / objectlib / effects / objectchange / ObjectChangeEffect.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.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.message.EffectListener;
27

    
28
import org.distorted.objectlib.main.ObjectPreRender;
29
import org.distorted.objectlib.main.TwistyObject;
30
import org.distorted.objectlib.effects.BaseEffect;
31
import org.distorted.objectlib.main.TwistyObjectNode;
32

    
33
///////////////////////////////////////////////////////////////////////////////////////////////////
34

    
35
public abstract class ObjectChangeEffect extends BaseEffect implements EffectListener
36
{
37
  public enum Type
38
    {
39
    NONE         (ObjectChangeEffectNone.class        ),
40
    TRANSPARENCY (ObjectChangeEffectTransparency.class),
41
    MOVE         (ObjectChangeEffectMove.class        ),
42
    ROUND        (ObjectChangeEffectRound.class       ),
43
    SCALE        (ObjectChangeEffectScale.class       ),
44
    ;
45

    
46
    final Class<? extends ObjectChangeEffect> effect;
47

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

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

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

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

    
70
  private int mDuration;
71
  private final int[] mEffectReturned;
72
  private final int[] mCubeEffectNumber, mNodeEffectNumber;
73
  private final int[] mEffectFinished;
74
  private final boolean[] mPhaseActive;
75

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

    
84
///////////////////////////////////////////////////////////////////////////////////////////////////
85

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

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

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

    
105
///////////////////////////////////////////////////////////////////////////////////////////////////
106

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

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

    
124
      if( effectID == id )
125
        {
126
        effectReturned(phase);
127
        mObjectNode.getEffects().abortById(id);
128
        return;
129
        }
130
      }
131
    }
132

    
133
///////////////////////////////////////////////////////////////////////////////////////////////////
134

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

    
139
    if( mEffectReturned[phase] == mEffectFinished[phase] )
140
      {
141
      switch(phase)
142
        {
143
        case 0: mPhaseActive[1] = true;
144
                mEffectFinished[1] = createEffectsPhase1(mDuration);
145
                assignEffects(1);
146
                mObjectNode.attach(mObject[1].getNode());
147
                mObjectNode.setFOV(mObject[1].getFOV());
148
                break;
149
        case 1: mPre.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
                mObjectNode.detach(mObject[0].getNode());
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 = mObjectNode.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, ObjectPreRender pre)
230
    {
231
    mObject[0] = pre.getOldObject();
232
    mObject[1] = pre.getObject();
233
    mObjectNode= pre.getObjectNode();
234
    mPre       = pre;
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
      mObjectNode.attach(mObject[1].getNode());
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)