diff options
author | pacien | 2018-02-04 22:29:23 +0100 |
---|---|---|
committer | pacien | 2018-02-04 22:29:23 +0100 |
commit | 652db1608644b790489ffd4295a63a340364b88f (patch) | |
tree | 1cebb0c35e1513d165b405016b030b455bfa956f /src/main/java | |
parent | fbaba931804b6ad4b5b9668afbcd1809266682b0 (diff) | |
download | wallj-652db1608644b790489ffd4295a63a340364b88f.tar.gz |
Refactor Viewer
Signed-off-by: pacien <pacien.trangirard@pacien.net>
Diffstat (limited to 'src/main/java')
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 @@ | |||
1 | package fr.umlv.java.wallj.viewer; | ||
2 | |||
3 | import fr.umlv.java.wallj.board.TileVec2; | ||
4 | import fr.umlv.java.wallj.event.*; | ||
5 | import fr.umlv.zen5.ApplicationContext; | ||
6 | import fr.umlv.zen5.Event; | ||
7 | import fr.umlv.zen5.KeyboardKey; | ||
8 | import org.jbox2d.common.Vec2; | ||
9 | |||
10 | import java.awt.geom.Point2D; | ||
11 | import java.util.LinkedList; | ||
12 | import java.util.List; | ||
13 | import 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 | */ | ||
21 | public 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 @@ | |||
1 | package fr.umlv.java.wallj.viewer; | ||
2 | |||
3 | import fr.umlv.java.wallj.context.GraphicsContext; | ||
4 | import fr.umlv.zen5.ApplicationContext; | ||
5 | import fr.umlv.zen5.ScreenInfo; | ||
6 | import org.jbox2d.common.Vec2; | ||
7 | |||
8 | import java.awt.Graphics2D; | ||
9 | import java.util.Objects; | ||
10 | |||
11 | /** | ||
12 | * Cleans the GraphicsContext | ||
13 | * | ||
14 | * @author Adam NAILI | ||
15 | */ | ||
16 | public 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 @@ | |||
1 | package fr.umlv.java.wallj.viewer; | 1 | package fr.umlv.java.wallj.viewer; |
2 | 2 | ||
3 | import fr.umlv.java.wallj.board.Board; | 3 | import fr.umlv.java.wallj.board.Board; |
4 | import fr.umlv.java.wallj.board.TileVec2; | ||
4 | import fr.umlv.java.wallj.context.Context; | 5 | import fr.umlv.java.wallj.context.Context; |
5 | import fr.umlv.java.wallj.context.Game; | 6 | import fr.umlv.java.wallj.context.Game; |
7 | import fr.umlv.java.wallj.context.GraphicsContext; | ||
8 | import fr.umlv.java.wallj.event.*; | ||
6 | import fr.umlv.java.wallj.event.Event; | 9 | import fr.umlv.java.wallj.event.Event; |
7 | import fr.umlv.zen5.ApplicationContext; | 10 | import fr.umlv.zen5.ApplicationContext; |
11 | import fr.umlv.zen5.ScreenInfo; | ||
12 | import org.jbox2d.common.Vec2; | ||
8 | 13 | ||
9 | import java.awt.*; | 14 | import java.awt.*; |
15 | import java.awt.geom.Point2D; | ||
10 | import java.time.Duration; | 16 | import java.time.Duration; |
11 | import java.util.LinkedList; | 17 | import java.util.LinkedList; |
12 | import java.util.List; | 18 | import java.util.List; |
13 | import java.util.Objects; | 19 | import java.util.Objects; |
14 | import java.util.stream.Collectors; | 20 | import java.util.stream.Collectors; |
21 | import 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 | */ |
21 | public final class Viewer { | 29 | public 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);< |