Project

General

Profile

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

distorted-objectlib / src / main / java / org / distorted / objectlib / effects / win / WinEffect.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.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
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 WinEffect extends BaseEffect implements EffectListener
37
{
38
  public enum Type
39
    {
40
    NONE   (WinEffectNone.class),
41
    GLOW   (WinEffectGlow.class),
42
    ;
43

    
44
    final Class<? extends WinEffect> effect;
45

    
46
    Type(Class<? extends WinEffect> effect)
47
      {
48
      this.effect = effect;
49
      }
50
    }
51

    
52
  private static final int NUM_EFFECTS = Type.values().length;
53
  private static final int FAKE_EFFECT_ID = -4;
54
  private static final Type[] types;
55

    
56
  static
57
    {
58
    int i=0;
59
    types = new Type[NUM_EFFECTS];
60

    
61
    for(Type type: Type.values())
62
      {
63
      types[i++] = type;
64
      }
65
    }
66

    
67
  private EffectController mController;
68
  private int mDuration;
69
  private int mEffectReturned;
70
  private int mCubeEffectNumber, mNodeEffectNumber;
71

    
72
  TwistyObject mObject;
73
  DistortedScreen mScreen;
74
  Effect[] mCubeEffects;
75
  int[] mCubeEffectPosition;
76
  Effect[] mNodeEffects;
77
  int[] mNodeEffectPosition;
78

    
79
///////////////////////////////////////////////////////////////////////////////////////////////////
80

    
81
  abstract void createEffects(int duration);
82

    
83
///////////////////////////////////////////////////////////////////////////////////////////////////
84

    
85
  private void assignEffects()
86
    {
87
    mCubeEffectNumber = ( mCubeEffects!=null ) ? mCubeEffects.length : 0;
88
    mNodeEffectNumber = ( mNodeEffects!=null ) ? mNodeEffects.length : 0;
89

    
90
    if( mCubeEffectNumber==0 && mNodeEffectNumber==0 )
91
      {
92
      throw new RuntimeException("Cube and Node Effects both not created!");
93
      }
94

    
95
    for(int i=0; i<mCubeEffectNumber; i++)
96
      {
97
      mObject.apply(mCubeEffects[i],mCubeEffectPosition[i]);
98
      mCubeEffects[i].notifyWhenFinished(this);
99
      }
100

    
101
    DistortedEffects nodeEffects = mObject.getEffects();
102

    
103
    for(int i=0; i<mNodeEffectNumber; i++)
104
      {
105
      nodeEffects.apply(mNodeEffects[i],mNodeEffectPosition[i]);
106
      mNodeEffects[i].notifyWhenFinished(this);
107
      }
108
    }
109

    
110
///////////////////////////////////////////////////////////////////////////////////////////////////
111
// PUBLIC API
112
///////////////////////////////////////////////////////////////////////////////////////////////////
113

    
114
  @SuppressWarnings("unused")
115
  public static String[] getNames()
116
    {
117
    String[] names = new String[NUM_EFFECTS];
118

    
119
    for( int i=0; i<NUM_EFFECTS; i++)
120
      {
121
      names[i] = types[i].name();
122
      }
123

    
124
    return names;
125
    }
126

    
127
///////////////////////////////////////////////////////////////////////////////////////////////////
128

    
129
  @SuppressWarnings("unused")
130
  public static WinEffect create(int ordinal) throws InstantiationException, IllegalAccessException
131
    {
132
    return types[ordinal].effect.newInstance();
133
    }
134

    
135
///////////////////////////////////////////////////////////////////////////////////////////////////
136

    
137
  public void effectFinished(final long effectID)
138
    {
139
    int total = mCubeEffectNumber+mNodeEffectNumber;
140

    
141
    for(int i=0; i<mCubeEffectNumber; i++)
142
      {
143
      long id = mCubeEffects[i].getID();
144

    
145
      if( effectID == id )
146
        {
147
        if( ++mEffectReturned == total ) mController.effectFinished(FAKE_EFFECT_ID);
148
        mObject.remove(id);
149
        return;
150
        }
151
      }
152
    for(int i=0; i<mNodeEffectNumber; i++)
153
      {
154
      long id = mNodeEffects[i].getID();
155

    
156
      if( effectID == id )
157
        {
158
        if( ++mEffectReturned == total ) mController.effectFinished(FAKE_EFFECT_ID);
159
        mObject.getEffects().abortById(id);
160
        return;
161
        }
162
      }
163
    }
164

    
165
///////////////////////////////////////////////////////////////////////////////////////////////////
166

    
167
  @SuppressWarnings("unused")
168
  public long start(int duration, DistortedScreen screen, EffectController cont)
169
    {
170
    mScreen    = screen;
171
    mObject    = cont.getObject();
172
    mController= cont;
173
    mDuration  = duration;
174

    
175
    createEffects(mDuration);
176
    assignEffects();
177

    
178
    return FAKE_EFFECT_ID;
179
    }
180

    
181
///////////////////////////////////////////////////////////////////////////////////////////////////
182

    
183
  @SuppressWarnings("unused")
184
  public static void enableEffects()
185
    {
186
    Method method;
187

    
188
    for(Type type: Type.values())
189
      {
190
      try
191
        {
192
        method = type.effect.getDeclaredMethod("enable"); // enable not public, thus getDeclaredMethod
193
        }
194
      catch(NoSuchMethodException ex)
195
        {
196
        android.util.Log.e("WinEffect", type.effect.getSimpleName()+": exception getting method: "+ex.getMessage());
197
        method = null;
198
        }
199

    
200
      try
201
        {
202
        if( method!=null ) method.invoke(null);
203
        }
204
      catch(Exception ex)
205
        {
206
        android.util.Log.e("WinEffect", type.effect.getSimpleName()+": exception invoking method: "+ex.getMessage());
207
        }
208
      }
209
    }
210
}
(1-1/3)