-
Notifications
You must be signed in to change notification settings - Fork 8
/
GameEngine.java
379 lines (307 loc) · 13.4 KB
/
GameEngine.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/*
* Copyright (c) 2009, Balazs Lecz <leczbalazs@gmail.com>
* Copyright (c) 2015, Amplitude Mobile Analytics
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the names of Balazs Lecz or Amplitude Mobile Analytics nor the names of
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
package com.lecz.android.tiltmazes;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.hardware.SensorListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Vibrator;
import android.widget.TextView;
import com.google.android.gms.tagmanager.DataLayer;
import com.google.android.gms.tagmanager.TagManager;
import org.json.JSONException;
import org.json.JSONObject;
public class GameEngine {
private SensorManager mSensorManager;
private Vibrator mVibrator;
private static float ACCEL_THRESHOLD = 2;
private float mAccelX = 0;
private float mAccelY = 0;
@SuppressWarnings("unused")
private float mAccelZ = 0;
private Handler mHandler;
private Map mMap;
private Ball mBall;
private int mCurrentMap = 0;
private int mMapToLoad = 0;
private int mStepCount = 0;
private Direction mCommandedRollDirection = Direction.NONE;
private TextView mMazeNameLabel;
private TextView mRemainingGoalsLabel;
private TextView mStepsView;
private MazeView mMazeView;
private final AlertDialog mMazeSolvedDialog;
private final AlertDialog mAllMazesSolvedDialog;
private boolean mSensorEnabled = true;
private TiltMazesDBAdapter mDB;
private final SensorListener mSensorAccelerometer = new SensorListener() {
public void onSensorChanged(int sensor, float[] values) {
if (!mSensorEnabled) return;
mAccelX = values[0];
mAccelY = values[1];
mAccelZ = values[2];
mCommandedRollDirection = Direction.NONE;
if (Math.abs(mAccelX) > Math.abs(mAccelY)) {
if (mAccelX < -ACCEL_THRESHOLD) mCommandedRollDirection = Direction.LEFT;
// FIXME(leczbalazs) elseif
if (mAccelX > ACCEL_THRESHOLD) mCommandedRollDirection = Direction.RIGHT;
} else {
if (mAccelY < -ACCEL_THRESHOLD) mCommandedRollDirection = Direction.DOWN;
// FIXME(leczbalazs) elseif
if (mAccelY > ACCEL_THRESHOLD) mCommandedRollDirection = Direction.UP;
}
if (mCommandedRollDirection != Direction.NONE && !mBall.isRolling()) {
rollBall(mCommandedRollDirection);
}
}
public void onAccuracyChanged(int sensor, int accuracy) {
}
};
public GameEngine(final Context context) {
// Open maze database
mDB = new TiltMazesDBAdapter(context).open();
mCurrentMap = mDB.getFirstUnsolved();
// Request vibrator service
mVibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
// Register the sensor listener
mSensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
mSensorManager.registerListener(mSensorAccelerometer, SensorManager.SENSOR_ACCELEROMETER,
SensorManager.SENSOR_DELAY_GAME);
mMap = new Map(MapDesigns.designList.get(mCurrentMap));
// Create ball
mBall = new Ball(this, mMap,
mMap.getInitialPositionX(),
mMap.getInitialPositionY());
// Congratulations dialog
mMazeSolvedDialog = new AlertDialog.Builder(context)
.setCancelable(true)
.setIcon(android.R.drawable.ic_dialog_info)
.setTitle("Congratulations!")
.setPositiveButton("Go to next maze!", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.cancel();
sendEmptyMessage(Messages.MSG_MAP_NEXT);
}
})
.create();
// Final congratulations dialog
mAllMazesSolvedDialog = new AlertDialog.Builder(context)
.setCancelable(true)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("Congratulations!")
.setPositiveButton("OK!", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.cancel();
sendEmptyMessage(Messages.MSG_MAP_NEXT);
}
})
.create();
// Create message handler
mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case Messages.MSG_INVALIDATE:
mMazeView.invalidate();
return;
case Messages.MSG_REACHED_GOAL:
mRemainingGoalsLabel.setText("" + mMap.getGoalCount());
mRemainingGoalsLabel.invalidate();
vibrate(100);
if (mMap.getGoalCount() == 0) {
// Solved!
mDB.updateMaze(mCurrentMap, mStepCount);
// Track the maze completion in amplitude
JSONObject eventProperties = new JSONObject();
try {
eventProperties.put("Maze", mMap.getName());
eventProperties.put("Steps", mStepCount);
} catch (JSONException exception) {
}
DataLayer dataLayer = TagManager.getInstance(context).getDataLayer();
dataLayer.pushEvent(
"logEvent",
DataLayer.mapOf("eventType", "Maze Completed", "eventProperties", eventProperties.toString())
);
if (mDB.unsolvedMazes().getCount() == 0) {
mAllMazesSolvedDialog.setMessage(
"Mad props!\nYou have solved all the mazes!\n" +
"Now go back and try to solve them in fewer steps! :)");
mAllMazesSolvedDialog.show();
} else {
mMazeSolvedDialog.setMessage(
"You have solved maze "
+ mMap.getName()
+ " in " + mStepCount + " steps."
);
mMazeSolvedDialog.show();
}
}
return;
case Messages.MSG_REACHED_WALL:
vibrate(12);
return;
case Messages.MSG_RESTART:
loadMap(mCurrentMap);
return;
case Messages.MSG_MAP_PREVIOUS:
case Messages.MSG_MAP_NEXT:
switch (msg.what) {
case (Messages.MSG_MAP_PREVIOUS):
if (mCurrentMap == 0) {
// Wrap around
mMapToLoad = MapDesigns.designList.size() - 1;
} else {
mMapToLoad = (mCurrentMap - 1) % MapDesigns.designList.size();
}
break;
case (Messages.MSG_MAP_NEXT):
mMapToLoad = (mCurrentMap + 1) % MapDesigns.designList.size();
break;
}
loadMap(mMapToLoad);
return;
}
super.handleMessage(msg);
}
};
}
public void loadMap(int mapID) {
mCurrentMap = mapID;
mBall.stop();
mMap = new Map(MapDesigns.designList.get(mCurrentMap));
mBall.setMap(mMap);
mBall.setX(mMap.getInitialPositionX());
mBall.setY(mMap.getInitialPositionY());
mBall.setXTarget(mMap.getInitialPositionX());
mBall.setYTarget(mMap.getInitialPositionY());
mMap.init();
mStepCount = 0;
mMazeNameLabel.setText(mMap.getName());
mMazeNameLabel.invalidate();
mRemainingGoalsLabel.setText("" + mMap.getGoalCount());
mRemainingGoalsLabel.invalidate();
mStepsView.setText("" + mStepCount);
mStepsView.invalidate();
mMazeView.calculateUnit();
mMazeView.invalidate();
}
public void setMazeNameLabel(TextView mazeNameLabel) {
mMazeNameLabel = mazeNameLabel;
}
public void setRemainingGoalsLabel(TextView remainingGoalsLabel) {
mRemainingGoalsLabel = remainingGoalsLabel;
}
public void setTiltMazesView(MazeView mazeView) {
mMazeView = mazeView;
mBall.setMazeView(mazeView);
}
public void setStepsLabel(TextView stepsView) {
mStepsView = stepsView;
}
public void sendEmptyMessage(int msg) {
mHandler.sendEmptyMessage(msg);
}
public void sendMessage(Message msg) {
mHandler.sendMessage(msg);
}
public void registerListener() {
mSensorManager.registerListener(mSensorAccelerometer, SensorManager.SENSOR_ACCELEROMETER,
SensorManager.SENSOR_DELAY_GAME);
}
public void unregisterListener() {
mSensorManager.unregisterListener(mSensorAccelerometer);
}
public void rollBall(Direction dir) {
if (mBall.roll(dir)) mStepCount++;
mStepsView.setText("" + mStepCount);
mStepsView.invalidate();
}
public Ball getBall() {
return mBall;
}
public Map getMap() {
return mMap;
}
public boolean isSensorEnabled() {
return mSensorEnabled;
}
public void toggleSensorEnabled() {
mSensorEnabled = !mSensorEnabled;
}
public void vibrate(long milliseconds) {
mVibrator.vibrate(milliseconds);
}
public void saveState(Bundle icicle) {
mBall.stop();
icicle.putInt("map.id", mCurrentMap);
int[][] goals = mMap.getGoals();
int sizeX = mMap.getSizeX();
int sizeY = mMap.getSizeY();
int[] goalsToSave = new int[sizeX * sizeY];
for (int y = 0; y < sizeY; y++)
for (int x = 0; x < sizeX; x++)
goalsToSave[y + x * sizeX] = goals[y][x];
icicle.putIntArray("map.goals", goalsToSave);
icicle.putInt("stepcount", mStepCount);
icicle.putInt("ball.x", Math.round(mBall.getX()));
icicle.putInt("ball.y", Math.round(mBall.getY()));
}
public void restoreState(Bundle icicle, boolean sensorEnabled) {
if (icicle != null) {
int mapID = icicle.getInt("map.id", -1);
if (mapID == -1) return;
loadMap(mapID);
int[] goals = icicle.getIntArray("map.goals");
if (goals == null) return;
int sizeX = mMap.getSizeX();
int sizeY = mMap.getSizeY();
for (int y = 0; y < sizeY; y++)
for (int x = 0; x < sizeX; x++)
mMap.setGoal(x, y, goals[y + x * sizeX]);
mBall.setX(icicle.getInt("ball.x"));
mBall.setY(icicle.getInt("ball.y"));
// We have probably moved the ball, so invalidate the Maze View
mMazeView.invalidate();
mStepCount = icicle.getInt("stepcount", 0);
}
mRemainingGoalsLabel.setText("" + mMap.getGoalCount());
mRemainingGoalsLabel.invalidate();
mStepsView.setText("" + mStepCount);
mStepsView.invalidate();
mSensorEnabled = sensorEnabled;
}
}