From 0bab3fbef6be550ae9dca05ab0179edcd6774d37 Mon Sep 17 00:00:00 2001 From: "Debucquoy Anthony (tonitch)" Date: Sat, 29 Apr 2023 19:01:31 +0200 Subject: [PATCH] Show Map and pieces shape in gameui --- app/src/main/java/school_project/GameUI.java | 29 +++++++++++++++ .../school_project/Utils/MatrixShape.java | 37 +++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 app/src/main/java/school_project/GameUI.java create mode 100644 app/src/main/java/school_project/Utils/MatrixShape.java diff --git a/app/src/main/java/school_project/GameUI.java b/app/src/main/java/school_project/GameUI.java new file mode 100644 index 0000000..82b4cf8 --- /dev/null +++ b/app/src/main/java/school_project/GameUI.java @@ -0,0 +1,29 @@ +package school_project; + +import javafx.geometry.Pos; +import javafx.scene.layout.BorderPane; +import javafx.scene.layout.HBox; +import javafx.scene.paint.Color; +import school_project.Utils.MatrixShape; + +public class GameUI extends BorderPane { + public final static int SEGMENT_SIZE = 50; + public GameUI(Map level) { + super(); + + MatrixShape grid = new MatrixShape(level); + grid.setAlignment(Pos.CENTER); + grid.setColor(Color.WHITE); + + HBox pieces = new HBox(); + pieces.setSpacing(10); + for (Piece p : level.getPieces()) { + MatrixShape _piece = new MatrixShape(p); + _piece.setColor(Color.RED); // TODO: Change with piece color + pieces.getChildren().add(_piece); + } + + setCenter(grid); + setBottom(pieces); + } +} diff --git a/app/src/main/java/school_project/Utils/MatrixShape.java b/app/src/main/java/school_project/Utils/MatrixShape.java new file mode 100644 index 0000000..8574295 --- /dev/null +++ b/app/src/main/java/school_project/Utils/MatrixShape.java @@ -0,0 +1,37 @@ +package school_project.Utils; + +import javafx.scene.layout.GridPane; +import javafx.scene.paint.Color; +import javafx.scene.paint.Paint; +import javafx.scene.shape.Rectangle; +import school_project.GameUI; +import school_project.Shape; + +import java.util.ArrayList; + +public class MatrixShape extends GridPane { + public MatrixShape(){ + super(); + } + public MatrixShape(Shape shape){ + super(); + boolean[][] shape_matrix = shape.getShape(); + for (int i = 0; i < shape_matrix.length; i++) { + for (int j = 0; j < shape_matrix[i].length; j++) { + if(shape_matrix[i][j]){ + Rectangle _cell = new Rectangle(GameUI.SEGMENT_SIZE, GameUI.SEGMENT_SIZE); + add(_cell, j, i); + } + } + } + } + + public void setColor(Paint p) { + getChildren().forEach(node -> { + if(node instanceof Rectangle r){ + r.setFill(p); + r.setStroke(Color.BLACK); + } + }); + } +}