aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/fr/umlv/java/wallj/block/GarbageBlock.java
blob: 362e680f251d37e6d377904d35ef9e2f01dc0b5f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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.Updateables;
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 java.awt.*;
import java.util.Objects;
import java.util.stream.Stream;

/**
 * A garbage block.
 *
 * @author Pacien TRAN-GIRARD
 */
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);
  }

  @Override
  public Stream<Event> update(Context context) {
    return Updateables.updateAll(context,
    this::handleExplosionBlasts,
    this::paint);
  }

  private Stream<Event> handleExplosionBlasts(Context context) {
    Events.filter(context.getEvents(), BombExplosionEvent.class).forEach(explosion -> {
      Vec2 source = explosion.getSource().toVec2();
      context.getGame().getCurrentStage().getWorld().raycast((fixture, point, normal, fraction) -> {
        if (isSelf(fixture)) getBody().applyForceToCenter(computeBlastForce(source));
        return STOP_RAYCAST;
      }, source, getPos());
    });
    return Stream.empty();
  }

  private Stream<Event> paint(Context context) {
    context.getGraphicsContext().paintCircle(COLOR, getPos(), TileVec2.TILE_DIM);
    return Stream.empty();
  }

  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);
  }
}