From 5bff076cc86aa7c09baea850c3617ee2c1253594 Mon Sep 17 00:00:00 2001 From: Adam NAILI Date: Thu, 1 Feb 2018 20:48:59 +0100 Subject: Modifying Input handling, Thread sleep and various things --- src/main/java/fr/umlv/java/wallj/Main.java | 2 +- src/main/java/fr/umlv/java/wallj/block/Stage.java | 3 ++- src/main/java/fr/umlv/java/wallj/context/Game.java | 3 ++- .../java/fr/umlv/java/wallj/context/InputHandler.java | 8 ++++++++ .../fr/umlv/java/wallj/controller/GameController.java | 17 +++++++++-------- .../java/wallj/controller/GarbagePhysicsController.java | 3 ++- .../fr/umlv/java/wallj/controller/StageController.java | 3 ++- .../java/wallj/controller/TrashPhysicsController.java | 3 ++- src/main/java/fr/umlv/java/wallj/viewer/Viewer.java | 10 ++++++++++ 9 files changed, 38 insertions(+), 14 deletions(-) (limited to 'src/main') diff --git a/src/main/java/fr/umlv/java/wallj/Main.java b/src/main/java/fr/umlv/java/wallj/Main.java index 0ac4136..37a99b0 100644 --- a/src/main/java/fr/umlv/java/wallj/Main.java +++ b/src/main/java/fr/umlv/java/wallj/Main.java @@ -27,7 +27,7 @@ public class Main { paths.add(Paths.get(string)); } } else { - paths.add(getResourcePath("/maps/smallvalid.txt")); + paths.add(getResourcePath("/maps/level0.txt")); } return paths; } diff --git a/src/main/java/fr/umlv/java/wallj/block/Stage.java b/src/main/java/fr/umlv/java/wallj/block/Stage.java index d34af97..5d2d07c 100644 --- a/src/main/java/fr/umlv/java/wallj/block/Stage.java +++ b/src/main/java/fr/umlv/java/wallj/block/Stage.java @@ -4,6 +4,7 @@ import fr.umlv.java.wallj.board.Board; import fr.umlv.java.wallj.context.Context; import fr.umlv.java.wallj.event.Event; +import java.util.Collections; import java.util.List; import java.util.Objects; @@ -31,7 +32,7 @@ public class Stage { */ public List update(Context context) { //TODO - return null; + return Collections.emptyList(); } public boolean isCleared() { 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 d5f9873..62454b0 100644 --- a/src/main/java/fr/umlv/java/wallj/context/Game.java +++ b/src/main/java/fr/umlv/java/wallj/context/Game.java @@ -64,7 +64,8 @@ public final class Game { public boolean isOver() { return over; } - public void setOver(){ + + public void setOver() { over = true; } 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 1a64667..faa80a7 100644 --- a/src/main/java/fr/umlv/java/wallj/context/InputHandler.java +++ b/src/main/java/fr/umlv/java/wallj/context/InputHandler.java @@ -1,7 +1,9 @@ package fr.umlv.java.wallj.context; import fr.umlv.java.wallj.board.TileVec2; +import fr.umlv.java.wallj.event.ConfirmEvent; import fr.umlv.java.wallj.event.DropBombEvent; +import fr.umlv.java.wallj.event.GameOverEvent; import fr.umlv.java.wallj.event.MoveRobotEvent; import fr.umlv.zen5.ApplicationContext; import fr.umlv.zen5.Event; @@ -53,6 +55,12 @@ public final class InputHandler { case SPACE: events.add(new DropBombEvent()); break; + case R: + events.add(new ConfirmEvent()); + break; + case Q: + events.add(new GameOverEvent()); + break; } } } diff --git a/src/main/java/fr/umlv/java/wallj/controller/GameController.java b/src/main/java/fr/umlv/java/wallj/controller/GameController.java index 456793a..df4eade 100644 --- a/src/main/java/fr/umlv/java/wallj/controller/GameController.java +++ b/src/main/java/fr/umlv/java/wallj/controller/GameController.java @@ -14,23 +14,24 @@ public class GameController implements Controller { @Override public List update(Context context) { boolean isConfirmEvent = context.getEvents().stream().anyMatch(event -> event instanceof ConfirmEvent); - boolean isGameOver = context.getEvents().stream().allMatch(event -> event instanceof GameOverEvent); + boolean isGameOverEvent = context.getEvents().stream().anyMatch(event -> event instanceof GameOverEvent); + Game currentGame = context.getGame(); LinkedList events = new LinkedList<>(); - if (isConfirmEvent) { - //TODO exiting - if (!isGameOver) { + //TODO exiting + if (isGameOverEvent) { + currentGame.setOver(); + } else { + if (isConfirmEvent) { if (currentGame.getCurrentStage().isCleared()) { if (currentGame.hasNextBoard()) { //continue currentGame.nextStage(); - } else { - events.add(new GameOverEvent()); + } else { //no more board so game over + currentGame.setOver(); } } else {//retry currentGame.retryStage(); } - } else { - currentGame.setOver(); } } return events; diff --git a/src/main/java/fr/umlv/java/wallj/controller/GarbagePhysicsController.java b/src/main/java/fr/umlv/java/wallj/controller/GarbagePhysicsController.java index f8a7c36..16adeba 100644 --- a/src/main/java/fr/umlv/java/wallj/controller/GarbagePhysicsController.java +++ b/src/main/java/fr/umlv/java/wallj/controller/GarbagePhysicsController.java @@ -4,6 +4,7 @@ import fr.umlv.java.wallj.context.Context; import fr.umlv.java.wallj.event.Event; import fr.umlv.java.wallj.block.GarbageBlock; +import java.util.Collections; import java.util.List; import java.util.Objects; @@ -19,7 +20,7 @@ public class GarbagePhysicsController extends PhysicsController { @Override public List update(Context context) { //TODO - return null; + return Collections.emptyList(); } } diff --git a/src/main/java/fr/umlv/java/wallj/controller/StageController.java b/src/main/java/fr/umlv/java/wallj/controller/StageController.java index d62e246..57a87fd 100644 --- a/src/main/java/fr/umlv/java/wallj/controller/StageController.java +++ b/src/main/java/fr/umlv/java/wallj/controller/StageController.java @@ -3,6 +3,7 @@ package fr.umlv.java.wallj.controller; import fr.umlv.java.wallj.context.Context; import fr.umlv.java.wallj.event.Event; +import java.util.Collections; import java.util.List; public class StageController implements Controller { @@ -10,7 +11,7 @@ public class StageController implements Controller { @Override public List update(Context context) { - return null; + return Collections.emptyList(); } } diff --git a/src/main/java/fr/umlv/java/wallj/controller/TrashPhysicsController.java b/src/main/java/fr/umlv/java/wallj/controller/TrashPhysicsController.java index 323f044..310a00f 100644 --- a/src/main/java/fr/umlv/java/wallj/controller/TrashPhysicsController.java +++ b/src/main/java/fr/umlv/java/wallj/controller/TrashPhysicsController.java @@ -4,6 +4,7 @@ import fr.umlv.java.wallj.context.Context; import fr.umlv.java.wallj.event.Event; import fr.umlv.java.wallj.block.TrashBlock; +import java.util.Collections; import java.util.List; import java.util.Objects; @@ -19,7 +20,7 @@ public class TrashPhysicsController extends PhysicsController { @Override public List update(Context context) { //TODO - return null; + return Collections.emptyList(); } } 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 a87f418..857453c 100644 --- a/src/main/java/fr/umlv/java/wallj/viewer/Viewer.java +++ b/src/main/java/fr/umlv/java/wallj/viewer/Viewer.java @@ -27,6 +27,7 @@ import java.util.Objects; public final class Viewer { private final Game currentGame; + /** * @param boards the valid list of boards charged in the application */ @@ -40,10 +41,19 @@ public final class Viewer { public void eventLoop(ApplicationContext applicationContext) { List events = new LinkedList<>(); while (!currentGame.isOver()) { + long timeBeforeExec = System.currentTimeMillis(); applicationContext.renderFrame(graphics2D -> { events.addAll(renderFrame(graphics2D, applicationContext, events)); //add the new events returned by updates }); + long timeAfterExec = System.currentTimeMillis(); + long delay = timeAfterExec - timeBeforeExec; + try { + Thread.sleep(delay > 0 ? delay : 0); + } catch (Exception e) { + applicationContext.exit(-1); + } } + applicationContext.exit(0); } /** -- cgit v1.2.3