aboutsummaryrefslogtreecommitdiff
path: root/src/main/java
diff options
context:
space:
mode:
authorpacien2018-02-02 22:39:27 +0100
committerpacien2018-02-02 22:39:27 +0100
commita627b2f4591147dadd82f802bc8391333034b4b8 (patch)
treeb7546cc191e5779379784d657f3f55911124940f /src/main/java
parentd799681be0e96652fa6781f40b763c4d2d708984 (diff)
downloadwallj-a627b2f4591147dadd82f802bc8391333034b4b8.tar.gz
Implement block deletion/creation ; Remove StageClearedEvent
Signed-off-by: pacien <pacien.trangirard@pacien.net>
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/fr/umlv/java/wallj/block/Block.java5
-rw-r--r--src/main/java/fr/umlv/java/wallj/block/JBoxBlock.java6
-rw-r--r--src/main/java/fr/umlv/java/wallj/block/RobotBlock.java5
-rw-r--r--src/main/java/fr/umlv/java/wallj/context/Stage.java43
-rw-r--r--src/main/java/fr/umlv/java/wallj/context/Updateable.java3
-rw-r--r--src/main/java/fr/umlv/java/wallj/event/StageClearedEvent.java10
6 files changed, 43 insertions, 29 deletions
diff --git a/src/main/java/fr/umlv/java/wallj/block/Block.java b/src/main/java/fr/umlv/java/wallj/block/Block.java
index 7ae9c06..003c782 100644
--- a/src/main/java/fr/umlv/java/wallj/block/Block.java
+++ b/src/main/java/fr/umlv/java/wallj/block/Block.java
@@ -45,4 +45,9 @@ public abstract class Block implements Updateable {
45 * @param world a JBox2D world 45 * @param world a JBox2D world
46 */ 46 */
47 public abstract void link(World world); 47 public abstract void link(World world);
48
49 /**
50 * @param world a JBox2D world
51 */
52 public abstract void unlink(World world);
48} 53}
diff --git a/src/main/java/fr/umlv/java/wallj/block/JBoxBlock.java b/src/main/java/fr/umlv/java/wallj/block/JBoxBlock.java
index 06ecca3..f0f2834 100644
--- a/src/main/java/fr/umlv/java/wallj/block/JBoxBlock.java
+++ b/src/main/java/fr/umlv/java/wallj/block/JBoxBlock.java
@@ -33,4 +33,10 @@ public abstract class JBoxBlock extends Block {
33 body.createFixture(fixtureDef); 33 body.createFixture(fixtureDef);
34 body.setUserData(this); 34 body.setUserData(this);
35 } 35 }
36
37 @Override
38 public void unlink(World world) {
39 if (body == null) throw new IllegalStateException("Block has not yet been linked.");
40 world.destroyBody(body);
41 }
36} 42}
diff --git a/src/main/java/fr/umlv/java/wallj/block/RobotBlock.java b/src/main/java/fr/umlv/java/wallj/block/RobotBlock.java
index 7d370e5..45ed5bd 100644
--- a/src/main/java/fr/umlv/java/wallj/block/RobotBlock.java
+++ b/src/main/java/fr/umlv/java/wallj/block/RobotBlock.java
@@ -45,6 +45,11 @@ public class RobotBlock extends Block {
45 } 45 }
46 46
47 @Override 47 @Override
48 public void unlink(World world) {
49 // no-op
50 }
51
52 @Override
48 public List<Event> update(Context context) { 53 public List<Event> update(Context context) {
49 Events.findFirst(context.getEvents(), MoveRobotOrder.class) 54 Events.findFirst(context.getEvents(), MoveRobotOrder.class)
50 .ifPresent(event -> updatePath(context.getGame().getCurrentStage().getBoard(), event.getTarget())); 55 .ifPresent(event -> updatePath(context.getGame().getCurrentStage().getBoard(), event.getTarget()));
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 860f451..8852568 100644
--- a/src/main/java/fr/umlv/java/wallj/context/Stage.java
+++ b/src/main/java/fr/umlv/java/wallj/context/Stage.java
@@ -1,6 +1,8 @@
1package fr.umlv.java.wallj.context; 1package fr.umlv.java.wallj.context;
2 2
3import fr.umlv.java.wallj.block.Block; 3import fr.umlv.java.wallj.block.Block;
4import fr.umlv.java.wallj.block.BlockFactory;
5import fr.umlv.java.wallj.block.BlockType;
4import fr.umlv.java.wallj.board.Board; 6import fr.umlv.java.wallj.board.Board;
5import fr.umlv.java.wallj.board.BoardConverter; 7import fr.umlv.java.wallj.board.BoardConverter;
6import fr.umlv.java.wallj.event.*; 8import fr.umlv.java.wallj.event.*;
@@ -8,7 +10,6 @@ import org.jbox2d.common.Vec2;
8import org.jbox2d.dynamics.World; 10import org.jbox2d.dynamics.World;
9 11
10import java.time.Duration; 12import java.time.Duration;
11import java.util.Collections;
12import java.util.List; 13import java.util.List;
13import java.util.Objects; 14import java.util.Objects;
14 15
@@ -23,38 +24,47 @@ public class Stage implements Updateable {
23 private final Board board; 24 private final Board board;
24 private final List<Block> blocks; 25 private final List<Block> blocks;
25 26
27 /**
28 * @param board the base board
29 */
26 public Stage(Board board) { 30 public Stage(Board board) {
27 this.board = Objects.requireNonNull(board); 31 this.board = Objects.requireNonNull(board);
28 this.blocks = BoardConverter.boardToWorld(board); 32 blocks = BoardConverter.boardToWorld(board);
29 // TODO: link blocks to world 33 blocks.forEach(block -> block.link(world));
30 } 34 }
31 35
36 /**
37 * @return the JBox2D world
38 */
32 public World getWorld() { 39 public World getWorld() {
33 return world; 40 return world;
34 } 41 }
35 42
36 /** 43 /**
37 * @return the current board of the game 44 * @return the base board
38 */ 45 */
39 public Board getBoard() { 46 public Board getBoard() {
40 return board; 47 return board;
41 } 48 }
42 49
43 private boolean isCleared() { 50 /**
44 // TODO 51 * @return T(this stage is cleared, i.e. does not contain any garbage block)
45 return false; 52 * @implNote TODO: profile this and consider a garbage block counter
53 */
54 public boolean isCleared() {
55 return blocks.stream().noneMatch(block -> block.getType() == BlockType.GARBAGE);
46 } 56 }
47 57
48 /** 58 /**
49 * @param context the current context 59 * @param context the current context
50 * @return a list of new events to perform 60 * @return the list of newly generated events
51 */ 61 */
52 @Override 62 @Override
53 public List<Event> update(Context context) { 63 public List<Event> update(Context context) {
54 updatePhysicalWorld(context.getTimeDelta()); 64 updatePhysicalWorld(context.getTimeDelta());
55 handleBlockDestruction(context.getEvents()); 65 handleBlockDestruction(context.getEvents());
56 handleBlockCreation(context.getEvents()); 66 handleBlockCreation(context.getEvents());
57 return generateEvents(); 67 return Updateable.updateAll(blocks, context);
58 } 68 }
59 69
60 private void updatePhysicalWorld(Duration timeDelta) { 70 private void updatePhysicalWorld(Duration timeDelta) {
@@ -64,18 +74,15 @@ public class Stage implements Updateable {
64 74
65 private void handleBlockDestruction(List<Event> events) { 75 private void handleBlockDestruction(List<Event> events) {
66 Events.filter(events, BlockDestroyEvent.class).forEach(event -> { 76 Events.filter(events, BlockDestroyEvent.class).forEach(event -> {
67 }); // TODO 77 if (blocks.remove(event.getBlock())) event.getBlock().unlink(world);
78 });
68 } 79 }
69 80
70 private void handleBlockCreation(List<Event> events) { 81 private void handleBlockCreation(List<Event> events) {
71 Events.filter(events, BlockCreateEvent.class).forEach(event -> { 82 Events.filter(events, BlockCreateEvent.class).forEach(event -> {
72 }); // TODO 83 Block block = BlockFactory.build(event.getBlockType(), event.getPos());
73 } 84 blocks.add(block);
74 85 block.link(world);
75 private List<Event> generateEvents() { 86 });
76 if (isCleared())
77 return Collections.singletonList(new StageClearedEvent());
78 else
79 return Collections.emptyList();
80 } 87 }
81} 88}
diff --git a/src/main/java/fr/umlv/java/wallj/context/Updateable.java b/src/main/java/fr/umlv/java/wallj/context/Updateable.java
index 468fe2c..45c217e 100644
--- a/src/main/java/fr/umlv/java/wallj/context/Updateable.java
+++ b/src/main/java/fr/umlv/java/wallj/context/Updateable.java
@@ -18,9 +18,10 @@ public interface Updateable {
18 /** 18 /**
19 * @param updateables list of updateables 19 * @param updateables list of updateables
20 * @param context an update context 20 * @param context an update context
21 * @param <T> the updateable type
21 * @return a list of collected generated events 22 * @return a list of collected generated events
22 */ 23 */
23 static List<Event> updateAll(List<Updateable> updateables, Context context) { 24 static <T extends Updateable> List<Event> updateAll(List<T> updateables, Context context) {
24 return updateables.stream() 25 return updateables.stream()
25 .flatMap(u -> u.update(context).stream()) 26 .flatMap(u -> u.update(context).stream())
26 .collect(Collectors.toList()); 27 .collect(Collectors.toList());
diff --git a/src/main/java/fr/umlv/java/wallj/event/StageClearedEvent.java b/src/main/java/fr/umlv/java/wallj/event/StageClearedEvent.java
deleted file mode 100644
index 2543ad9..0000000
--- a/src/main/java/fr/umlv/java/wallj/event/StageClearedEvent.java
+++ /dev/null
@@ -1,10 +0,0 @@
1package fr.umlv.java.wallj.event;
2
3/**
4 * Signals that a stage is cleared.
5 *
6 * @author Pacien TRAN-GIRARD
7 */
8public final class StageClearedEvent implements GameEvent {
9 // void
10}