package school_project; import javafx.scene.paint.Color; import javafx.scene.paint.Paint; import java.util.ArrayList; import java.util.Random; /** * Represent a Piece in the game. * Every Piece should be contained in a Map Object. * A piece has a position witch is the position of its top-leftest position in its matrix. * If the piece is not placed in the Map (in a floating state) the position should be null; */ public class Piece extends Shape{ private Vec2 Position; private Map linked_map; private transient Paint color; // https://www.baeldung.com/java-transient-keyword public Piece(boolean[][] matrix) { super(matrix); Random rand = new Random(); color = new Color(rand.nextDouble(), rand.nextDouble(), rand.nextDouble(), 1); } public void setColor(Paint p){ color = p; } public Paint getColor(){ return color; } public Vec2 getPosition() { return Position; } public void setPosition(Vec2 position){ this.Position = position; } public ArrayList getOccupation(){ ArrayList ret = new ArrayList<>(); if(Position == null) return ret; for (int x = 0; x < height; x++) { for (int y = 0; y < width; y++) { if(getShape()[x][y]){ ret.add(new Vec2(getPosition().x + x, getPosition().y + y)); } } } return ret; } /** * set the map the piece is into the the map argument * @param map map where to place the piece */ public void setLinked_map(Map map){ this.linked_map = map; } /** * Rotate the matrix of the piece. Used when the player right click * * @param times Set the amount of time the rotation should be executed. Should be set between 1 and 3. */ public void RotateRight(int times){ while(times > 0) { boolean[][] temp_matrix = new boolean[width][height]; for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { temp_matrix[i][j] = matrix[-j+height-1][i]; } } times--; matrix = temp_matrix; height = matrix.length; width = matrix[0].length; } } @Override public boolean equals(Object obj) { if(obj instanceof Piece){ Piece pieceObj = (Piece) obj; if (pieceObj.getPosition() != null && this.getPosition() != null){ return pieceObj.getPosition().equals(this.getPosition()) && pieceObj.getShape().equals(getShape()); } return pieceObj.getShape().equals(getShape()); } return false; } }