Method to add a piece to a map with it's position

This commit is contained in:
Debucquoy Anthony 2023-05-10 22:55:42 +02:00
parent 90d6d47cc8
commit 9aa09f8fbd
Signed by: tonitch
GPG Key ID: A78D6421F083D42E

View File

@ -28,6 +28,35 @@ public class Map extends Shape{
this.addPiece(p); 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 * Return a matrix with all used space on the map to see if a piece can fit in a space
* *