Piece rotation

This commit is contained in:
Debucquoy Anthony 2023-05-03 18:21:54 +02:00
parent 15e1745ad1
commit 382af6b541
Signed by: tonitch
GPG Key ID: A78D6421F083D42E
2 changed files with 36 additions and 14 deletions

View File

@ -2,6 +2,7 @@ package school_project;
import javafx.geometry.Insets; import javafx.geometry.Insets;
import javafx.geometry.Pos; import javafx.geometry.Pos;
import javafx.scene.input.MouseButton;
import javafx.scene.layout.BorderPane; import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox; import javafx.scene.layout.HBox;
import javafx.scene.paint.Color; import javafx.scene.paint.Color;
@ -15,15 +16,20 @@ public class GameUI extends BorderPane {
public GameUI(Map level) throws FileNotFoundException { public GameUI(Map level) throws FileNotFoundException {
super(); super();
MatrixShape grid = new MatrixShape(level, true); MatrixShape grid = new MatrixShape(level);
grid.setAlignment(Pos.CENTER); grid.setAlignment(Pos.CENTER);
grid.setColor(Color.WHITE); grid.setColor(Color.WHITE);
HBox pieces = new HBox(); HBox pieces = new HBox();
pieces.setSpacing(10); pieces.setSpacing(10);
for (Piece p : level.getPieces()) { for (Piece p : level.getPieces()) {
MatrixShape _piece = new MatrixShape(p, false); MatrixShape _piece = new MatrixShape(p);
_piece.setColor(p.getColor()); _piece.setOnMouseClicked(event -> {
if(event.getButton() == MouseButton.SECONDARY){
((Piece) _piece.shape).RotateRight(1);
_piece.update();
}
});
pieces.getChildren().add(_piece); pieces.getChildren().add(_piece);
} }

View File

@ -7,37 +7,53 @@ import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color; import javafx.scene.paint.Color;
import javafx.scene.paint.Paint; import javafx.scene.paint.Paint;
import javafx.scene.shape.Rectangle; import javafx.scene.shape.Rectangle;
import org.checkerframework.common.reflection.qual.GetClass;
import school_project.Controller; import school_project.Controller;
import school_project.GameUI; import school_project.GameUI;
import school_project.Piece;
import school_project.Shape; import school_project.Shape;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.util.ArrayList;
public class MatrixShape extends GridPane { public class MatrixShape extends GridPane {
public MatrixShape(Shape shape, boolean texture) throws FileNotFoundException { public Shape shape;
private Paint color;
public MatrixShape(Shape shape) {
super(); super();
this.shape = shape;
if(shape instanceof Piece p){
color = p.getColor();
}
update();
setHgap(GameUI.SPACE_SIZE); setHgap(GameUI.SPACE_SIZE);
setVgap(GameUI.SPACE_SIZE); setVgap(GameUI.SPACE_SIZE);
}
public void update(){
getChildren().clear();
boolean[][] shape_matrix = shape.getShape(); boolean[][] shape_matrix = shape.getShape();
for (int i = 0; i < shape_matrix.length; i++) { for (int i = 0; i < shape_matrix.length; i++) {
for (int j = 0; j < shape_matrix[i].length; j++) { for (int j = 0; j < shape_matrix[i].length; j++) {
if(shape_matrix[i][j]){ if(shape_matrix[i][j]){
Node _cell = texture ? new ImageView(new Image(new FileInputStream(Controller.class.getResource("tile.png").getFile()))) : new Rectangle(GameUI.SEGMENT_SIZE, GameUI.SEGMENT_SIZE); Node _cell;
if(shape instanceof Piece p){
_cell = new Rectangle(GameUI.SEGMENT_SIZE, GameUI.SEGMENT_SIZE);
((Rectangle) _cell).setFill(color);
}else{
try {
_cell = new ImageView(new Image(new FileInputStream(Controller.class.getResource("tile.png").getFile())));
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
}
add(_cell, j, i); add(_cell, j, i);
} }
} }
} }
} }
public void setColor(Paint p) { public void setColor(Paint p) {
getChildren().forEach(node -> { color = p;
if(node instanceof Rectangle r){
r.setFill(p);
r.setStroke(Color.BLACK);
}
});
} }
} }