Project

General

Profile

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

magiccube / src / main / java / org / distorted / objects / RubikObjectList.java @ 806329e3

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 mNumExtras;
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
    mNumExtras = 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.getExtrasJsonID()!=0 )
89
        {
90
        obj.setExtrasOrdinal(mNumExtras);
91
        mNumExtras++;
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 addDownloadedObject(String shortName, boolean icon, boolean object, boolean extras)
127
    {
128

    
129
    }
130

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

    
133
  public static void setMeshState(int ordinal, int state)
134
    {
135
    if( ordinal>=0 && ordinal<mNumObjects ) mObjects.get(ordinal).setMeshState(state);
136
    }
137

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

    
140
  public static int getMeshState(int ordinal)
141
    {
142
    return (ordinal>=0 && ordinal<mNumObjects) ? mObjects.get(ordinal).getMeshState() : MESH_NICE;
143
    }
144

    
145
///////////////////////////////////////////////////////////////////////////////////////////////////
146

    
147
  public static void savePreferences(SharedPreferences.Editor editor)
148
    {
149
    RubikObject obj = getObject(mObject);
150
    if( obj!=null ) editor.putString("rol_objName", obj.getName() );
151
    }
152

    
153
///////////////////////////////////////////////////////////////////////////////////////////////////
154

    
155
  public static void saveMeshState(SharedPreferences.Editor editor)
156
    {
157
    for(int i=0; i<mNumObjects; i++)
158
      {
159
      RubikObject obj = getObject(i);
160

    
161
      if( obj!=null )
162
        {
163
        String name = obj.getName();
164
        editor.putInt("rol_"+name, obj.getMeshState() );
165
        }
166
      }
167
    }
168

    
169
///////////////////////////////////////////////////////////////////////////////////////////////////
170

    
171
  public static void restorePreferences(SharedPreferences preferences)
172
    {
173
    RubikObject object = getObject(DEF_OBJECT);
174
    String defName = object==null ? "CUBE_3" : object.getName();
175
    String objName= preferences.getString("rol_objName",defName);
176
    mObject = getOrdinal(objName);
177

    
178
    if( mObject<0 || mObject>=mNumObjects ) mObject = DEF_OBJECT;
179
    }
180

    
181
///////////////////////////////////////////////////////////////////////////////////////////////////
182

    
183
  public static void restoreMeshState(SharedPreferences preferences)
184
    {
185
    for(int i=0; i<mNumObjects; i++)
186
      {
187
      RubikObject obj = getObject(i);
188

    
189
      if( obj!=null )
190
        {
191
        String name  = obj.getName();
192
        int meshState= preferences.getInt("rol_"+name,MESH_NICE);
193
        obj.setMeshState(meshState);
194
        }
195
      }
196
    }
197

    
198
///////////////////////////////////////////////////////////////////////////////////////////////////
199

    
200
  public static boolean setCurrObject(RubikActivity act, int ordinal)
201
    {
202
    if( mObject!=ordinal )
203
      {
204
      mObject = ordinal;
205
      RubikScreenPlay play = (RubikScreenPlay) ScreenList.PLAY.getScreenClass();
206
      play.setCurrObject(act);
207
      return true;
208
      }
209

    
210
    return false;
211
    }
212

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

    
215
  public static int getCurrObject()
216
    {
217
    return mObject;
218
    }
219

    
220
///////////////////////////////////////////////////////////////////////////////////////////////////
221

    
222
  public static RubikObject getObject(int ordinal)
223
    {
224
    if( mType==null ) mType = new RubikObjectList();
225
    return ordinal>=0 && ordinal<mNumObjects ? mObjects.get(ordinal) : null;
226
    }
227

    
228
///////////////////////////////////////////////////////////////////////////////////////////////////
229

    
230
  public static int getNumObjects()
231
    {
232
    if( mType==null ) mType = new RubikObjectList();
233
    return mNumObjects;
234
    }
235

    
236
///////////////////////////////////////////////////////////////////////////////////////////////////
237

    
238
  public static int getOrdinal(String name)
239
    {
240
    if( mType==null ) mType = new RubikObjectList();
241

    
242
    for(int i=0; i<mNumObjects; i++)
243
      {
244
      RubikObject obj = mObjects.get(i);
245

    
246
      if( obj.getName().equals(name) ) return i;
247
      }
248

    
249
    return -1;
250
    }
251

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

    
254
  public static int getNumExtrasObjects()
255
    {
256
    return mNumExtras;
257
    }
258

    
259
///////////////////////////////////////////////////////////////////////////////////////////////////
260

    
261
  public static int getNumTutorialObjects()
262
    {
263
    return mNumExtras;
264
    }
265

    
266
///////////////////////////////////////////////////////////////////////////////////////////////////
267

    
268
  public static InputStream getExtrasStream(int extrasOrdinal, Activity act)
269
    {
270
    int objectOrdinal = getObjectOrdinal(extrasOrdinal);
271
    RubikObject object= getObject(objectOrdinal);
272

    
273
    if( object!=null )
274
      {
275
      int jsonID = object.getExtrasJsonID();
276
      return ObjectJson.getExtrasStream(jsonID,act);
277
      }
278

    
279
    return null;
280
    }
281

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

    
284
  public static int getObjectOrdinal(int extrasOrdinal)
285
    {
286
    for(int i=extrasOrdinal; i<mNumObjects; i++)
287
      {
288
      RubikObject object = getObject(i);
289
      int extOrd = object!=null ? object.getExtrasOrdinal() : -1;
290
      if( extOrd==extrasOrdinal ) return i;
291
      }
292

    
293
    return -1;
294
    }
295

    
296
///////////////////////////////////////////////////////////////////////////////////////////////////
297

    
298
  public static int getExtrasOrdinal(int objectOrdinal)
299
    {
300
    RubikObject object = getObject(objectOrdinal);
301
    return object!=null ? object.getExtrasOrdinal() : -1;
302
    }
303

    
304
///////////////////////////////////////////////////////////////////////////////////////////////////
305

    
306
  public static int getTutorialOrdinal(int objectOrdinal)
307
    {
308
    RubikObject object = getObject(objectOrdinal);
309
    return object!=null ? object.getExtrasOrdinal() : -1;
310
    }
311

    
312
///////////////////////////////////////////////////////////////////////////////////////////////////
313

    
314
  public static int getLocalObjectMinor(int objectOrdinal)
315
    {
316
    RubikObject object = getObject(objectOrdinal);
317
    return object!=null ? object.getObjectMinor() : -1;
318
    }
319

    
320
///////////////////////////////////////////////////////////////////////////////////////////////////
321

    
322
  public static int getLocalExtrasMinor(int objectOrdinal)
323
    {
324
    RubikObject object = getObject(objectOrdinal);
325
    return object!=null ? object.getExtrasMinor() : -1;
326
    }
327
}
(2-2/2)