Project

General

Profile

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

magiccube / src / main / java / org / distorted / effect / DisappearEffect.java @ 34747dd1

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
  private final int FAKE_EFFECT_ID = -2;
52

    
53
  private int mCubeEffectNumber, mCubeEffectFinished, mCubeEffectReturned;
54

    
55
  private EffectListener mListener;
56
  private DistortedScreen mScreen;
57
  private RubikCube mCube;
58

    
59
  Effect[] mCubeEffects;
60
  int[] mCubeEffectPosition;
61

    
62
///////////////////////////////////////////////////////////////////////////////////////////////////
63

    
64
  DisappearEffect()
65
    {
66
    mCubeEffectReturned = 0;
67
    }
68

    
69
///////////////////////////////////////////////////////////////////////////////////////////////////
70

    
71
  abstract int createEffects(int duration);
72

    
73
///////////////////////////////////////////////////////////////////////////////////////////////////
74

    
75
  public void effectMessage(final EffectMessage em, final long effectID, final long objectID)
76
    {
77
    for(int i=0; i<mCubeEffectNumber; i++)
78
      {
79
      long id = mCubeEffects[i].getID();
80

    
81
      if( effectID == id )
82
        {
83
        mCubeEffectReturned++;
84

    
85
        if( mCubeEffectReturned == mCubeEffectFinished )
86
          {
87
          mListener.effectMessage(null, FAKE_EFFECT_ID, 0);
88
          }
89

    
90
        if( mCubeEffectReturned == mCubeEffectNumber )
91
          {
92
          mScreen.detach(mCube);
93
          mCube.deregisterForMessages(this);
94
          }
95

    
96
        mCube.remove(id);
97
        break;
98
        }
99
      }
100
    }
101

    
102
///////////////////////////////////////////////////////////////////////////////////////////////////
103

    
104
  public long start(DistortedScreen screen, RubikCube cube, EffectListener listener)
105
    {
106
    mScreen   = screen;
107
    mCube     = cube;
108
    mListener = listener;
109

    
110
    mCubeEffectFinished = createEffects(2000);
111

    
112
    if( mCubeEffects!=null )
113
      {
114
      mCubeEffectNumber = mCubeEffects.length;
115
      }
116
    else
117
      {
118
      throw new RuntimeException("Disappear Cube Effects not created!");
119
      }
120

    
121
    for(int i=0; i<mCubeEffectNumber; i++)
122
      {
123
      mCube.apply(mCubeEffects[i],mCubeEffectPosition[i]);
124
      }
125

    
126
    mCube.registerForMessages(this);
127

    
128
    return FAKE_EFFECT_ID;
129
    }
130

    
131
///////////////////////////////////////////////////////////////////////////////////////////////////
132

    
133
  public static DisappearEffect create(Type type) throws InstantiationException, IllegalAccessException
134
    {
135
    return type.effectClass.newInstance();
136
    }
137

    
138
///////////////////////////////////////////////////////////////////////////////////////////////////
139

    
140
  public static void enableEffects()
141
    {
142
    Method method=null;
143

    
144
    for(Type type: Type.values())
145
      {
146
      Class<? extends DisappearEffect> cls = type.effectClass;
147

    
148
      try
149
        {
150
        method = cls.getMethod("enable");
151
        }
152
      catch(NoSuchMethodException ex)
153
        {
154
        android.util.Log.e("transitionEffect", "exception getting method: "+ex.getMessage());
155
        }
156

    
157
      try
158
        {
159
        method.invoke(null);
160
        }
161
      catch(Exception ex)
162
        {
163
        android.util.Log.e("transitionEffect", "exception invoking method: "+ex.getMessage());
164
        }
165
      }
166
    }
167
  }
(7-7/12)