aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/fr/umlv/java/wallj/block/BombBlock.java
blob: 4f310eb83dadac1cc2981f5fb4df82a8b715447c (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
68
69
70
71
72
73
74
75
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.context.Updateables;
import fr.umlv.java.wallj.event.*;
import fr.umlv.java.wallj.event.Event;
import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.BodyType;

import java.awt.*;
import java.time.Duration;
import java.util.Objects;
import java.util.stream.Stream;

/**
 * A bomb block.
 *
 * @author Pacien TRAN-GIRARD
 * @author Adam NAILI
 */
public class BombBlock extends JBoxBlock {
  private static final Duration TIME_INCREMENT = Duration.ofSeconds(1);
  private static final Duration MIN_TIME = Duration.ofSeconds(1);
  private static final Duration MAX_TIME = Duration.ofSeconds(9);

  private Duration timer = MIN_TIME;

  BombBlock(Vec2 pos) {
    super(BlockType.BOMB, BodyType.STATIC, SolidDef.squareShape(), pos);
  }

  private void incrementTimer() {
    timer = timer.compareTo(MAX_TIME) <= 0 ? timer.plus(TIME_INCREMENT) : MIN_TIME;
  }

  private void decrementTimer(Duration d) {
    if (timer.isNegative()) throw new IllegalStateException("This bomb has already exploded.");
    timer = timer.minus(d);
  }

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

  private Stream<Event> handleBombConfiguration(Context context) {
    Events.findFirst(context.getEvents(), BombTimerIncrEvent.class)
    .filter(event -> Objects.equals(event.getPos(), TileVec2.of(getPos())))
    .ifPresent(event -> incrementTimer());
    return Stream.empty();
  }

  private Stream<Event> consume(Context context) {
    decrementTimer(context.getTimeDelta());
    return timer.isNegative() ?
           Stream.of(new BombExplosionEvent(TileVec2.of(getPos())), new BlockDestroyEvent(this)) :
           Stream.empty();
  }

  private Stream<Event> paint(Context context) {
    GraphicsContext graphicsContext = context.getGraphicsContext();
    graphicsContext.paintCircle(Color.BLACK, getPos(), TileVec2.TILE_DIM);
    Vec2 textPosition = getPos();
    textPosition.x += TileVec2.TILE_DIM / 4.0f;
    textPosition.y += 3 * TileVec2.TILE_DIM / 4.0f;
    graphicsContext.paintString(Color.RED, textPosition, Long.toString(timer.getSeconds()));

    return Stream.empty();
  }
}