Project

General

Profile

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

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

1 a7d8c3cd Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 804293f0 Leszek Koltunski
import java.io.InputStream;
23
import java.util.ArrayList;
24
25
import android.app.Activity;
26 400ff34d Leszek Koltunski
import android.content.SharedPreferences;
27
28 804293f0 Leszek Koltunski
import org.distorted.jsons.ObjectJson;
29 400ff34d Leszek Koltunski
import org.distorted.main.RubikActivity;
30 d433b50e Leszek Koltunski
import org.distorted.objectlib.main.ObjectConstants;
31 a7d8c3cd Leszek Koltunski
import org.distorted.objectlib.main.ObjectType;
32 400ff34d Leszek Koltunski
import org.distorted.screens.RubikScreenPlay;
33
import org.distorted.screens.ScreenList;
34 a7d8c3cd Leszek Koltunski
35 09cf2a36 Leszek Koltunski
import static org.distorted.objectlib.main.TwistyObject.MESH_NICE;
36 a7d8c3cd Leszek Koltunski
import static org.distorted.objectlib.main.ObjectType.NUM_OBJECTS;
37
38
///////////////////////////////////////////////////////////////////////////////////////////////////
39
40
public class RubikObjectList
41
{
42 400ff34d Leszek Koltunski
  public static final int DEF_OBJECT= ObjectConstants.CUBE_3;
43
44 d433b50e Leszek Koltunski
  public static int MAX_LEVEL;
45
46 a7d8c3cd Leszek Koltunski
  private static RubikObjectList mType;
47
  private static int mNumObjects;
48 fcf7320f Leszek Koltunski
  private static int mNumExtras;
49 a7d8c3cd Leszek Koltunski
  private static ArrayList<RubikObject> mObjects;
50 400ff34d Leszek Koltunski
  private static int mObject = DEF_OBJECT;
51 a7d8c3cd Leszek Koltunski
52 d433b50e Leszek Koltunski
  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 a7d8c3cd Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
66
67
  private RubikObjectList()
68
    {
69 fcf7320f Leszek Koltunski
    mNumObjects= 0;
70
    mNumExtras = 0;
71 804293f0 Leszek Koltunski
72 a7d8c3cd Leszek Koltunski
    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 d433b50e Leszek Koltunski
      RubikObject obj = new RubikObject(type);
85 a7d8c3cd Leszek Koltunski
      mObjects.add(obj);
86
      mNumObjects++;
87 804293f0 Leszek Koltunski
88 fcf7320f Leszek Koltunski
      if( obj.getExtrasJsonID()!=0 )
89 804293f0 Leszek Koltunski
        {
90 fcf7320f Leszek Koltunski
        obj.setExtrasOrdinal(mNumExtras);
91
        mNumExtras++;
92 804293f0 Leszek Koltunski
        }
93 a7d8c3cd Leszek Koltunski
      }
94
    }
95
96
///////////////////////////////////////////////////////////////////////////////////////////////////
97
// PUBLIC API
98 d433b50e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 806329e3 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
125
126
  public static void addDownloadedObject(String shortName, boolean icon, boolean object, boolean extras)
127
    {
128
129
    }
130
131 f12e4de9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 400ff34d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
146
147
  public static void savePreferences(SharedPreferences.Editor editor)
148
    {
149 f12e4de9 Leszek Koltunski
    RubikObject obj = getObject(mObject);
150
    if( obj!=null ) editor.putString("rol_objName", obj.getName() );
151
    }
152 400ff34d Leszek Koltunski
153 f12e4de9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
154
155
  public static void saveMeshState(SharedPreferences.Editor editor)
156
    {
157
    for(int i=0; i<mNumObjects; i++)
158 400ff34d Leszek Koltunski
      {
159 f12e4de9 Leszek Koltunski
      RubikObject obj = getObject(i);
160
161
      if( obj!=null )
162
        {
163
        String name = obj.getName();
164
        editor.putInt("rol_"+name, obj.getMeshState() );
165
        }
166 400ff34d Leszek Koltunski
      }
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 f12e4de9 Leszek Koltunski
    String objName= preferences.getString("rol_objName",defName);
176 400ff34d Leszek Koltunski
    mObject = getOrdinal(objName);
177
178
    if( mObject<0 || mObject>=mNumObjects ) mObject = DEF_OBJECT;
179
    }
180
181 f12e4de9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 400ff34d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 d433b50e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
221 a7d8c3cd Leszek Koltunski
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 804293f0 Leszek Koltunski
252 fcf7320f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
253
254
  public static int getNumExtrasObjects()
255
    {
256
    return mNumExtras;
257
    }
258
259 804293f0 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
260
261
  public static int getNumTutorialObjects()
262
    {
263 fcf7320f Leszek Koltunski
    return mNumExtras;
264 804293f0 Leszek Koltunski
    }
265
266
///////////////////////////////////////////////////////////////////////////////////////////////////
267
268 fcf7320f Leszek Koltunski
  public static InputStream getExtrasStream(int extrasOrdinal, Activity act)
269 804293f0 Leszek Koltunski
    {
270 fcf7320f Leszek Koltunski
    int objectOrdinal = getObjectOrdinal(extrasOrdinal);
271 804293f0 Leszek Koltunski
    RubikObject object= getObject(objectOrdinal);
272
273
    if( object!=null )
274
      {
275 fcf7320f Leszek Koltunski
      int jsonID = object.getExtrasJsonID();
276
      return ObjectJson.getExtrasStream(jsonID,act);
277 804293f0 Leszek Koltunski
      }
278
279
    return null;
280
    }
281
282
///////////////////////////////////////////////////////////////////////////////////////////////////
283
284 fcf7320f Leszek Koltunski
  public static int getObjectOrdinal(int extrasOrdinal)
285 804293f0 Leszek Koltunski
    {
286 fcf7320f Leszek Koltunski
    for(int i=extrasOrdinal; i<mNumObjects; i++)
287 804293f0 Leszek Koltunski
      {
288
      RubikObject object = getObject(i);
289 fcf7320f Leszek Koltunski
      int extOrd = object!=null ? object.getExtrasOrdinal() : -1;
290
      if( extOrd==extrasOrdinal ) return i;
291 804293f0 Leszek Koltunski
      }
292
293
    return -1;
294
    }
295
296 fcf7320f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
297
298
  public static int getExtrasOrdinal(int objectOrdinal)
299
    {
300
    RubikObject object = getObject(objectOrdinal);
301
    return object!=null ? object.getExtrasOrdinal() : -1;
302
    }
303
304 804293f0 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
305
306
  public static int getTutorialOrdinal(int objectOrdinal)
307
    {
308
    RubikObject object = getObject(objectOrdinal);
309 fcf7320f Leszek Koltunski
    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 804293f0 Leszek Koltunski
    }
327 a7d8c3cd Leszek Koltunski
}