Project

General

Profile

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

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

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2019 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Distorted.                                                               //
5
//                                                                                               //
6
// Distorted 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
// Distorted 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 Distorted.  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.RubikCube;
28
import org.distorted.magic.RubikRenderer;
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
    SPIN   (WinEffectSpin.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 = -2;
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 EffectListener mListener;
67
  private int mDuration;
68
  private int mEffectReturned;
69
  private int mCubeEffectNumber, mNodeEffectNumber;
70

    
71
  RubikCube mCube;
72
  DistortedScreen mScreen;
73
  Effect[] mCubeEffects;
74
  int[] mCubeEffectPosition;
75
  Effect[] mNodeEffects;
76
  int[] mNodeEffectPosition;
77

    
78
///////////////////////////////////////////////////////////////////////////////////////////////////
79

    
80
  public static String[] getNames()
81
    {
82
    String[] names = new String[NUM_EFFECTS];
83

    
84
    for( int i=0; i<NUM_EFFECTS; i++)
85
      {
86
      names[i] = types[i].name();
87
      }
88

    
89
    return names;
90
    }
91

    
92
///////////////////////////////////////////////////////////////////////////////////////////////////
93

    
94
  public static WinEffect create(int ordinal) throws InstantiationException, IllegalAccessException
95
    {
96
    return types[ordinal].effect.newInstance();
97
    }
98

    
99
///////////////////////////////////////////////////////////////////////////////////////////////////
100

    
101
  abstract void createEffects(int duration);
102

    
103
///////////////////////////////////////////////////////////////////////////////////////////////////
104

    
105
  public void effectFinished(final long effectID)
106
    {
107
    int total = mCubeEffectNumber+mNodeEffectNumber;
108

    
109
    for(int i=0; i<mCubeEffectNumber; i++)
110
      {
111
      long id = mCubeEffects[i].getID();
112

    
113
      if( effectID == id )
114
        {
115
        if( ++mEffectReturned == total ) mListener.effectFinished(FAKE_EFFECT_ID);
116
        mCube.remove(id);
117
        return;
118
        }
119
      }
120
    for(int i=0; i<mNodeEffectNumber; i++)
121
      {
122
      long id = mNodeEffects[i].getID();
123

    
124
      if( effectID == id )
125
        {
126
        if( ++mEffectReturned == total ) mListener.effectFinished(FAKE_EFFECT_ID);
127
        mCube.getEffects().abortById(id);
128
        return;
129
        }
130
      }
131
    }
132

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

    
135
  public long start(int duration, RubikRenderer renderer)
136
    {
137
    mScreen   = renderer.getScreen();
138
    mCube     = renderer.getCube();
139
    mListener = renderer;
140
    mDuration = duration;
141

    
142
    createEffects(mDuration);
143
    assignEffects();
144

    
145
    return FAKE_EFFECT_ID;
146
    }
147

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

    
150
  private void assignEffects()
151
    {
152
    mCubeEffectNumber = ( mCubeEffects!=null ) ? mCubeEffects.length : 0;
153
    mNodeEffectNumber = ( mNodeEffects!=null ) ? mNodeEffects.length : 0;
154

    
155
    if( mCubeEffectNumber==0 && mNodeEffectNumber==0 )
156
      {
157
      throw new RuntimeException("Cube and Node Effects both not created!");
158
      }
159

    
160
    for(int i=0; i<mCubeEffectNumber; i++)
161
      {
162
      mCube.apply(mCubeEffects[i],mCubeEffectPosition[i]);
163
      mCubeEffects[i].notifyWhenFinished(this);
164
      }
165

    
166
    DistortedEffects nodeEffects = mCube.getEffects();
167

    
168
    for(int i=0; i<mNodeEffectNumber; i++)
169
      {
170
      nodeEffects.apply(mNodeEffects[i],mNodeEffectPosition[i]);
171
      mNodeEffects[i].notifyWhenFinished(this);
172
      }
173
    }
174

    
175
///////////////////////////////////////////////////////////////////////////////////////////////////
176

    
177
  @SuppressWarnings("unused")
178
  public static void enableEffects()
179
    {
180
    Method method;
181

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

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