Project

General

Profile

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

magiccube / src / main / java / org / distorted / object / RubikObject.java @ 70b76549

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2020 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.object;
21

    
22
import android.content.SharedPreferences;
23

    
24
import org.distorted.library.effect.Effect;
25
import org.distorted.library.effect.MatrixEffectMove;
26
import org.distorted.library.effect.MatrixEffectQuaternion;
27
import org.distorted.library.effect.MatrixEffectScale;
28
import org.distorted.library.effect.VertexEffectSink;
29
import org.distorted.library.main.DistortedEffects;
30
import org.distorted.library.main.DistortedNode;
31
import org.distorted.library.main.DistortedTexture;
32
import org.distorted.library.mesh.MeshRectangles;
33
import org.distorted.library.message.EffectListener;
34
import org.distorted.library.type.Dynamic1D;
35
import org.distorted.library.type.Static1D;
36
import org.distorted.library.type.Static3D;
37
import org.distorted.library.type.Static4D;
38

    
39
///////////////////////////////////////////////////////////////////////////////////////////////////
40

    
41
public abstract class RubikObject extends DistortedNode
42
  {
43
  static final float OBJECT_SCREEN_RATIO = 0.5f;
44
  static final int TEXTURE_SIZE = 100;
45

    
46
  private static final int POST_ROTATION_MILLISEC = 500;
47
  final float[] LEGAL_QUATS;
48

    
49
  private Static3D mMove, mScale, mNodeMove, mNodeScale;
50
  private Static4D mQuatAccumulated;
51
  private DistortedTexture mNodeTexture;
52

    
53
  int mSize, mRotAxis, mRotRow;
54

    
55
  Static1D mRotationAngleStatic, mRotationAngleMiddle, mRotationAngleFinal;
56
  DistortedTexture mTexture;
57

    
58
  VertexEffectSink mSinkEffect;
59
  MatrixEffectMove mMoveEffect;
60
  MatrixEffectScale mScaleEffect;
61
  MatrixEffectQuaternion mQuatCEffect;
62
  MatrixEffectQuaternion mQuatAEffect;
63

    
64
///////////////////////////////////////////////////////////////////////////////////////////////////
65

    
66
  RubikObject(int size, Static4D quatCur, Static4D quatAcc, DistortedTexture texture, MeshRectangles mesh, DistortedEffects effects)
67
    {
68
    super(texture,effects,mesh);
69

    
70
    LEGAL_QUATS = getLegalQuats();
71
    mNodeTexture = texture;
72
    mSize = size;
73

    
74
    mRotationAngleStatic = new Static1D(0);
75
    mRotationAngleMiddle = new Static1D(0);
76
    mRotationAngleFinal  = new Static1D(0);
77

    
78
    mMove     = new Static3D(0,0,0);
79
    mScale    = new Static3D(1,1,1);
80
    mNodeMove = new Static3D(0,0,0);
81
    mNodeScale= new Static3D(1,1,1);
82

    
83
    mQuatAccumulated = quatAcc;
84

    
85
    Static3D sinkCenter = new Static3D(TEXTURE_SIZE*0.5f, TEXTURE_SIZE*0.5f, TEXTURE_SIZE*0.5f);
86
    Static3D matrCenter = new Static3D(0,0,0);
87
    Static4D region = new Static4D(0,0,0, TEXTURE_SIZE*0.72f);
88

    
89
    mSinkEffect = new VertexEffectSink( new Static1D(getSinkStrength()), sinkCenter, region );
90
    mMoveEffect = new MatrixEffectMove(mMove);
91
    mScaleEffect = new MatrixEffectScale(mScale);
92
    mQuatCEffect = new MatrixEffectQuaternion(quatCur, matrCenter);
93
    mQuatAEffect = new MatrixEffectQuaternion(quatAcc, matrCenter);
94

    
95
    MatrixEffectMove  nodeMoveEffect  = new MatrixEffectMove(mNodeMove);
96
    MatrixEffectScale nodeScaleEffect = new MatrixEffectScale(mNodeScale);
97

    
98
    effects.apply(nodeScaleEffect);
99
    effects.apply(nodeMoveEffect);
100
    }
101

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

    
104
  void resetRotationAngle(Dynamic1D rotationAngle)
105
    {
106
    rotationAngle.setDuration(POST_ROTATION_MILLISEC);
107
    rotationAngle.resetToBeginning();
108
    rotationAngle.removeAll();
109
    rotationAngle.add(mRotationAngleStatic);
110
    rotationAngle.add(mRotationAngleMiddle);
111
    rotationAngle.add(mRotationAngleFinal);
112
    }
113

    
114
///////////////////////////////////////////////////////////////////////////////////////////////////
115

    
116
  private float getSinkStrength()
117
    {
118
    switch(mSize)
119
      {
120
      case 1 : return 1.1f;
121
      case 2 : return 1.5f;
122
      case 3 : return 1.8f;
123
      case 4 : return 2.0f;
124
      default: return 3.0f - 4.0f/mSize;
125
      }
126
    }
127

    
128
///////////////////////////////////////////////////////////////////////////////////////////////////
129

    
130
  public int getSize()
131
    {
132
    return mSize;
133
    }
134

    
135
///////////////////////////////////////////////////////////////////////////////////////////////////
136

    
137
  public void continueRotation(float angleInDegrees)
138
    {
139
    mRotationAngleStatic.set0(angleInDegrees);
140
    }
141

    
142
///////////////////////////////////////////////////////////////////////////////////////////////////
143

    
144
  public Static4D getRotationQuat()
145
      {
146
      return mQuatAccumulated;
147
      }
148

    
149
///////////////////////////////////////////////////////////////////////////////////////////////////
150

    
151
  public void recomputeScaleFactor(int screenWidth, int screenHeight)
152
    {
153
    int texW = mNodeTexture.getWidth();
154
    int texH = mNodeTexture.getHeight();
155

    
156
    if( (float)texH/texW > (float)screenHeight/screenWidth )
157
      {
158
      int w = (screenHeight*texW)/texH;
159
      float factor = (float)screenHeight/texH;
160
      mNodeMove.set((screenWidth-w)*0.5f ,0, 0);
161
      mNodeScale.set(factor,factor,factor);
162
      }
163
    else
164
      {
165
      int h = (screenWidth*texH)/texW;
166
      float factor = (float)screenWidth/texW;
167
      mNodeMove.set(0,(screenHeight-h)*0.5f,0);
168
      mNodeScale.set(factor,factor,factor);
169
      }
170

    
171
    float scaleFactor = (OBJECT_SCREEN_RATIO*texW/(TEXTURE_SIZE*mSize));
172

    
173
    mMove.set( texW*0.5f , texH*0.5f , 0.0f );
174
    mScale.set(scaleFactor,scaleFactor,scaleFactor);
175
    }
176

    
177
///////////////////////////////////////////////////////////////////////////////////////////////////
178

    
179
  abstract float[] getLegalQuats();
180

    
181
  public abstract void savePreferences(SharedPreferences.Editor editor);
182
  public abstract void restorePreferences(SharedPreferences preferences);
183

    
184
  public abstract void beginNewRotation(int vector, int row );
185
  public abstract long addNewRotation(int vector, int row, int angle, long durationMillis, EffectListener listener );
186
  public abstract long finishRotationNow(EffectListener listener);
187
  public abstract void removeRotationNow();
188

    
189
  public abstract void apply(Effect effect, int position);
190
  public abstract void remove(long effectID);
191

    
192
  public abstract void releaseResources();
193
  public abstract void createTexture();
194

    
195
  public abstract void solve();
196
  public abstract boolean isSolved();
197
  }
(4-4/6)