From 7aadcb18bcf78e6d898dcab23ab25afe0fd449d8 Mon Sep 17 00:00:00 2001 From: Adam NAILI Date: Mon, 8 Jan 2018 19:21:37 +0100 Subject: Implementing some context classes (Context, GraphicsContext, InputHandler, Game) --- .../java/fr/umlv/java/wallj/context/Context.java | 46 +++++++++++++++- src/main/java/fr/umlv/java/wallj/context/Game.java | 62 +++++++++++++++++++++- .../umlv/java/wallj/context/GraphicsContext.java | 35 +++++++++++- .../fr/umlv/java/wallj/context/InputHandler.java | 55 ++++++++++++++++++- 4 files changed, 194 insertions(+), 4 deletions(-) (limited to 'src/main/java/fr') diff --git a/src/main/java/fr/umlv/java/wallj/context/Context.java b/src/main/java/fr/umlv/java/wallj/context/Context.java index a20da6f..a924c2b 100644 --- a/src/main/java/fr/umlv/java/wallj/context/Context.java +++ b/src/main/java/fr/umlv/java/wallj/context/Context.java @@ -1,5 +1,49 @@ package fr.umlv.java.wallj.context; -public class Context { +import fr.umlv.java.wallj.event.Event; +import fr.umlv.java.wallj.model.Stage; + +import java.util.List; +import java.util.Objects; + +/** + * A context used to store the current state of the application at one tick. + */ +public final class Context { //TODO Class Context + private final Stage stage; + private final List events; + private final GraphicsContext graphicsContext; + + /** + * @param stage the current stage + * @param events the list of events of the tick + * @param graphicsContext the current graphics context + */ + public Context(Stage stage, List events, GraphicsContext graphicsContext) { + this.stage = Objects.requireNonNull(stage); + this.events = Objects.requireNonNull(events); + this.graphicsContext = Objects.requireNonNull(graphicsContext); + } + + /** + * @return the stage + */ + public Stage getStage() { + return stage; + } + + /** + * @return the list of events + */ + public List getEvents() { + return events; + } + + /** + * @return the graphics context + */ + public GraphicsContext getGraphicsContext() { + return graphicsContext; + } } diff --git a/src/main/java/fr/umlv/java/wallj/context/Game.java b/src/main/java/fr/umlv/java/wallj/context/Game.java index f32ed65..b3418c4 100644 --- a/src/main/java/fr/umlv/java/wallj/context/Game.java +++ b/src/main/java/fr/umlv/java/wallj/context/Game.java @@ -1,5 +1,65 @@ package fr.umlv.java.wallj.context; -public class Game { +import fr.umlv.java.wallj.board.Board; +import fr.umlv.java.wallj.controller.GameController; + +import java.util.Collections; +import java.util.List; +import java.util.NoSuchElementException; +import java.util.Objects; + +/** + * A game + */ +public final class Game { //TODO + private final GameController gameController; + private int indexBoard; + private final List boards; + + /** + * @param gameController the controller of the game + * @param boards the list of boards charged for the game + */ + public Game(GameController gameController, List boards) { + this.gameController = Objects.requireNonNull(gameController); + Objects.requireNonNull(boards); + if (boards.isEmpty()) { + throw new IllegalArgumentException("The list of boards is empty, not able to create a correct game from this."); + } + this.boards = Collections.unmodifiableList(boards); + this.indexBoard = 0; + } + + /** + * @return the game controller + */ + public GameController getGameController() { + return gameController; + } + + /** + * @return the current board of the game. + */ + public Board getCurrentBoard() { + return boards.get(indexBoard); + } + + /** + * @return a boolean on the condition of having a next Board in our game. + */ + public boolean hasNextBoard() { + return indexBoard + 1 < boards.size(); + } + + /** + * @return the next board + */ + public Board nextBoard() { + if (indexBoard >= boards.size()) { + throw new NoSuchElementException("No more board for the game."); + } + indexBoard++; + return boards.get(indexBoard); + } } diff --git a/src/main/java/fr/umlv/java/wallj/context/GraphicsContext.java b/src/main/java/fr/umlv/java/wallj/context/GraphicsContext.java index 57e21fe..214eeb0 100644 --- a/src/main/java/fr/umlv/java/wallj/context/GraphicsContext.java +++ b/src/main/java/fr/umlv/java/wallj/context/GraphicsContext.java @@ -1,5 +1,38 @@ package fr.umlv.java.wallj.context; -public class GraphicsContext { +import fr.umlv.zen5.ScreenInfo; + +import java.awt.Graphics2D; +import java.util.Objects; + +/** + * A context of the current graphic status of the application + */ +public final class GraphicsContext { //TODO Class GraphicsContext + private final Graphics2D graphics2D; + private final ScreenInfo screenInfo; + + /** + * @param graphics2D the current drawable canvas + * @param screenInfo the informations about the screen + */ + public GraphicsContext(Graphics2D graphics2D, ScreenInfo screenInfo) { + this.graphics2D = Objects.requireNonNull(graphics2D); + this.screenInfo = Objects.requireNonNull(screenInfo); + } + + /** + * @return the drawable canvas + */ + public Graphics2D getGraphics2D() { + return graphics2D; + } + + /** + * @return the screen informations + */ + public ScreenInfo getScreenInfo() { + return screenInfo; + } } diff --git a/src/main/java/fr/umlv/java/wallj/context/InputHandler.java b/src/main/java/fr/umlv/java/wallj/context/InputHandler.java index 142a8e8..2881da9 100644 --- a/src/main/java/fr/umlv/java/wallj/context/InputHandler.java +++ b/src/main/java/fr/umlv/java/wallj/context/InputHandler.java @@ -1,5 +1,58 @@ package fr.umlv.java.wallj.context; -public class InputHandler { +import fr.umlv.java.wallj.board.TileVec2; +import fr.umlv.java.wallj.event.DropBombEvent; +import fr.umlv.java.wallj.event.MoveRobotEvent; +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; + +import static fr.umlv.java.wallj.board.TileVec2.of; + +public final class InputHandler { //TODO Class InputHandler + private final ApplicationContext applicationContext; + + /** + * @param applicationContext + */ + public InputHandler(ApplicationContext applicationContext) { + this.applicationContext = Objects.requireNonNull(applicationContext); + } + + /** + * @return + */ + 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 MoveRobotEvent(mouseTileLocation)); + } + } + KeyboardKey keyboardKey = event.getKey(); + if (keyboardKey != null) { //Keyboard Handling + if (action == Event.Action.KEY_PRESSED) { + switch (keyboardKey) { + case SPACE: + events.add(new DropBombEvent()); + break; + } + } + } + } + return events; + } } -- cgit v1.2.3