diff options
Diffstat (limited to 'src/main/java/fr')
10 files changed, 166 insertions, 113 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 ac63a87..4f310eb 100644 --- a/src/main/java/fr/umlv/java/wallj/block/BombBlock.java +++ b/src/main/java/fr/umlv/java/wallj/block/BombBlock.java | |||
@@ -3,6 +3,7 @@ package fr.umlv.java.wallj.block; | |||
3 | import fr.umlv.java.wallj.board.TileVec2; | 3 | import fr.umlv.java.wallj.board.TileVec2; |
4 | import fr.umlv.java.wallj.context.Context; | 4 | import fr.umlv.java.wallj.context.Context; |
5 | import fr.umlv.java.wallj.context.GraphicsContext; | 5 | import fr.umlv.java.wallj.context.GraphicsContext; |
6 | import fr.umlv.java.wallj.context.Updateables; | ||
6 | import fr.umlv.java.wallj.event.*; | 7 | import fr.umlv.java.wallj.event.*; |
7 | import fr.umlv.java.wallj.event.Event; | 8 | import fr.umlv.java.wallj.event.Event; |
8 | import org.jbox2d.common.Vec2; | 9 | import org.jbox2d.common.Vec2; |
@@ -10,10 +11,8 @@ import org.jbox2d.dynamics.BodyType; | |||
10 | 11 | ||
11 | import java.awt.*; | 12 | import java.awt.*; |
12 | import java.time.Duration; | 13 | import java.time.Duration; |
13 | import java.util.Arrays; | ||
14 | import java.util.Collections; | ||
15 | import java.util.List; | ||
16 | import java.util.Objects; | 14 | import java.util.Objects; |
15 | import java.util.stream.Stream; | ||
17 | 16 | ||
18 | /** | 17 | /** |
19 | * A bomb block. | 18 | * A bomb block. |
@@ -42,32 +41,35 @@ public class BombBlock extends JBoxBlock { | |||
42 | } | 41 | } |
43 | 42 | ||
44 | @Override | 43 | @Override |
45 | public List<Event> update(Context context) { | 44 | public Stream<Event> update(Context context) { |
46 | handleBombConfiguration(context.getEvents()); | 45 | return Updateables.updateAll(context, |
47 | paint(context.getGraphicsContext()); | 46 | this::handleBombConfiguration, |
48 | return consume(context.getTimeDelta()); | 47 | this::consume, |
48 | this::paint); | ||
49 | } | 49 | } |
50 | 50 | ||
51 | private void handleBombConfiguration(List<Event> events) { | 51 | private Stream<Event> handleBombConfiguration(Context context) { |
52 | Events.findFirst(events, BombTimerIncrEvent.class) | 52 | Events.findFirst(context.getEvents(), BombTimerIncrEvent.class) |
53 | .filter(event -> Objects.equals(event.getPos(), TileVec2.of(getPos()))) | 53 | .filter(event -> Objects.equals(event.getPos(), TileVec2.of(getPos()))) |
54 | .ifPresent(event -> incrementTimer()); | 54 | .ifPresent(event -> incrementTimer()); |
55 | return Stream.empty(); | ||
55 | } | 56 | } |
56 | 57 | ||
57 | private List<Event> consume(Duration timeDelta) { | 58 | private Stream<Event> consume(Context context) { |
58 | decrementTimer(timeDelta); | 59 | decrementTimer(context.getTimeDelta()); |
59 | 60 | return timer.isNegative() ? | |
60 | if (timer.isNegative()) | 61 | Stream.of(new BombExplosionEvent(TileVec2.of(getPos())), new BlockDestroyEvent(this)) : |
61 | return Arrays.asList(new BombExplosionEvent(TileVec2.of(getPos())), new BlockDestroyEvent(this)); | 62 | Stream.empty(); |
62 | else | ||
63 | return Collections.emptyList(); | ||
64 | } | 63 | } |
65 | 64 | ||
66 | private void paint(GraphicsContext graphicsContext) { | 65 | private Stream<Event> paint(Context context) { |
66 | GraphicsContext graphicsContext = context.getGraphicsContext(); | ||
67 | graphicsContext.paintCircle(Color.BLACK, getPos(), TileVec2.TILE_DIM); | 67 | graphicsContext.paintCircle(Color.BLACK, getPos(), TileVec2.TILE_DIM); |
68 | Vec2 textPosition = getPos(); | 68 | Vec2 textPosition = getPos(); |
69 | textPosition.x += TileVec2.TILE_DIM / 4.0f; | 69 | textPosition.x += TileVec2.TILE_DIM / 4.0f; |
70 | textPosition.y += 3 * TileVec2.TILE_DIM / 4.0f; | 70 | textPosition.y += 3 * TileVec2.TILE_DIM / 4.0f; |
71 | graphicsContext.paintString(Color.RED, textPosition, Long.toString(timer.getSeconds())); | 71 | graphicsContext.paintString(Color.RED, textPosition, Long.toString(timer.getSeconds())); |
72 | |||
73 | return Stream.empty(); | ||
72 | } | 74 | } |
73 | } | 75 | } |
diff --git a/src/main/java/fr/umlv/java/wallj/block/GarbageBlock.java b/src/main/java/fr/umlv/java/wallj/block/GarbageBlock.java index 9338888..362e680 100644 --- a/src/main/java/fr/umlv/java/wallj/block/GarbageBlock.java +++ b/src/main/java/fr/umlv/java/wallj/block/GarbageBlock.java | |||
@@ -2,19 +2,17 @@ package fr.umlv.java.wallj.block; | |||
2 | 2 | ||
3 | import fr.umlv.java.wallj.board.TileVec2; | 3 | import fr.umlv.java.wallj.board.TileVec2; |
4 | import fr.umlv.java.wallj.context.Context; | 4 | import fr.umlv.java.wallj.context.Context; |
5 | import fr.umlv.java.wallj.context.GraphicsContext; | 5 | import fr.umlv.java.wallj.context.Updateables; |
6 | import fr.umlv.java.wallj.event.BombExplosionEvent; | 6 | import fr.umlv.java.wallj.event.BombExplosionEvent; |
7 | import fr.umlv.java.wallj.event.Event; | 7 | import fr.umlv.java.wallj.event.Event; |
8 | import fr.umlv.java.wallj.event.Events; | 8 | import fr.umlv.java.wallj.event.Events; |
9 | import org.jbox2d.common.Vec2; | 9 | import org.jbox2d.common.Vec2; |
10 | import org.jbox2d.dynamics.BodyType; | 10 | import org.jbox2d.dynamics.BodyType; |
11 | import org.jbox2d.dynamics.Fixture; | 11 | import org.jbox2d.dynamics.Fixture; |
12 | import org.jbox2d.dynamics.World; | ||
13 | 12 | ||
14 | import java.awt.*; | 13 | import java.awt.*; |
15 | import java.util.Collections; | ||
16 | import java.util.List; | ||
17 | import java.util.Objects; | 14 | import java.util.Objects; |
15 | import java.util.stream.Stream; | ||
18 | 16 | ||
19 | /** | 17 | /** |
20 | * A garbage block. | 18 | * A garbage block. |
@@ -31,24 +29,26 @@ public class GarbageBlock extends JBoxBlock { | |||
31 | } | 29 | } |
32 | 30 | ||
33 | @Override | 31 | @Override |
34 | public List<Event> update(Context context) { | 32 | public Stream<Event> update(Context context) { |
35 | handleExplosionBlasts(context.getEvents(), context.getGame().getCurrentStage().getWorld()); | 33 | return Updateables.updateAll(context, |
36 | paint(context.getGraphicsContext()); | 34 | this::handleExplosionBlasts, |
37 | return Collections.emptyList(); | 35 | this::paint); |
38 | } | 36 | } |
39 | 37 | ||
40 | private void handleExplosionBlasts(List<Event> events, World world) { | 38 | private Stream<Event> handleExplosionBlasts(Context context) { |
41 | Events.filter(events, BombExplosionEvent.class).forEach(explosion -> { | 39 | Events.filter(context.getEvents(), BombExplosionEvent.class).forEach(explosion -> { |
42 | Vec2 source = explosion.getSource().toVec2(); | 40 | Vec2 source = explosion.getSource().toVec2(); |
43 | world.raycast((fixture, point, normal, fraction) -> { | 41 | context.getGame().getCurrentStage().getWorld().raycast((fixture, point, normal, fraction) -> { |
44 | if (isSelf(fixture)) getBody().applyForceToCenter(computeBlastForce(source)); | 42 | if (isSelf(fixture)) getBody().applyForceToCenter(computeBlastForce(source)); |
45 | return STOP_RAYCAST; | 43 | return STOP_RAYCAST; |
46 | }, source, getPos()); | 44 | }, source, getPos()); |
47 | }); | 45 | }); |
46 | return Stream.empty(); | ||
48 | } | 47 | } |
49 | 48 | ||
50 | private void paint(GraphicsContext graphicsContext) { | 49 | private Stream<Event> paint(Context context) { |
51 | graphicsContext.paintCircle(COLOR, getPos(), TileVec2.TILE_DIM); | 50 | context.getGraphicsContext().paintCircle(COLOR, getPos(), TileVec2.TILE_DIM); |
51 | return Stream.empty(); | ||
52 | } | 52 | } |
53 | 53 | ||
54 | private boolean isSelf(Fixture fixture) { | 54 | private boolean isSelf(Fixture fixture) { |
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 f81f423..11cf25f 100644 --- a/src/main/java/fr/umlv/java/wallj/block/RobotBlock.java +++ b/src/main/java/fr/umlv/java/wallj/block/RobotBlock.java | |||
@@ -4,17 +4,16 @@ import fr.umlv.java.wallj.board.Board; | |||
4 | import fr.umlv.java.wallj.board.PathFinder; | 4 | 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; | ||
8 | import fr.umlv.java.wallj.context.Stage; | 7 | import fr.umlv.java.wallj.context.Stage; |
8 | import fr.umlv.java.wallj.context.Updateables; | ||
9 | import fr.umlv.java.wallj.event.*; | 9 | import fr.umlv.java.wallj.event.*; |
10 | import fr.umlv.java.wallj.event.Event; | 10 | import fr.umlv.java.wallj.event.Event; |
11 | import org.jbox2d.common.Vec2; | 11 | import org.jbox2d.common.Vec2; |
12 | import org.jbox2d.dynamics.World; | 12 | import org.jbox2d.dynamics.World; |
13 | 13 | ||
14 | import java.awt.*; | 14 | import java.awt.*; |
15 | import java.time.Duration; | ||
16 | import java.util.*; | 15 | import java.util.*; |
17 | import java.util.List; | 16 | import java.util.stream.Stream; |
18 | 17 | ||
19 | /** | 18 | /** |
20 | * A robot block. | 19 | * A robot block. |
@@ -50,47 +49,55 @@ public class RobotBlock extends Block { | |||
50 | } | 49 | } |
51 | 50 | ||
52 | @Override | 51 | @Override |
53 | public List<Event> update(Context context) { | 52 | public Stream<Event> update(Context context) { |
54 | Events.findFirst(context.getEvents(), MoveRobotOrder.class) | 53 | return Updateables.updateAll(context, |
55 | .ifPresent(event -> updatePath(context.getGame().getCurrentStage().getBoard(), event.getTarget())); | 54 | this::updatePath, |
56 | 55 | this::move, | |