Project

General

Profile

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

magiccube / src / main / java / org / distorted / effect / AppearEffect.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 AppearEffect implements EffectListener
33
  {
34
  public enum Type
35
    {
36
    EMPTY         (AppearEffectEmpty.class       ),
37
    TRANSPARENCY  (AppearEffectTransparency.class),
38
    MOVE          (AppearEffectMove.class        ),
39
    ROUND         (AppearEffectRound.class       ),
40
    SCALE         (AppearEffectScale.class       ),
41
    ;
42

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

    
45
    Type(Class<? extends AppearEffect> 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 = -1;
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
  AppearEffect()
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
          mCube.deregisterForMessages(this);
116
          }
117

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

    
124
///////////////////////////////////////////////////////////////////////////////////////////////////
125

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

    
132
    mCubeEffectFinished = createEffects(duration);
133

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

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

    
148
    mCube.registerForMessages(this);
149
    mScreen.attach(mCube);
150

    
151
    return FAKE_EFFECT_ID;
152
    }
153

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

    
156
  public static AppearEffect 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 AppearEffect> 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
  }
(1-1/12)