Project

General

Profile

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

magiccube / src / main / java / org / distorted / objects / RubikObjectList.java @ 400ff34d

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 android.content.SharedPreferences;
23

    
24
import org.distorted.main.RubikActivity;
25
import org.distorted.objectlib.main.ObjectConstants;
26
import org.distorted.objectlib.main.ObjectType;
27
import org.distorted.screens.RubikScreenPlay;
28
import org.distorted.screens.ScreenList;
29

    
30
import java.util.ArrayList;
31
import static org.distorted.objectlib.main.ObjectType.NUM_OBJECTS;
32

    
33
///////////////////////////////////////////////////////////////////////////////////////////////////
34

    
35
public class RubikObjectList
36
{
37
  public static final int DEF_OBJECT= ObjectConstants.CUBE_3;
38

    
39
  public static int MAX_LEVEL;
40

    
41
  private static RubikObjectList mType;
42
  private static int mNumObjects;
43
  private static ArrayList<RubikObject> mObjects;
44
  private static int mObject = DEF_OBJECT;
45

    
46
  static
47
    {
48
    int max = Integer.MIN_VALUE;
49

    
50
    for (int i=0; i<NUM_OBJECTS; i++)
51
      {
52
      int cur = getDBLevel(i);
53
      if( cur>max ) max = cur;
54
      }
55

    
56
    MAX_LEVEL = max;
57
    }
58

    
59
///////////////////////////////////////////////////////////////////////////////////////////////////
60

    
61
  private RubikObjectList()
62
    {
63
    mNumObjects = 0;
64
    mObjects = new ArrayList<>();
65

    
66
    createBuiltinObjects();
67
    }
68

    
69
///////////////////////////////////////////////////////////////////////////////////////////////////
70

    
71
  private void createBuiltinObjects()
72
    {
73
    for(int i=0; i<NUM_OBJECTS; i++)
74
      {
75
      ObjectType type = ObjectType.getObject(i);
76
      RubikObject obj = new RubikObject(type);
77
      mObjects.add(obj);
78
      mNumObjects++;
79
      }
80
    }
81

    
82
///////////////////////////////////////////////////////////////////////////////////////////////////
83
// PUBLIC API
84
///////////////////////////////////////////////////////////////////////////////////////////////////
85
// historically older versions of the app had lower 'maxScrambles' in case of several objects and
86
// those got remembered in the server-side DB already, so we need to keep using them. This function
87
// provides a map between 'maxScramble' of an object and its 'dbLevel'. All new objects will have
88
// those two values the same.
89

    
90
  public static int getDBLevel(int ordinal)
91
    {
92
    if( ordinal==ObjectConstants.CUBE_3 ) return 16;
93
    if( ordinal==ObjectConstants.CUBE_4 ) return 20;
94
    if( ordinal==ObjectConstants.CUBE_5 ) return 24;
95
    if( ordinal==ObjectConstants.PYRA_4 ) return 15;
96
    if( ordinal==ObjectConstants.PYRA_5 ) return 20;
97
    if( ordinal==ObjectConstants.MEGA_5 ) return 35;
98
    if( ordinal==ObjectConstants.DIAM_2 ) return 10;
99
    if( ordinal==ObjectConstants.DIAM_3 ) return 18;
100
    if( ordinal==ObjectConstants.REDI_3 ) return 14;
101
    if( ordinal==ObjectConstants.HELI_3 ) return 18;
102
    if( ordinal==ObjectConstants.SKEW_3 ) return 17;
103
    if( ordinal==ObjectConstants.REX_3  ) return 16;
104
    if( ordinal==ObjectConstants.MIRR_3 ) return 16;
105

    
106
    ObjectType type = ObjectType.getObject(ordinal);
107
    return type.getNumScramble();
108
    }
109

    
110
///////////////////////////////////////////////////////////////////////////////////////////////////
111

    
112
  public static void savePreferences(SharedPreferences.Editor editor)
113
    {
114
    RubikObject object = getObject(mObject);
115

    
116
    if( object!=null )
117
      {
118
      editor.putString("statePlay_objName", object.getName() );
119
      }
120
    }
121

    
122
///////////////////////////////////////////////////////////////////////////////////////////////////
123

    
124
  public static void restorePreferences(SharedPreferences preferences)
125
    {
126
    RubikObject object = getObject(DEF_OBJECT);
127
    String defName = object==null ? "CUBE_3" : object.getName();
128
    String objName= preferences.getString("statePlay_objName",defName);
129
    mObject = getOrdinal(objName);
130

    
131
    if( mObject<0 || mObject>=mNumObjects ) mObject = DEF_OBJECT;
132
    }
133

    
134
///////////////////////////////////////////////////////////////////////////////////////////////////
135

    
136
  public static boolean setCurrObject(RubikActivity act, int ordinal)
137
    {
138
    if( mObject!=ordinal )
139
      {
140
      mObject = ordinal;
141
      RubikScreenPlay play = (RubikScreenPlay) ScreenList.PLAY.getScreenClass();
142
      play.setCurrObject(act);
143
      return true;
144
      }
145

    
146
    return false;
147
    }
148

    
149
///////////////////////////////////////////////////////////////////////////////////////////////////
150

    
151
  public static int getCurrObject()
152
    {
153
    return mObject;
154
    }
155

    
156
///////////////////////////////////////////////////////////////////////////////////////////////////
157

    
158
  public static RubikObject getObject(int ordinal)
159
    {
160
    if( mType==null ) mType = new RubikObjectList();
161
    return ordinal>=0 && ordinal<mNumObjects ? mObjects.get(ordinal) : null;
162
    }
163

    
164
///////////////////////////////////////////////////////////////////////////////////////////////////
165

    
166
  public static int getNumObjects()
167
    {
168
    if( mType==null ) mType = new RubikObjectList();
169
    return mNumObjects;
170
    }
171

    
172
///////////////////////////////////////////////////////////////////////////////////////////////////
173

    
174
  public static int getOrdinal(String name)
175
    {
176
    if( mType==null ) mType = new RubikObjectList();
177

    
178
    for(int i=0; i<mNumObjects; i++)
179
      {
180
      RubikObject obj = mObjects.get(i);
181

    
182
      if( obj.getName().equals(name) ) return i;
183
      }
184

    
185
    return -1;
186
    }
187
}
(2-2/2)