aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/fr/umlv
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/fr/umlv')
-rw-r--r--src/main/java/fr/umlv/java/wallj/viewer/InputHandler.java69
-rw-r--r--src/main/java/fr/umlv/java/wallj/viewer/Main.java3
-rw-r--r--src/main/java/fr/umlv/java/wallj/viewer/ScreenManager.java40
-rw-r--r--src/main/java/fr/umlv/java/wallj/viewer/Viewer.java104
4 files changed, 77 insertions, 139 deletions
diff --git a/src/main/java/fr/umlv/java/wallj/viewer/InputHandler.java b/src/main/java/fr/umlv/java/wallj/viewer/InputHandler.java
deleted file mode 100644
index 9d5aa9f..0000000
--- a/src/main/java/fr/umlv/java/wallj/viewer/InputHandler.java
+++ /dev/null
@@ -1,69 +0,0 @@
1package fr.umlv.java.wallj.viewer;
2
3import fr.umlv.java.wallj.board.TileVec2;
4import fr.umlv.java.wallj.event.*;
5import fr.umlv.zen5.ApplicationContext;
6import fr.umlv.zen5.Event;
7import fr.umlv.zen5.KeyboardKey;
8import org.jbox2d.common.Vec2;
9
10import java.awt.geom.Point2D;
11import java.util.LinkedList;
12import java.util.List;
13import java.util.Objects;
14
15
16/**
17 * Treats the inputs from the keyboard and mouse provided by Zen 5 and creates Events meaningful for the game.
18 *
19 * @author Adam NAILI
20 */
21public final class InputHandler {
22 private final ApplicationContext applicationContext;
23
24 /**
25 * @param applicationContext the Zen5 application context
26 */
27 public InputHandler(ApplicationContext applicationContext) {
28 this.applicationContext = Objects.requireNonNull(applicationContext);
29 }
30
31 /**
32 * @return the list of events converted from Zen 5 events to game events
33 */
34 public List<fr.umlv.java.wallj.event.Event> getEvents() {
35 LinkedList<fr.umlv.java.wallj.event.Event> events = new LinkedList<>();
36 fr.umlv.zen5.Event event = applicationContext.pollEvent();
37 if (event != null) {
38 Event.Action action = event.getAction();
39 Point2D.Float location = event.getLocation();
40 if (location != null) { //Mouse Handling
41 if (action == Event.Action.POINTER_DOWN) {
42 Vec2 mouseLocation = new Vec2(location.x, location.y);
43 TileVec2 mouseTileLocation = TileVec2.of(mouseLocation);
44 events.add(new MoveRobotOrder(mouseTileLocation));
45 }
46 }
47 KeyboardKey keyboardKey = event.getKey();
48 if (keyboardKey != null) { //Keyboard Handling
49 if (action == Event.Action.KEY_PRESSED) {
50 switch (keyboardKey) {
51 case SPACE:
52 events.add(new BombSetupOrder());
53 break;
54 case R:
55 events.add(new ConfirmOrder());
56 break;
57 case Q:
58 events.add(new QuitGameOrder());
59 break;
60 case S:
61 events.add(new SimulationStartOrder());
62 break;
63 }
64 }
65 }
66 }
67 return events;
68 }
69}
diff --git a/src/main/java/fr/umlv/java/wallj/viewer/Main.java b/src/main/java/fr/umlv/java/wallj/viewer/Main.java
index 4483069..064eed3 100644
--- a/src/main/java/fr/umlv/java/wallj/viewer/Main.java
+++ b/src/main/java/fr/umlv/java/wallj/viewer/Main.java
@@ -83,8 +83,7 @@ public final class Main {
83 .map(Main::validateBoard) 83 .map(Main::validateBoard)
84 .collect(Collectors.toList()); 84 .collect(Collectors.toList());
85 85
86 Viewer viewer = new Viewer(levels); 86 Application.run(Viewer.BACKGROUND_COLOR, appContext -> (new Viewer(appContext, levels)).run());
87 Application.run(Viewer.BACKGROUND_COLOR, viewer::eventLoop);
88 } 87 }
89 88
90 private Main() { 89 private Main() {
diff --git a/src/main/java/fr/umlv/java/wallj/viewer/ScreenManager.java b/src/main/java/fr/umlv/java/wallj/viewer/ScreenManager.java
deleted file mode 100644
index 70377ba..0000000
--- a/src/main/java/fr/umlv/java/wallj/viewer/ScreenManager.java
+++ /dev/null
@@ -1,40 +0,0 @@
1package fr.umlv.java.wallj.viewer;
2
3import fr.umlv.java.wallj.context.GraphicsContext;
4import fr.umlv.zen5.ApplicationContext;
5import fr.umlv.zen5.ScreenInfo;
6import org.jbox2d.common.Vec2;
7
8import java.awt.Graphics2D;
9import java.util.Objects;
10
11/**
12 * Cleans the GraphicsContext
13 *
14 * @author Adam NAILI
15 */
16public final class ScreenManager {
17 private final ApplicationContext applicationContext;
18 private final Graphics2D graphics2D;
19
20 /**
21 *
22 * @param applicationContext the current application context
23 * @param graphics2D the current graphics2D
24 */
25 public ScreenManager(ApplicationContext applicationContext, Graphics2D graphics2D) {
26 this.applicationContext = Objects.requireNonNull(applicationContext);
27 this.graphics2D = Objects.requireNonNull(graphics2D);
28 }
29
30 /**
31 *
32 * @return a graphic context
33 */
34 public GraphicsContext clearScreen() {
35 ScreenInfo screenInfo = applicationContext.getScreenInfo();
36 GraphicsContext graphicsContext = new GraphicsContext(graphics2D, screenInfo);
37 graphicsContext.paintRectangle(graphics2D.getBackground(), new Vec2(0, 0), screenInfo.getWidth(), screenInfo.getHeight());
38 return graphicsContext;
39 }
40}
diff --git a/src/main/java/fr/umlv/java/wallj/viewer/Viewer.java b/src/main/java/fr/umlv/java/wallj/viewer/Viewer.java
index b9686b0..ea6f3d0 100644
--- a/src/main/java/fr/umlv/java/wallj/viewer/Viewer.java
+++ b/src/main/java/fr/umlv/java/wallj/viewer/Viewer.java
@@ -1,63 +1,111 @@
1package fr.umlv.java.wallj.viewer; 1package fr.umlv.java.wallj.viewer;
2 2
3import fr.umlv.java.wallj.board.Board; 3import fr.umlv.java.wallj.board.Board;
4import fr.umlv.java.wallj.board.TileVec2;
4import fr.umlv.java.wallj.context.Context; 5import fr.umlv.java.wallj.context.Context;
5import fr.umlv.java.wallj.context.Game; 6import fr.umlv.java.wallj.context.Game;
7import fr.umlv.java.wallj.context.GraphicsContext;
8import fr.umlv.java.wallj.event.*;
6import fr.umlv.java.wallj.event.Event; 9import fr.umlv.java.wallj.event.Event;
7import fr.umlv.zen5.ApplicationContext; 10import fr.umlv.zen5.ApplicationContext;
11import fr.umlv.zen5.ScreenInfo;
12import org.jbox2d.common.Vec2;
8 13
9import java.awt.*; 14import java.awt.*;
15import java.awt.geom.Point2D;
10import java.time.Duration; 16import java.time.Duration;
11import java.util.LinkedList; 17import java.util.LinkedList;
12import java.util.List; 18import java.util.List;
13import java.util.Objects; 19import java.util.Objects;
14import java.util.stream.Collectors; 20import java.util.stream.Collectors;
21import java.util.stream.Stream;
15 22
16/** 23/**
17 * Link between application and Zen 5 24 * Link between application and Zen 5
18 * 25 *
19 * @author Adam NAILI 26 * @author Adam NAILI
27 * @author Pacien TRAN-GIRARD
20 */ 28 */
21public final class Viewer { 29public final class Viewer {
22 public static final Color BACKGROUND_COLOR = Color.WHITE; 30 public static final Color BACKGROUND_COLOR = Color.WHITE;
31
23 private static final Duration FRAME_DURATION = Duration.ofMillis(1000 / 60); 32 private static final Duration FRAME_DURATION = Duration.ofMillis(1000 / 60);
33 private static int EXIT_OK = 0;
24 34
25 private final Game currentGame; 35 private final ApplicationContext appContext;
36 private final Game game;
26 37
27 /** 38 /**
28 * @param boards the valid list of boards charged in the application 39 * @param boards the valid list of boards charged in the application
29 */ 40 */
30 public Viewer(List<Board> boards) { 41 public Viewer(ApplicationContext appContext, List<Board> boards) {
31 this.currentGame = new Game(Objects.requireNonNull(boards)); 42 this.appContext = Objects.requireNonNull(appContext);
43 this.game = new Game(boards);
32 } 44 }
33 45
34 /** 46 public void run() {
35 * @param applicationContext the application context from Zen 5 47 List<Event> forwardEvents = new LinkedList<>();
36 */ 48 Duration lastExec = Duration.ZERO;
37 public void eventLoop(ApplicationContext applicationContext) { 49
38 List<Event> events = new LinkedList<>(); 50 while (!game.isOver()) {
39 Duration lastExecDuration = Duration.ZERO; 51 final Duration lastExecCopy = lastExec;
40 while (!currentGame.isOver()) {
41 Duration last = lastExecDuration;
42 StopWatch stopWatch = new StopWatch(); 52 StopWatch stopWatch = new StopWatch();
43 applicationContext.renderFrame(graphics2D -> { 53 List<Event> events = Stream.concat(forwardEvents.stream(), mapInputEvent()).collect(Collectors.toList());
44 InputHandler inputHandler = new InputHandler(applicationContext); 54 forwardEvents.clear();
45 ScreenManager screenManager = new ScreenManager(applicationContext, graphics2D); 55
46 events.addAll(inputHandler.getEvents()); 56 appContext.renderFrame(graphics2D -> {
47 Context context = new Context(current