2023-02-27 00:52:19 +01:00
|
|
|
package school_project;
|
|
|
|
|
2023-05-03 17:27:10 +02:00
|
|
|
import javafx.scene.paint.Color;
|
|
|
|
import javafx.scene.paint.Paint;
|
|
|
|
|
|
|
|
import java.util.Random;
|
|
|
|
|
2023-03-21 09:56:13 +01:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
*/
|
2023-02-27 00:52:19 +01:00
|
|
|
public class Piece extends Shape{
|
|
|
|
|
2023-03-21 09:56:13 +01:00
|
|
|
private Vec2 Position;
|
2023-03-21 14:14:39 +01:00
|
|
|
private Map linked_map;
|
2023-05-03 17:27:10 +02:00
|
|
|
private transient Paint color; // https://www.baeldung.com/java-transient-keyword
|
2023-02-27 00:52:19 +01:00
|
|
|
|
|
|
|
public Piece(boolean[][] matrix) {
|
|
|
|
super(matrix);
|
2023-05-03 17:27:10 +02:00
|
|
|
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;
|
2023-02-27 00:52:19 +01:00
|
|
|
}
|
|
|
|
|
2023-03-21 09:56:13 +01:00
|
|
|
public Vec2 getPosition() {
|
|
|
|
return Position;
|
|
|
|
}
|
|
|
|
|
2023-03-21 14:14:39 +01:00
|
|
|
public void setPosition(Vec2 position){
|
|
|
|
this.Position = position;
|
|
|
|
}
|
|
|
|
|
2023-05-03 17:27:10 +02:00
|
|
|
|
2023-03-21 14:14:39 +01:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
2023-03-21 09:56:13 +01:00
|
|
|
|
2023-02-27 00:52:19 +01:00
|
|
|
/**
|
|
|
|
* 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];
|
2023-02-27 11:05:32 +01:00
|
|
|
for (int i = 0; i < width; i++) {
|
|
|
|
for (int j = 0; j < height; j++) {
|
|
|
|
temp_matrix[i][j] = matrix[-j+height-1][i];
|
2023-02-27 00:52:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
times--;
|
|
|
|
matrix = temp_matrix;
|
|
|
|
}
|
|
|
|
}
|
2023-04-21 20:00:15 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean equals(Object obj) {
|
|
|
|
if(obj instanceof Piece pieceObj){
|
|
|
|
if( pieceObj.getPosition().equals(this.getPosition()) && pieceObj.getShape().equals(getShape())) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2023-02-27 00:52:19 +01:00
|
|
|
}
|