Project

General

Profile

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

distorted-objectlib / src / main / java / org / distorted / objectlib / effects / resticker / RestickerEffect.java @ 4c2c0f44

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2023 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.resticker;
11

    
12
import org.distorted.library.effect.Effect;
13
import org.distorted.library.main.DistortedEffects;
14
import org.distorted.library.message.EffectListener;
15
import org.distorted.objectlib.effects.BaseEffect;
16
import org.distorted.objectlib.main.ObjectPreRender;
17
import org.distorted.objectlib.main.TwistyObject;
18
import org.distorted.objectlib.main.TwistyObjectNode;
19

    
20
import java.lang.reflect.Method;
21

    
22
///////////////////////////////////////////////////////////////////////////////////////////////////
23

    
24
public abstract class RestickerEffect extends BaseEffect implements EffectListener
25
{
26
  public enum Type
27
    {
28
    SPIN   (RestickerEffectSpin.class),
29
    ;
30

    
31
    final Class<? extends RestickerEffect> effect;
32

    
33
    Type(Class<? extends RestickerEffect> effect)
34
      {
35
      this.effect = effect;
36
      }
37
    }
38

    
39
  private static final int NUM_EFFECTS = Type.values().length;
40
  private static final int NUM_PHASES  = 2;
41
  private static final int FAKE_EFFECT_ID = -2;
42
  private static final Type[] types;
43

    
44
  static
45
    {
46
    int i=0;
47
    types = new Type[NUM_EFFECTS];
48

    
49
    for(Type type: Type.values())
50
      {
51
      types[i++] = type;
52
      }
53
    }
54

    
55
  private int mDuration;
56
  private int mEffectReturned;
57
  private final int[] mCubeEffectNumber, mNodeEffectNumber;
58
  private int mPhase;
59

    
60
  ObjectPreRender mPre;
61
  TwistyObject mObject;
62
  TwistyObjectNode mObjectNode;
63
  Effect[][] mCubeEffects;
64
  int[][] mCubeEffectPosition;
65
  Effect[][] mNodeEffects;
66
  int[][] mNodeEffectPosition;
67

    
68
///////////////////////////////////////////////////////////////////////////////////////////////////
69

    
70
  RestickerEffect()
71
    {
72
    mPhase= 0;
73

    
74
    mCubeEffectNumber   = new int[NUM_PHASES];
75
    mNodeEffectNumber   = new int[NUM_PHASES];
76
    mCubeEffectPosition = new int[NUM_PHASES][];
77
    mNodeEffectPosition = new int[NUM_PHASES][];
78
    mCubeEffects        = new Effect[NUM_PHASES][];
79
    mNodeEffects        = new Effect[NUM_PHASES][];
80
    }
81

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

    
84
  abstract void createEffectsPhase0(int duration);
85
  abstract void createEffectsPhase1(int duration);
86

    
87
///////////////////////////////////////////////////////////////////////////////////////////////////
88

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

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

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

    
105
    DistortedEffects nodeEffects = mObjectNode.getEffects();
106

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

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

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

    
131
///////////////////////////////////////////////////////////////////////////////////////////////////
132
// PUBLIC API
133
///////////////////////////////////////////////////////////////////////////////////////////////////
134

    
135
  @SuppressWarnings("unused")
136
  public static String[] getNames()
137
    {
138
    String[] names = new String[NUM_EFFECTS];
139

    
140
    for( int i=0; i<NUM_EFFECTS; i++)
141
      {
142
      names[i] = types[i].name();
143
      }
144

    
145
    return names;
146
    }
147

    
148
///////////////////////////////////////////////////////////////////////////////////////////////////
149

    
150
  @SuppressWarnings("unused")
151
  public static RestickerEffect create(int ordinal) throws InstantiationException, IllegalAccessException
152
    {
153
    return types[ordinal].effect.newInstance();
154
    }
155

    
156
///////////////////////////////////////////////////////////////////////////////////////////////////
157

    
158
  public void effectFinished(final long effectID)
159
    {
160
    int total = mCubeEffectNumber[mPhase]+mNodeEffectNumber[mPhase];
161

    
162
    for(int i=0; i<mCubeEffectNumber[mPhase]; i++)
163
      {
164
      long id = mCubeEffects[mPhase][i].getID();
165

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

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

    
186
///////////////////////////////////////////////////////////////////////////////////////////////////
187

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

    
196
    createEffectsPhase0(mDuration);
197
    assignEffects(mPhase);
198

    
199
    return FAKE_EFFECT_ID;
200
    }
201

    
202
///////////////////////////////////////////////////////////////////////////////////////////////////
203

    
204
  @SuppressWarnings("unused")
205
  public static void enableEffects()
206
    {
207
    Method method;
208

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

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