62 lines
1.9 KiB
Java
Raw Normal View History

2023-04-29 19:01:31 +02:00
package school_project;
import javafx.scene.Group;
2023-05-03 18:21:54 +02:00
import javafx.scene.input.MouseButton;
2023-04-29 19:01:31 +02:00
import school_project.Utils.MatrixShape;
2023-05-03 11:11:16 +02:00
import java.io.FileNotFoundException;
public class GameUI extends Group{
2023-04-29 19:01:31 +02:00
public final static int SEGMENT_SIZE = 50;
2023-05-03 11:11:16 +02:00
public final static int SPACE_SIZE = 5;
2023-05-05 00:15:38 +02:00
private final Vec2 piece_pos_click = new Vec2();
2023-05-03 11:11:16 +02:00
public GameUI(Map level) throws FileNotFoundException {
2023-04-29 19:01:31 +02:00
super();
2023-05-03 18:21:54 +02:00
MatrixShape grid = new MatrixShape(level);
2023-04-29 19:01:31 +02:00
2023-05-04 23:24:18 +02:00
//center the grid
grid.setLayoutX((Controller.screen_size.x - grid.boundary_size.x) >> 1);
grid.setLayoutY((Controller.screen_size.y - grid.boundary_size.y) >> 1);
getChildren().add(grid);
2023-05-05 12:03:40 +02:00
Vec2 piece_space = new Vec2(SPACE_SIZE, SPACE_SIZE);
int column = 0;
2023-04-29 19:01:31 +02:00
for (Piece p : level.getPieces()) {
2023-05-03 18:21:54 +02:00
MatrixShape _piece = new MatrixShape(p);
2023-05-05 00:15:38 +02:00
2023-05-05 12:03:40 +02:00
_piece.setLayoutX(piece_space.x);
_piece.setLayoutY(piece_space.y);
piece_space.y += _piece.boundary_size.y;
if(piece_space.y >= Controller.screen_size.y){
column++;
piece_space.y = SPACE_SIZE;
piece_space.x = (SEGMENT_SIZE*3 + SPACE_SIZE*4 )* column;
}
2023-05-05 00:15:38 +02:00
// Pieces Events
2023-05-03 18:21:54 +02:00
_piece.setOnMouseClicked(event -> {
if(event.getButton() == MouseButton.SECONDARY){
((Piece) _piece.shape).RotateRight(1);
_piece.update();
}
});
2023-05-05 00:15:38 +02:00
_piece.setOnMousePressed(event -> {
piece_pos_click.x = (int) event.getX();
piece_pos_click.y = (int) event.getY();
});
_piece.setOnMouseDragged(event -> {
2023-05-05 00:15:38 +02:00
_piece.setLayoutX(event.getSceneX() - piece_pos_click.x);
_piece.setLayoutY(event.getSceneY() - piece_pos_click.y);
});
2023-05-05 00:15:38 +02:00
2023-05-04 23:24:18 +02:00
getChildren().add(_piece);
2023-04-29 19:01:31 +02:00
}
}
}