diff options
author | pacien | 2018-02-03 16:06:06 +0100 |
---|---|---|
committer | pacien | 2018-02-03 16:06:06 +0100 |
commit | d03ad6430bdc0b3552149435ba20fb2bca4f294e (patch) | |
tree | 065440e3d6090353fa84b2d2bfd75b6902247622 | |
parent | 9b05a7b77876fac4cc603aa2d7300fc8f443822b (diff) | |
download | wallj-d03ad6430bdc0b3552149435ba20fb2bca4f294e.tar.gz |
Implement initial robot placement
Signed-off-by: pacien <pacien.trangirard@pacien.net>
-rw-r--r-- | src/main/java/fr/umlv/java/wallj/board/BoardConverter.java | 2 | ||||
-rw-r--r-- | src/main/java/fr/umlv/java/wallj/context/Stage.java | 16 |
2 files changed, 15 insertions, 3 deletions
diff --git a/src/main/java/fr/umlv/java/wallj/board/BoardConverter.java b/src/main/java/fr/umlv/java/wallj/board/BoardConverter.java index 064df7a..84feb41 100644 --- a/src/main/java/fr/umlv/java/wallj/board/BoardConverter.java +++ b/src/main/java/fr/umlv/java/wallj/board/BoardConverter.java | |||
@@ -33,9 +33,9 @@ public final class BoardConverter { | |||
33 | * @return the list of blocks converted | 33 | * @return the list of blocks converted |
34 | */ | 34 | */ |
35 | public static List<Block> boardToWorld(Board board) { | 35 | public static List<Block> boardToWorld(Board board) { |
36 | ArrayList<Block> blocks = new ArrayList<>(); | ||
37 | int nbRow = board.getDim().getRow(); | 36 | int nbRow = board.getDim().getRow(); |
38 | int nbCol = board.getDim().getCol(); | 37 | int nbCol = board.getDim().getCol(); |
38 | ArrayList<Block> blocks = new ArrayList<>(nbCol * nbRow); | ||
39 | for (int i = 0; i < nbRow; i++) { | 39 | for (int i = 0; i < nbRow; i++) { |
40 | for (int j = 0; j < nbCol; j++) { | 40 | for (int j = 0; j < nbCol; j++) { |
41 | Block block; | 41 | Block block; |
diff --git a/src/main/java/fr/umlv/java/wallj/context/Stage.java b/src/main/java/fr/umlv/java/wallj/context/Stage.java index 8852568..8ef4558 100644 --- a/src/main/java/fr/umlv/java/wallj/context/Stage.java +++ b/src/main/java/fr/umlv/java/wallj/context/Stage.java | |||
@@ -5,12 +5,15 @@ import fr.umlv.java.wallj.block.BlockFactory; | |||
5 | import fr.umlv.java.wallj.block.BlockType; | 5 | import fr.umlv.java.wallj.block.BlockType; |
6 | import fr.umlv.java.wallj.board.Board; | 6 | import fr.umlv.java.wallj.board.Board; |
7 | import fr.umlv.java.wallj.board.BoardConverter; | 7 | import fr.umlv.java.wallj.board.BoardConverter; |
8 | import fr.umlv.java.wallj.board.TileVec2; | ||
8 | import fr.umlv.java.wallj.event.*; | 9 | import fr.umlv.java.wallj.event.*; |
9 | import org.jbox2d.common.Vec2; | 10 | import org.jbox2d.common.Vec2; |
10 | import org.jbox2d.dynamics.World; | 11 | import org.jbox2d.dynamics.World; |
11 | 12 | ||
12 | import java.time.Duration; | 13 | import java.time.Duration; |
14 | import java.util.LinkedList; | ||
13 | import java.util.List; | 15 | import java.util.List; |
16 | import java.util.Map; | ||
14 | import java.util.Objects; | 17 | import java.util.Objects; |
15 | 18 | ||
16 | /** | 19 | /** |
@@ -21,15 +24,16 @@ public class Stage implements Updateable { | |||
21 | private static final int POSITION_TICK_PER_MS = 2; | 24 | private static final int POSITION_TICK_PER_MS = 2; |
22 | 25 | ||
23 | private final World world = new World(new Vec2()); | 26 | private final World world = new World(new Vec2()); |
27 | private final List<Block> blocks = new LinkedList<>(); | ||
24 | private final Board board; | 28 | private final Board board; |
25 | private final List<Block> blocks; | ||
26 | 29 | ||
27 | /** | 30 | /** |
28 | * @param board the base board | 31 | * @param board the base board |
29 | */ | 32 | */ |
30 | public Stage(Board board) { | 33 | public Stage(Board board) { |
31 | this.board = Objects.requireNonNull(board); | 34 | this.board = Objects.requireNonNull(board); |
32 | blocks = BoardConverter.boardToWorld(board); | 35 | blocks.addAll(BoardConverter.boardToWorld(board)); |
36 | blocks.add(BlockFactory.build(BlockType.ROBOT, findAnyFreeTile(board))); | ||
33 | blocks.forEach(block -> block.link(world)); | 37 | blocks.forEach(block -> block.link(world)); |
34 | } | 38 | } |
35 | 39 | ||
@@ -85,4 +89,12 @@ public class Stage implements Updateable { | |||
85 | block.link(world); | 89 | block.link(world); |
86 | }); | 90 | }); |
87 | } | 91 | } |
92 | |||
93 | private static TileVec2 findAnyFreeTile(Board board) { | ||
94 | return board.stream() | ||
95 | .filter(entry -> entry.getValue() == BlockType.FREE) | ||
96 | .findAny() | ||
97 | .map(Map.Entry::getKey) | ||
98 | .orElseThrow(IllegalArgumentException::new); | ||
99 | } | ||
88 | } | 100 | } |