diff options
author | pacien | 2018-02-02 01:58:15 +0100 |
---|---|---|
committer | pacien | 2018-02-02 01:58:30 +0100 |
commit | 79f02268fc5c303b1e44b38a88b3e7815e764dd5 (patch) | |
tree | 6a83042892a4ee479079b9f29189637f6a34db35 | |
parent | 79e23774da04497cb83f0cf233e1e9559769d37d (diff) | |
download | wallj-79f02268fc5c303b1e44b38a88b3e7815e764dd5.tar.gz |
Create new events and move filter functions
Signed-off-by: pacien <pacien.trangirard@pacien.net>
10 files changed, 128 insertions, 16 deletions
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 f5e2584..e41c57f 100644 --- a/src/main/java/fr/umlv/java/wallj/block/RobotBlock.java +++ b/src/main/java/fr/umlv/java/wallj/block/RobotBlock.java | |||
@@ -5,10 +5,8 @@ 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.event.BombSetupEvent; | 8 | import fr.umlv.java.wallj.event.*; |
9 | import fr.umlv.java.wallj.event.BombSetupOrder; | ||
10 | import fr.umlv.java.wallj.event.Event; | 9 | import fr.umlv.java.wallj.event.Event; |
11 | import fr.umlv.java.wallj.event.MoveRobotOrder; | ||
12 | import org.jbox2d.common.Vec2; | 10 | import org.jbox2d.common.Vec2; |
13 | import org.jbox2d.dynamics.World; | 11 | import org.jbox2d.dynamics.World; |
14 | 12 | ||
@@ -45,7 +43,7 @@ public class RobotBlock extends Block { | |||
45 | 43 | ||
46 | @Override | 44 | @Override |
47 | public List<Event> update(Context context) { | 45 | public List<Event> update(Context context) { |
48 | Event.findFirst(context.getEvents(), MoveRobotOrder.class) | 46 | Events.findFirst(context.getEvents(), MoveRobotOrder.class) |
49 | .ifPresent(event -> updatePath(context.getGame().getCurrentStage().getBoard(), event.getTarget())); | 47 | .ifPresent(event -> updatePath(context.getGame().getCurrentStage().getBoard(), event.getTarget())); |
50 | 48 | ||
51 | if (!path.isEmpty()) move(context.getTimeDelta()); | 49 | if (!path.isEmpty()) move(context.getTimeDelta()); |
@@ -54,7 +52,7 @@ public class RobotBlock extends Block { | |||
54 | } | 52 | } |
55 | 53 | ||
56 | private List<Event> setupBomb(List<Event> events) { | 54 | private List<Event> setupBomb(List<Event> events) { |
57 | return Event.findFirst(events, BombSetupOrder.class) | 55 | return Events.findFirst(events, BombSetupOrder.class) |
58 | .map(event -> Collections.<Event>singletonList(new BombSetupEvent(TileVec2.of(pos)))) | 56 | .map(event -> Collections.<Event>singletonList(new BombSetupEvent(TileVec2.of(pos)))) |
59 | .orElse(Collections.emptyList()); | 57 | .orElse(Collections.emptyList()); |
60 | } | 58 | } |
diff --git a/src/main/java/fr/umlv/java/wallj/event/BlockCreateEvent.java b/src/main/java/fr/umlv/java/wallj/event/BlockCreateEvent.java new file mode 100644 index 0000000..1df177a --- /dev/null +++ b/src/main/java/fr/umlv/java/wallj/event/BlockCreateEvent.java | |||
@@ -0,0 +1,39 @@ | |||
1 | package fr.umlv.java.wallj.event; | ||
2 | |||
3 | import fr.umlv.java.wallj.block.BlockType; | ||
4 | import fr.umlv.java.wallj.board.TileVec2; | ||
5 | |||
6 | import java.util.Objects; | ||
7 | |||
8 | /** | ||
9 | * Block creation request. | ||
10 | * | ||
11 | * @author Pacien TRAN-GIRARD | ||
12 | */ | ||
13 | public class BlockCreateEvent implements GameEvent { | ||
14 | private final BlockType blockType; | ||
15 | private final TileVec2 pos; | ||
16 | |||
17 | /** | ||
18 | * @param blockType the type of the block to create | ||
19 | * @param pos the position of the block to create | ||
20 | */ | ||
21 | public BlockCreateEvent(BlockType blockType, TileVec2 pos) { | ||
22 | this.blockType = Objects.requireNonNull(blockType); | ||
23 | this.pos = Objects.requireNonNull(pos); | ||
24 | } | ||
25 | |||
26 | /** | ||
27 | * @return the type of the block to create | ||
28 | */ | ||
29 | public BlockType getBlockType() { | ||
30 | return blockType; | ||
31 | } | ||
32 | |||
33 | /** | ||
34 | * @return the position of the block to create | ||
35 | */ | ||
36 | public TileVec2 getPos() { | ||
37 | return pos; | ||
38 | } | ||
39 | } | ||
diff --git a/src/main/java/fr/umlv/java/wallj/event/BlockDestroyEvent.java b/src/main/java/fr/umlv/java/wallj/event/BlockDestroyEvent.java new file mode 100644 index 0000000..d954fa0 --- /dev/null +++ b/src/main/java/fr/umlv/java/wallj/event/BlockDestroyEvent.java | |||
@@ -0,0 +1,28 @@ | |||
1 | package fr.umlv.java.wallj.event; | ||
2 | |||
3 | import fr.umlv.java.wallj.block.Block; | ||
4 | |||
5 | import java.util.Objects; | ||
6 | |||
7 | /** | ||
8 | * Block destroy request. | ||
9 | * | ||
10 | * @author Pacien TRAN-GIRARD | ||
11 | */ | ||
12 | public class BlockDestroyEvent implements GameEvent { | ||
13 | private final Block block; | ||
14 | |||
15 | /** | ||
16 | * @param block the block to destroy | ||
17 | */ | ||
18 | public BlockDestroyEvent(Block block) { | ||
19 | this.block = Objects.requireNonNull(block); | ||
20 | } | ||
21 | |||
22 | /** | ||
23 | * @return the block to destroy | ||
24 | */ | ||
25 | public Block getBlock() { | ||
26 | return block; | ||
27 | } | ||
28 | } | ||
diff --git a/src/main/java/fr/umlv/java/wallj/event/BombSetupOrder.java b/src/main/java/fr/umlv/java/wallj/event/BombSetupOrder.java index 99c0942..25f0048 100644 --- a/src/main/java/fr/umlv/java/wallj/event/BombSetupOrder.java +++ b/src/main/java/fr/umlv/java/wallj/event/BombSetupOrder.java | |||
@@ -6,5 +6,5 @@ package fr.umlv.java.wallj.event; | |||
6 | * @author Adam NAILI | 6 | * @author Adam NAILI |
7 | */ | 7 | */ |
8 | public final class BombSetupOrder implements InputEvent { | 8 | public final class BombSetupOrder implements InputEvent { |
9 | 9 | // void | |
10 | } | 10 | } |
diff --git a/src/main/java/fr/umlv/java/wallj/event/ConfirmOrder.java b/src/main/java/fr/umlv/java/wallj/event/ConfirmOrder.java index a3e3525..eb384cb 100644 --- a/src/main/java/fr/umlv/java/wallj/event/ConfirmOrder.java +++ b/src/main/java/fr/umlv/java/wallj/event/ConfirmOrder.java | |||
@@ -6,4 +6,5 @@ package fr.umlv.java.wallj.event; | |||
6 | * @author Adam NAILI | 6 | * @author Adam NAILI |
7 | */ | 7 | */ |
8 | public class ConfirmOrder implements InputEvent { | 8 | public class ConfirmOrder implements InputEvent { |
9 | // void | ||
9 | } | 10 | } |
diff --git a/src/main/java/fr/umlv/java/wallj/event/Event.java b/src/main/java/fr/umlv/java/wallj/event/Event.java index 5b8b500..e373002 100644 --- a/src/main/java/fr/umlv/java/wallj/event/Event.java +++ b/src/main/java/fr/umlv/java/wallj/event/Event.java | |||
@@ -1,18 +1,10 @@ | |||
1 | package fr.umlv.java.wallj.event; | 1 | package fr.umlv.java.wallj.event; |
2 | 2 | ||
3 | import java.util.List; | ||
4 | import java.util.Optional; | ||
5 | |||
6 | /** | 3 | /** |
7 | * An application event | 4 | * An application event |
8 | * | 5 | * |
9 | * @author Pacien TRAN-GIRARD | 6 | * @author Pacien TRAN-GIRARD |
10 | */ | 7 | */ |
11 | public interface Event { | 8 | public interface Event { |
12 | static <T extends Event> Optional<T> findFirst(List<Event> eventList, Class<T> eventClass) { | 9 | // void |
13 | return eventList.stream() | ||
14 | .filter(e -> e.getClass().isInstance(eventClass)) | ||
15 | .findFirst() | ||
16 | .map(eventClass::cast); | ||
17 | } | ||
18 | } | 10 | } |
diff --git a/src/main/java/fr/umlv/java/wallj/event/Events.java b/src/main/java/fr/umlv/java/wallj/event/Events.java new file mode 100644 index 0000000..da8b199 --- /dev/null +++ b/src/main/java/fr/umlv/java/wallj/event/Events.java | |||
@@ -0,0 +1,38 @@ | |||
1 | package fr.umlv.java.wallj.event; | ||
2 | |||
3 | import java.util.List; | ||
4 | import java.util.Optional; | ||
5 | import java.util.stream.Stream; | ||
6 | |||
7 | /** | ||
8 | * Events filtering utilities. | ||
9 | * | ||
10 | * @author Pacien TRAN-GIRARD | ||
11 | */ | ||
12 | public final class Events { | ||
13 | /** | ||
14 | * @param eventList list of events to filter | ||
15 | * @param eventClass event class to keep | ||
16 | * @param <T> type of events to keep | ||
17 | * @return a filtered stream of events | ||
18 | */ | ||
19 | public static <T extends Event> Stream<T> filter(List<Event> eventList, Class<T> eventClass) { | ||
20 | return eventList.stream() | ||
21 | .filter(e -> e.getClass().isInstance(eventClass)) | ||
22 | .map(eventClass::cast); | ||
23 | } | ||
24 | |||
25 | /** | ||
26 | * @param eventList list of events to filter | ||
27 | * @param eventClass event class to keep | ||
28 | * @param <T> type of events to keep | ||
29 | * @return any matching event | ||
30 | */ | ||
31 | public static <T extends Event> Optional<T> findFirst(List<Event> eventList, Class<T> eventClass) { | ||
32 | return filter(eventList, eventClass).findFirst(); | ||
33 | } | ||
34 | |||
35 | private Events() { | ||
36 | // static class | ||
37 | } | ||
38 | } | ||
diff --git a/src/main/java/fr/umlv/java/wallj/event/GameOverEvent.java b/src/main/java/fr/umlv/java/wallj/event/GameOverEvent.java index 93ec049..4365dc4 100644 --- a/src/main/java/fr/umlv/java/wallj/event/GameOverEvent.java +++ b/src/main/java/fr/umlv/java/wallj/event/GameOverEvent.java | |||
@@ -1,4 +1,10 @@ | |||
1 | package fr.umlv.java.wallj.event; | 1 | package fr.umlv.java.wallj.event; |
2 | 2 | ||
3 | /** | ||
4 | * Signals that the game is over. | ||
5 | * | ||
6 | * @author Pacien TRAN-GIRARD | ||
7 | */ | ||