aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpacien2018-02-02 01:59:10 +0100
committerpacien2018-02-02 01:59:10 +0100
commit143e6e0db41c16568d48051fb25d04410895f4c6 (patch)
tree23fb6fac392d419f52c385839d1dc7dc68026458
parent79f02268fc5c303b1e44b38a88b3e7815e764dd5 (diff)
downloadwallj-143e6e0db41c16568d48051fb25d04410895f4c6.tar.gz
Move controller functions for Game and Stage
Signed-off-by: pacien <pacien.trangirard@pacien.net>
-rw-r--r--src/main/java/fr/umlv/java/wallj/context/Game.java29
-rw-r--r--src/main/java/fr/umlv/java/wallj/context/Stage.java41
-rw-r--r--src/main/java/fr/umlv/java/wallj/controller/Controller.java11
-rw-r--r--src/main/java/fr/umlv/java/wallj/controller/GameStateController.java38
-rw-r--r--src/main/java/fr/umlv/java/wallj/controller/StageController.java17
-rw-r--r--src/main/java/fr/umlv/java/wallj/controller/StagePhysicsController.java30
6 files changed, 55 insertions, 111 deletions
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 41d51db..2f5b1ca 100644
--- a/src/main/java/fr/umlv/java/wallj/context/Game.java
+++ b/src/main/java/fr/umlv/java/wallj/context/Game.java
@@ -2,8 +2,10 @@ package fr.umlv.java.wallj.context;
2 2
3import fr.umlv.java.wallj.board.Board; 3import fr.umlv.java.wallj.board.Board;
4import fr.umlv.java.wallj.controller.Controller; 4import fr.umlv.java.wallj.controller.Controller;
5import fr.umlv.java.wallj.controller.GameStateController; 5import fr.umlv.java.wallj.event.ConfirmOrder;
6import fr.umlv.java.wallj.event.Event; 6import fr.umlv.java.wallj.event.Event;
7import fr.umlv.java.wallj.event.Events;
8import fr.umlv.java.wallj.event.GameOverEvent;
7 9
8import java.util.*; 10import java.util.*;
9 11
@@ -14,7 +16,6 @@ import java.util.*;
14 */ 16 */
15public final class Game implements Updateable { 17public final class Game implements Updateable {
16 private Stage currentStage; 18 private Stage currentStage;
17 private final List<Controller> controllers;
18 private int indexBoard; 19 private int indexBoard;
19 private final List<Board> boards; 20 private final List<Board> boards;
20 private boolean over; 21 private boolean over;
@@ -23,8 +24,6 @@ public final class Game implements Updateable {
23 * @param boards the list of boards charged for the game 24 * @param boards the list of boards charged for the game
24 */ 25 */
25 public Game(List<Board> boards) { 26 public Game(List<Board> boards) {
26 this.controllers = new LinkedList<>();
27 this.controllers.add(new GameStateController());
28 Objects.requireNonNull(boards); 27 Objects.requireNonNull(boards);
29 if (boards.isEmpty()) { 28 if (boards.isEmpty()) {
30 throw new IllegalArgumentException("The list of boards is empty, not able to create a correct game from this."); 29 throw new IllegalArgumentException("The list of boards is empty, not able to create a correct game from this.");
@@ -84,11 +83,27 @@ public final class Game implements Updateable {
84 */ 83 */
85 @Override 84 @Override
86 public List<Event> update(Context context) { 85 public List<Event> update(Context context) {
86 boolean isConfirmOrder = Events.findFirst(context.getEvents(),ConfirmOrder.class).isPresent();
87 boolean isGameOverEvent = Events.findFirst(context.getEvents(),GameOverEvent.class).isPresent();
88 Game currentGame = context.getGame();
87 LinkedList<Event> events = new LinkedList<>(); 89 LinkedList<Event> events = new LinkedList<>();
88 for (Controller controller : controllers) { 90 if (isGameOverEvent) {
89 events.addAll(controller.update(context)); 91 currentGame.setOver();
92 } else {
93 if (isConfirmOrder) {
94 if (currentGame.getCurrentStage().isCleared()) { // FIXME: use StageClearedEvent
95 if (currentGame.hasNextBoard()) { //continue
96 currentGame.nextStage();
97 } else { //no more board so game over => exiting
98 currentGame.setOver();
99 }
100 } else {//retry
101 currentGame.retryStage();
102 }
103 }
90 } 104 }
91 events.addAll(currentStage.update(context)); 105
106 // FIXME: update underlying stage and merge generated events
92 return events; 107 return events;
93 } 108 }
94 109
diff --git a/src/main/java/fr/umlv/java/wallj/context/Stage.java b/src/main/java/fr/umlv/java/wallj/context/Stage.java
index a124888..860f451 100644
--- a/src/main/java/fr/umlv/java/wallj/context/Stage.java
+++ b/src/main/java/fr/umlv/java/wallj/context/Stage.java
@@ -3,12 +3,11 @@ package fr.umlv.java.wallj.context;
3import fr.umlv.java.wallj.block.Block; 3import fr.umlv.java.wallj.block.Block;
4import fr.umlv.java.wallj.board.Board; 4import fr.umlv.java.wallj.board.Board;
5import fr.umlv.java.wallj.board.BoardConverter; 5import fr.umlv.java.wallj.board.BoardConverter;
6import fr.umlv.java.wallj.controller.Controller; 6import fr.umlv.java.wallj.event.*;
7import fr.umlv.java.wallj.controller.StagePhysicsController;
8import fr.umlv.java.wallj.event.Event;
9import org.jbox2d.common.Vec2; 7import org.jbox2d.common.Vec2;
10import org.jbox2d.dynamics.World; 8import org.jbox2d.dynamics.World;
11 9
10import java.time.Duration;
12import java.util.Collections; 11import java.util.Collections;
13import java.util.List; 12import java.util.List;
14import java.util.Objects; 13import java.util.Objects;
@@ -17,15 +16,17 @@ import java.util.Objects;
17 * @author Pacien TRAN-GIRARD 16 * @author Pacien TRAN-GIRARD
18 */ 17 */
19public class Stage implements Updateable { 18public class Stage implements Updateable {
20 private final List<Controller> controllers = Collections.singletonList(new StagePhysicsController(this)); 19 private static final int VELOCITY_TICK_PER_MS = 6;
21 private final World world = new World(new Vec2()); 20 private static final int POSITION_TICK_PER_MS = 2;
22 21
22 private final World world = new World(new Vec2());
23 private final Board board; 23 private final Board board;
24 private final List<Block> blocks; 24 private final List<Block> blocks;
25 25
26 public Stage(Board board) { 26 public Stage(Board board) {
27 this.board = Objects.requireNonNull(board); 27 this.board = Objects.requireNonNull(board);
28 this.blocks = BoardConverter.boardToWorld(board); 28 this.blocks = BoardConverter.boardToWorld(board);
29 // TODO: link blocks to world
29 } 30 }
30 31
31 public World getWorld() { 32 public World getWorld() {
@@ -39,7 +40,7 @@ public class Stage implements Updateable {
39 return board; 40 return board;
40 } 41 }
41 42
42 public boolean isCleared() { 43 private boolean isCleared() {
43 // TODO 44 // TODO
44 return false; 45 return false;
45 } 46 }
@@ -50,7 +51,31 @@ public class Stage implements Updateable {
50 */ 51 */
51 @Override 52 @Override
52 public List<Event> update(Context context) { 53 public List<Event> update(Context context) {
53 // TODO 54 updatePhysicalWorld(context.getTimeDelta());
54 return null; 55 handleBlockDestruction(context.getEvents());
56 handleBlockCreation(context.getEvents());
57 return generateEvents();
58 }
59
60 private void updatePhysicalWorld(Duration timeDelta) {
61 int dt = (int) timeDelta.toMillis();
62 world.step(dt, dt * VELOCITY_TICK_PER_MS, dt * POSITION_TICK_PER_MS);
63 }
64
65 private void handleBlockDestruction(List<Event> events) {
66 Events.filter(events, BlockDestroyEvent.class).forEach(event -> {
67 }); // TODO
68 }
69
70 private void handleBlockCreation(List<Event> events) {
71 Events.filter(events, BlockCreateEvent.class).forEach(event -> {
72 }); // TODO
73 }
74
75 private List<Event> generateEvents() {
76 if (isCleared())
77 return Collections.singletonList(new StageClearedEvent());
78 else
79 return Collections.emptyList();
55 } 80 }
56} 81}
diff --git a/src/main/java/fr/umlv/java/wallj/controller/Controller.java b/src/main/java/fr/umlv/java/wallj/controller/Controller.java
deleted file mode 100644
index e975bcc..0000000
--- a/src/main/java/fr/umlv/java/wallj/controller/Controller.java
+++ /dev/null
@@ -1,11 +0,0 @@
1package fr.umlv.java.wallj.controller;
2
3import fr.umlv.java.wallj.context.Updateable;
4
5/**
6 * @author Pacien TRAN-GIRARD
7 */
8@FunctionalInterface
9public interface Controller extends Updateable {
10
11}
diff --git a/src/main/java/fr/umlv/java/wallj/controller/GameStateController.java b/src/main/java/fr/umlv/java/wallj/controller/GameStateController.java
deleted file mode 100644
index 7f0464c..0000000
--- a/src/main/java/fr/umlv/java/wallj/controller/GameStateController.java
+++ /dev/null
@@ -1,38 +0,0 @@
1package fr.umlv.java.wallj.controller;
2
3import fr.umlv.java.wallj.context.Context;
4import fr.umlv.java.wallj.context.Game;
5import fr.umlv.java.wallj.event.ConfirmOrder;
6import fr.umlv.java.wallj.event.Event;
7import fr.umlv.java.wallj.event.GameOverEvent;
8
9import java.util.LinkedList;
10import java.util.List;
11
12public class GameStateController implements Controller {
13
14 @Override
15 public List<Event> update(Context context) {
16 boolean isConfirmOrder = Event.findFirst(context.getEvents(),ConfirmOrder.class).isPresent();
17 boolean isGameOverEvent = Event.findFirst(context.getEvents(),GameOverEvent.class).isPresent();
18 Game currentGame = context.getGame();
19 LinkedList<Event> events = new LinkedList<>();
20 if (isGameOverEvent) {
21 currentGame.setOver();
22 } else {
23 if (isConfirmOrder) {
24 if (currentGame.getCurrentStage().isCleared()) {
25 if (currentGame.hasNextBoard()) { //continue
26 currentGame.nextStage();
27 } else { //no more board so game over => exiting