diff options
author | pacien | 2018-01-08 13:28:39 +0100 |
---|---|---|
committer | pacien | 2018-01-08 13:28:39 +0100 |
commit | 173328fdbda79754ca40278b0ba142f7d20822eb (patch) | |
tree | 047924f26e6454247551dbb72199b241b1b60e25 /src/main/java/fr | |
parent | 0a426ff97b4d4006f651630c94fe094b313cca25 (diff) | |
download | wallj-173328fdbda79754ca40278b0ba142f7d20822eb.tar.gz |
Implement TileVec2 and Board, adding missing BlockType
Signed-off-by: pacien <pacien.trangirard@pacien.net>
Diffstat (limited to 'src/main/java/fr')
4 files changed, 129 insertions, 12 deletions
diff --git a/src/main/java/fr/umlv/java/wallj/board/Board.java b/src/main/java/fr/umlv/java/wallj/board/Board.java index d20517a..ebe1a7e 100644 --- a/src/main/java/fr/umlv/java/wallj/board/Board.java +++ b/src/main/java/fr/umlv/java/wallj/board/Board.java | |||
@@ -1,5 +1,42 @@ | |||
1 | package fr.umlv.java.wallj.board; | 1 | package fr.umlv.java.wallj.board; |
2 | 2 | ||
3 | public class Board { | 3 | import fr.umlv.java.wallj.model.BlockType; |
4 | //TODO | 4 | |
5 | /** | ||
6 | * A mutable BlockType matrix. | ||
7 | */ | ||
8 | public final class Board { | ||
9 | |||
10 | private final BlockType[][] map; | ||
11 | |||
12 | public Board(int width, int height) { | ||
13 | map = new BlockType[width][height]; | ||
14 | } | ||
15 | |||
16 | /** | ||
17 | * @param pos the tile position vector | ||
18 | * @return the element at the given position | ||
19 | */ | ||
20 | public BlockType getBlockTypeAt(TileVec2 pos) { | ||
21 | return map[pos.getCol()][pos.getRow()]; | ||
22 | } | ||
23 | |||
24 | /** | ||
25 | * @param pos the tile position vector | ||
26 | * @param type the BlockType to set | ||
27 | * @return the overwritten element | ||
28 | */ | ||
29 | public BlockType setBlockTypeAt(TileVec2 pos, BlockType type) { | ||
30 | BlockType old = map[pos.getCol()][pos.getRow()]; | ||
31 | map[pos.getCol()][pos.getRow()] = type; | ||
32 | return old; | ||
33 | } | ||
34 | |||
35 | /** | ||
36 | * @return the dimension of the Board | ||
37 | */ | ||
38 | public TileVec2 getDim() { | ||
39 | return TileVec2.of(map.length, map.length > 0 ? map[0].length : 0); | ||
40 | } | ||
41 | |||
5 | } | 42 | } |
diff --git a/src/main/java/fr/umlv/java/wallj/board/TileVec2.java b/src/main/java/fr/umlv/java/wallj/board/TileVec2.java new file mode 100644 index 0000000..ac28b6d --- /dev/null +++ b/src/main/java/fr/umlv/java/wallj/board/TileVec2.java | |||
@@ -0,0 +1,81 @@ | |||
1 | package fr.umlv.java.wallj.board; | ||
2 | |||
3 | import org.jbox2d.common.Vec2; | ||
4 | |||
5 | import java.util.Objects; | ||
6 | |||
7 | /** | ||
8 | * A typed immutable tile coordinate vector containing the coordinates of a Tile in a Board. | ||
9 | */ | ||
10 | public final class TileVec2 { | ||
11 | |||
12 | private static final int TILE_DIM = 20; | ||
13 | |||
14 | /** | ||
15 | * @param row the row | ||
16 | * @param col the column | ||
17 | * @return a corresponding tile vector | ||
18 | */ | ||
19 | public static TileVec2 of(int row, int col) { | ||
20 | return new TileVec2(row, col); | ||
21 | } | ||
22 | |||
23 | /** | ||
24 | * @param v a JBox2D Vec2 vector | ||
25 | * @return the coordinates of the tile containing the given point | ||
26 | */ | ||
27 | public static TileVec2 of(Vec2 v) { | ||
28 | return new TileVec2((int) (v.x / TILE_DIM), (int) (v.y / TILE_DIM)); | ||
29 | } | ||
30 | |||
31 | private final int row, col; | ||
32 | |||
33 | private TileVec2(int row, int col) { | ||
34 | this.row = row; | ||
35 | this.col = col; | ||
36 | } | ||
37 | |||
38 | /** | ||
39 | * @return the row | ||
40 | */ | ||
41 | public int getRow() { | ||
42 | return row; | ||
43 | } | ||
44 | |||
45 | /** | ||
46 | * @return the column | ||
47 | */ | ||
48 | public int getCol() { | ||
49 | return col; | ||
50 | } | ||
51 | |||
52 | /** | ||
53 | * @return the corresponding JBox2D coordinates of the top-left corner of the tile | ||
54 | */ | ||
55 | public Vec2 toVec2() { | ||
56 | return new Vec2(row * TILE_DIM, col * TILE_DIM); | ||
57 | } | ||
58 | |||
59 | @Override | ||
60 | public boolean equals(Object o) { | ||
61 | if (this == o) return true; | ||
62 | if (!(o instanceof TileVec2)) return false; | ||
63 | TileVec2 tileVec2 = (TileVec2) o; | ||
64 | return row == tileVec2.row && | ||
65 | col == tileVec2.col; | ||
66 | } | ||
67 | |||
68 | @Override | ||
69 | public int hashCode() { | ||
70 | return Objects.hash(row, col); | ||
71 | } | ||
72 | |||
73 | @Override | ||
74 | public String toString() { | ||
75 | return "TileVec2{" + | ||
76 | "row=" + row + | ||
77 | ", col=" + col + | ||
78 | '}'; | ||
79 | } | ||
80 | |||
81 | } | ||
diff --git a/src/main/java/fr/umlv/java/wallj/model/BlockType.java b/src/main/java/fr/umlv/java/wallj/model/BlockType.java index 0cd9eda..64ef602 100644 --- a/src/main/java/fr/umlv/java/wallj/model/BlockType.java +++ b/src/main/java/fr/umlv/java/wallj/model/BlockType.java | |||
@@ -1,9 +1,13 @@ | |||
1 | package fr.umlv.java.wallj.model; | 1 | package fr.umlv.java.wallj.model; |
2 | 2 | ||
3 | /** | ||
4 | * Enumeration of the types of blocks handled in the game. | ||
5 | */ | ||
3 | public enum BlockType { | 6 | public enum BlockType { |
4 | Wall, | 7 | FREE, |
5 | Trash, | 8 | wALL, |
6 | Garbage, | 9 | TRASH, |
7 | Robot, | 10 | GARBAGE, |
8 | Bomb | 11 | ROBOT, |
12 | BOMB | ||
9 | } | 13 | } |
diff --git a/src/main/java/fr/umlv/java/wallj/utils/TileVec2.java b/src/main/java/fr/umlv/java/wallj/utils/TileVec2.java deleted file mode 100644 index e88558a..0000000 --- a/src/main/java/fr/umlv/java/wallj/utils/TileVec2.java +++ /dev/null | |||
@@ -1,5 +0,0 @@ | |||
1 | package fr.umlv.java.wallj.utils; | ||
2 | |||
3 | public class TileVec2 { | ||
4 | //TODO | ||
5 | } | ||