diff options
author | pacien | 2018-02-03 18:13:50 +0100 |
---|---|---|
committer | pacien | 2018-02-03 18:13:50 +0100 |
commit | c14df96cac5da3a029ed388a15124aadb12f4262 (patch) | |
tree | 19e9bbaedc718594145cb6adae0e715a8d0182a7 /src/main/java/fr | |
parent | a5b47073e0042de6580d9c7bb045e579af22d26e (diff) | |
download | wallj-c14df96cac5da3a029ed388a15124aadb12f4262.tar.gz |
Handle bomb drop
Signed-off-by: pacien <pacien.trangirard@pacien.net>
Diffstat (limited to 'src/main/java/fr')
-rw-r--r-- | src/main/java/fr/umlv/java/wallj/block/BombBlock.java | 9 | ||||
-rw-r--r-- | src/main/java/fr/umlv/java/wallj/block/RobotBlock.java | 21 | ||||
-rw-r--r-- | src/main/java/fr/umlv/java/wallj/context/Stage.java | 12 | ||||
-rw-r--r-- | src/main/java/fr/umlv/java/wallj/event/BombTimerIncrEvent.java (renamed from src/main/java/fr/umlv/java/wallj/event/BombSetupEvent.java) | 6 |
4 files changed, 31 insertions, 17 deletions
diff --git a/src/main/java/fr/umlv/java/wallj/block/BombBlock.java b/src/main/java/fr/umlv/java/wallj/block/BombBlock.java index baf10b3..ac63a87 100644 --- a/src/main/java/fr/umlv/java/wallj/block/BombBlock.java +++ b/src/main/java/fr/umlv/java/wallj/block/BombBlock.java | |||
@@ -13,6 +13,7 @@ import java.time.Duration; | |||
13 | import java.util.Arrays; | 13 | import java.util.Arrays; |
14 | import java.util.Collections; | 14 | import java.util.Collections; |
15 | import java.util.List; | 15 | import java.util.List; |
16 | import java.util.Objects; | ||
16 | 17 | ||
17 | /** | 18 | /** |
18 | * A bomb block. | 19 | * A bomb block. |
@@ -42,13 +43,15 @@ public class BombBlock extends JBoxBlock { | |||
42 | 43 | ||
43 | @Override | 44 | @Override |
44 | public List<Event> update(Context context) { | 45 | public List<Event> update(Context context) { |
45 | if (containsUpdateEvent(context.getEvents())) incrementTimer(); | 46 | handleBombConfiguration(context.getEvents()); |
46 | paint(context.getGraphicsContext()); | 47 | paint(context.getGraphicsContext()); |
47 | return consume(context.getTimeDelta()); | 48 | return consume(context.getTimeDelta()); |
48 | } | 49 | } |
49 | 50 | ||
50 | private boolean containsUpdateEvent(List<Event> events) { | 51 | private void handleBombConfiguration(List<Event> events) { |
51 | return Events.findFirst(events, BombSetupEvent.class).isPresent(); | 52 | Events.findFirst(events, BombTimerIncrEvent.class) |
53 | .filter(event -> Objects.equals(event.getPos(), TileVec2.of(getPos()))) | ||
54 | .ifPresent(event -> incrementTimer()); | ||
52 | } | 55 | } |
53 | 56 | ||
54 | private List<Event> consume(Duration timeDelta) { | 57 | private List<Event> consume(Duration timeDelta) { |
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 45ed5bd..0969009 100644 --- a/src/main/java/fr/umlv/java/wallj/block/RobotBlock.java +++ b/src/main/java/fr/umlv/java/wallj/block/RobotBlock.java | |||
@@ -5,6 +5,7 @@ import fr.umlv.java.wallj.board.PathFinder; | |||
5 | import fr.umlv.java.wallj.board.TileVec2; | 5 | import fr.umlv.java.wallj.board.TileVec2; |
6 | import fr.umlv.java.wallj.context.Context; | 6 | import fr.umlv.java.wallj.context.Context; |
7 | import fr.umlv.java.wallj.context.GraphicsContext; | 7 | import fr.umlv.java.wallj.context.GraphicsContext; |
8 | import fr.umlv.java.wallj.context.Stage; | ||
8 | import fr.umlv.java.wallj.event.*; | 9 | import fr.umlv.java.wallj.event.*; |
9 | import fr.umlv.java.wallj.event.Event; | 10 | import fr.umlv.java.wallj.event.Event; |
10 | import org.jbox2d.common.Vec2; | 11 | import org.jbox2d.common.Vec2; |
@@ -12,9 +13,7 @@ import org.jbox2d.dynamics.World; | |||
12 | 13 | ||
13 | import java.awt.*; | 14 | import java.awt.*; |
14 | import java.time.Duration; | 15 | import java.time.Duration; |
15 | import java.util.Collections; | 16 | import java.util.*; |
16 | import java.util.Deque; | ||
17 | import java.util.LinkedList; | ||
18 | import java.util.List; | 17 | import java.util.List; |
19 | 18 | ||
20 | /** | 19 | /** |
@@ -23,7 +22,7 @@ import java.util.List; | |||
23 | * @author Pacien TRAN-GIRARD | 22 | * @author Pacien TRAN-GIRARD |
24 | */ | 23 | */ |
25 | public class RobotBlock extends Block { | 24 | public class RobotBlock extends Block { |
26 | private static final float SPEED = 0.2f; // px/ms | 25 | private static final float SPEED = 4f; // px/ms |
27 | 26 | ||
28 | private Vec2 pos; | 27 | private Vec2 pos; |
29 | private PathFinder pathFinder; | 28 | private PathFinder pathFinder; |
@@ -56,12 +55,14 @@ public class RobotBlock extends Block { | |||
56 | 55 | ||
57 | move(context.getTimeDelta()); | 56 | move(context.getTimeDelta()); |
58 | paint(context.getGraphicsContext()); | 57 | paint(context.getGraphicsContext()); |
59 | return setupBomb(context.getEvents()); | 58 | return setupBomb(context.getEvents(), context.getGame().getCurrentStage()); |
60 | } | 59 | } |
61 | 60 | ||
62 | private List<Event> setupBomb(List<Event> events) { | 61 | private List<Event> setupBomb(List<Event> events, Stage stage) { |
63 | return Events.findFirst(events, BombSetupOrder.class) | 62 | return Events.findFirst(events, BombSetupOrder.class) |
64 | .map(event -> Collections.<Event>singletonList(new BombSetupEvent(TileVec2.of(pos)))) | 63 | .map(event -> isOnBomb(stage) ? |
64 | Collections.<Event>singletonList(new BombTimerIncrEvent(getTile())) : | ||
65 | Collections.<Event>singletonList(new BlockCreateEvent(BlockType.BOMB, getTile()))) | ||
65 | .orElse(Collections.emptyList()); | 66 | .orElse(Collections.emptyList()); |
66 | } | 67 | } |
67 | 68 | ||
@@ -83,4 +84,10 @@ public class RobotBlock extends Block { | |||
83 | private void paint(GraphicsContext graphicsContext) { | 84 | private void paint(GraphicsContext graphicsContext) { |
84 | graphicsContext.paintCircle(Color.BLUE, getPos(), TileVec2.TILE_DIM / 2); | 85 | graphicsContext.paintCircle(Color.BLUE, getPos(), TileVec2.TILE_DIM / 2); |
85 | } | 86 | } |
87 | |||
88 | private boolean isOnBomb(Stage stage) { | ||
89 | return stage.getBlocks().stream() | ||
90 | .anyMatch(block -> Objects.equals(block.getType(), BlockType.BOMB) && | ||
91 | Objects.equals(block.getPos(), getTile().toVec2())); | ||
92 | } | ||
86 | } | 93 | } |
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 8ef4558..49aa196 100644 --- a/src/main/java/fr/umlv/java/wallj/context/Stage.java +++ b/src/main/java/fr/umlv/java/wallj/context/Stage.java | |||
@@ -11,10 +11,7 @@ import org.jbox2d.common.Vec2; | |||
11 | import org.jbox2d.dynamics.World; | 11 | import org.jbox2d.dynamics.World; |
12 | 12 | ||
13 | import java.time.Duration; | 13 | import java.time.Duration; |
14 | import java.util.LinkedList; | 14 | import java.util.*; |
15 | import java.util.List; | ||
16 | import java.util.Map; | ||
17 | import java.util.Objects; | ||
18 | 15 | ||
19 | /** | 16 | /** |
20 | * @author Pacien TRAN-GIRARD | 17 | * @author Pacien TRAN-GIRARD |
@@ -52,6 +49,13 @@ public class Stage implements Updateable { | |||
52 | } | 49 | } |
53 | 50 | ||
54 | /** | 51 | /** |
52 | * @return the list of blocks | ||
53 | */ | ||
54 | public List<Block> getBlocks() { | ||
55 | return Collections.unmodifiableList(blocks); | ||
56 | } | ||
57 | |||
58 | /** | ||
55 | * @return T(this stage is cleared, i.e. does not contain any garbage block) | 59 | * @return T(this stage is cleared, i.e. does not contain any garbage block) |
56 | * @implNote TODO: profile this and consider a garbage block counter | 60 | * @implNote TODO: profile this and consider a garbage block counter |
57 | */ | 61 | */ |
diff --git a/src/main/java/fr/umlv/java/wallj/event/BombSetupEvent.java b/src/main/java/fr/umlv/java/wallj/event/BombTimerIncrEvent.java index 620eea2..cbfe6e5 100644 --- a/src/main/java/fr/umlv/java/wallj/event/BombSetupEvent.java +++ b/src/main/java/fr/umlv/java/wallj/event/BombTimerIncrEvent.java | |||
@@ -5,17 +5,17 @@ import fr.umlv.java.wallj.board.TileVec2; | |||
5 | import java.util.Objects; | 5 | import java.util.Objects; |
6 | 6 | ||
7 | /** | 7 | /** |
8 | * Bomb setup event signalling the creation or the update of a bomb at a given position. | 8 | * Bomb setup event signalling the update of a bomb at a given position. |
9 | * | 9 | * |
10 | * @author Pacien TRAN-GIRARD | 10 | * @author Pacien TRAN-GIRARD |
11 | */ | 11 | */ |
12 | public final class BombSetupEvent implements GameEvent { | 12 | public final class BombTimerIncrEvent implements GameEvent { |
13 | private final TileVec2 pos; | 13 | private final TileVec2 pos; |
14 | 14 | ||
15 | /** | 15 | /** |
16 | * @param pos requested setup position | 16 | * @param pos requested setup position |
17 | */ | 17 | */ |
18 | public BombSetupEvent(TileVec2 pos) { | 18 | public BombTimerIncrEvent(TileVec2 pos) { |
19 | this.pos = Objects.requireNonNull(pos); | 19 | this.pos = Objects.requireNonNull(pos); |
20 | } | 20 | } |
21 | 21 | ||