diff options
author | pacien | 2018-02-03 15:51:18 +0100 |
---|---|---|
committer | pacien | 2018-02-03 15:51:18 +0100 |
commit | 2e5e21102ea5b1a7f5b3c77126f891b657eb8434 (patch) | |
tree | 27c1319b4e0ac6086bff0778b8e763e4b770d26a | |
parent | 96351df6f73094f4005568eeecaee7e7504395a0 (diff) | |
download | wallj-2e5e21102ea5b1a7f5b3c77126f891b657eb8434.tar.gz |
Implement explosion blast handling
Signed-off-by: pacien <pacien.trangirard@pacien.net>
-rw-r--r-- | src/main/java/fr/umlv/java/wallj/block/GarbageBlock.java | 32 | ||||
-rw-r--r-- | src/main/java/fr/umlv/java/wallj/block/JBoxBlock.java | 10 |
2 files changed, 41 insertions, 1 deletions
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 a8635ec..9338888 100644 --- a/src/main/java/fr/umlv/java/wallj/block/GarbageBlock.java +++ b/src/main/java/fr/umlv/java/wallj/block/GarbageBlock.java | |||
@@ -3,13 +3,18 @@ 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.event.BombExplosionEvent; | ||
6 | import fr.umlv.java.wallj.event.Event; | 7 | import fr.umlv.java.wallj.event.Event; |
8 | import fr.umlv.java.wallj.event.Events; | ||
7 | import org.jbox2d.common.Vec2; | 9 | import org.jbox2d.common.Vec2; |
8 | import org.jbox2d.dynamics.BodyType; | 10 | import org.jbox2d.dynamics.BodyType; |
11 | import org.jbox2d.dynamics.Fixture; | ||
12 | import org.jbox2d.dynamics.World; | ||
9 | 13 | ||
10 | import java.awt.*; | 14 | import java.awt.*; |
11 | import java.util.Collections; | 15 | import java.util.Collections; |
12 | import java.util.List; | 16 | import java.util.List; |
17 | import java.util.Objects; | ||
13 | 18 | ||
14 | /** | 19 | /** |
15 | * A garbage block. | 20 | * A garbage block. |
@@ -18,6 +23,8 @@ import java.util.List; | |||
18 | */ | 23 | */ |
19 | public class GarbageBlock extends JBoxBlock { | 24 | public class GarbageBlock extends JBoxBlock { |
20 | private static final Color COLOR = new Color(102, 51, 0); | 25 | private static final Color COLOR = new Color(102, 51, 0); |
26 | private static final int STOP_RAYCAST = 0; | ||
27 | private static final float EXPLOSION_RANGE = 20f * TileVec2.TILE_DIM; | ||
21 | 28 | ||
22 | GarbageBlock(Vec2 pos) { | 29 | GarbageBlock(Vec2 pos) { |
23 | super(BlockType.GARBAGE, BodyType.DYNAMIC, SolidDef.circleShape(), pos); | 30 | super(BlockType.GARBAGE, BodyType.DYNAMIC, SolidDef.circleShape(), pos); |
@@ -25,11 +32,36 @@ public class GarbageBlock extends JBoxBlock { | |||
25 | 32 | ||
26 | @Override | 33 | @Override |
27 | public List<Event> update(Context context) { | 34 | public List<Event> update(Context context) { |
35 | handleExplosionBlasts(context.getEvents(), context.getGame().getCurrentStage().getWorld()); | ||
28 | paint(context.getGraphicsContext()); | 36 | paint(context.getGraphicsContext()); |
29 | return Collections.emptyList(); | 37 | return Collections.emptyList(); |
30 | } | 38 | } |
31 | 39 | ||
40 | private void handleExplosionBlasts(List<Event> events, World world) { | ||
41 | Events.filter(events, BombExplosionEvent.class).forEach(explosion -> { | ||
42 | Vec2 source = explosion.getSource().toVec2(); | ||
43 | world.raycast((fixture, point, normal, fraction) -> { | ||
44 | if (isSelf(fixture)) getBody().applyForceToCenter(computeBlastForce(source)); | ||
45 | return STOP_RAYCAST; | ||
46 | }, source, getPos()); | ||
47 | }); | ||
48 | } | ||
49 | |||
32 | private void paint(GraphicsContext graphicsContext) { | 50 | private void paint(GraphicsContext graphicsContext) { |
33 | graphicsContext.paintCircle(COLOR, getPos(), TileVec2.TILE_DIM); | 51 | graphicsContext.paintCircle(COLOR, getPos(), TileVec2.TILE_DIM); |
34 | } | 52 | } |
53 | |||
54 | private boolean isSelf(Fixture fixture) { | ||
55 | for (Fixture self = getBody().getFixtureList(); self != null; self = self.getNext()) | ||
56 | if (Objects.equals(fixture, self)) | ||
57 | return true; | ||
58 | |||
59 | return false; | ||
60 | } | ||
61 | |||
62 | private Vec2 computeBlastForce(Vec2 source) { | ||
63 | Vec2 diff = getPos().sub(source); | ||
64 | float dist = diff.normalize(); | ||
65 | return diff.mul(EXPLOSION_RANGE / dist); | ||
66 | } | ||
35 | } | 67 | } |
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 f0f2834..e01f4b0 100644 --- a/src/main/java/fr/umlv/java/wallj/block/JBoxBlock.java +++ b/src/main/java/fr/umlv/java/wallj/block/JBoxBlock.java | |||
@@ -20,6 +20,13 @@ public abstract class JBoxBlock extends Block { | |||
20 | fixtureDef = SolidDef.fixtureDefOf(Objects.requireNonNull(bodyShape)); | 20 | fixtureDef = SolidDef.fixtureDefOf(Objects.requireNonNull(bodyShape)); |
21 | } | 21 | } |
22 | 22 | ||
23 | /** | ||
24 | * @return the JBox2D body | ||
25 | */ | ||
26 | public Body getBody() { | ||
27 | return body; | ||
28 | } | ||
29 | |||
23 | @Override | 30 | @Override |
24 | public Vec2 getPos() { | 31 | public Vec2 getPos() { |
25 | if (body == null) throw new IllegalStateException("Uninitialised block."); | 32 | if (body == null) throw new IllegalStateException("Uninitialised block."); |
@@ -30,8 +37,9 @@ public abstract class JBoxBlock extends Block { | |||
30 | public void link(World world) { | 37 | public void link(World world) { |
31 | if (body != null) throw new IllegalStateException("Block is already linked."); | 38 | if (body != null) throw new IllegalStateException("Block is already linked."); |
32 | body = world.createBody(bodyDef); | 39 | body = world.createBody(bodyDef); |
33 | body.createFixture(fixtureDef); | ||
34 | body.setUserData(this); | 40 | body.setUserData(this); |
41 | Fixture fixture = body.createFixture(fixtureDef); | ||
42 | fixture.setUserData(body); | ||
35 | } | 43 | } |
36 | 44 | ||
37 | @Override | 45 | @Override |