From 8903ba543f2001d1939dbea58dda1f76907835ec Mon Sep 17 00:00:00 2001 From: pacien Date: Fri, 2 Feb 2018 10:43:27 +0100 Subject: Reorganize classes Signed-off-by: pacien --- build.xml | 2 +- src/main/java/fr/umlv/java/wallj/Main.java | 67 --------------------- .../fr/umlv/java/wallj/context/InputHandler.java | 69 ---------------------- .../fr/umlv/java/wallj/context/ScreenManager.java | 39 ------------ .../fr/umlv/java/wallj/viewer/InputHandler.java | 69 ++++++++++++++++++++++ src/main/java/fr/umlv/java/wallj/viewer/Main.java | 66 +++++++++++++++++++++ .../fr/umlv/java/wallj/viewer/ScreenManager.java | 40 +++++++++++++ .../java/fr/umlv/java/wallj/viewer/Viewer.java | 11 ---- 8 files changed, 176 insertions(+), 187 deletions(-) delete mode 100644 src/main/java/fr/umlv/java/wallj/Main.java delete mode 100644 src/main/java/fr/umlv/java/wallj/context/InputHandler.java delete mode 100644 src/main/java/fr/umlv/java/wallj/context/ScreenManager.java create mode 100644 src/main/java/fr/umlv/java/wallj/viewer/InputHandler.java create mode 100644 src/main/java/fr/umlv/java/wallj/viewer/Main.java create mode 100644 src/main/java/fr/umlv/java/wallj/viewer/ScreenManager.java diff --git a/build.xml b/build.xml index b900698..18b1a74 100644 --- a/build.xml +++ b/build.xml @@ -17,7 +17,7 @@ - + diff --git a/src/main/java/fr/umlv/java/wallj/Main.java b/src/main/java/fr/umlv/java/wallj/Main.java deleted file mode 100644 index 946a61c..0000000 --- a/src/main/java/fr/umlv/java/wallj/Main.java +++ /dev/null @@ -1,67 +0,0 @@ -package fr.umlv.java.wallj; - -import fr.umlv.java.wallj.board.Board; -import fr.umlv.java.wallj.board.BoardParser; -import fr.umlv.java.wallj.board.BoardValidator; -import fr.umlv.java.wallj.viewer.Viewer; -import fr.umlv.zen5.Application; - -import java.awt.*; -import java.io.IOException; -import java.net.URI; -import java.net.URISyntaxException; -import java.nio.file.*; -import java.util.HashMap; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; - -public class Main { - - private static final String DEFAULT_MAP_NAME = "/maps/level0.txt"; - private static java.util.List getResourcePaths(String[] args) throws URISyntaxException,IOException { - LinkedList paths = new LinkedList<>(); - String strFolder = System.getProperty("levels"); - if (strFolder != null) { - Path pathFolder = Paths.get(strFolder); - Files.newDirectoryStream(pathFolder).forEach(paths::add); - } else { - final URI uri = Main.class.getResource(DEFAULT_MAP_NAME).toURI(); - Map env = new HashMap<>(); - env.put("create", "true"); - FileSystem zipfs = FileSystems.newFileSystem(uri, env); - paths.add(Paths.get(uri)); - } - return paths; - } - - // TODO: use System.getProperty("levels") to get the path to the level directory - public static void main(String[] args) { - List boards = new LinkedList<>(); - try { - List boardPaths = Main.getResourcePaths(args); - for (Path path : boardPaths) { - BoardValidator boardValidator = new BoardValidator(BoardParser.parse(path)); - boards.add( - boardValidator.validate(BoardValidator.Constraint::hasActualReachableBlocks, "Some supposed reachable blocks are not reachable.") - .validate(BoardValidator.Constraint::hasMandatoryBlocks, "Some mandatory blocks are missing.") - .validate(BoardValidator.Constraint::isBounded, "The board is not correctly bounded.") - .validate(BoardValidator.Constraint::isHollow, "The board must have a unique and simple interior.") - .get()); - } - Viewer viewer = new Viewer(boards); - Application.run(Color.WHITE, viewer::eventLoop); - } catch (URISyntaxException e) { - System.err.println(e.getMessage()); - System.exit(1); - } catch (IOException e) { - System.err.println(e.getMessage()); - System.exit(2); - } catch (BoardValidator.ValidationException e) { - for (Throwable throwable : e.getSuppressed()) { - System.err.println(throwable.getMessage()); - } - System.exit(3); - } - } -} diff --git a/src/main/java/fr/umlv/java/wallj/context/InputHandler.java b/src/main/java/fr/umlv/java/wallj/context/InputHandler.java deleted file mode 100644 index b137061..0000000 --- a/src/main/java/fr/umlv/java/wallj/context/InputHandler.java +++ /dev/null @@ -1,69 +0,0 @@ -package fr.umlv.java.wallj.context; - -import fr.umlv.java.wallj.board.TileVec2; -import fr.umlv.java.wallj.event.BombSetupOrder; -import fr.umlv.java.wallj.event.ConfirmOrder; -import fr.umlv.java.wallj.event.GameOverEvent; -import fr.umlv.java.wallj.event.MoveRobotOrder; -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 GameOverEvent()); - break; - } - } - } - } - return events; - } -} diff --git a/src/main/java/fr/umlv/java/wallj/context/ScreenManager.java b/src/main/java/fr/umlv/java/wallj/context/ScreenManager.java deleted file mode 100644 index 1e73674..0000000 --- a/src/main/java/fr/umlv/java/wallj/context/ScreenManager.java +++ /dev/null @@ -1,39 +0,0 @@ -package fr.umlv.java.wallj.context; - -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/InputHandler.java b/src/main/java/fr/umlv/java/wallj/viewer/InputHandler.java new file mode 100644 index 0000000..a8f53d3 --- /dev/null +++ b/src/main/java/fr/umlv/java/wallj/viewer/InputHandler.java @@ -0,0 +1,69 @@ +package fr.umlv.java.wallj.viewer; + +import fr.umlv.java.wallj.board.TileVec2; +import fr.umlv.java.wallj.event.BombSetupOrder; +import fr.umlv.java.wallj.event.ConfirmOrder; +import fr.umlv.java.wallj.event.GameOverEvent; +import fr.umlv.java.wallj.event.MoveRobotOrder; +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 GameOverEvent()); + 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 new file mode 100644 index 0000000..ac1a088 --- /dev/null +++ b/src/main/java/fr/umlv/java/wallj/viewer/Main.java @@ -0,0 +1,66 @@ +package fr.umlv.java.wallj.viewer; + +import fr.umlv.java.wallj.board.Board; +import fr.umlv.java.wallj.board.BoardParser; +import fr.umlv.java.wallj.board.BoardValidator; +import fr.umlv.zen5.Application; + +import java.awt.*; +import java.io.IOException; +import java.net.URI; +import java.net.URISyntaxException; +import java.nio.file.*; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; + +public class Main { + + private static final String DEFAULT_MAP_NAME = "/maps/level0.txt"; + private static java.util.List getResourcePaths(String[] args) throws URISyntaxException,IOException { + LinkedList paths = new LinkedList<>(); + String strFolder = System.getProperty("levels"); + if (strFolder != null) { + Path pathFolder = Paths.get(strFolder); + Files.newDirectoryStream(pathFolder).forEach(paths::add); + } else { + final URI uri = Main.class.getResource(DEFAULT_MAP_NAME).toURI(); + Map env = new HashMap<>(); + env.put("create", "true"); + FileSystem zipfs = FileSystems.newFileSystem(uri, env); + paths.add(Paths.get(uri)); + } + return paths; + } + + // TODO: use System.getProperty("levels") to get the path to the level directory + public static void main(String[] args) { + List boards = new LinkedList<>(); + try { + List boardPaths = Main.getResourcePaths(args); + for (Path path : boardPaths) { + BoardValidator boardValidator = new BoardValidator(BoardParser.parse(path)); + boards.add( + boardValidator.validate(BoardValidator.Constraint::hasActualReachableBlocks, "Some supposed reachable blocks are not reachable.") + .validate(BoardValidator.Constraint::hasMandatoryBlocks, "Some mandatory blocks are missing.") + .validate(BoardValidator.Constraint::isBounded, "The board is not correctly bounded.") + .validate(BoardValidator.Constraint::isHollow, "The board must have a unique and simple interior.") + .get()); + } + Viewer viewer = new Viewer(boards); + Application.run(Color.WHITE, viewer::eventLoop); + } catch (URISyntaxException e) { + System.err.println(e.getMessage()); + System.exit(1); + } catch (IOException e) { + System.err.println(e.getMessage()); + System.exit(2); + } catch (BoardValidator.ValidationException e) { + for (Throwable throwable : e.getSuppressed()) { + System.err.println(throwable.getMessage()); + } + System.exit(3); + } + } +} diff --git a/src/main/java/fr/umlv/java/wallj/viewer/ScreenManager.java b/src/main/java/fr/umlv/java/wallj/viewer/ScreenManager.java new file mode 100644 index 0000000..70377ba --- /dev/null +++ b/src/main/java/fr/umlv/java/wallj/viewer/ScreenManager.java @@ -0,0 +1,40 @@ +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 430bff7..01a6468 100644 --- a/src/main/java/fr/umlv/java/wallj/viewer/Viewer.java +++ b/src/main/java/fr/umlv/java/wallj/viewer/Viewer.java @@ -1,22 +1,11 @@ package fr.umlv.java.wallj.viewer; import fr.umlv.java.wallj.board.Board; -import fr.umlv.java.wallj.board.BoardParser; -import fr.umlv.java.wallj.board.BoardValidator; import fr.umlv.java.wallj.context.Context; import fr.umlv.java.wallj.context.Game; -import fr.umlv.java.wallj.context.InputHandler; -import fr.umlv.java.wallj.context.ScreenManager; import fr.umlv.java.wallj.event.Event; -import fr.umlv.zen5.Application; import fr.umlv.zen5.ApplicationContext; -import sun.awt.image.ImageWatched; -import java.awt.*; -import java.io.IOException; -import java.net.URISyntaxException; -import java.nio.file.Path; -import java.nio.file.Paths; import java.time.Duration; import java.util.LinkedList; import java.util.List; -- cgit v1.2.3