aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/fr/umlv/java/wallj/context/Stage.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/fr/umlv/java/wallj/context/Stage.java')
-rw-r--r--src/main/java/fr/umlv/java/wallj/context/Stage.java45
1 files changed, 30 insertions, 15 deletions
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 44fad7b..9b66e03 100644
--- a/src/main/java/fr/umlv/java/wallj/context/Stage.java
+++ b/src/main/java/fr/umlv/java/wallj/context/Stage.java
@@ -6,10 +6,7 @@ import fr.umlv.java.wallj.block.BlockType;
6import fr.umlv.java.wallj.board.Board; 6import fr.umlv.java.wallj.board.Board;
7import fr.umlv.java.wallj.board.BoardConverter; 7import fr.umlv.java.wallj.board.BoardConverter;
8import fr.umlv.java.wallj.board.TileVec2; 8import fr.umlv.java.wallj.board.TileVec2;
9import fr.umlv.java.wallj.event.BlockCreateEvent; 9import fr.umlv.java.wallj.event.*;
10import fr.umlv.java.wallj.event.BlockDestroyEvent;
11import fr.umlv.java.wallj.event.Event;
12import fr.umlv.java.wallj.event.Events;
13import org.jbox2d.common.Vec2; 10import org.jbox2d.common.Vec2;
14import org.jbox2d.dynamics.World; 11import org.jbox2d.dynamics.World;
15 12
@@ -27,6 +24,7 @@ public class Stage implements Updateable {
27 private final World world = new World(new Vec2()); 24 private final World world = new World(new Vec2());
28 private final List<Block> blocks = new LinkedList<>(); 25 private final List<Block> blocks = new LinkedList<>();
29 private final Board board; 26 private final Board board;
27 private boolean running = false;
30 28
31 /** 29 /**
32 * @param board the base board 30 * @param board the base board
@@ -69,30 +67,38 @@ public class Stage implements Updateable {
69 } 67 }
70 68
71 /** 69 /**
72 * @return T(the physics simulation can start, i.e. the player has placed all their bombs)
73 */
74 public boolean isReady() {
75 return blocks.stream()
76 .filter(block -> block.getType() == BlockType.BOMB)
77 .count() == BOMB_PLACEMENTS;
78 }
79
80 /**
81 * @param context the current context 70 * @param context the current context
82 * @return the stream of newly generated events 71 * @return the stream of newly generated events
83 */ 72 */
84 @Override 73 @Override
85 public Stream<Event> update(Context context) { 74 public Stream<Event> update(Context context) {
86 return Updateables.updateAll(context, 75 return Updateables.updateAll(context,
76 this::handleSimulationStartOrder,
77 this::handleSimulationStartEvent,
87 this::updatePhysicalWorld, 78 this::updatePhysicalWorld,
88 this::handleBlockDestruction, 79 this::handleBlockDestruction,
89 this::handleBlockCreation, 80 this::handleBlockCreation,
90 ctx -> Updateables.updateAll(ctx, blocks)); 81 ctx -> Updateables.updateAll(ctx, blocks));
91 } 82 }
92 83
84 private Stream<Event> handleSimulationStartOrder(Context context) {
85 return Events.findFirst(context.getEvents(), SimulationStartOrder.class)
86 .flatMap(order -> isReady() ? Optional.<Event>of(new SimulationStartEvent()) : Optional.empty())
87 .map(Stream::of) // Optional.stream() only available in Java 9
88 .orElseGet(Stream::empty);
89 }
90
91 private Stream<Event> handleSimulationStartEvent(Context context) {
92 Events.findFirst(context.getEvents(), SimulationStartEvent.class)
93 .ifPresent(startEvent -> running = true);
94 return Stream.empty();
95 }
96
93 private Stream<Event> updatePhysicalWorld(Context context) { 97 private Stream<Event> updatePhysicalWorld(Context context) {
94 int dt = (int) context.getTimeDelta().toMillis(); 98 if (running) {
95 world.step(dt, dt * VELOCITY_TICK_PER_MS, dt * POSITION_TICK_PER_MS); 99 int dt = (int) context.getTimeDelta().toMillis();
100 world.step(dt, dt * VELOCITY_TICK_PER_MS, dt * POSITION_TICK_PER_MS);
101 }
96 return Stream.empty(); 102 return Stream.empty();
97 } 103 }
98 104
@@ -112,6 +118,15 @@ public class Stage implements Updateable {
112 return Stream.empty(); 118 return Stream.empty();
113 } 119 }
114 120
121 /**
122 * @implNote TODO: profile this and consider a bomb block counter
123 */
124 private boolean isReady() {
125 return blocks.stream()
126 .filter(block -> block.getType() == BlockType.BOMB)
127 .count() == BOMB_PLACEMENTS;
128 }
129
115 private static TileVec2 findAnyFreeTile(Board board) { 130 private static TileVec2 findAnyFreeTile(Board board) {
116 return board.stream() 131 return board.stream()
117 .filter(entry -> entry.getValue() == BlockType.FREE) 132 .filter(entry -> entry.getValue() == BlockType.FREE)