-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMazelerometer.pde
435 lines (385 loc) · 10.6 KB
/
Mazelerometer.pde
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
import ketai.sensors.*;
import shiffman.box2d.*;
import org.jbox2d.common.*;
import org.jbox2d.dynamics.joints.*;
import org.jbox2d.collision.shapes.*;
import org.jbox2d.collision.shapes.Shape;
import org.jbox2d.common.*;
import org.jbox2d.dynamics.*;
import org.jbox2d.dynamics.contacts.*;
KetaiSensor sensor;
Vec2 velocity, position;
UI ui;
Box2DProcessing box2d;
MapEntity ex;
MapEntity pl;
JSONObject maze;
JSONArray levels;
int currentLevel = 1, availableMaps = -1, timeSpent = 0, timeBest = 0, timeNow;
int cellsCount; // level size, available maps
int cellSize; // size of one wall / empty / player / exit cell
int hitHistory = 5; // how much hit walls will be displayed
int mapTimeEnd = 3, mapDuration = 3; // when to stop showing map, for how long show map
float mapShift; // if map / cellsCount is not integer
float ballMaxSpeed, ballAcceleration;
float accelerometerX, accelerometerY, accelerometerZ;
ArrayList<int[]> levelPattern;
ArrayList<MapEntity> levelPatternWalls;
ArrayList<MapEntity> hitWalls;
String currentScreen;
boolean isStart = true, isInfo = false, isPause = false, isWin = false, isError = false;
boolean sound = true, click = false, useMap = true;
void setup() {
orientation(LANDSCAPE);
fullScreen();
background(255);
init();
}
void init() {
sensor = new KetaiSensor(this);
sensor.start();
noStroke();
ui = new UI();
ui.border = Math.round(height * .00625);
velocity = new Vec2(0, 0);
hitWalls = new ArrayList();
try {
maze = loadJSONObject("maze.json");
if (maze != null) { // loaded successfully
levels = maze.getJSONArray("levels");
if (levels == null) { // levels does not exist
isError = true;
println("Can not load levels");
}
} else { // not successfull load
isError = true;
println("File is broken");
}
}
catch (Exception e) {
println("There is no such file");
isError = true;
}
box2d = new Box2DProcessing(this);
box2d.createWorld();
box2d.listenForCollisions();
box2d.setGravity(0, 0);
ui.initControlls();
ui.initLeftBlock();
ui.initMapBlock();
ui.initRightBlock();
}
void draw() {
if (isStart) { // start screen
startScreen();
} else if (isInfo) { // info screen
if (isError) { // show error
infoScreen(1);
} else { // show help
infoScreen(0);
}
} else if (isPause) { // pause screen
if (isWin) { // show win
pauseScreen(1);
} else { // show pause
pauseScreen(0);
}
} else { // game screen
gameScreen();
}
click = false;
}
void startScreen() {
if (currentScreen != "start") {
currentScreen = "start";
ui.drawStartScreen();
}
updateStartScreen();
}
void updateStartScreen() {
ui.playButton.show(click);
ui.helpButton.show(click);
ui.exitButton.show(click);
if (ui.playButton.isClicked(click)) {
playButtonEvent();
}
if (ui.helpButton.isClicked(click)) {
helpButtonEvent();
}
if (ui.exitButton.isClicked(click)) {
exitButtonEvent();
}
}
void playButtonEvent() {
isStart = false;
}
void helpButtonEvent() {
isStart = false;
isInfo = true;
}
void exitButtonEvent() {
System.exit(0);
}
void infoScreen(int type) {
if (currentScreen != "info") {
currentScreen = "info";
ui.drawInfoScreen(type);
}
updateInfoScreen();
}
void updateInfoScreen() {
ui.infoExitButton.show(click);
if (ui.infoExitButton.isClicked(click)) {
infoExitButtonEvent();
}
}
void infoExitButtonEvent() {
if (isError) {
System.exit(0);
} else {
isStart = true;
isInfo = false;
}
}
void pauseScreen(int type) {
if (currentScreen != "pause") {
if (type == 1) {
if (timeSpent<timeBest || timeBest == 0) {
timeBest = timeSpent;
updateJson();
}
} else {
position = pl.pos;
}
ui.drawPauseScreen(type);
currentScreen = "pause";
}
updatePauseScreen(type);
}
void updatePauseScreen(int type) {
ui.pauseSoundButton.show(click);
ui.pauseNextButton.show(click);
ui.pauseExitButton.show(click);
if (ui.pauseSoundButton.isClicked(click)) { // need to check back or exit
pauseSoundButtonEvent();
}
if (ui.pauseNextButton.isClicked(click)) { // need to check back or exit
pauseNextButtonEvent();
}
if (ui.pauseExitButton.isClicked(click)) { // need to check back or exit
pauseExitButtonEvent();
}
}
void pauseSoundButtonEvent() {
sound = !sound;
}
void pauseNextButtonEvent() {
if (isWin) {
if (currentLevel>=levels.size()) {
currentLevel=1;
} else {
currentLevel++;
}
timeSpent = 0;
timeBest = 0;
hitWalls = new ArrayList();
isWin = false;
}
isPause = false;
}
void pauseExitButtonEvent() {
System.exit(0);
}
void gameScreen() {
if (currentScreen != "game") {
currentScreen = "game";
timeNow = millis();
loadLevel();
ui.drawGameScreen();
}
updateGameScreen();
}
void loadLevel() {
if (pl != null) {
availableMaps = -1;
pl.killBody();
ex.killBody();
for (MapEntity e : levelPatternWalls) {
e.killBody();
}
}
levelPattern = new ArrayList();
levelPatternWalls = new ArrayList();
pl = null;
ex = null;
JSONObject level = levels.getJSONObject(currentLevel-1);
JSONArray rawPattern = level.getJSONArray("pattern"); // jsonArray of jsonArrays
cellsCount = level.getInt("size");
availableMaps = availableMaps == -1 ? level.getInt("availableMaps") : availableMaps;
timeBest = level.getInt("bestTime");
cellSize = ui.mapInnerWidth / cellsCount;
mapShift = (ui.mapInnerWidth - (cellSize * cellsCount))/2;
ballMaxSpeed = cellSize*3.0/5.0;
ballAcceleration = ballMaxSpeed/15.0;
for (int i = 0; i < cellsCount; i++) {
levelPattern.add(rawPattern.getJSONArray(i).getIntArray()); // arrayList of int arrays
for (int j = 0; j<cellsCount; j++) {
if (levelPattern.get(i)[j] == 1) {
if (!checkWall(j*cellSize, i*cellSize)) {
levelPatternWalls.add(new MapEntity(j*cellSize, i*cellSize, cellSize, cellSize, 1).changeBack());
} else {
levelPatternWalls.add(new MapEntity(j*cellSize, i*cellSize, cellSize, cellSize, 1).change());
}
} else if (levelPattern.get(i)[j] == 2) {
if (position == null) {
pl = new MapEntity(j*cellSize, i*cellSize, cellSize, cellSize, 2).setColor(color(255, 255, 0));
} else {
pl = new MapEntity(position.x, position.y, cellSize, cellSize, 2).setColor(color(255, 255, 0));
}
} else if (levelPattern.get(i)[j] == 3) {
ex = new MapEntity(j*cellSize, i*cellSize, cellSize, cellSize, 3).setColor(color(0, 255, 0));
}
}
}
}
boolean checkWall(float x, float y) {
for (MapEntity e : hitWalls) {
if (e.pos.x == x && e.pos.y == y) {
return true;
}
}
return false;
}
void updateGameScreen() {
if (millis()-timeNow>=1000) {
timeNow = millis();
timeSpent++;
if (timeSpent >= mapTimeEnd) {
useMap = false;
}
}
ui.pauseButton.show(click);
ui.showMapButton.show(click);
ui.soundButton.show(click);
ui.mapCountLabel.setText("Maps:"+availableMaps)
.show(false);
ui.timeLabel.setText("Time:"+timeSpent+"s.")
.show(false);
updateMap();
if (ui.pauseButton.isClicked(click)) {
pauseButtonEvent();
}
if (ui.showMapButton.isClicked(click)) {
showMapButtonEvent();
}
if (ui.soundButton.isClicked(click)) {
soundButtonEvent();
}
}
void updateMap() {
ui.drawBlock(1, useMap);
moveBall();
pushMatrix();
translate(ui.mapInnerX+mapShift+cellSize/2, ui.mapInnerY+mapShift+cellSize/2);
box2d.step();
position = null;
for (MapEntity w : levelPatternWalls) {
w.display();
}
pl.display();
ex.display();
popMatrix();
}
void pauseButtonEvent() {
isStart = false;
isInfo = false;
isPause = true;
}
void showMapButtonEvent() {
if (availableMaps>0) {
mapTimeEnd = timeSpent+mapDuration;
useMap = true;
availableMaps--;
}
}
void soundButtonEvent() {
sound = !sound;
}
void moveBall() {
if (keyPressed) {
if (keyCode == UP) {
velocity = new Vec2(0, constrain(velocity.y+ballAcceleration, -ballMaxSpeed, ballMaxSpeed));
}
if (keyCode == RIGHT) {
velocity = new Vec2(constrain(velocity.x+ballAcceleration, -ballMaxSpeed, ballMaxSpeed), 0);
}
if (keyCode == DOWN) {
velocity = new Vec2(0, constrain(velocity.y-ballAcceleration, -ballMaxSpeed, ballMaxSpeed));
}
if (keyCode == LEFT) {
velocity = new Vec2(constrain(velocity.x-ballAcceleration, -ballMaxSpeed, ballMaxSpeed), 0);
}
pl.body.setLinearVelocity(velocity);
}
if (Math.abs(accelerometerX)>1 || Math.abs(accelerometerY)>1) {
float accY = accelerometerX*2;
float accX = accelerometerY*2;
velocity = new Vec2(
constrain(velocity.x+accX, -ballMaxSpeed, ballMaxSpeed),
constrain(velocity.y-accY, -ballMaxSpeed, ballMaxSpeed)
);
pl.body.setLinearVelocity(velocity);
}
}
void beginContact(Contact cp) {
// Get both shapes
Fixture f1 = cp.getFixtureA();
Fixture f2 = cp.getFixtureB();
// Get both bodies
Body b1 = f1.getBody();
Body b2 = f2.getBody();
// Get our objects that reference these bodies
Object o1 = b1.getUserData();
Object o2 = b2.getUserData();
if ((o1 == pl.body.getUserData() && o2 == ex.body.getUserData()) || (o1 == ex.body.getUserData() && o2 == pl.body.getUserData())) {
isWin = true;
isPause = true;
}
if (o1 == pl.body.getUserData()) {
MapEntity w = (MapEntity) o2;
w.change();
hit(w);
velocity = new Vec2(0, 0);
}
if (o2 == pl.body.getUserData()) {
MapEntity w = (MapEntity) o1;
w.change();
hit(w);
velocity = new Vec2(0, 0);
}
}
void hit(MapEntity wall) {
if (!checkWall(wall.pos.x, wall.pos.y)) { // no hit
if (hitWalls.size() >= hitHistory) {
hitWalls.get(0).changeBack();
hitWalls.remove(0);
}
hitWalls.add(wall);
}
}
void endContact(Contact cp) {
}
void mousePressed() {
click = true;
}
void updateJson() {
levels.setJSONObject(currentLevel-1, levels.getJSONObject(currentLevel-1).setInt("bestTime", timeBest));
maze.setJSONArray("levels", levels);
saveJSONObject(maze, "maze.json", "compact");
}
void onAccelerometerEvent(float x, float y, float z)
{
accelerometerX = x;
accelerometerY = y;
accelerometerZ = z;
}