From 173328fdbda79754ca40278b0ba142f7d20822eb Mon Sep 17 00:00:00 2001 From: pacien Date: Mon, 8 Jan 2018 13:28:39 +0100 Subject: Implement TileVec2 and Board, adding missing BlockType Signed-off-by: pacien --- src/docs/class.puml | 33 +++++---- src/main/java/fr/umlv/java/wallj/board/Board.java | 41 ++++++++++- .../java/fr/umlv/java/wallj/board/TileVec2.java | 81 ++++++++++++++++++++++ .../java/fr/umlv/java/wallj/model/BlockType.java | 14 ++-- .../java/fr/umlv/java/wallj/utils/TileVec2.java | 5 -- 5 files changed, 147 insertions(+), 27 deletions(-) create mode 100644 src/main/java/fr/umlv/java/wallj/board/TileVec2.java delete mode 100644 src/main/java/fr/umlv/java/wallj/utils/TileVec2.java (limited to 'src') diff --git a/src/docs/class.puml b/src/docs/class.puml index f4766db..7a00135 100644 --- a/src/docs/class.puml +++ b/src/docs/class.puml @@ -6,15 +6,6 @@ package utils { class PathFinder { static List findPath(Board, TileVec2, TileVec2) } - - class TileVec2 { - static final int TILE_DIM - - Vec2 - TileVec2(Vec2) - Vec2 toPixelPos() - static TileVec2 fromPixelPos(Vec2) - } } package viewer { @@ -76,7 +67,9 @@ package event { package board { class Board { - BlockType[][] + Board(width, height) + BlockType getBlockTypeAt(TileVec2) + BlockType setBlockTypeAt(TileVec2, BlockType) } class BoardParser { @@ -91,15 +84,25 @@ package board { static Board worldToBoard(List) static List boardToWorld(Board) } + + class TileVec2 { + static final int TILE_DIM + static TileVec2 fromVec2(Vec2) + + Vec2 + TileVec2(col, row) + Vec2 toPixelPos() + } } package model { enum BlockType { - Wall - Trash - Garbage - Robot - Bomb + FREE + WALL + TRASH + GARBAGE + ROBOT + BOMB } class BlockFactory { 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 @@ package fr.umlv.java.wallj.board; -public class Board { - //TODO +import fr.umlv.java.wallj.model.BlockType; + +/** + * A mutable BlockType matrix. + */ +public final class Board { + + private final BlockType[][] map; + + public Board(int width, int height) { + map = new BlockType[width][height]; + } + + /** + * @param pos the tile position vector + * @return the element at the given position + */ + public BlockType getBlockTypeAt(TileVec2 pos) { + return map[pos.getCol()][pos.getRow()]; + } + + /** + * @param pos the tile position vector + * @param type the BlockType to set + * @return the overwritten element + */ + public BlockType setBlockTypeAt(TileVec2 pos, BlockType type) { + BlockType old = map[pos.getCol()][pos.getRow()]; + map[pos.getCol()][pos.getRow()] = type; + return old; + } + + /** + * @return the dimension of the Board + */ + public TileVec2 getDim() { + return TileVec2.of(map.length, map.length > 0 ? map[0].length : 0); + } + } 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 @@ +package fr.umlv.java.wallj.board; + +import org.jbox2d.common.Vec2; + +import java.util.Objects; + +/** + * A typed immutable tile coordinate vector containing the coordinates of a Tile in a Board. + */ +public final class TileVec2 { + + private static final int TILE_DIM = 20; + + /** + * @param row the row + * @param col the column + * @return a corresponding tile vector + */ + public static TileVec2 of(int row, int col) { + return new TileVec2(row, col); + } + + /** + * @param v a JBox2D Vec2 vector + * @return the coordinates of the tile containing the given point + */ + public static TileVec2 of(Vec2 v) { + return new TileVec2((int) (v.x / TILE_DIM), (int) (v.y / TILE_DIM)); + } + + private final int row, col; + + private TileVec2(int row, int col) { + this.row = row; + this.col = col; + } + + /** + * @return the row + */ + public int getRow() { + return row; + } + + /** + * @return the column + */ + public int getCol() { + return col; + } + + /** + * @return the corresponding JBox2D coordinates of the top-left corner of the tile + */ + public Vec2 toVec2() { + return new Vec2(row * TILE_DIM, col * TILE_DIM); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof TileVec2)) return false; + TileVec2 tileVec2 = (TileVec2) o; + return row == tileVec2.row && + col == tileVec2.col; + } + + @Override + public int hashCode() { + return Objects.hash(row, col); + } + + @Override + public String toString() { + return "TileVec2{" + + "row=" + row + + ", col=" + col + + '}'; + } + +} 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 @@ package fr.umlv.java.wallj.model; +/** + * Enumeration of the types of blocks handled in the game. + */ public enum BlockType { - Wall, - Trash, - Garbage, - Robot, - Bomb + FREE, + wALL, + TRASH, + GARBAGE, + ROBOT, + BOMB } 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 @@ -package fr.umlv.java.wallj.utils; - -public class TileVec2 { - //TODO -} -- cgit v1.2.3