From 652db1608644b790489ffd4295a63a340364b88f Mon Sep 17 00:00:00 2001 From: pacien Date: Sun, 4 Feb 2018 22:29:23 +0100 Subject: Refactor Viewer Signed-off-by: pacien --- .../fr/umlv/java/wallj/viewer/InputHandler.java | 69 -------------- src/main/java/fr/umlv/java/wallj/viewer/Main.java | 3 +- .../fr/umlv/java/wallj/viewer/ScreenManager.java | 40 -------- .../java/fr/umlv/java/wallj/viewer/Viewer.java | 104 +++++++++++++++------ 4 files changed, 77 insertions(+), 139 deletions(-) delete mode 100644 src/main/java/fr/umlv/java/wallj/viewer/InputHandler.java delete mode 100644 src/main/java/fr/umlv/java/wallj/viewer/ScreenManager.java (limited to 'src/main/java') 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 @@ -package fr.umlv.java.wallj.viewer; - -import fr.umlv.java.wallj.board.TileVec2; -import fr.umlv.java.wallj.event.*; -import fr.umlv.zen5.ApplicationContext; -import fr.umlv.zen5.Event; -import fr.umlv.zen5.KeyboardKey; -import org.jbox2d.common.Vec2; - -import java.awt.geom.Point2D; -import java.util.LinkedList; -import java.util.List; -import java.util.Objects; - - -/** - * Treats the inputs from the keyboard and mouse provided by Zen 5 and creates Events meaningful for the game. - * - * @author Adam NAILI - */ -public final class InputHandler { - private final ApplicationContext applicationContext; - - /** - * @param applicationContext the Zen5 application context - */ - public InputHandler(ApplicationContext applicationContext) { - this.applicationContext = Objects.requireNonNull(applicationContext); - } - - /** - * @return the list of events converted from Zen 5 events to game events - */ - public List getEvents() { - LinkedList events = new LinkedList<>(); - fr.umlv.zen5.Event event = applicationContext.pollEvent(); - if (event != null) { - Event.Action action = event.getAction(); - Point2D.Float location = event.getLocation(); - if (location != null) { //Mouse Handling - if (action == Event.Action.POINTER_DOWN) { - Vec2 mouseLocation = new Vec2(location.x, location.y); - TileVec2 mouseTileLocation = TileVec2.of(mouseLocation); - events.add(new MoveRobotOrder(mouseTileLocation)); - } - } - KeyboardKey keyboardKey = event.getKey(); - if (keyboardKey != null) { //Keyboard Handling - if (action == Event.Action.KEY_PRESSED) { - switch (keyboardKey) { - case SPACE: - events.add(new BombSetupOrder()); - break; - case R: - events.add(new ConfirmOrder()); - break; - case Q: - events.add(new QuitGameOrder()); - break; - case S: - events.add(new SimulationStartOrder()); - break; - } - } - } - } - return events; - } -} 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 { .map(Main::validateBoard) .collect(Collectors.toList()); - Viewer viewer = new Viewer(levels); - Application.run(Viewer.BACKGROUND_COLOR, viewer::eventLoop); + Application.run(Viewer.BACKGROUND_COLOR, appContext -> (new Viewer(appContext, levels)).run()); } 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 @@ -package fr.umlv.java.wallj.viewer; - -import fr.umlv.java.wallj.context.GraphicsContext; -import fr.umlv.zen5.ApplicationContext; -import fr.umlv.zen5.ScreenInfo; -import org.jbox2d.common.Vec2; - -import java.awt.Graphics2D; -import java.util.Objects; - -/** - * Cleans the GraphicsContext - * - * @author Adam NAILI - */ -public final class ScreenManager { - private final ApplicationContext applicationContext; - private final Graphics2D graphics2D; - - /** - * - * @param applicationContext the current application context - * @param graphics2D the current graphics2D - */ - public ScreenManager(ApplicationContext applicationContext, Graphics2D graphics2D) { - this.applicationContext = Objects.requireNonNull(applicationContext); - this.graphics2D = Objects.requireNonNull(graphics2D); - } - - /** - * - * @return a graphic context - */ - public GraphicsContext clearScreen() { - ScreenInfo screenInfo = applicationContext.getScreenInfo(); - GraphicsContext graphicsContext = new GraphicsContext(graphics2D, screenInfo); - graphicsContext.paintRectangle(graphics2D.getBackground(), new Vec2(0, 0), screenInfo.getWidth(), screenInfo.getHeight()); - return graphicsContext; - } -} 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 @@ package fr.umlv.java.wallj.viewer; import fr.umlv.java.wallj.board.Board; +import fr.umlv.java.wallj.board.TileVec2; import fr.umlv.java.wallj.context.Context; import fr.umlv.java.wallj.context.Game; +import fr.umlv.java.wallj.context.GraphicsContext; +import fr.umlv.java.wallj.event.*; import fr.umlv.java.wallj.event.Event; import fr.umlv.zen5.ApplicationContext; +import fr.umlv.zen5.ScreenInfo; +import org.jbox2d.common.Vec2; import java.awt.*; +import java.awt.geom.Point2D; import java.time.Duration; import java.util.LinkedList; import java.util.List; import java.util.Objects; import java.util.stream.Collectors; +import java.util.stream.Stream; /** * Link between application and Zen 5 * * @author Adam NAILI + * @author Pacien TRAN-GIRARD */ public final class Viewer { public static final Color BACKGROUND_COLOR = Color.WHITE; + private static final Duration FRAME_DURATION = Duration.ofMillis(1000 / 60); + private static int EXIT_OK = 0; - private final Game currentGame; + private final ApplicationContext appContext; + private final Game game; /** * @param boards the valid list of boards charged in the application */ - public Viewer(List boards) { - this.currentGame = new Game(Objects.requireNonNull(boards)); + public Viewer(ApplicationContext appContext, List boards) { + this.appContext = Objects.requireNonNull(appContext); + this.game = new Game(boards); } - /** - * @param applicationContext the application context from Zen 5 - */ - public void eventLoop(ApplicationContext applicationContext) { - List events = new LinkedList<>(); - Duration lastExecDuration = Duration.ZERO; - while (!currentGame.isOver()) { - Duration last = lastExecDuration; + public void run() { + List forwardEvents = new LinkedList<>(); + Duration lastExec = Duration.ZERO; + + while (!game.isOver()) { + final Duration lastExecCopy = lastExec; StopWatch stopWatch = new StopWatch(); - applicationContext.renderFrame(graphics2D -> { - InputHandler inputHandler = new InputHandler(applicationContext); - ScreenManager screenManager = new ScreenManager(applicationContext, graphics2D); - events.addAll(inputHandler.getEvents()); - Context context = new Context(currentGame, events, screenManager.clearScreen(), last); - List newEvents = currentGame.update(context).collect(Collectors.toList()); //return new events created from update(); - events.clear(); - events.addAll(newEvents); //add the new events returned by updates + List events = Stream.concat(forwardEvents.stream(), mapInputEvent()).collect(Collectors.toList()); + forwardEvents.clear(); + + appContext.renderFrame(graphics2D -> { + GraphicsContext graphicsContext = new GraphicsContext(graphics2D, appContext.getScreenInfo()); + clearScreen(graphicsContext); + Context context = new Context(game, events, graphicsContext, lastExecCopy); + game.update(context).forEach(forwardEvents::add); }); - try { - Duration sleepDuration = FRAME_DURATION.minus(stopWatch.peek()); - if (!sleepDuration.isNegative()) Thread.sleep(sleepDuration.toMillis()); - } catch (Exception e) { - applicationContext.exit(-1); - } - lastExecDuration = stopWatch.peek(); + + sleep(FRAME_DURATION.minus(stopWatch.peek())); + lastExec = stopWatch.peek(); } - applicationContext.exit(0); + + appContext.exit(EXIT_OK); } -} + private Stream mapInputEvent() { + fr.umlv.zen5.Event inputEvent = appContext.pollEvent(); + if (inputEvent == null) return Stream.empty(); + + switch (inputEvent.getAction()) { + case POINTER_DOWN: + Point2D.Float clickLocation = inputEvent.getLocation(); + TileVec2 tile = TileVec2.of(new Vec2(clickLocation.x, clickLocation.y)); + return Stream.of(new MoveRobotOrder(tile)); + + case KEY_PRESSED: + switch (inputEvent.getKey()) { + case SPACE: + return Stream.of(new BombSetupOrder()); + case R: + return Stream.of(new ConfirmOrder()); + case Q: + return Stream.of(new QuitGameOrder()); + case S: + return Stream.of(new SimulationStartOrder()); + default: + return Stream.empty(); + } + + default: + return Stream.empty(); + } + } + + private void clearScreen(GraphicsContext graphicsContext) { + ScreenInfo window = graphicsContext.getScreenInfo(); + graphicsContext.paintRectangle(BACKGROUND_COLOR, new Vec2(), window.getWidth(), window.getHeight()); + } + + private void sleep(Duration duration) { + try { + if (!duration.isNegative()) Thread.sleep(duration.toMillis()); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + } +} -- cgit v1.2.3