Project

General

Profile

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

magiccube / src / main / java / org / distorted / objects / RubikObjectList.java @ 506f7ceb

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 d433b50e Leszek Koltunski
  public static int MAX_LEVEL;
44
45 506f7ceb Leszek Koltunski
  private static RubikObjectList mThis;
46 a7d8c3cd Leszek Koltunski
  private static int mNumObjects;
47 fcf7320f Leszek Koltunski
  private static int mNumExtras;
48 a7d8c3cd Leszek Koltunski
  private static ArrayList<RubikObject> mObjects;
49 400ff34d Leszek Koltunski
  private static int mObject = DEF_OBJECT;
50 a7d8c3cd Leszek Koltunski
51 506f7ceb Leszek Koltunski
  private static class DownloadedObject
52
    {
53
    String shortName;
54
    boolean icon,object,extras;
55
56
    DownloadedObject(String sName, boolean i, boolean o, boolean e)
57
      {
58
      shortName = sName;
59
      icon   = i;
60
      object = o;
61
      extras = e;
62
      }
63
    }
64
65
  private static ArrayList<DownloadedObject> mDownloadedObjects;
66
67 d433b50e Leszek Koltunski
  static
68
    {
69
    int max = Integer.MIN_VALUE;
70
71
    for (int i=0; i<NUM_OBJECTS; i++)
72
      {
73
      int cur = getDBLevel(i);
74
      if( cur>max ) max = cur;
75
      }
76
77
    MAX_LEVEL = max;
78
    }
79
80 a7d8c3cd Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
81
82
  private RubikObjectList()
83
    {
84 fcf7320f Leszek Koltunski
    mNumObjects= 0;
85
    mNumExtras = 0;
86 804293f0 Leszek Koltunski
87 506f7ceb Leszek Koltunski
    mObjects           = new ArrayList<>();
88
    mDownloadedObjects = new ArrayList<>();
89 a7d8c3cd Leszek Koltunski
90
    createBuiltinObjects();
91
    }
92
93
///////////////////////////////////////////////////////////////////////////////////////////////////
94
95
  private void createBuiltinObjects()
96
    {
97
    for(int i=0; i<NUM_OBJECTS; i++)
98
      {
99
      ObjectType type = ObjectType.getObject(i);
100 d433b50e Leszek Koltunski
      RubikObject obj = new RubikObject(type);
101 a7d8c3cd Leszek Koltunski
      mObjects.add(obj);
102
      mNumObjects++;
103 804293f0 Leszek Koltunski
104 fcf7320f Leszek Koltunski
      if( obj.getExtrasJsonID()!=0 )
105 804293f0 Leszek Koltunski
        {
106 fcf7320f Leszek Koltunski
        obj.setExtrasOrdinal(mNumExtras);
107
        mNumExtras++;
108 804293f0 Leszek Koltunski
        }
109 a7d8c3cd Leszek Koltunski
      }
110
    }
111
112 506f7ceb Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
113
114
  private static void internalAddDownloadedObject(DownloadedObject object)
115
    {
116
117
    }
118
119 a7d8c3cd Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
120
// PUBLIC API
121 d433b50e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
122
// historically older versions of the app had lower 'maxScrambles' in case of several objects and
123
// those got remembered in the server-side DB already, so we need to keep using them. This function
124
// provides a map between 'maxScramble' of an object and its 'dbLevel'. All new objects will have
125
// those two values the same.
126
127
  public static int getDBLevel(int ordinal)
128
    {
129
    if( ordinal==ObjectConstants.CUBE_3 ) return 16;
130
    if( ordinal==ObjectConstants.CUBE_4 ) return 20;
131
    if( ordinal==ObjectConstants.CUBE_5 ) return 24;
132
    if( ordinal==ObjectConstants.PYRA_4 ) return 15;
133
    if( ordinal==ObjectConstants.PYRA_5 ) return 20;
134
    if( ordinal==ObjectConstants.MEGA_5 ) return 35;
135
    if( ordinal==ObjectConstants.DIAM_2 ) return 10;
136
    if( ordinal==ObjectConstants.DIAM_3 ) return 18;
137
    if( ordinal==ObjectConstants.REDI_3 ) return 14;
138
    if( ordinal==ObjectConstants.HELI_3 ) return 18;
139
    if( ordinal==ObjectConstants.SKEW_3 ) return 17;
140
    if( ordinal==ObjectConstants.REX_3  ) return 16;
141
    if( ordinal==ObjectConstants.MIRR_3 ) return 16;
142
143
    ObjectType type = ObjectType.getObject(ordinal);
144
    return type.getNumScramble();
145
    }
146
147 806329e3 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
148
149
  public static void addDownloadedObject(String shortName, boolean icon, boolean object, boolean extras)
150
    {
151 506f7ceb Leszek Koltunski
    for( DownloadedObject obj : mDownloadedObjects )
152
      {
153
      if( obj.shortName.equals(shortName) )
154
        {
155
        obj.icon  |= icon;
156
        obj.object|= object;
157
        obj.extras|= extras;
158
159
        android.util.Log.e("D", "UPDATING "+shortName);
160
161
        return;
162
        }
163
      }
164
165
    DownloadedObject extra = new DownloadedObject(shortName,icon,object,extras);
166
    mDownloadedObjects.add(extra);
167 806329e3 Leszek Koltunski
168 506f7ceb Leszek Koltunski
    internalAddDownloadedObject(extra);
169 806329e3 Leszek Koltunski
    }
170
171 f12e4de9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
172
173
  public static void setMeshState(int ordinal, int state)
174
    {
175
    if( ordinal>=0 && ordinal<mNumObjects ) mObjects.get(ordinal).setMeshState(state);
176
    }
177
178
///////////////////////////////////////////////////////////////////////////////////////////////////
179
180
  public static int getMeshState(int ordinal)
181
    {
182
    return (ordinal>=0 && ordinal<mNumObjects) ? mObjects.get(ordinal).getMeshState() : MESH_NICE;
183
    }
184
185 400ff34d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
186
187
  public static void savePreferences(SharedPreferences.Editor editor)
188
    {
189 f12e4de9 Leszek Koltunski
    RubikObject obj = getObject(mObject);
190
    if( obj!=null ) editor.putString("rol_objName", obj.getName() );
191 506f7ceb Leszek Koltunski
192
    int numDownloaded = mDownloadedObjects.size();
193
194
    if( numDownloaded>0 )
195
      {
196
      StringBuilder downloadedObjects = new StringBuilder();
197
198
      for(int i=0; i<numDownloaded; i++)
199
        {
200
        if( i>0 ) downloadedObjects.append(',');
201
202
        DownloadedObject object = mDownloadedObjects.get(i);
203
        downloadedObjects.append(object.shortName);
204
        downloadedObjects.append(' ');
205
        downloadedObjects.append(object.icon   ? "1":"0");
206
        downloadedObjects.append(' ');
207
        downloadedObjects.append(object.object ? "1":"0");
208
        downloadedObjects.append(' ');
209
        downloadedObjects.append(object.extras ? "1":"0");
210
        }
211
212
      editor.putString("rol_downloaded", downloadedObjects.toString() );
213
      }
214 f12e4de9 Leszek Koltunski
    }
215 400ff34d Leszek Koltunski
216 f12e4de9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
217
218
  public static void saveMeshState(SharedPreferences.Editor editor)
219
    {
220
    for(int i=0; i<mNumObjects; i++)
221 400ff34d Leszek Koltunski
      {
222 f12e4de9 Leszek Koltunski
      RubikObject obj = getObject(i);
223
224
      if( obj!=null )
225
        {
226
        String name = obj.getName();
227
        editor.putInt("rol_"+name, obj.getMeshState() );
228
        }
229 400ff34d Leszek Koltunski
      }
230
    }
231
232
///////////////////////////////////////////////////////////////////////////////////////////////////
233
234
  public static void restorePreferences(SharedPreferences preferences)
235
    {
236
    RubikObject object = getObject(DEF_OBJECT);
237
    String defName = object==null ? "CUBE_3" : object.getName();
238 f12e4de9 Leszek Koltunski
    String objName= preferences.getString("rol_objName",defName);
239 400ff34d Leszek Koltunski
    mObject = getOrdinal(objName);
240
241
    if( mObject<0 || mObject>=mNumObjects ) mObject = DEF_OBJECT;
242 506f7ceb Leszek Koltunski
243
    String downloaded = preferences.getString("rol_downloaded","");
244
245
    android.util.Log.e("D", downloaded);
246
247
    if( !downloaded.equals(""))
248
      {
249
      String[] dObjects = downloaded.split(",");
250
251
      for(String dObj : dObjects)
252
        {
253
        String[] parts = dObj.split(" ");
254
255
        if( parts.length==4 )
256
          {
257
          String name = parts[0];
258
          String icon = parts[1];
259
          String obje = parts[2];
260
          String extr = parts[3];
261
262
          boolean bIcon = icon.equals("1");
263
          boolean bObje = obje.equals("1");
264
          boolean bExtr = extr.equals("1");
265
266
          android.util.Log.e("D", "adding: "+name);
267
268
          addDownloadedObject(name,bIcon,bObje,bExtr);
269
          }
270
        }
271
      }
272 400ff34d Leszek Koltunski
    }
273
274 f12e4de9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
275
276
  public static void restoreMeshState(SharedPreferences preferences)
277
    {
278
    for(int i=0; i<mNumObjects; i++)
279
      {
280
      RubikObject obj = getObject(i);
281
282
      if( obj!=null )
283
        {
284
        String name  = obj.getName();
285
        int meshState= preferences.getInt("rol_"+name,MESH_NICE);
286
        obj.setMeshState(meshState);
287
        }
288
      }
289
    }
290
291 400ff34d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
292
293
  public static boolean setCurrObject(RubikActivity act, int ordinal)
294
    {
295
    if( mObject!=ordinal )
296
      {
297
      mObject = ordinal;
298
      RubikScreenPlay play = (RubikScreenPlay) ScreenList.PLAY.getScreenClass();
299
      play.setCurrObject(act);
300
      return true;
301
      }
302
303
    return false;
304
    }
305
306
///////////////////////////////////////////////////////////////////////////////////////////////////
307
308
  public static int getCurrObject()
309
    {
310
    return mObject;
311
    }
312
313 d433b50e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
314 a7d8c3cd Leszek Koltunski
315
  public static RubikObject getObject(int ordinal)
316
    {
317 506f7ceb Leszek Koltunski
    if( mThis==null ) mThis = new RubikObjectList();
318 a7d8c3cd Leszek Koltunski
    return ordinal>=0 && ordinal<mNumObjects ? mObjects.get(ordinal) : null;
319
    }
320
321
///////////////////////////////////////////////////////////////////////////////////////////////////
322
323
  public static int getNumObjects()
324
    {
325 506f7ceb Leszek Koltunski
    if( mThis==null ) mThis = new RubikObjectList();
326 a7d8c3cd Leszek Koltunski
    return mNumObjects;
327
    }
328
329
///////////////////////////////////////////////////////////////////////////////////////////////////
330
331
  public static int getOrdinal(String name)
332
    {
333 506f7ceb Leszek Koltunski
    if( mThis==null ) mThis = new RubikObjectList();
334 a7d8c3cd Leszek Koltunski
335
    for(int i=0; i<mNumObjects; i++)
336
      {
337
      RubikObject obj = mObjects.get(i);
338
339
      if( obj.getName().equals(name) ) return i;
340
      }
341
342
    return -1;
343
    }
344 804293f0 Leszek Koltunski
345 fcf7320f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
346
347
  public static int getNumExtrasObjects()
348
    {
349
    return mNumExtras;
350
    }
351
352 804293f0 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
353
354
  public static int getNumTutorialObjects()
355
    {
356 fcf7320f Leszek Koltunski
    return mNumExtras;
357 804293f0 Leszek Koltunski
    }
358
359
///////////////////////////////////////////////////////////////////////////////////////////////////
360
361 fcf7320f Leszek Koltunski
  public static InputStream getExtrasStream(int extrasOrdinal, Activity act)
362 804293f0 Leszek Koltunski
    {
363 fcf7320f Leszek Koltunski
    int objectOrdinal = getObjectOrdinal(extrasOrdinal);
364 804293f0 Leszek Koltunski
    RubikObject object= getObject(objectOrdinal);
365
366
    if( object!=null )
367
      {
368 fcf7320f Leszek Koltunski
      int jsonID = object.getExtrasJsonID();
369
      return ObjectJson.getExtrasStream(jsonID,act);
370 804293f0 Leszek Koltunski
      }
371
372
    return null;
373
    }
374
375
///////////////////////////////////////////////////////////////////////////////////////////////////
376
377 fcf7320f Leszek Koltunski
  public static int getObjectOrdinal(int extrasOrdinal)
378 804293f0 Leszek Koltunski
    {
379 fcf7320f Leszek Koltunski
    for(int i=extrasOrdinal; i<mNumObjects; i++)
380 804293f0 Leszek Koltunski
      {
381
      RubikObject object = getObject(i);
382 fcf7320f Leszek Koltunski
      int extOrd = object!=null ? object.getExtrasOrdinal() : -1;
383
      if( extOrd==extrasOrdinal ) return i;
384 804293f0 Leszek Koltunski
      }
385
386
    return -1;
387
    }
388
389 fcf7320f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
390
391
  public static int getExtrasOrdinal(int objectOrdinal)
392
    {
393
    RubikObject object = getObject(objectOrdinal);
394
    return object!=null ? object.getExtrasOrdinal() : -1;
395
    }
396
397 804293f0 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
398
399
  public static int getTutorialOrdinal(int objectOrdinal)
400
    {
401
    RubikObject object = getObject(objectOrdinal);
402 fcf7320f Leszek Koltunski
    return object!=null ? object.getExtrasOrdinal() : -1;
403
    }
404
405
///////////////////////////////////////////////////////////////////////////////////////////////////
406
407
  public static int getLocalObjectMinor(int objectOrdinal)
408
    {
409
    RubikObject object = getObject(objectOrdinal);
410
    return object!=null ? object.getObjectMinor() : -1;
411
    }
412
413
///////////////////////////////////////////////////////////////////////////////////////////////////
414
415
  public static int getLocalExtrasMinor(int objectOrdinal)
416
    {
417
    RubikObject object = getObject(objectOrdinal);
418
    return object!=null ? object.getExtrasMinor() : -1;
419 804293f0 Leszek Koltunski
    }
420 a7d8c3cd Leszek Koltunski
}