aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/fr/umlv
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/fr/umlv')
-rw-r--r--src/main/java/fr/umlv/java/wallj/block/JBoxBlock.java44
-rw-r--r--src/main/java/fr/umlv/java/wallj/block/TrashBlock.java38
2 files changed, 44 insertions, 38 deletions
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 e01f4b0..1ef5503 100644
--- a/src/main/java/fr/umlv/java/wallj/block/JBoxBlock.java
+++ b/src/main/java/fr/umlv/java/wallj/block/JBoxBlock.java
@@ -3,10 +3,17 @@ package fr.umlv.java.wallj.block;
3import org.jbox2d.collision.shapes.Shape; 3import org.jbox2d.collision.shapes.Shape;
4import org.jbox2d.common.Vec2; 4import org.jbox2d.common.Vec2;
5import org.jbox2d.dynamics.*; 5import org.jbox2d.dynamics.*;
6import org.jbox2d.dynamics.contacts.ContactEdge;
6 7
7import java.util.Objects; 8import java.util.Objects;
9import java.util.Spliterator;
10import java.util.function.Consumer;
11import java.util.stream.Stream;
12import java.util.stream.StreamSupport;
8 13
9/** 14/**
15 * A block linked to a physical body.
16 *
10 * @author Pacien TRAN-GIRARD 17 * @author Pacien TRAN-GIRARD
11 */ 18 */
12public abstract class JBoxBlock extends Block { 19public abstract class JBoxBlock extends Block {
@@ -27,6 +34,43 @@ public abstract class JBoxBlock extends Block {
27 return body; 34 return body;
28 } 35 }
29 36
37 /**
38 * @return a stream of contact edges of this body
39 */
40 public Stream<ContactEdge> streamContactEdges() {
41 // If only we had (a working) Java 9...
42 // return Stream.iterate(getBody().getContactList(), c -> c.next)
43 // .takeWhile(Objects::nonNull);
44
45 return StreamSupport.stream(new Spliterator<ContactEdge>() {
46 private ContactEdge contactEdge = getBody().getContactList();
47
48 @Override
49 public boolean tryAdvance(Consumer<? super ContactEdge> consumer) {
50 if (contactEdge == null) return false;
51
52 consumer.accept(contactEdge);
53 contactEdge = contactEdge.next;
54 return true;
55 }
56
57 @Override
58 public Spliterator<ContactEdge> trySplit() {
59 return null;
60 }
61
62 @Override
63 public long estimateSize() {
64 return Long.MAX_VALUE;
65 }
66
67 @Override
68 public int characteristics() {
69 return NONNULL;
70 }
71 }, false);
72 }
73
30 @Override 74 @Override
31 public Vec2 getPos() { 75 public Vec2 getPos() {
32 if (body == null) throw new IllegalStateException("Uninitialised block."); 76 if (body == null) throw new IllegalStateException("Uninitialised block.");
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 1f2dfa6..cc76917 100644
--- a/src/main/java/fr/umlv/java/wallj/block/TrashBlock.java
+++ b/src/main/java/fr/umlv/java/wallj/block/TrashBlock.java
@@ -7,13 +7,9 @@ import fr.umlv.java.wallj.event.BlockDestroyEvent;
7import fr.umlv.java.wallj.event.Event; 7import fr.umlv.java.wallj.event.Event;
8import org.jbox2d.common.Vec2; 8import org.jbox2d.common.Vec2;
9import org.jbox2d.dynamics.BodyType; 9import org.jbox2d.dynamics.BodyType;
10import org.jbox2d.dynamics.contacts.ContactEdge;
11 10
12import java.awt.*; 11import java.awt.*;
13import java.util.Spliterator;
14import java.util.function.Consumer;
15import java.util.stream.Stream; 12import java.util.stream.Stream;
16import java.util.stream.StreamSupport;
17 13
18/** 14/**
19 * A trash block. 15 * A trash block.
@@ -43,38 +39,4 @@ public class TrashBlock extends JBoxBlock {
43 context.getGraphicsContext().paintRectangle(Color.RED, getPos(), TileVec2.TILE_DIM, TileVec2.TILE_DIM); 39 context.getGraphicsContext().paintRectangle(Color.RED, getPos(), TileVec2.TILE_DIM, TileVec2.TILE_DIM);
44 return Stream.empty(); 40 return Stream.empty();
45 } 41 }
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 }
80} 42}