-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathView.java
1059 lines (990 loc) · 46.4 KB
/
View.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
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
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import javafx.animation.*;
import java.time.LocalDate;
import javafx.beans.binding.Bindings;
import javafx.beans.binding.MapExpression;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.shape.*;
import javafx.stage.Stage;
import javafx.beans.binding.StringBinding;
import javafx.beans.property.*;
import javafx.collections.ObservableList;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Rectangle2D;
import javafx.geometry.Pos;
import javafx.scene.*;
import javafx.scene.control.Label;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.ToolBar;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.*;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.paint.PhongMaterial;
import javafx.scene.transform.Rotate;
import javafx.stage.Screen;
import javafx.util.Duration;
import java.awt.geom.Point2D;
import java.awt.Point;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.LinkedList;
public class View extends Scene {
protected StackPane main;
protected Stage st;
protected ToolBar tool;
protected VBox display;
protected GameVersion game;
protected TimePane timePane;
protected MazePane mazePane;
protected GameControl control;
protected SubScene mazeScene;
protected Label timeLabel;
protected Pane map;
protected boolean debug=false;
public View(StackPane root, GameVersion game) {
super(root);
this.game = game;
main = root;
Rectangle2D screenBounds = Screen.getPrimary().getBounds();
PerspectiveCamera cam = new PerspectiveCamera(true);
this.mazePane = new MazePane(cam);
mazeScene = new SubScene(mazePane, screenBounds.getWidth(), screenBounds.getHeight(), true, SceneAntialiasing.BALANCED);
mazeScene.setCamera(cam);
if (game instanceof SoloVersion) timePane = new SoloTimePane();
else if (game instanceof TimeTrialVersion) timePane = new TimeTrialPane(((TimeTrialVersion) game).timeLimit);
main.getChildren().addAll(mazeScene, timePane);
StackPane.setAlignment(timePane, Pos.TOP_LEFT);
}
public void setStage(Stage stage) {
st = stage;
}
public class TimeProperty extends SimpleIntegerProperty {
public TimeProperty(int time) {
super(time);
}
@Override
public StringBinding asString() {
return new StringBinding() {
{
super.bind(TimeProperty.this);
}
@Override
protected String computeValue() {
return MazeInterface.getT(TimeProperty.this.getValue());
}
};
}
}
/**
Classe pour le timer
*/
protected abstract class TimePane extends VBox {
protected Timeline timeLine;
protected Label timeLabel;
protected IntegerProperty timeSeconds;
public TimePane(int time) {
timeSeconds = new TimeProperty(time);
timeLabel = new Label();
timeLine = new Timeline();
timeLabel.textProperty().bind(timeSeconds.asString());
timeLabel.setStyle("-fx-alignment:center;-fx-text-fill:crimson;-fx-font-size:50pt");
this.getChildren().add(timeLabel);
this.setSpacing(200);
}
public String getElapsedTime() {
return MazeInterface.getT(game.getElapsed());
}
public int getElapsedSeconds() {
return game.getElapsed();
}
public boolean timeOver() {
return false;
}
public void setTimeSeconds(int time) {
timeSeconds.set(time);
}
public void stop() {
timeLine.stop();
}
public void pause() {
timeLine.pause();
}
public void start() {
timeLine.playFromStart();
}
public void play() {
timeLine.play();
}
}
protected class TimeTrialPane extends TimePane {
protected int timeLimit;
public TimeTrialPane(int timeLimit) {
super(timeLimit);
this.timeLimit = timeLimit;
timeLine.setCycleCount(timeLimit);
timeLine.getKeyFrames().add(new KeyFrame(Duration.seconds(1), (event) -> {
game.elapse(1);
timeSeconds.set(timeLimit - game.getElapsed());
}));
}
public boolean timeOver() {
return timeSeconds.get() == 0;
}
}
protected class SoloTimePane extends TimePane {
public SoloTimePane() {
super(0);
timeLine.setCycleCount(Timeline.INDEFINITE);
timeLine.getKeyFrames().add(new KeyFrame(Duration.seconds(1), (event) -> {
game.elapse(1);
timeSeconds.set(game.getElapsed());
}));
}
}
protected class MazePane extends Group {
protected Group world;
protected LinkedList<Maze> floors=game.floors();
protected PerspectiveCamera camera;
protected AmbientLight light;
protected final int MAZE_LENGTH = game.current().getHeight();
protected final int MAZE_WIDTH = game.current().getWidth();
protected final int SIZE_BOX = 400;
protected final int STEP = 50;
protected int posFx, posFz;
protected PhongMaterial COLOR_WALL = new PhongMaterial();
protected PhongMaterial COLOR_WAY = new PhongMaterial(Color.DARKGREY);
protected PhongMaterial COLOR_ENTRY = new PhongMaterial(Color.RED);
protected PhongMaterial COLOR_END = new PhongMaterial(Color.LIGHTGOLDENRODYELLOW);
protected PhongMaterial COLOR_DOOR = new PhongMaterial();
protected PhongMaterial COLOR_STAIRS = new PhongMaterial(Color.WHITESMOKE);
protected DoubleProperty x = new SimpleDoubleProperty(0);
protected DoubleProperty z = new SimpleDoubleProperty(0);
protected DoubleProperty y = new SimpleDoubleProperty(0);
protected DoubleProperty angle = new SimpleDoubleProperty(0);
protected Rotate rotateX;
protected ArrayList<Node> keyOrBonus=new ArrayList<>();
protected Vector3D[] coordSwitch = new Vector3D[floors.size()];
protected Group[] floorGroups = new Group[floors.size()];
public MazePane(PerspectiveCamera cam) {
camera = cam;
display = new VBox();
display.setAlignment(Pos.CENTER);
}
public void setLight() {
DoubleProperty x = camera.translateXProperty();
DoubleProperty y = camera.translateYProperty();
DoubleProperty z = camera.translateZProperty();
DoubleProperty angle=camera.rotateProperty();
PointLight lightL = new PointLight();
lightL.translateXProperty().bind(x.add(-SIZE_BOX*Math.cos(Math.toRadians(angle.get()))));
lightL.translateYProperty().bind(y.subtract(SIZE_BOX/2));
lightL.translateZProperty().bind(z.add(-SIZE_BOX*Math.sin(Math.toRadians(angle.get()))));
/*PointLight lightL = new PointLight();
PointLight lightR = new PointLight();
PointLight lightU = new PointLight();
PointLight lightD = new PointLight();
lightL.translateXProperty().bind(x.subtract(SIZE_BOX));
lightL.translateYProperty().bind(y);
lightL.translateZProperty().bind(z);
lightR.translateZProperty().bind(z);
lightR.translateYProperty().bind(y);
lightR.translateZProperty().bind(x.add(SIZE_BOX));
/*lightU.translateXProperty().bind(x);
lightU.translateYProperty().bind(y);
lightU.translateZProperty().bind(z.add(SIZE_BOX));
lightD.translateXProperty().bind(x);
lightD.translateYProperty().bind(y);
lightD.translateZProperty().bind(z.subtract(SIZE_BOX));*/
/*licht.translateXProperty().bind(x.add(SIZE_BOX));
licht.translateZProperty().bind(z.add(SIZE_BOX));
licht.translateYProperty().bind(y);
lum.translateXProperty().bind(x.subtract(SIZE_BOX));
lum.translateZProperty().bind(z.subtract(SIZE_BOX));
lum.translateYProperty().bind(y);*/
Group groupLight = new Group(lightL,new AmbientLight(Color.BLACK));//,light,new AmbientLight(Color.BLACK));//,new AmbientLight(Color.BLACK),new AmbientLight(Color.BLACK));
this.getChildren().add(groupLight);
/*light = new Light.Point();
light.setColor(Color.LIGHTGOLDENRODYELLOW);
light.xProperty().bind(x);
light.yProperty().bind(y);
light.zProperty().bind(z);
Lighting lighting = new Lighting(light);
lighting.setSurfaceScale(8.0);
main.setEffect(lighting);*/
//cam.setEffect(lighting);
}
/**
Cette fonction est utilisée pour voir si les coordonées en x ou en z des escaliers
sont égales à 0 ou à la largeur/longueur - 1
*/
public boolean test(int a, int b, int c, int d) {
return ((a == b) && (b == c || b == d));
}
/**
Création par appel successif de createMaze
À la fin, de chaque createMaze, on effectue des translations sur les étages pour les placer côte à côte selon les escaliers.
*/
public void initMaze() throws IOException {
floors = game.floors();
keyOrBonus = new ArrayList<>();
Group world = new Group();
Group floor = new Group();
Group first = floor;
Point before = null;
Box square = null;
COLOR_WALL.setBumpMap(new Image("images/brick.jpg"));
COLOR_WALL.setSpecularColor(Color.BLACK);
COLOR_DOOR.setDiffuseMap(new Image("images/safe.jpg"));
COLOR_ENTRY.setSpecularColor(Color.BLACK);
COLOR_END.setSpecularColor(Color.WHITE);
int i = 0;
for (Maze a : floors) {
createMaze(floor, a, i);
if (before != null) {
int aposx = (int) a.beginning().getX();
int aposz = (int) a.beginning().getY();
floor.setTranslateX((posFx - aposx) * SIZE_BOX);
floor.setTranslateZ((posFz - aposz) * SIZE_BOX);
posFx = (int) a.ending().getX() + (posFx - aposx);
posFz = (int) a.ending().getY() + (posFz - aposz);
if (!test(aposx, (int) before.getX(), MAZE_WIDTH - 1, 0) && !test(aposz, (int) before.getY(), MAZE_LENGTH - 1, 0)) {
setUp(aposx, aposz, MAZE_LENGTH, MAZE_WIDTH, floor, 1);
setUp((int) before.getX(), (int) before.getY(), MAZE_LENGTH, MAZE_WIDTH, floor, -1);
square = new Box(SIZE_BOX, 0, SIZE_BOX);
square.setTranslateX(aposx * SIZE_BOX);
square.setTranslateZ(aposz * SIZE_BOX);
if (aposx == 0) square.setTranslateX(square.getTranslateX() - SIZE_BOX);
else if (aposx == MAZE_WIDTH - 1) square.setTranslateX(square.getTranslateX() + SIZE_BOX);
else if (aposz == 0) square.setTranslateZ(square.getTranslateZ() - SIZE_BOX);
else if (aposz == MAZE_LENGTH - 1) square.setTranslateZ(square.getTranslateZ() + SIZE_BOX);
} else if (test(aposx, (int) before.getX(), MAZE_WIDTH - 1, 0)) {
int coeff = (aposx == 0) ? 1 : -1;
floor.setTranslateZ(floor.getTranslateZ() - SIZE_BOX);
square = new Box(SIZE_BOX, 0, 2 * SIZE_BOX);
square.setTranslateX((aposx - coeff) * SIZE_BOX);
square.setTranslateZ(aposz * SIZE_BOX + SIZE_BOX / 2);
posFz--;
} else {
int coeff = (aposz == 0) ? 1 : -1;
floor.setTranslateX(floor.getTranslateX() - SIZE_BOX);
square = new Box(2 * SIZE_BOX, 0, SIZE_BOX);
square.setTranslateX(aposx * SIZE_BOX + SIZE_BOX / 2);
square.setTranslateZ((aposz - coeff) * SIZE_BOX);
posFx--;
}
square.setMaterial(new PhongMaterial(Color.MAROON));
square.setTranslateY((-i + 1) * SIZE_BOX + SIZE_BOX / 2);
floor.getChildren().add(square);
} else {
posFx = (int) a.ending().getX();
posFz = (int) a.ending().getY();
}
before = a.ending();
coordSwitch[i / 2] = (new Vector3D(floor.getTranslateX(), floor.getTranslateY(), floor.getTranslateZ()).subtract(new Vector3D(first.getTranslateX(), first.getTranslateY(), first.getTranslateZ()))).multiply(1.0 / 400);
world.getChildren().add(floor);
floorGroups[i / 2] = floor;
floor = new Group();
i += 2;
}
this.getChildren().add(world);
buildCamera(this);
setLight();
}
/**
Création de la vue d'un étage de labyrinthe selon l'objet Maze donné en argument
*/
public void createMaze(Group root, Maze maze, int floor) throws IOException {
int CASE;
Box cell;
for (int i = 0; i < maze.getHeight(); i++) {
for (int j = 0; j < maze.getWidth(); j++) {
CASE = maze.getCase(i, j);
switch (CASE) {
case Maze.START:
cell = makeFloor(COLOR_ENTRY);
setBox(cell, i, j, root, floor, maze);
break;
case Maze.END:
cell = makeFloor(COLOR_END);
setBox(cell, i, j, root, floor, maze);
break;
case Maze.WAY:
cell = makeFloor(COLOR_WAY);
setBox(cell, i, j, root, floor, maze);
break;
case Maze.DOOR:
cell = new Box(SIZE_BOX, SIZE_BOX, SIZE_BOX);
cell.setMaterial(COLOR_DOOR);
setBox(cell, i, j, root, floor, maze);
break;
case Maze.KEY:
drawKey(root, j, i, floor);
cell = makeFloor(COLOR_WAY);
setBox(cell, i, j, root, floor, maze);
break;
case Maze.TELEPORT:
cell = makeFloor(COLOR_WAY);
drawTeleport(root, i, j, maze, floor);
setBox(cell, i, j, root, floor, maze);
break;
case Maze.MONSTRE:
cell = makeFloor(COLOR_WAY);
drawMonster(root, i, j, floor, maze);
setBox(cell, i, j, root, floor, maze);
break;
case Maze.BONUS:
cell = makeFloor(COLOR_WAY);
drawBonus(root, i, j, maze, floor);
setBox(cell, i, j, root, floor, maze);
break;
case Maze.OBSTACLE:
cell = makeFloor(COLOR_STAIRS);
drawObstacle(root, i, j, floor, maze);
//faire drawObstacle, attention à la forme de l'obstacle
setBox(cell, i, j, root, floor, maze);
break;
case Maze.STAIRSUP:
drawStair(0, root, i, j, maze, floor);
break;
case Maze.STAIRSDOWN:
drawStair(1, root, i, j, maze, floor);
break;
case Maze.WALL:
cell = new Box(SIZE_BOX, SIZE_BOX, SIZE_BOX);
cell.setMaterial(COLOR_WALL);
setBox(cell, i, j, root, floor, maze);
break;
}
}
}
}
public Box makeFloor(PhongMaterial color) {
Box cell = new Box(SIZE_BOX, 0, SIZE_BOX);
cell.setMaterial(color);
cell.setTranslateY(SIZE_BOX / 2);
return cell;
}
/*
Place une Box aux coordonées données aux arguments, et une Box rectangulaire au-dessus de la box crée
*/
public void setBox(Box cell, int i, int j, Group root, int floor, Maze maze) {
cell.setTranslateX(j * SIZE_BOX);
cell.setTranslateZ(i * SIZE_BOX);
int place = maze.getCase(i, j);
if (place == Maze.WALL || place == Maze.DOOR || place == Maze.STAIRSUP || place == Maze.STAIRSDOWN)
cell.setTranslateY((-floor) * SIZE_BOX);
else
cell.setTranslateY((-floor) * SIZE_BOX + SIZE_BOX / 2);
setRoof(i, j, root, floor);
root.getChildren().add(cell);
}
public void setRoof(int i, int j, Group root, int floor) {
Box roof = new Box(SIZE_BOX, 0, SIZE_BOX);
roof.setTranslateX(j * SIZE_BOX);
roof.setTranslateZ(i * SIZE_BOX);
roof.setTranslateY(-floor * SIZE_BOX - SIZE_BOX / 2);
roof.setMaterial(new PhongMaterial(Color.BLACK));
root.getChildren().add(roof);
}
/*
Translate un étage selon la position des escaliers.
*/
public void setUp(int posx, int posz, int height, int width, Group floor, int a) {
if (posx == 0) {
floor.setTranslateX(floor.getTranslateX() + SIZE_BOX * a);
posFx += a;
} else if (posx == width - 1) {
floor.setTranslateX(floor.getTranslateX() - SIZE_BOX * a);
posFx -= a;
} else if (posz == 0) {
floor.setTranslateZ(floor.getTranslateZ() + SIZE_BOX * a);
posFz += a;
} else if (posz == height - 1) {
floor.setTranslateZ(floor.getTranslateZ() - SIZE_BOX * a);
posFz -= a;
}
}
//Fonctions pour dessiner les différents objets du labyrinthe
public void drawStair(int dir, Group root, int i, int j, Maze maze, int floor) {
Box step;
int nbStep = 8;
Group stairs = new Group();
int size = SIZE_BOX / nbStep, a = 0;
while (nbStep != a) {
step = new Box(SIZE_BOX, size * (a + 1), size);
step.setMaterial(COLOR_STAIRS);
step.setTranslateX(j * SIZE_BOX);
step.setTranslateZ(i * SIZE_BOX + size * a);//+SIZE_BOX/2);//-size);
step.setTranslateY(SIZE_BOX / 2 + (size / 2) * (-1) * (a + 1));
a++;
stairs.getChildren().add(step);
}
stairs.setRotationAxis(Rotate.Y_AXIS);
stairs.setTranslateZ(stairs.getTranslateZ() - SIZE_BOX / 2 + size / 2);
stairs.setTranslateY(stairs.getTranslateY() - floor * SIZE_BOX);
if (dir == 1) stairs.setTranslateY(stairs.getTranslateY() + SIZE_BOX);
if (j == 0) {
//System.out.println("pat");
stairs.setRotate(-90);
} else if (j == maze.getWidth() - 1) {
// System.out.println("sac");
stairs.setRotate(90);
} else if (i == maze.getHeight() - 1) {
//System.out.println("sec");
stairs.setRotate(0);
} else if (i == 0) {
//System.out.println("zed");
stairs.setRotate(180);
}
if (dir == 1) stairs.setRotate(stairs.getRotate() + 180);
root.getChildren().add(stairs);
}
public void drawKey(Group root, int posx, int posy, int floor) throws IOException {
FXMLLoader fxmlLoader = new FXMLLoader();
fxmlLoader.setLocation(this.getClass().getResource("fxml/key.fxml"));
MeshView key = fxmlLoader.<MeshView>load();
key.setRotationAxis(Rotate.Z_AXIS);
key.setRotate(180.0);
key.setTranslateX(posx * SIZE_BOX);
key.setTranslateZ(posy * SIZE_BOX);
key.setTranslateY(-floor * SIZE_BOX);
PhongMaterial mat = new PhongMaterial();
mat.setSpecularColor(Color.LIGHTGOLDENRODYELLOW);
mat.setDiffuseColor(Color.YELLOW);
key.setMaterial(mat);
RotateTransition rt = new RotateTransition(Duration.millis(1000));
rt.setByAngle(360.0);
rt.setAxis(Rotate.X_AXIS);
rt.setCycleCount(TranslateTransition.INDEFINITE);
rt.setAutoReverse(false);
rt.setInterpolator(Interpolator.LINEAR);
rt.setNode(key);
rt.play();
root.getChildren().add(key);
keyOrBonus.add(key);
}
public void drawTeleport(Group root, int i, int j, Maze maze, int floor) throws IOException {
Teleporteur last = maze.getTeleport().getLast();
MeshView teleport = last.initTeleport();
teleport.setTranslateX(j * SIZE_BOX);
teleport.setTranslateZ(i * SIZE_BOX);
teleport.setTranslateY((-floor) * SIZE_BOX+SIZE_BOX/4);
teleport.setScaleX(teleport.getScaleX() * SIZE_BOX / 4);
teleport.setScaleZ(teleport.getScaleZ() * SIZE_BOX / 4);
teleport.setScaleY(teleport.getScaleY() * SIZE_BOX / 4);
RotateTransition rt = new RotateTransition(Duration.millis(3000));
rt.setByAngle(360.0);
rt.setAxis(Rotate.Y_AXIS);
rt.setCycleCount(TranslateTransition.INDEFINITE);
rt.setAutoReverse(false);
rt.setInterpolator(Interpolator.LINEAR);
rt.setNode(teleport);
rt.play();
root.getChildren().add(teleport);
}
public void drawMonster(Group root, int i, int j, int floor, Maze maze) throws IOException {
Monstres last = maze.getMonstres().getLast();
MeshView ghost = last.initMonster();
ghost.setTranslateY((-floor) * SIZE_BOX - SIZE_BOX / 2);
root.getChildren().add(ghost);
}
public void drawObstacle(Group root, int i, int j, int floor, Maze maze) throws IOException {
FXMLLoader fxmlLoader = new FXMLLoader();
float scale = 0, scaleY, scaleZ, posy;
if (maze.getTypeObstacle().equals("Cercle")) {
fxmlLoader.setLocation(this.getClass().getResource("fxml/Spider.fxml"));
scale = 2.1f;
scaleZ = 2.5f;
scaleY = 1.5f;
posy = SIZE_BOX / 10;
} else {
fxmlLoader.setLocation(this.getClass().getResource("fxml/gate.fxml"));
scale = 3.8f;
scaleY = 6;
scaleZ = 3;
posy = SIZE_BOX / 20;
}
Group obs = fxmlLoader.load();
PhongMaterial mat = new PhongMaterial();
mat.setSpecularColor(Color.BLACK);
mat.setDiffuseColor(Color.DARKGREY);
for (Node n : obs.getChildren()) {
if (n instanceof Shape3D) {
((Shape3D) n).setTranslateX(j * SIZE_BOX);
((Shape3D) n).setTranslateZ(i * SIZE_BOX);
((Shape3D) n).setMaterial(mat);
((Shape3D) n).setScaleX(((Shape3D) n).getScaleX() * scale);
((Shape3D) n).setScaleY(((Shape3D) n).getScaleY() * scaleY);
((Shape3D) n).setScaleZ(((Shape3D) n).getScaleZ() * scaleZ);
((Shape3D) n).setTranslateY(SIZE_BOX / 2 - posy - (floor * SIZE_BOX));
}
}
root.getChildren().add(obs);
}
public void drawBonus(Group root, int i, int j, Maze maze, int floor) throws IOException {
Bonus last = maze.getBonus().getLast();
MeshView bonus = last.initBonus();
RotateTransition rt = new RotateTransition(Duration.millis(3000));
rt.setByAngle(360.0);
rt.setAxis(Rotate.Y_AXIS);
rt.setCycleCount(TranslateTransition.INDEFINITE);
rt.setAutoReverse(false);
rt.setInterpolator(Interpolator.LINEAR);
bonus.setTranslateX(j * SIZE_BOX);
bonus.setTranslateZ(i * SIZE_BOX);
bonus.setTranslateY((-floor) * SIZE_BOX + SIZE_BOX / 8);
bonus.setScaleX(bonus.getScaleX() * SIZE_BOX / 10);
bonus.setScaleZ(bonus.getScaleZ() * SIZE_BOX / 10);
bonus.setScaleY(bonus.getScaleY() * SIZE_BOX / 10);
rt.setNode(bonus);
rt.play();
root.getChildren().add(bonus);
keyOrBonus.add(bonus);
}
public void remove(Group root, int i, int j) {
int posx = i * SIZE_BOX;
int posz = j * SIZE_BOX;
Node removable;
for (Node a : keyOrBonus) {
if (a.getTranslateX() == posx && a.getTranslateZ() == posz) {
System.out.println(a.getTranslateX() + " " + a.getTranslateZ());
removable = a;
System.out.println(root.getChildren().contains(a));
root.getChildren().remove(removable);
System.out.println(root.getChildren().contains(a));
break;
}
}
}
public void buildCamera(Group root) {
camera.setFarClip(10000);
camera.setNearClip(0.1);
Point2D position = game.player().getPosition();
double yPos = game.player.getY();
int floor = game.floor();
x = new SimpleDoubleProperty((position.getX() + coordSwitch[floor].x()) * SIZE_BOX - SIZE_BOX / 2);
y = new SimpleDoubleProperty(-yPos * SIZE_BOX);
z = new SimpleDoubleProperty((position.getY() + coordSwitch[floor].z()) * SIZE_BOX - SIZE_BOX / 2);
angle = new SimpleDoubleProperty(90 - game.player().orientation());
camera.translateXProperty().bind(x);
camera.translateYProperty().bind(y);
camera.translateZProperty().bind(z);
camera.rotateProperty().bind(angle);
camera.setRotationAxis(Rotate.Y_AXIS);
rotateX = new Rotate();
rotateX.setAxis(Rotate.X_AXIS);
camera.getTransforms().add(rotateX);
}
public void reset() throws IOException {
this.getChildren().clear();
initMaze();
}
}
/**
Classe contrôleur
*/
protected abstract class GameControl {
protected double mouseXOld;
protected double mouseYOld;
protected int action = -1;
protected LongProperty lastUpdate;
protected AnimationTimer gameTimer;
protected Button save, inv, quit, help, restart, pause, plan;
/**
Création de la barre de menu avec les boutons sauvegarder, quitter, pause, plan active si le joueur possède plus de 5 pièces
*/
public void setToolBar(boolean multi) {
String style = "-fx-background-color: rgba(0, 0, 0, 0);";
String style1="-fx-background-color: rgba(0, 0, 0, 0);-fx-font-size:23px;-fx-text-fill: white;";
quit = new Button();
configButton("images/quit.png", quit);
quit.setOnMouseClicked(e -> {
st.close();
});
plan = new Button();
configButton("images/map.png", plan);
plan.setOnMouseClicked(e -> {
if (!main.getChildren().contains(map)) {
main.getChildren().add(map);
StackPane.setAlignment(map, Pos.BOTTOM_LEFT);
tool.toFront();
}
else main.getChildren().remove(map);
});
plan.setDisable(true);
pause = new Button();
configButton("images/pause.png", pause);
pause.setOnMouseClicked(e -> {
if (!main.getChildren().contains(display)) {
configButton("images/play.png", pause);
timePane.pause();
display.getChildren().clear();
Label pau = new Label("PAUSE");
pau.setStyle("-fx-effect: dropshadow(gaussian,rgba(0,0,0,0.75) , 4,0,0,1 );-fx-text-fill: lightgrey;-fx-font-size: 150px;-fx-font-weight:bold;");
display.getChildren().add(pau);
display.setStyle("-fx-background-color: rgba(0, 0, 0, 0.4); -fx-background-radius: 10;");
main.getChildren().add(display);
tool.toFront();
} else {
configButton("images/pause.png", pause);
timePane.play();
main.getChildren().remove(display);
}
});
IntegerProperty nbPiece = new SimpleIntegerProperty();
IntegerProperty nbKey = new SimpleIntegerProperty();
Timeline pocket = new Timeline();
pocket.setCycleCount(Timeline.INDEFINITE);
pocket.getKeyFrames().add(new KeyFrame(Duration.millis(500),(evt)->{
nbPiece.set(game.player().getBonus().size());
nbKey.set(game.player().keys().size());
}));
pocket.playFromStart();
Label piece = new Label();
piece.textProperty().bind(nbPiece.asString());
MazeInterface.configLabel(piece,"/images/coin.png",style1);
Label key = new Label();
key.textProperty().bind(nbKey.asString());
MazeInterface.configLabel(key,"/images/key.png",style1);
save = new Button();
configButton("images/save.png", save);
save.setOnMouseClicked(e -> {
LocalDate now = LocalDate.now();
String date[] = now.toString().split("-");
try {
game.save("savings/"+game.player().getName()+"--"+date[2] + "-" + date[1] + "-" + date[0].charAt(2) +date[0].charAt(3));
} catch (Exception exc) {
exc.printStackTrace();
}
});
help = new Button();
configButton("images/help.png", help);
help.setOnMouseClicked(e -> {
if(main.getChildren().contains(display)){
if(!multi)timePane.play();
main.getChildren().remove(display);
}
else {
if(!multi)timePane.pause();
displayHelp();
main.getChildren().add(display);
tool.toFront();
}
});
Region rg = new Region();
HBox.setHgrow(rg, Priority.SOMETIMES);
rg.setFocusTraversable(false);
if(multi) tool=new ToolBar(rg,help,plan);
else tool = new ToolBar(rg, help,save,plan,pause);
if(game.current().getBonus()!=null) tool.getItems().add(piece);
if(game.current().getDoors()!=null) tool.getItems().add(key);
tool.getItems().add(quit);
for (Node a : tool.getItems()) {
if (a instanceof Button) {
a.setStyle(style);
a.setFocusTraversable(false);
}
}
if(game instanceof TimeTrialVersion)tool.getItems().remove(piece);
tool.setStyle("-fx-background-color: rgba(0, 0, 0, 0);");
main.getChildren().add(tool);
StackPane.setAlignment(tool,Pos.TOP_RIGHT);
}
public void configButton(String path, Button button) {
Image img = new Image(getClass().getResourceAsStream(path), 50, 50, false, false);
ImageView im = new ImageView(img);
im.setFitHeight(20);
im.setFitWidth(20);
button.setGraphic(new ImageView(img));
}
public GameControl(boolean multi) throws IOException {
lastUpdate = new SimpleLongProperty();
gameTimer = new AnimationTimer() {
@Override
public void handle(long now) {
if (game.gameOver()) {
whenIsFinished();
} else if (game.player.state() == Player.PlayerState.DEAD ) {
timePane.stop();
final ImageView imv = new ImageView();
final Image img = new Image(View.class.getResourceAsStream("images/death.png"));
imv.setImage(img);
main.getChildren().add(imv);
View.this.setOnKeyPressed(null);
gameTimer.stop();
isDead();
}
else if (timePane.timeOver()) {
MazeInterface.sounds(6).play();
final ImageView imv = new ImageView();
final Image img = new Image(View.class.getResourceAsStream("images/over.png"));
imv.setImage(img);
main.getChildren().add(imv);
View.this.setOnKeyPressed(null);
gameTimer.stop();
} else {
if (lastUpdate.get() > 0) {
double elapsedTime = (now - lastUpdate.get()) / 1000000000.0;
game.update(elapsedTime);
Point2D pos = game.player().getPosition();
double yPos = game.player().getY();
int floor = game.player().getMazeIndex();
mazePane.x.set((pos.getX() + mazePane.coordSwitch[floor].x()) * mazePane.SIZE_BOX - mazePane.SIZE_BOX / 2);
mazePane.z.set((pos.getY() + mazePane.coordSwitch[floor].z()) * mazePane.SIZE_BOX - mazePane.SIZE_BOX / 2);
mazePane.y.set(-yPos * mazePane.SIZE_BOX);
mazePane.angle.set(90 - game.player().orientation());
mazePane.rotateX.setAngle(game.player().orientationX());
if (game.player.hasPickedUp()) {
System.out.println((int) pos.getX() + " " + (int) pos.getY());
MazeInterface.sounds(0).play();
mazePane.remove(mazePane.floorGroups[floor], (int) pos.getX(), (int) pos.getY());
game.player.pick(false);
}
}
lastUpdate.set(now);
}
}
};
handleAction();
setToolBar(multi);
}
//Fonction de gestion des actions clavier du joueur
public void handleAction() throws IOException {
game.start();
mazePane.initMaze();
System.out.println(mazePane.floors);
System.out.println(game.floors());
gameTimer.start();
timePane.start();
int size=500/Math.max(mazePane.MAZE_LENGTH,mazePane.MAZE_WIDTH);
map = new Pane();
setUpMap(size);
View.this.setOnKeyPressed(e -> {
boolean pane = main.getChildren().contains(display);
if (!pane) {
if (game.player.state() != Player.PlayerState.JUMPING) {
switch (e.getCode()) {
case UP:
game.player.up(true);
break;
case RIGHT:
if (game.player.state() != Player.PlayerState.STAIRSDOWN && game.player.state() != Player.PlayerState.STAIRSUP)
game.player.right(true);
break;
case DOWN:
game.player.down(true);
break;
case LEFT:
if (game.player.state() != Player.PlayerState.STAIRSDOWN && game.player.state() != Player.PlayerState.STAIRSUP)
game.player.left(true);
break;
case SPACE:
if (game.player.state() != Player.PlayerState.STAIRSDOWN && game.player.state() != Player.PlayerState.STAIRSUP)
game.player.jump(true);
break;
case SHIFT:
if (e.isControlDown()) game.player.lookDown(true);
else game.player.lookUp(true);
break;
case Q:
if (e.isControlDown()) st.close();
break;
case M:
if(!plan.isDisable()){
if(!main.getChildren().contains(map))main.getChildren().add(map);
else main.getChildren().remove(map);
}
break;
}
map.getChildren().clear();
setUpMap(size);
StackPane.setAlignment(map,Pos.BOTTOM_LEFT);
}
}
});
View.this.setOnKeyReleased(e -> {
switch (e.getCode()) {
case UP:
game.player.up(false);
break;
case RIGHT:
game.player.right(false);
break;
case DOWN:
game.player.down(false);
break;
case LEFT:
game.player.left(false);
break;
case SPACE:
game.player.jump(false);
break;
case SHIFT:
game.player.lookUp(false);
game.player.lookDown(false);
break;
case CONTROL:game.player.lookDown(false);
break;
}
});
View.this.addEventHandler(MouseEvent.ANY, evt -> {
if (evt.getEventType() == MouseEvent.MOUSE_DRAGGED || evt.getEventType() == MouseEvent.MOUSE_PRESSED) {
double mouseXNew = evt.getSceneX();
double mouseYNew = evt.getSceneY();
if (evt.getEventType() == MouseEvent.MOUSE_DRAGGED) {
double pitchRotate = mazePane.rotateX.getAngle() + (mouseYNew - mouseYOld);
mazePane.rotateX.setAngle(pitchRotate);
double yawRotate = mazePane.angle.get() - (mouseXNew - mouseXOld);
mazePane.angle.set(yawRotate);
}
mouseXOld = mouseXNew;
mouseYOld = mouseYNew;
}
if (evt.getEventType() == MouseEvent.MOUSE_CLICKED) {
}
});
}
public void recenterMouse(double x, double y) {
Rectangle2D screen = Screen.getPrimary().getBounds();
x = screen.getWidth() / 2;
y = screen.getHeight() / 2;
}
public abstract void whenIsFinished();
public void moveMap(int size, Circle player) {
player.centerXProperty().set(game.player().getPosition().getX() * size+10);
player.centerYProperty().set(game.player().getPosition().getY() * size+300);
}
public void isDead() {
}
public void setUpMap(int size) {
Canvas mapDraw = new Canvas(600, 800);
GraphicsContext gc = mapDraw.getGraphicsContext2D();
int x = 10, y=300;
Maze maze = game.current();
for (int i = 0; i < maze.getHeight(); i++) {
for (int j = 0; j < maze.getWidth(); j++) {
switch (maze.getCase(i, j)) {
case Maze.WAY:
gc.setFill(Color.TRANSPARENT);
break;
case Maze.START:
gc.setFill(Color.BLUE);
break;
case Maze.END:
gc.setFill(Color.RED);
break;
case Maze.WALL:
gc.setFill(Color.LIGHTGREY);
break;
case Maze.STAIRSUP:
gc.setFill(Color.BROWN);
break;
case Maze.STAIRSDOWN:
gc.setFill(Color.BROWN);
break;
case Maze.OBSTACLE:
gc.setFill(Color.GREY);
break;
case Maze.MONSTRE:
gc.setFill(Color.WHITE);
break;
case Maze.TELEPORT:
gc.setFill(Color.PURPLE);
break;
case Maze.DOOR:
gc.setFill(Color.LIGHTBLUE);
break;
case Maze.KEY:
gc.setFill(Color.YELLOW);
break;
case Maze.BONUS:
gc.setFill(Color.YELLOW);
break;
}
gc.fillRect(x, y, size, size);
x += size;
}
y += size;
x = 10;
}
Circle player = new Circle(size/4);
player.setFill(Color.WHITESMOKE);
map.getChildren().addAll(mapDraw,player);
moveMap(size,player);
}
public void displayScore(Pane root) {
root.getChildren().clear();
String display = game.scores().getScores();
String[] splits = display.split("\n");
for (String s : splits) root.getChildren().add(new Label(s));
}
public void displayScores(Scores s) {
ScorePane sp = new ScorePane(s);
main.getChildren().clear();
main.getChildren().add(sp);
sp.printScores();
}
public void countDownToStart() {
Label label = new Label();
label.setStyle("-fx-background-color:transparent");
IntegerProperty count = new SimpleIntegerProperty(5);