From 2e5e21102ea5b1a7f5b3c77126f891b657eb8434 Mon Sep 17 00:00:00 2001 From: pacien Date: Sat, 3 Feb 2018 15:51:18 +0100 Subject: Implement explosion blast handling Signed-off-by: pacien --- .../fr/umlv/java/wallj/block/GarbageBlock.java | 32 ++++++++++++++++++++++ .../java/fr/umlv/java/wallj/block/JBoxBlock.java | 10 ++++++- 2 files changed, 41 insertions(+), 1 deletion(-) 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; import fr.umlv.java.wallj.board.TileVec2; import fr.umlv.java.wallj.context.Context; import fr.umlv.java.wallj.context.GraphicsContext; +import fr.umlv.java.wallj.event.BombExplosionEvent; import fr.umlv.java.wallj.event.Event; +import fr.umlv.java.wallj.event.Events; import org.jbox2d.common.Vec2; import org.jbox2d.dynamics.BodyType; +import org.jbox2d.dynamics.Fixture; +import org.jbox2d.dynamics.World; import java.awt.*; import java.util.Collections; import java.util.List; +import java.util.Objects; /** * A garbage block. @@ -18,6 +23,8 @@ import java.util.List; */ public class GarbageBlock extends JBoxBlock { private static final Color COLOR = new Color(102, 51, 0); + private static final int STOP_RAYCAST = 0; + private static final float EXPLOSION_RANGE = 20f * TileVec2.TILE_DIM; GarbageBlock(Vec2 pos) { super(BlockType.GARBAGE, BodyType.DYNAMIC, SolidDef.circleShape(), pos); @@ -25,11 +32,36 @@ public class GarbageBlock extends JBoxBlock { @Override public List update(Context context) { + handleExplosionBlasts(context.getEvents(), context.getGame().getCurrentStage().getWorld()); paint(context.getGraphicsContext()); return Collections.emptyList(); } + private void handleExplosionBlasts(List events, World world) { + Events.filter(events, BombExplosionEvent.class).forEach(explosion -> { + Vec2 source = explosion.getSource().toVec2(); + world.raycast((fixture, point, normal, fraction) -> { + if (isSelf(fixture)) getBody().applyForceToCenter(computeBlastForce(source)); + return STOP_RAYCAST; + }, source, getPos()); + }); + } + private void paint(GraphicsContext graphicsContext) { graphicsContext.paintCircle(COLOR, getPos(), TileVec2.TILE_DIM); } + + private boolean isSelf(Fixture fixture) { + for (Fixture self = getBody().getFixtureList(); self != null; self = self.getNext()) + if (Objects.equals(fixture, self)) + return true; + + return false; + } + + private Vec2 computeBlastForce(Vec2 source) { + Vec2 diff = getPos().sub(source); + float dist = diff.normalize(); + return diff.mul(EXPLOSION_RANGE / dist); + } } 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 { fixtureDef = SolidDef.fixtureDefOf(Objects.requireNonNull(bodyShape)); } + /** + * @return the JBox2D body + */ + public Body getBody() { + return body; + } + @Override public Vec2 getPos() { if (body == null) throw new IllegalStateException("Uninitialised block."); @@ -30,8 +37,9 @@ public abstract class JBoxBlock extends Block { public void link(World world) { if (body != null) throw new IllegalStateException("Block is already linked."); body = world.createBody(bodyDef); - body.createFixture(fixtureDef); body.setUserData(this); + Fixture fixture = body.createFixture(fixtureDef); + fixture.setUserData(body); } @Override -- cgit v1.2.3