Project

General

Profile

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

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

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