From 9aa09f8fbd4fe676c6b4eecaefc7ca9a6a553dff Mon Sep 17 00:00:00 2001 From: Anthony Debucquoy Date: Wed, 10 May 2023 22:55:42 +0200 Subject: [PATCH] Method to add a piece to a map with it's position --- app/src/main/java/school_project/Map.java | 29 +++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/app/src/main/java/school_project/Map.java b/app/src/main/java/school_project/Map.java index 2beea88..5782721 100644 --- a/app/src/main/java/school_project/Map.java +++ b/app/src/main/java/school_project/Map.java @@ -28,6 +28,35 @@ public class Map extends Shape{ this.addPiece(p); } + /** + * try to place a piece on the map, return true if succeed and false if it failed + * @param piece the piece to place + * @param pos the position to place the piece in matrix position + * @return true if the piece can and is placed and false if it can't and won't not be palced + */ + public boolean placePiece(Piece piece, Vec2 pos){ + + if(!pieces.contains(piece)) + return false; + + // In the map limits + if ( pos.x + piece.height > height + || pos.y+piece.width > width + || pos.x < 0 + || pos.y < 0) + return false; + + for (int x = pos.x; x < pos.x + piece.height; x++) { + for (int y = pos.y; y < pos.y + piece.width; y++) { + if (!getShape()[x][y] && piece.getShape()[x - pos.x][y - pos.y]) { + return false; + } + } + } + piece.setPosition(pos); + return true; + } + /** * Return a matrix with all used space on the map to see if a piece can fit in a space *