Project

General

Profile

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

magiccube / src / main / java / org / distorted / objects / RubikObjectList.java @ 804293f0

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2021 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.objects;
21

    
22
import java.io.InputStream;
23
import java.util.ArrayList;
24

    
25
import android.app.Activity;
26
import android.content.SharedPreferences;
27

    
28
import org.distorted.jsons.ObjectJson;
29
import org.distorted.main.RubikActivity;
30
import org.distorted.objectlib.main.ObjectConstants;
31
import org.distorted.objectlib.main.ObjectType;
32
import org.distorted.screens.RubikScreenPlay;
33
import org.distorted.screens.ScreenList;
34

    
35
import static org.distorted.objectlib.main.TwistyObject.MESH_NICE;
36
import static org.distorted.objectlib.main.ObjectType.NUM_OBJECTS;
37

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

    
40
public class RubikObjectList
41
{
42
  public static final int DEF_OBJECT= ObjectConstants.CUBE_3;
43

    
44
  public static int MAX_LEVEL;
45

    
46
  private static RubikObjectList mType;
47
  private static int mNumObjects;
48
  private static int mNumTutorials;
49
  private static ArrayList<RubikObject> mObjects;
50
  private static int mObject = DEF_OBJECT;
51

    
52
  static
53
    {
54
    int max = Integer.MIN_VALUE;
55

    
56
    for (int i=0; i<NUM_OBJECTS; i++)
57
      {
58
      int cur = getDBLevel(i);
59
      if( cur>max ) max = cur;
60
      }
61

    
62
    MAX_LEVEL = max;
63
    }
64

    
65
///////////////////////////////////////////////////////////////////////////////////////////////////
66

    
67
  private RubikObjectList()
68
    {
69
    mNumObjects   = 0;
70
    mNumTutorials = 0;
71

    
72
    mObjects = new ArrayList<>();
73

    
74
    createBuiltinObjects();
75
    }
76

    
77
///////////////////////////////////////////////////////////////////////////////////////////////////
78

    
79
  private void createBuiltinObjects()
80
    {
81
    for(int i=0; i<NUM_OBJECTS; i++)
82
      {
83
      ObjectType type = ObjectType.getObject(i);
84
      RubikObject obj = new RubikObject(type);
85
      mObjects.add(obj);
86
      mNumObjects++;
87

    
88
      if( obj.getTutorialJsonID()!=0 )
89
        {
90
        obj.setTutorialOrdinal(mNumTutorials);
91
        mNumTutorials++;
92
        }
93
      }
94
    }
95

    
96
///////////////////////////////////////////////////////////////////////////////////////////////////
97
// PUBLIC API
98
///////////////////////////////////////////////////////////////////////////////////////////////////
99
// historically older versions of the app had lower 'maxScrambles' in case of several objects and
100
// those got remembered in the server-side DB already, so we need to keep using them. This function
101
// provides a map between 'maxScramble' of an object and its 'dbLevel'. All new objects will have
102
// those two values the same.
103

    
104
  public static int getDBLevel(int ordinal)
105
    {
106
    if( ordinal==ObjectConstants.CUBE_3 ) return 16;
107
    if( ordinal==ObjectConstants.CUBE_4 ) return 20;
108
    if( ordinal==ObjectConstants.CUBE_5 ) return 24;
109
    if( ordinal==ObjectConstants.PYRA_4 ) return 15;
110
    if( ordinal==ObjectConstants.PYRA_5 ) return 20;
111
    if( ordinal==ObjectConstants.MEGA_5 ) return 35;
112
    if( ordinal==ObjectConstants.DIAM_2 ) return 10;
113
    if( ordinal==ObjectConstants.DIAM_3 ) return 18;
114
    if( ordinal==ObjectConstants.REDI_3 ) return 14;
115
    if( ordinal==ObjectConstants.HELI_3 ) return 18;
116
    if( ordinal==ObjectConstants.SKEW_3 ) return 17;
117
    if( ordinal==ObjectConstants.REX_3  ) return 16;
118
    if( ordinal==ObjectConstants.MIRR_3 ) return 16;
119

    
120
    ObjectType type = ObjectType.getObject(ordinal);
121
    return type.getNumScramble();
122
    }
123

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

    
126
  public static void setMeshState(int ordinal, int state)
127
    {
128
    if( ordinal>=0 && ordinal<mNumObjects ) mObjects.get(ordinal).setMeshState(state);
129
    }
130

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

    
133
  public static int getMeshState(int ordinal)
134
    {
135
    return (ordinal>=0 && ordinal<mNumObjects) ? mObjects.get(ordinal).getMeshState() : MESH_NICE;
136
    }
137

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

    
140
  public static void savePreferences(SharedPreferences.Editor editor)
141
    {
142
    RubikObject obj = getObject(mObject);
143
    if( obj!=null ) editor.putString("rol_objName", obj.getName() );
144
    }
145

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

    
148
  public static void saveMeshState(SharedPreferences.Editor editor)
149
    {
150
    for(int i=0; i<mNumObjects; i++)
151
      {
152
      RubikObject obj = getObject(i);
153

    
154
      if( obj!=null )
155
        {
156
        String name = obj.getName();
157
        editor.putInt("rol_"+name, obj.getMeshState() );
158
        }
159
      }
160
    }
161

    
162
///////////////////////////////////////////////////////////////////////////////////////////////////
163

    
164
  public static void restorePreferences(SharedPreferences preferences)
165
    {
166
    RubikObject object = getObject(DEF_OBJECT);
167
    String defName = object==null ? "CUBE_3" : object.getName();
168
    String objName= preferences.getString("rol_objName",defName);
169
    mObject = getOrdinal(objName);
170

    
171
    if( mObject<0 || mObject>=mNumObjects ) mObject = DEF_OBJECT;
172
    }
173

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

    
176
  public static void restoreMeshState(SharedPreferences preferences)
177
    {
178
    for(int i=0; i<mNumObjects; i++)
179
      {
180
      RubikObject obj = getObject(i);
181

    
182
      if( obj!=null )
183
        {
184
        String name  = obj.getName();
185
        int meshState= preferences.getInt("rol_"+name,MESH_NICE);
186
        obj.setMeshState(meshState);
187
        }
188
      }
189
    }
190

    
191
///////////////////////////////////////////////////////////////////////////////////////////////////
192

    
193
  public static boolean setCurrObject(RubikActivity act, int ordinal)
194
    {
195
    if( mObject!=ordinal )
196
      {
197
      mObject = ordinal;
198
      RubikScreenPlay play = (RubikScreenPlay) ScreenList.PLAY.getScreenClass();
199
      play.setCurrObject(act);
200
      return true;
201
      }
202

    
203
    return false;
204
    }
205

    
206
///////////////////////////////////////////////////////////////////////////////////////////////////
207

    
208
  public static int getCurrObject()
209
    {
210
    return mObject;
211
    }
212

    
213
///////////////////////////////////////////////////////////////////////////////////////////////////
214

    
215
  public static RubikObject getObject(int ordinal)
216
    {
217
    if( mType==null ) mType = new RubikObjectList();
218
    return ordinal>=0 && ordinal<mNumObjects ? mObjects.get(ordinal) : null;
219
    }
220

    
221
///////////////////////////////////////////////////////////////////////////////////////////////////
222

    
223
  public static int getNumObjects()
224
    {
225
    if( mType==null ) mType = new RubikObjectList();
226
    return mNumObjects;
227
    }
228

    
229
///////////////////////////////////////////////////////////////////////////////////////////////////
230

    
231
  public static int getOrdinal(String name)
232
    {
233
    if( mType==null ) mType = new RubikObjectList();
234

    
235
    for(int i=0; i<mNumObjects; i++)
236
      {
237
      RubikObject obj = mObjects.get(i);
238

    
239
      if( obj.getName().equals(name) ) return i;
240
      }
241

    
242
    return -1;
243
    }
244

    
245
///////////////////////////////////////////////////////////////////////////////////////////////////
246

    
247
  public static int getNumTutorialObjects()
248
    {
249
    return mNumTutorials;
250
    }
251

    
252
///////////////////////////////////////////////////////////////////////////////////////////////////
253

    
254
  public static InputStream getTutorialStream(int tutorialOrdinal, Activity act)
255
    {
256
    int objectOrdinal = getObjectOrdinal(tutorialOrdinal);
257
    RubikObject object= getObject(objectOrdinal);
258

    
259
    if( object!=null )
260
      {
261
      int jsonID = object.getTutorialJsonID();
262
      return ObjectJson.getTutorialStream(jsonID,act);
263
      }
264

    
265
    return null;
266
    }
267

    
268
///////////////////////////////////////////////////////////////////////////////////////////////////
269

    
270
  public static int getObjectOrdinal(int tutorialOrdinal)
271
    {
272
    for(int i=tutorialOrdinal; i<mNumObjects; i++)
273
      {
274
      RubikObject object = getObject(i);
275
      int tutOrd = object!=null ? object.getTutorialOrdinal() : -1;
276
      if( tutOrd==tutorialOrdinal ) return i;
277
      }
278

    
279
    return -1;
280
    }
281

    
282
///////////////////////////////////////////////////////////////////////////////////////////////////
283

    
284
  public static int getTutorialOrdinal(int objectOrdinal)
285
    {
286
    RubikObject object = getObject(objectOrdinal);
287
    return object!=null ? object.getTutorialOrdinal() : -1;
288
    }
289
}
(2-2/2)