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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
|
package fr.umlv.java.wallj.block;
import fr.umlv.java.wallj.board.Board;
import fr.umlv.java.wallj.board.PathFinder;
import fr.umlv.java.wallj.board.TileVec2;
import fr.umlv.java.wallj.context.Context;
import fr.umlv.java.wallj.context.Stage;
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.World;
import java.awt.*;
import java.util.*;
import java.util.stream.Stream;
/**
* A robot block.
*
* @author Pacien TRAN-GIRARD
*/
public class RobotBlock extends Block {
private static final float SPEED = 10f; // px/ms
private Vec2 pos;
private PathFinder pathFinder;
private Deque<TileVec2> path = new LinkedList<>();
private int droppedBombCount = 0;
RobotBlock(Vec2 pos) {
super(BlockType.ROBOT);
this.pos = pos;
}
@Override
public Vec2 getPos() {
return new Vec2(pos);
}
@Override
public void link(World world) {
// no-op
}
@Override
public void unlink(World world) {
// no-op
}
@Override
public Stream<Event> update(Context context) {
return Updateables.updateAll(context,
this::updatePath,
this::move,
this::paint,
this::setupBomb);
}
private Stream<Event> setupBomb(Context context) {
return Events.findFirst(context.getEvents(), BombSetupOrder.class)
.flatMap(event -> isOnBomb(context.getGame().getCurrentStage()) ?
Optional.of(new BombTimerIncrEvent(getTile())) :
dropBomb())
.map(Stream::of) // Optional.stream() only available in Java 9
.orElseGet(Stream::empty);
}
private Optional<Event> dropBomb() {
if (droppedBombCount >= Stage.BOMB_PLACEMENTS) return Optional.empty();
droppedBombCount++;
return Optional.of(new BlockCreateEvent(BlockType.BOMB, getTile()));
}
private Stream<Event> updatePath(Context context) {
Events.findFirst(context.getEvents(), MoveRobotOrder.class)
.ifPresent(event -> {
Board board = context.getGame().getCurrentStage().getBoard();
TileVec2 target = event.getTarget();
if (!board.inside(target) || !board.getBlockTypeAt(target).isTraversable()) return;
if (pathFinder == null) pathFinder = new PathFinder(board);
path = new LinkedList<>(pathFinder.findPath(TileVec2.of(pos), target));
});
return Stream.empty();
}
private Stream<Event> move(Context context) {
if (!path.isEmpty()) {
Vec2 dest = path.getFirst().toVec2();
Vec2 dir = dest.sub(pos);
float dist = dir.normalize();
Vec2 dp = dir.mul(context.getTimeDelta().toMillis() * SPEED);
pos = dp.length() < dist ? pos.add(dp) : path.removeFirst().toVec2();
}
return Stream.empty();
}
private Stream<Event> paint(Context context) {
context.getGraphicsContext().paintCircle(Color.BLUE, getPos(), TileVec2.TILE_DIM / 2);
return Stream.empty();
}
/**
* @implNote TODO: profile this and consider a mapping (pos: block) for faster lookup in Stage
*/
private boolean isOnBomb(Stage stage) {
return stage.getBlocks().stream()
.anyMatch(block -> Objects.equals(block.getType(), BlockType.BOMB) &&
Objects.equals(block.getPos(), getTile().toVec2()));
}
}
|