Anthony Debucquoy a394ad009f
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/pr Build is passing
Save the level when pressing escape in a level
2023-05-18 17:55:58 +02:00

90 lines
3.5 KiB
Java

package school_project;
import javafx.scene.Group;
import javafx.scene.input.MouseButton;
import school_project.Menu.MenuAccueil;
import school_project.Utils.MatrixShape;
import java.io.FileNotFoundException;
public class GameUI extends Group{
public final static int SEGMENT_SIZE = 50;
public final static int SPACE_SIZE = 5;
private final Vec2 piece_pos_click = new Vec2();
private Map level;
public GameUI(Map level) throws FileNotFoundException {
super();
this.level = level;
MatrixShape grid = new MatrixShape(level);
//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);
Vec2 piece_space = new Vec2(SPACE_SIZE, SPACE_SIZE);
int column = 0;
for (Piece p : level.getPieces()) {
MatrixShape _piece = new MatrixShape(p);
if(piece_space.y + _piece.boundary_size.y >= Controller.screen_size.y){
column++;
piece_space.y = SPACE_SIZE;
piece_space.x = (SEGMENT_SIZE*3 + SPACE_SIZE*4 )* column;
}
_piece.setLayoutX(piece_space.x);
_piece.setLayoutY(piece_space.y);
piece_space.y += _piece.boundary_size.y;
// Pieces Events
_piece.setOnMouseClicked(event -> {
if(event.getButton() == MouseButton.SECONDARY){
((Piece) _piece.shape).RotateRight(1);
_piece.update();
}
});
_piece.setOnMousePressed(event -> {
piece_pos_click.x = (int) event.getX();
piece_pos_click.y = (int) event.getY();
});
_piece.setOnMouseDragged(event -> {
_piece.toFront();
_piece.setLayoutX(event.getSceneX() - piece_pos_click.x);
_piece.setLayoutY(event.getSceneY() - piece_pos_click.y);
});
_piece.setOnMouseReleased(event -> {
if(event.getButton() != MouseButton.PRIMARY)
return;
p.setPosition(null);
if(event.getSceneX() > grid.getLayoutX() && event.getSceneX() < grid.getLayoutX() + grid.boundary_size.x
&& event.getSceneY() > grid.getLayoutY() && event.getSceneY() < grid.getLayoutY() + grid.boundary_size.y )
{
// Inverted because screen is x →; y ↓ and matrix is x ↓; y →
Vec2 piece_position_in_grid = new Vec2(
(int) (_piece.getLayoutY() + (SEGMENT_SIZE+SPACE_SIZE)/2 - grid.getLayoutY())/(SEGMENT_SIZE+SPACE_SIZE),
(int) (_piece.getLayoutX() + (SEGMENT_SIZE+SPACE_SIZE)/2 - grid.getLayoutX())/(SEGMENT_SIZE+SPACE_SIZE)
);
level.placePiece(p, piece_position_in_grid);
if(p.getPosition() != null){
_piece.setLayoutX(grid.getLayoutX() + p.getPosition().y * (SEGMENT_SIZE+SPACE_SIZE));
_piece.setLayoutY(grid.getLayoutY() + p.getPosition().x * (SEGMENT_SIZE+SPACE_SIZE));
}
if(level.gameDone()){
Controller.switchRoot(new MenuAccueil());
}
}
});
getChildren().add(_piece);
}
}
public Map getLevel() {
return level;
}
}