Project

General

Profile

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

magiccube / src / main / java / org / distorted / effect / DisappearEffect.java @ 5560eea9

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;
21

    
22
import org.distorted.library.effect.Effect;
23
import org.distorted.library.main.DistortedScreen;
24
import org.distorted.library.message.EffectListener;
25
import org.distorted.library.message.EffectMessage;
26
import org.distorted.magic.RubikCube;
27

    
28
import java.lang.reflect.Method;
29

    
30
///////////////////////////////////////////////////////////////////////////////////////////////////
31

    
32
public abstract class DisappearEffect implements EffectListener
33
  {
34
  public enum Type
35
    {
36
    EMPTY         (DisappearEffectEmpty.class       ),
37
    TRANSPARENCY  (DisappearEffectTransparency.class),
38
    MOVE          (DisappearEffectMove.class        ),
39
    ROUND         (DisappearEffectRound.class       ),
40
    SCALE         (DisappearEffectScale.class       ),
41
    ;
42

    
43
    private final Class<? extends DisappearEffect> effectClass;
44

    
45
    Type(Class<? extends DisappearEffect> effectClass)
46
      {
47
      this.effectClass = effectClass;
48
      }
49
    }
50

    
51
  public static final int NUM_EFFECTS = Type.values().length;
52
  private static final Type[] types;
53

    
54
  static
55
    {
56
    int i=0;
57

    
58
    types = new Type[NUM_EFFECTS];
59

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

    
67
  private final int FAKE_EFFECT_ID = -2;
68

    
69
  private int mCubeEffectNumber, mCubeEffectFinished, mCubeEffectReturned;
70

    
71
  private EffectListener mListener;
72
  private DistortedScreen mScreen;
73
  private RubikCube mCube;
74

    
75
  Effect[] mCubeEffects;
76
  int[] mCubeEffectPosition;
77

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

    
80
  DisappearEffect()
81
    {
82
    mCubeEffectReturned = 0;
83
    }
84

    
85
///////////////////////////////////////////////////////////////////////////////////////////////////
86

    
87
  abstract int createEffects(int duration);
88

    
89
///////////////////////////////////////////////////////////////////////////////////////////////////
90

    
91
  public static Type getType(int ordinal)
92
    {
93
    return types[ordinal];
94
    }
95

    
96
///////////////////////////////////////////////////////////////////////////////////////////////////
97

    
98
  public void effectMessage(final EffectMessage em, final long effectID, final long objectID)
99
    {
100
    for(int i=0; i<mCubeEffectNumber; i++)
101
      {
102
      long id = mCubeEffects[i].getID();
103

    
104
      if( effectID == id )
105
        {
106
        mCubeEffectReturned++;
107

    
108
        if( mCubeEffectReturned == mCubeEffectFinished )
109
          {
110
          mListener.effectMessage(null, FAKE_EFFECT_ID, 0);
111
          }
112

    
113
        if( mCubeEffectReturned == mCubeEffectNumber )
114
          {
115
          mScreen.detach(mCube);
116
          mCube.deregisterForMessages(this);
117
          }
118

    
119
        mCube.remove(id);
120
        break;
121
        }
122
      }
123
    }
124

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

    
127
  public long start(int duration, DistortedScreen screen, RubikCube cube, EffectListener listener)
128
    {
129
    mScreen   = screen;
130
    mCube     = cube;
131
    mListener = listener;
132

    
133
    mCubeEffectFinished = createEffects(duration);
134

    
135
    if( mCubeEffects!=null )
136
      {
137
      mCubeEffectNumber = mCubeEffects.length;
138
      }
139
    else
140
      {
141
      throw new RuntimeException("Disappear Cube Effects not created!");
142
      }
143

    
144
    for(int i=0; i<mCubeEffectNumber; i++)
145
      {
146
      mCube.apply(mCubeEffects[i],mCubeEffectPosition[i]);
147
      }
148

    
149
    mCube.registerForMessages(this);
150

    
151
    return FAKE_EFFECT_ID;
152
    }
153

    
154
///////////////////////////////////////////////////////////////////////////////////////////////////
155

    
156
  public static DisappearEffect create(Type type) throws InstantiationException, IllegalAccessException
157
    {
158
    return type.effectClass.newInstance();
159
    }
160

    
161
///////////////////////////////////////////////////////////////////////////////////////////////////
162

    
163
  public static void enableEffects()
164
    {
165
    Method method=null;
166

    
167
    for(Type type: Type.values())
168
      {
169
      Class<? extends DisappearEffect> cls = type.effectClass;
170

    
171
      try
172
        {
173
        method = cls.getMethod("enable");
174
        }
175
      catch(NoSuchMethodException ex)
176
        {
177
        android.util.Log.e("transitionEffect", "exception getting method: "+ex.getMessage());
178
        }
179

    
180
      try
181
        {
182
        method.invoke(null);
183
        }
184
      catch(Exception ex)
185
        {
186
        android.util.Log.e("transitionEffect", "exception invoking method: "+ex.getMessage());
187
        }
188
      }
189
    }
190
  }
(7-7/12)