Project

General

Profile

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

magiccube / src / main / java / org / distorted / object / RubikObject.java @ 74686c71

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.MeshFlat;
33
import org.distorted.library.message.EffectListener;
34
import org.distorted.library.type.Static1D;
35
import org.distorted.library.type.Static3D;
36
import org.distorted.library.type.Static4D;
37

    
38
///////////////////////////////////////////////////////////////////////////////////////////////////
39

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

    
46
  private Static3D mMove, mScale, mNodeMove, mNodeScale;
47
  private Static4D mQuatAccumulated;
48
  private DistortedTexture mNodeTexture;
49

    
50
  int mSize, mRotAxis, mRotRow;
51

    
52
  Static1D mRotationAngleStatic, mRotationAngleMiddle, mRotationAngleFinal;
53
  DistortedTexture mTexture;
54

    
55
  VertexEffectSink mSinkEffect;
56
  MatrixEffectMove mMoveEffect;
57
  MatrixEffectScale mScaleEffect;
58
  MatrixEffectQuaternion mQuatCEffect;
59
  MatrixEffectQuaternion mQuatAEffect;
60

    
61
///////////////////////////////////////////////////////////////////////////////////////////////////
62

    
63
  RubikObject(int size, Static4D quatCur, Static4D quatAcc, DistortedTexture texture, MeshFlat mesh, DistortedEffects effects)
64
    {
65
    super(texture,effects,mesh);
66

    
67
    mNodeTexture = texture;
68

    
69
    mSize = size;
70

    
71
    mRotationAngleStatic = new Static1D(0);
72
    mRotationAngleMiddle = new Static1D(0);
73
    mRotationAngleFinal  = new Static1D(0);
74

    
75
    mMove     = new Static3D(0,0,0);
76
    mScale    = new Static3D(1,1,1);
77
    mNodeMove = new Static3D(0,0,0);
78
    mNodeScale= new Static3D(1,1,1);
79

    
80
    mQuatAccumulated = quatAcc;
81

    
82
    Static3D sinkCenter = new Static3D(TEXTURE_SIZE*0.5f, TEXTURE_SIZE*0.5f, TEXTURE_SIZE*0.5f);
83
    Static3D matrCenter = new Static3D(0,0,0);
84
    Static4D region = new Static4D(0,0,0, TEXTURE_SIZE*0.72f);
85

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

    
92
    MatrixEffectMove  nodeMoveEffect  = new MatrixEffectMove(mNodeMove);
93
    MatrixEffectScale nodeScaleEffect = new MatrixEffectScale(mNodeScale);
94

    
95
    effects.apply(nodeScaleEffect);
96
    effects.apply(nodeMoveEffect);
97
    }
98

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

    
101
  int computeNearestAngle(float angle)
102
    {
103
    final int NEAREST = 90;
104

    
105
    int tmp = (int)((angle+NEAREST/2)/NEAREST);
106
    if( angle< -(NEAREST*0.5) ) tmp-=1;
107

    
108
    return NEAREST*tmp;
109
    }
110

    
111
///////////////////////////////////////////////////////////////////////////////////////////////////
112

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

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

    
127
  public int getSize()
128
    {
129
    return mSize;
130
    }
131

    
132
///////////////////////////////////////////////////////////////////////////////////////////////////
133

    
134
  public void continueRotation(float angleInDegrees)
135
    {
136
    mRotationAngleStatic.set0(angleInDegrees);
137
    }
138

    
139
///////////////////////////////////////////////////////////////////////////////////////////////////
140

    
141
  public Static4D getRotationQuat()
142
      {
143
      return mQuatAccumulated;
144
      }
145

    
146
///////////////////////////////////////////////////////////////////////////////////////////////////
147

    
148
  public void recomputeScaleFactor(int screenWidth, int screenHeight)
149
    {
150
    int texW = mNodeTexture.getWidth();
151
    int texH = mNodeTexture.getHeight();
152

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

    
168
    float scaleFactor = (OBJECT_SCREEN_RATIO*texW/(TEXTURE_SIZE*mSize));
169

    
170
    mMove.set( texW*0.5f , texH*0.5f , 0.0f );
171
    mScale.set(scaleFactor,scaleFactor,scaleFactor);
172
    }
173

    
174
///////////////////////////////////////////////////////////////////////////////////////////////////
175

    
176
  public abstract void savePreferences(SharedPreferences.Editor editor);
177
  public abstract void restorePreferences(SharedPreferences preferences);
178

    
179
  public abstract void beginNewRotation(int vector, int row );
180
  public abstract long addNewRotation(int vector, int row, int angle, long durationMillis, EffectListener listener );
181
  public abstract long finishRotationNow(EffectListener listener);
182
  public abstract void removeRotationNow();
183

    
184
  public abstract void apply(Effect effect, int position);
185
  public abstract void remove(long effectID);
186

    
187
  public abstract void releaseResources();
188
  public abstract void createTexture();
189

    
190
  public abstract void solve();
191
  public abstract boolean isSolved();
192
  }
(3-3/5)