Project

General

Profile

Download (6.8 KB) Statistics
| Branch: | Tag: | Revision:

magiccube / src / main / java / org / distorted / effect / win / WinEffect.java @ 8becce57

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.effect.win;
21

    
22
import org.distorted.effect.BaseEffect;
23
import org.distorted.library.effect.Effect;
24
import org.distorted.library.main.DistortedEffects;
25
import org.distorted.library.main.DistortedScreen;
26
import org.distorted.library.message.EffectListener;
27
import org.distorted.magic.RubikPostRender;
28
import org.distorted.object.RubikObject;
29

    
30
import java.lang.reflect.Method;
31

    
32
///////////////////////////////////////////////////////////////////////////////////////////////////
33

    
34
public abstract class WinEffect extends BaseEffect implements EffectListener
35
{
36
  public enum Type
37
    {
38
    NONE   (WinEffectNone.class),
39
    GLOW   (WinEffectGlow.class),
40
    ;
41

    
42
    final Class<? extends WinEffect> effect;
43

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

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

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

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

    
65
  private EffectListener mListener;
66
  private int mDuration;
67
  private int mEffectReturned;
68
  private int mCubeEffectNumber, mNodeEffectNumber;
69

    
70
  RubikObject mObject;
71
  DistortedScreen mScreen;
72
  Effect[] mCubeEffects;
73
  int[] mCubeEffectPosition;
74
  Effect[] mNodeEffects;
75
  int[] mNodeEffectPosition;
76

    
77
///////////////////////////////////////////////////////////////////////////////////////////////////
78

    
79
  abstract void createEffects(int duration);
80

    
81
///////////////////////////////////////////////////////////////////////////////////////////////////
82

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

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

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

    
99
    DistortedEffects nodeEffects = mObject.getEffects();
100

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

    
108
///////////////////////////////////////////////////////////////////////////////////////////////////
109
// PUBLIC API
110
///////////////////////////////////////////////////////////////////////////////////////////////////
111

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

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

    
122
    return names;
123
    }
124

    
125
///////////////////////////////////////////////////////////////////////////////////////////////////
126

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

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

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

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

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

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

    
163
///////////////////////////////////////////////////////////////////////////////////////////////////
164

    
165
  @SuppressWarnings("unused")
166
  public long start(int duration, DistortedScreen screen, RubikPostRender post)
167
    {
168
    mScreen   = screen;
169
    mObject   = post.getObject();
170
    mListener = post;
171
    mDuration = duration;
172

    
173
    createEffects(mDuration);
174
    assignEffects();
175

    
176
    return FAKE_EFFECT_ID;
177
    }
178

    
179
///////////////////////////////////////////////////////////////////////////////////////////////////
180

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

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

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