-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGUI.java
More file actions
535 lines (469 loc) · 15 KB
/
GUI.java
File metadata and controls
535 lines (469 loc) · 15 KB
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import javafx.animation.FadeTransition;
import javafx.animation.Interpolator;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.animation.TranslateTransition;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Point3D;
import javafx.scene.Camera;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.PerspectiveCamera;
import javafx.scene.PointLight;
import javafx.scene.Scene;
import javafx.scene.SceneAntialiasing;
import javafx.scene.SubScene;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseEvent;
import javafx.scene.input.SwipeEvent;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.paint.PhongMaterial;
import javafx.scene.shape.Box;
import javafx.scene.shape.DrawMode;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.scene.text.TextAlignment;
import javafx.scene.transform.Rotate;
import javafx.stage.Stage;
import javafx.util.Duration;
/**
*
* Last Edited 24/05/2017 04:00 : Antony.J Last Edited 24/05/2017 12:35 :
* Antony.J
*
*/
public class GUI extends Application {
private final int sceneScale = 100;
private final Vector2 sceneDimensions = new Vector2(500, 500);
private final int cameraHeight = 10;
private final float cameraAngle = 20;
private final float edgeHeight = 20;
private TranslateTransition tt = new TranslateTransition();
private final Vector2 in = new Vector2(-200, 200);
private final Vector2 out = new Vector2(-50, 200);
private final Vector2 mid = new Vector2(295, 200);
private String type = "";
private boolean mainMenu = false;
private boolean ingameMenu = false;
private Box[] boxes;
private Group playerGroup;
private IGenerator g;
Menu m = new Menu();
Parent mRoot = m.createContent();
IngameMenu im = new IngameMenu();
Parent root = im.createContent();
private PhongMaterial box;
private PhongMaterial floor;
private PhongMaterial goal;
private PhongMaterial player;
private PhongMaterial edge;
private TranslateTransition cameraTransition;
private FadeTransition textTransition;
private Timeline playerTransition;
private Rotate rotation = new Rotate();
private PerspectiveCamera camera;
private Group level;
private Text text;
private CachedLevels cachedLevels;
private Text time;
private final DateFormat formatter = new SimpleDateFormat("mm:ss");
private Timer timer;
private boolean settingRecord;
private Timeline timeline;
private Record record;
private long currTime;
private long startTime;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) throws IOException {
if (getType().equals("USER")) {
g = new GeneratorUser();
} else {
g = new Generator();
}
text = new Text("");
text.setFill(Color.WHITE);
text.setFont(new Font(20));
textTransition = new FadeTransition();
textTransition.setNode(text);
textTransition.setDuration(Duration.millis(2000));
textTransition.setInterpolator(Interpolator.EASE_IN);
textTransition.setFromValue(1);
textTransition.setToValue(0);
time = new Text();
time.setFont(new Font(50));
time.setFill(Color.WHITE);
time.setTranslateY(200);
time.setTextAlignment(TextAlignment.LEFT);
time.setVisible(false);
Notification notification = new Notification();
cachedLevels = new CachedLevels(System.getProperty("user.name"));
timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
if (!settingRecord) {
Record r = cachedLevels.getRecord();
if (r != null && !r.equals(record)) {
record = r;
notification.showAlert(record.opponent + " challenged you");
}
}
}
}, 10000, 10000);
notification.onClick(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent m) {
if (!settingRecord) { // is the user is playing his generated
// game, then desable this
newLevel(record.seed);
text.setText("");
settingRecord = true;
time.setVisible(true);
notification.hideAlert();
currTime = record.time;
timeline = new Timeline();
timeline.setCycleCount(Timeline.INDEFINITE);
timeline.getKeyFrames().add(new KeyFrame(Duration.millis(10), new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
Date date = new Date(currTime -= 10);
time.setText(formatter.format(date));
if (currTime < 10) {
currTime = 0;
timeline.stop();
lost();
}
}
}));
timeline.playFromStart();
}
}
});
camera = new PerspectiveCamera(false);
camera.setTranslateZ(-sceneScale * cameraHeight);
camera.setRotationAxis(new Point3D(1, 0, 0));
camera.setRotate(cameraAngle);
cameraTransition = new TranslateTransition();
cameraTransition.setNode(camera);
cameraTransition.setDuration(Duration.millis(400));
cameraTransition.setInterpolator(Interpolator.EASE_OUT);
level = new Group();
setupMaterials();
newLevel();
// Group 3D nodes into a sub-scene with perspective camera
Group root3D = new Group(camera, text, level);
SubScene subScene = new SubScene(root3D, sceneDimensions.x, sceneDimensions.y, true,
SceneAntialiasing.DISABLED);
subScene.setCamera(camera);
subScene.setFill(Color.BLACK);
// Group sub-scene and 2D elements into stack pane
StackPane pane = new StackPane();
// set translations for menu
pane.getChildren().addAll(subScene, notification, time, text);
tt.setNode(mRoot);
tt.setDuration(Duration.millis(1000));
tt.setInterpolator(Interpolator.EASE_BOTH);
pane.getChildren().add(mRoot);
mRoot.setTranslateX(in.x);
mRoot.setTranslateY(in.y);
// Create main scene
turnOnMenu();
mainMenu = true;
Scene scene = new Scene(pane);
setupKeyEvents(scene, stage, pane);
setupSwipeEvents(scene);
// Set up engine events
g.setOnPlayerMove(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
movePlayer(g.getPlayerLocation(), camera);
}
});
g.setOnBoxMove(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
moveBox(g.getBoxMovedBox(), g.getBoxLocations()[g.getBoxMovedBox()]);
}
});
g.setOnWin(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
newLevel();
won();
System.out.println("You won");
}
});
g.setOnLose(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
newLevel();
System.out.println("You lost");
}
});
stage.setTitle("Project");
stage.setScene(scene);
stage.show();
}
@Override
public void stop() {
timer.cancel();
}
private void won() {
if (settingRecord) {
timeline.stop();
text.setText((cachedLevels.setRecord(record.seed, record.time - currTime) ? "You beat " : "You lost to ")
+ record.opponent);
time.setVisible(false);
record = null;
} else {
text.setText("You " + (cachedLevels.setRecord(g.getSeed(), System.currentTimeMillis() - startTime)
? "set a record!" : "won!"));
}
newLevel();
}
private void lost() {
if (settingRecord) {
timeline.stop();
text.setText("You lost to " + record.opponent);
time.setVisible(false);
cachedLevels.setRecord(record.seed, record.time);
record = null;
} else {
text.setText("You lost!");
}
newLevel();
}
private void newLevel() {
g.generateLevel(5, 20, 100);
drawLevel();
}
private void newLevel(long seed) {
// g = new Generator();
g.generateLevel(5, 20, 100, seed);
drawLevel();
System.out.println("Game Got created.......");
}
public void drawLevel() {
settingRecord = false;
startTime = System.currentTimeMillis();
createPlayerGroup(g.getPlayerLocation());
createBoxes();
level.getChildren().clear();
level.getChildren().add(playerGroup);
level.getChildren().addAll(boxes);
addFloor(level);
addGoals(level);
addEdges(level);
centreCamera(g.getPlayerLocation());
PhongMaterial fog = new PhongMaterial();
// fog.setDiffuseMap(new
// Image("http://www.stickpng.com/assets/images/580b585b2edbce24c47b2636.png"));
fog.setDiffuseMap(
new Image("http://www.imagequalitylabs.com/resources/uploads/product/product_image1338282549.jpg"));
Tile t = new Tile(sceneScale, fog);
t.setTranslateX(g.getPlayerLocation().x);
t.setTranslateY(g.getPlayerLocation().y);
t.setTranslateZ(sceneScale);
level.getChildren().add(t);
textTransition.playFromStart();
}
private void centreCamera(Vector2 pos) {
cameraTransition.stop();
cameraTransition.setToX(sceneScale * pos.x - sceneDimensions.x / 2.0 * camera.getScaleX());
cameraTransition.setToY(sceneScale * pos.y - sceneDimensions.y / 2.0 * camera.getScaleY()
+ (cameraAngle * cameraHeight * sceneScale) / (90 - cameraAngle));
cameraTransition.play();
}
private void createPlayerGroup(Vector2 start) {
Box p = new Box(sceneScale, sceneScale, sceneScale);
p.setMaterial(player);
PointLight l = new PointLight();
l.setColor(Color.LIGHTGOLDENRODYELLOW);
l.setTranslateZ(-sceneScale);
playerGroup = new Group(p);
playerGroup.setTranslateX(sceneScale * start.x);
playerGroup.setTranslateY(sceneScale * start.y);
playerTransition = new Timeline(new KeyFrame(Duration.ZERO, new KeyValue(rotation.angleProperty(), -90)),
new KeyFrame(Duration.millis(150), new KeyValue(rotation.angleProperty(), 0)));
}
public void createBoxes() {
Vector2[] positions = g.getBoxLocations();
boxes = new Box[positions.length];
for (int i = 0; i < boxes.length; i++) {
boxes[i] = new Box(sceneScale, sceneScale, sceneScale);
boxes[i].setTranslateX(sceneScale * positions[i].x);
boxes[i].setTranslateY(sceneScale * positions[i].y);
boxes[i].setMaterial(box);
boxes[i].setDrawMode(DrawMode.FILL);
}
}
private void addFloor(Group root) {
for (Vector2 v : g.getFloor()) {
if (!g.hasGoal(v)) {
Tile t = new Tile(sceneScale, floor);
t.setTranslateX(sceneScale * v.x);
t.setTranslateY(sceneScale * v.y);
t.setTranslateZ(sceneScale / 2);
root.getChildren().add(t);
}
}
}
private void addGoals(Group root) {
for (Vector2 v : g.getGoals()) {
Tile t = new Tile(sceneScale, goal);
t.setTranslateX(sceneScale * v.x);
t.setTranslateY(sceneScale * v.y);
t.setTranslateZ(sceneScale / 2);
root.getChildren().add(t);
}
}
private void addEdges(Group root) {
for (Vector2 v : g.getEdges()) {
Box b = new Box(sceneScale, sceneScale, sceneScale * edgeHeight);
b.setTranslateX(sceneScale * v.x);
b.setTranslateY(sceneScale * v.y);
b.setTranslateZ(sceneScale * (edgeHeight - 1) / 2);
b.setMaterial(edge);
b.setDrawMode(DrawMode.FILL);
root.getChildren().add(b);
}
}
private void movePlayer(Vector2 newPos, Camera c) {
Vector2 dir = new Vector2(newPos.x - (int) playerGroup.getTranslateX() / sceneScale,
newPos.y - (int) playerGroup.getTranslateY() / sceneScale);
rotation.setAxis(new Point3D(dir.y, -dir.x, 0));
rotation.setPivotZ(sceneScale / 2);
rotation.setPivotY(-dir.y * sceneScale / 2);
rotation.setPivotX(-dir.x * sceneScale / 2);
playerGroup.getTransforms().remove(rotation);
playerGroup.setTranslateX(newPos.x * sceneScale);
playerGroup.setTranslateY(newPos.y * sceneScale);
playerGroup.getTransforms().add(rotation);
playerTransition.playFromStart();
centreCamera(newPos);
}
private void moveBox(int id, Vector2 newPos) {
boxes[id].setTranslateX(sceneScale * newPos.x);
boxes[id].setTranslateY(sceneScale * newPos.y);
}
private void setupMaterials() {
box = new PhongMaterial();
box.setSpecularColor(new Color(0.05, 0.05, 0.05, 1));
box.setBumpMap(new Image("https://www.filterforge.com/filters/9452-normal.jpg"));
box.setDiffuseMap(new Image("https://www.filterforge.com/filters/9452.jpg"));
floor = new PhongMaterial();
floor.setSpecularColor(Color.DIMGRAY);
floor.setDiffuseMap(new Image("https://www.filterforge.com/filters/10767.jpg"));
floor.setBumpMap(new Image("https://www.filterforge.com/filters/10767-normal.jpg"));
player = new PhongMaterial(Color.DIMGRAY);
goal = new PhongMaterial(Color.GREY);
goal.setSpecularColor(Color.SLATEGRAY);
goal.setDiffuseMap(new Image("https://www.filterforge.com/filters/10767.jpg")); // 11075
goal.setBumpMap(new Image("https://www.filterforge.com/filters/10767-normal.jpg"));
edge = new PhongMaterial(Color.BLACK);
edge.setSpecularPower(0);
}
private void setupKeyEvents(Scene scene, Stage stage, StackPane pane) {
scene.setOnKeyPressed(new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent key) {
if (mainMenu == false && ingameMenu == false) {
switch (key.getCode()) {
case LEFT:
g.moveCharacter(Vector2.LEFT);
break;
case UP:
g.moveCharacter(Vector2.DOWN);
break;
case RIGHT:
g.moveCharacter(Vector2.RIGHT);
break;
case DOWN:
g.moveCharacter(Vector2.UP);
break;
case BACK_SPACE:
g.undo();
break;
case ENTER:
newLevel();
break;
case ESCAPE:
turnOnMenu();
break;
default:
break;
}
} else if (mainMenu == true) {
switch (key.getCode()) {
case ESCAPE:
turnOffMenu();
default:
break;
}
}
}
});
}
public void turnOnMenu() {
tt.setToX(out.x);
tt.playFromStart();
mainMenu = true;
}
public void turnOffMenu() {
tt.setToX(in.x);
tt.playFromStart();
mainMenu = false;
}
public void setG(String type) {
this.type = type;
}
private String getType() {
return type;
}
private void setupSwipeEvents(Scene scene) {
scene.setOnSwipeDown(new EventHandler<SwipeEvent>() {
@Override
public void handle(SwipeEvent event) {
g.moveCharacter(Vector2.DOWN);
}
});
scene.setOnSwipeUp(new EventHandler<SwipeEvent>() {
@Override
public void handle(SwipeEvent event) {
g.moveCharacter(Vector2.UP);
}
});
scene.setOnSwipeLeft(new EventHandler<SwipeEvent>() {
@Override
public void handle(SwipeEvent event) {
g.moveCharacter(Vector2.LEFT);
}
});
scene.setOnSwipeRight(new EventHandler<SwipeEvent>() {
@Override
public void handle(SwipeEvent event) {
g.moveCharacter(Vector2.RIGHT);
}
});
}
}