This commit will align piece to their in game position #48
@ -2,6 +2,7 @@ package school_project;
|
|||||||
|
|
||||||
import javafx.scene.Group;
|
import javafx.scene.Group;
|
||||||
import javafx.scene.input.MouseButton;
|
import javafx.scene.input.MouseButton;
|
||||||
|
import school_project.Menu.MenuAcceuil;
|
||||||
import school_project.Utils.MatrixShape;
|
import school_project.Utils.MatrixShape;
|
||||||
|
|
||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
@ -50,10 +51,13 @@ public class GameUI extends Group{
|
|||||||
piece_pos_click.y = (int) event.getY();
|
piece_pos_click.y = (int) event.getY();
|
||||||
});
|
});
|
||||||
_piece.setOnMouseDragged(event -> {
|
_piece.setOnMouseDragged(event -> {
|
||||||
|
_piece.toFront();
|
||||||
_piece.setLayoutX(event.getSceneX() - piece_pos_click.x);
|
_piece.setLayoutX(event.getSceneX() - piece_pos_click.x);
|
||||||
_piece.setLayoutY(event.getSceneY() - piece_pos_click.y);
|
_piece.setLayoutY(event.getSceneY() - piece_pos_click.y);
|
||||||
});
|
});
|
||||||
_piece.setOnMouseReleased(event -> {
|
_piece.setOnMouseReleased(event -> {
|
||||||
|
if(event.getButton() != MouseButton.PRIMARY)
|
||||||
|
return;
|
||||||
if(event.getSceneX() > grid.getLayoutX() && event.getSceneX() < grid.getLayoutX() + grid.boundary_size.x
|
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 )
|
&& event.getSceneY() > grid.getLayoutY() && event.getSceneY() < grid.getLayoutY() + grid.boundary_size.y )
|
||||||
{
|
{
|
||||||
@ -63,6 +67,13 @@ public class GameUI extends Group{
|
|||||||
(int) (_piece.getLayoutX() + (SEGMENT_SIZE+SPACE_SIZE)/2 - grid.getLayoutX())/(SEGMENT_SIZE+SPACE_SIZE)
|
(int) (_piece.getLayoutX() + (SEGMENT_SIZE+SPACE_SIZE)/2 - grid.getLayoutX())/(SEGMENT_SIZE+SPACE_SIZE)
|
||||||
);
|
);
|
||||||
System.out.println(level.placePiece(p, piece_position_in_grid) + piece_position_in_grid.toString());
|
System.out.println(level.placePiece(p, piece_position_in_grid) + piece_position_in_grid.toString());
|
||||||
|
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 MenuAcceuil());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -68,6 +68,18 @@ public class Map extends Shape{
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if every pieces has a space on the board to know if the game is finished
|
||||||
|
* @return true if the game is finished, false if not
|
||||||
|
*/
|
||||||
|
public boolean gameDone(){
|
||||||
|
ArrayList<Vec2> posList = getPosList();
|
||||||
|
for(Piece p: pieces){
|
||||||
|
posList.removeAll(p.getOccupation());
|
||||||
|
}
|
||||||
|
return posList.isEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return a matrix with all used space on the map to see if a piece can fit in a space
|
* Return a matrix with all used space on the map to see if a piece can fit in a space
|
||||||
*
|
*
|
||||||
|
@ -42,6 +42,8 @@ public class Piece extends Shape{
|
|||||||
|
|
||||||
public ArrayList<Vec2> getOccupation(){
|
public ArrayList<Vec2> getOccupation(){
|
||||||
ArrayList<Vec2> ret = new ArrayList<>();
|
ArrayList<Vec2> ret = new ArrayList<>();
|
||||||
|
if(Position == null)
|
||||||
|
return ret;
|
||||||
for (int x = 0; x < height; x++) {
|
for (int x = 0; x < height; x++) {
|
||||||
for (int y = 0; y < width; y++) {
|
for (int y = 0; y < width; y++) {
|
||||||
if(getShape()[x][y]){
|
if(getShape()[x][y]){
|
||||||
|
@ -4,6 +4,7 @@ package school_project;
|
|||||||
import school_project.Utils.Array;
|
import school_project.Utils.Array;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Base class for everything that is a shape kind, like map and pieces
|
* Base class for everything that is a shape kind, like map and pieces
|
||||||
@ -84,6 +85,22 @@ public class Shape implements Serializable, Cloneable{
|
|||||||
return matrix;
|
return matrix;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the list of all the open possition of the map
|
||||||
|
* @return ArrayList of vec2 of all the positions
|
||||||
|
*/
|
||||||
|
public ArrayList<Vec2> getPosList(){
|
||||||
|
ArrayList<Vec2> ret = new ArrayList<>();
|
||||||
|
for (int x = 0; x < height; x++) {
|
||||||
|
for (int y = 0; y < width; y++) {
|
||||||
|
if(matrix[x][y]){
|
||||||
|
ret.add(new Vec2(x, y));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
String ret = "";
|
String ret = "";
|
||||||
|
Loading…
Reference in New Issue
Block a user