Project

General

Profile

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

distorted-objectlib / src / main / java / org / distorted / objectlib / effects / win / WinEffect.java @ b26ea579

1 a5a52e8d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2019 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6 9ec1348c Leszek Koltunski
// 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 a5a52e8d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
9
10
package org.distorted.objectlib.effects.win;
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 ecf3d6e3 Leszek Koltunski
import org.distorted.objectlib.main.ObjectPreRender;
19 a5a52e8d Leszek Koltunski
import org.distorted.objectlib.main.TwistyObject;
20
import org.distorted.objectlib.effects.BaseEffect;
21 7ba38dd4 Leszek Koltunski
import org.distorted.objectlib.main.TwistyObjectNode;
22 a5a52e8d Leszek Koltunski
23
///////////////////////////////////////////////////////////////////////////////////////////////////
24
25
public abstract class WinEffect extends BaseEffect implements EffectListener
26
{
27
  public enum Type
28
    {
29
    NONE   (WinEffectNone.class),
30
    GLOW   (WinEffectGlow.class),
31
    ;
32
33
    final Class<? extends WinEffect> effect;
34
35
    Type(Class<? extends WinEffect> effect)
36
      {
37
      this.effect = effect;
38
      }
39
    }
40
41
  private static final int NUM_EFFECTS = Type.values().length;
42
  private static final int FAKE_EFFECT_ID = -4;
43
  private static final Type[] types;
44
45
  static
46
    {
47
    int i=0;
48
    types = new Type[NUM_EFFECTS];
49
50
    for(Type type: Type.values())
51
      {
52
      types[i++] = type;
53
      }
54
    }
55
56
  private int mDuration;
57
  private int mEffectReturned;
58
  private int mCubeEffectNumber, mNodeEffectNumber;
59
60 02d80fe6 Leszek Koltunski
  ObjectPreRender mPre;
61 a5a52e8d Leszek Koltunski
  TwistyObject mObject;
62 7ba38dd4 Leszek Koltunski
  TwistyObjectNode mObjectNode;
63 a5a52e8d Leszek Koltunski
  Effect[] mCubeEffects;
64
  int[] mCubeEffectPosition;
65
  Effect[] mNodeEffects;
66
  int[] mNodeEffectPosition;
67
68
///////////////////////////////////////////////////////////////////////////////////////////////////
69
70
  abstract void createEffects(int duration);
71
72
///////////////////////////////////////////////////////////////////////////////////////////////////
73
74
  private void assignEffects()
75
    {
76
    mCubeEffectNumber = ( mCubeEffects!=null ) ? mCubeEffects.length : 0;
77
    mNodeEffectNumber = ( mNodeEffects!=null ) ? mNodeEffects.length : 0;
78
79
    if( mCubeEffectNumber==0 && mNodeEffectNumber==0 )
80
      {
81
      throw new RuntimeException("Cube and Node Effects both not created!");
82
      }
83
84
    for(int i=0; i<mCubeEffectNumber; i++)
85
      {
86 826d293e Leszek Koltunski
      mObject.applyEffect(mCubeEffects[i],mCubeEffectPosition[i]);
87 a5a52e8d Leszek Koltunski
      mCubeEffects[i].notifyWhenFinished(this);
88
      }
89
90 7ba38dd4 Leszek Koltunski
    DistortedEffects nodeEffects = mObjectNode.getEffects();
91 a5a52e8d Leszek Koltunski
92
    for(int i=0; i<mNodeEffectNumber; i++)
93
      {
94
      nodeEffects.apply(mNodeEffects[i],mNodeEffectPosition[i]);
95
      mNodeEffects[i].notifyWhenFinished(this);
96
      }
97
    }
98
99
///////////////////////////////////////////////////////////////////////////////////////////////////
100
// PUBLIC API
101
///////////////////////////////////////////////////////////////////////////////////////////////////
102
103
  @SuppressWarnings("unused")
104
  public static String[] getNames()
105
    {
106
    String[] names = new String[NUM_EFFECTS];
107
108
    for( int i=0; i<NUM_EFFECTS; i++)
109
      {
110
      names[i] = types[i].name();
111
      }
112
113
    return names;
114
    }
115
116
///////////////////////////////////////////////////////////////////////////////////////////////////
117
118
  @SuppressWarnings("unused")
119
  public static WinEffect create(int ordinal) throws InstantiationException, IllegalAccessException
120
    {
121
    return types[ordinal].effect.newInstance();
122
    }
123
124
///////////////////////////////////////////////////////////////////////////////////////////////////
125
126
  public void effectFinished(final long effectID)
127
    {
128
    int total = mCubeEffectNumber+mNodeEffectNumber;
129
130
    for(int i=0; i<mCubeEffectNumber; i++)
131
      {
132
      long id = mCubeEffects[i].getID();
133
134
      if( effectID == id )
135
        {
136 826d293e Leszek Koltunski
        mObject.removeEffect(id);
137 93eedb37 Leszek Koltunski
        if( ++mEffectReturned == total ) mPre.effectFinished(FAKE_EFFECT_ID);
138 a5a52e8d Leszek Koltunski
        return;
139
        }
140
      }
141
    for(int i=0; i<mNodeEffectNumber; i++)
142
      {
143
      long id = mNodeEffects[i].getID();
144
145
      if( effectID == id )
146
        {
147 7ba38dd4 Leszek Koltunski
        mObjectNode.getEffects().abortById(id);
148 93eedb37 Leszek Koltunski
        if( ++mEffectReturned == total ) mPre.effectFinished(FAKE_EFFECT_ID);
149 a5a52e8d Leszek Koltunski
        return;
150
        }
151
      }
152
    }
153
154
///////////////////////////////////////////////////////////////////////////////////////////////////
155
156
  @SuppressWarnings("unused")
157 7ba38dd4 Leszek Koltunski
  public long start(int duration, ObjectPreRender pre)
158 a5a52e8d Leszek Koltunski
    {
159 7ba38dd4 Leszek Koltunski
    mObject     = pre.getObject();
160
    mObjectNode = pre.getObjectNode();
161
    mPre        = pre;
162
    mDuration   = duration;
163 a5a52e8d Leszek Koltunski
164
    createEffects(mDuration);
165
    assignEffects();
166
167
    return FAKE_EFFECT_ID;
168
    }
169
170
///////////////////////////////////////////////////////////////////////////////////////////////////
171
172
  @SuppressWarnings("unused")
173
  public static void enableEffects()
174
    {
175
    Method method;
176
177
    for(Type type: Type.values())
178
      {
179
      try
180
        {
181
        method = type.effect.getDeclaredMethod("enable"); // enable not public, thus getDeclaredMethod
182
        }
183
      catch(NoSuchMethodException ex)
184
        {
185
        android.util.Log.e("WinEffect", type.effect.getSimpleName()+": exception getting method: "+ex.getMessage());
186
        method = null;
187
        }
188
189
      try
190
        {
191
        if( method!=null ) method.invoke(null);
192
        }
193
      catch(Exception ex)
194
        {
195
        android.util.Log.e("WinEffect", type.effect.getSimpleName()+": exception invoking method: "+ex.getMessage());
196
        }
197
      }
198
    }
199
}