diff options
author | pacien | 2018-02-04 19:01:05 +0100 |
---|---|---|
committer | pacien | 2018-02-04 19:01:05 +0100 |
commit | cde2d368871646bb08934fd6b7f459c1b91d9016 (patch) | |
tree | 6f1671b0e7439eafde1ac387d160395db0bbb50e | |
parent | 8c81e97b69b582a17ba80bac694004591a4d1939 (diff) | |
download | wallj-cde2d368871646bb08934fd6b7f459c1b91d9016.tar.gz |
Implement garbage destruction on trash meeting
Signed-off-by: pacien <pacien.trangirard@pacien.net>
-rw-r--r-- | src/main/java/fr/umlv/java/wallj/block/TrashBlock.java | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/src/main/java/fr/umlv/java/wallj/block/TrashBlock.java b/src/main/java/fr/umlv/java/wallj/block/TrashBlock.java index 33b5a6b..1f2dfa6 100644 --- a/src/main/java/fr/umlv/java/wallj/block/TrashBlock.java +++ b/src/main/java/fr/umlv/java/wallj/block/TrashBlock.java | |||
@@ -3,12 +3,17 @@ 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.Updateables; | 5 | import fr.umlv.java.wallj.context.Updateables; |
6 | import fr.umlv.java.wallj.event.BlockDestroyEvent; | ||
6 | import fr.umlv.java.wallj.event.Event; | 7 | import fr.umlv.java.wallj.event.Event; |
7 | import org.jbox2d.common.Vec2; | 8 | import org.jbox2d.common.Vec2; |
8 | import org.jbox2d.dynamics.BodyType; | 9 | import org.jbox2d.dynamics.BodyType; |
10 | import org.jbox2d.dynamics.contacts.ContactEdge; | ||
9 | 11 | ||
10 | import java.awt.*; | 12 | import java.awt.*; |
13 | import java.util.Spliterator; | ||
14 | import java.util.function.Consumer; | ||
11 | import java.util.stream.Stream; | 15 | import java.util.stream.Stream; |
16 | import java.util.stream.StreamSupport; | ||
12 | 17 | ||
13 | /** | 18 | /** |
14 | * A trash block. | 19 | * A trash block. |
@@ -23,11 +28,53 @@ public class TrashBlock extends JBoxBlock { | |||
23 | @Override | 28 | @Override |
24 | public Stream<Event> update(Context context) { | 29 | public Stream<Event> update(Context context) { |
25 | return Updateables.updateAll(context, | 30 | return Updateables.updateAll(context, |
31 | this::handleContacts, | ||
26 | this::paint); | 32 | this::paint); |
27 | } | 33 | } |
28 | 34 | ||
35 | private Stream<Event> handleContacts(Context context) { | ||
36 | return streamContactEdges() | ||
37 | .map(contactEdge -> ((Block) contactEdge.other.getUserData())) | ||
38 | .filter(otherBody -> otherBody.getType() == BlockType.GARBAGE) | ||
39 | .map(BlockDestroyEvent::new); | ||
40 | } | ||
41 | |||
29 | private Stream<Event> paint(Context context) { | 42 | private Stream<Event> paint(Context context) { |
30 | context.getGraphicsContext().paintRectangle(Color.RED, getPos(), TileVec2.TILE_DIM, TileVec2.TILE_DIM); | 43 | context.getGraphicsContext().paintRectangle(Color.RED, getPos(), TileVec2.TILE_DIM, TileVec2.TILE_DIM); |
31 | return Stream.empty(); | 44 | return Stream.empty(); |
32 | } | 45 | } |
46 | |||
47 | private Stream<ContactEdge> streamContactEdges() { | ||
48 | // If only we had (a working) Java 9... | ||
49 | // return Stream.iterate(getBody().getContactList(), c -> c.next) | ||
50 | // .takeWhile(Objects::nonNull); | ||
51 | |||
52 | return StreamSupport.stream(new Spliterator<ContactEdge>() { | ||
53 | private ContactEdge contactEdge = getBody().getContactList(); | ||
54 | |||
55 | @Override | ||
56 | public boolean tryAdvance(Consumer<? super ContactEdge> consumer) { | ||
57 | if (contactEdge == null) return false; | ||
58 | |||
59 | consumer.accept(contactEdge); | ||
60 | contactEdge = contactEdge.next; | ||
61 | return true; | ||
62 | } | ||
63 | |||
64 | @Override | ||
65 | public Spliterator<ContactEdge> trySplit() { | ||
66 | return null; | ||
67 | } | ||
68 | |||
69 | @Override | ||
70 | public long estimateSize() { | ||
71 | return Long.MAX_VALUE; | ||
72 | } | ||
73 | |||
74 | @Override | ||
75 | public int characteristics() { | ||
76 | return NONNULL; | ||
77 | } | ||
78 | }, false); | ||
79 | } | ||
33 | } | 80 | } |