Merge pull request 'Place pieces on the board' (#46) from pieceInteraction into master
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
Reviewed-on: #46 Reviewed-by: Mat_02 <diletomatteo@gmail.com>
This commit is contained in:
commit
d389b22f2d
@ -53,9 +53,20 @@ public class GameUI extends Group{
|
|||||||
_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 -> {
|
||||||
|
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)
|
||||||
|
);
|
||||||
|
System.out.println(level.placePiece(p, piece_position_in_grid) + piece_position_in_grid.toString());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
getChildren().add(_piece);
|
getChildren().add(_piece);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -28,6 +28,46 @@ public class Map extends Shape{
|
|||||||
this.addPiece(p);
|
this.addPiece(p);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* try to place a piece on the map, return true if succeed and false if it failed
|
||||||
|
* @param piece the piece to place
|
||||||
|
* @param pos the position to place the piece in matrix position
|
||||||
|
* @return true if the piece can and is placed and false if it can't and won't not be palced
|
||||||
|
*/
|
||||||
|
public boolean placePiece(Piece piece, Vec2 pos){
|
||||||
|
|
||||||
|
if(!pieces.contains(piece))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
piece.setPosition(null);
|
||||||
|
// In the map limits
|
||||||
|
if ( pos.x + piece.height > height
|
||||||
|
|| pos.y+piece.width > width
|
||||||
|
|| pos.x < 0
|
||||||
|
|| pos.y < 0)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
ArrayList<Vec2> occupation = new ArrayList<>();
|
||||||
|
for(Piece p: pieces){
|
||||||
|
if(p.getPosition() == null || p == piece)
|
||||||
|
continue;
|
||||||
|
for (Vec2 o : p.getOccupation()) {
|
||||||
|
occupation.add(o);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int x = pos.x; x < pos.x + piece.height; x++) {
|
||||||
|
for (int y = pos.y; y < pos.y + piece.width; y++) {
|
||||||
|
if ((!getShape()[x][y] || occupation.contains(new Vec2(x, y))) && piece.getShape()[x - pos.x][y - pos.y]) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
piece.setPosition(pos);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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
|
||||||
*
|
*
|
||||||
|
@ -3,6 +3,7 @@ package school_project;
|
|||||||
import javafx.scene.paint.Color;
|
import javafx.scene.paint.Color;
|
||||||
import javafx.scene.paint.Paint;
|
import javafx.scene.paint.Paint;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -39,6 +40,17 @@ public class Piece extends Shape{
|
|||||||
this.Position = position;
|
this.Position = position;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public ArrayList<Vec2> getOccupation(){
|
||||||
|
ArrayList<Vec2> ret = new ArrayList<>();
|
||||||
|
for (int x = 0; x < height; x++) {
|
||||||
|
for (int y = 0; y < width; y++) {
|
||||||
|
if(getShape()[x][y]){
|
||||||
|
ret.add(new Vec2(getPosition().x + x, getPosition().y + y));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* set the map the piece is into the the map argument
|
* set the map the piece is into the the map argument
|
||||||
@ -55,8 +67,6 @@ public class Piece extends Shape{
|
|||||||
*/
|
*/
|
||||||
public void RotateRight(int times){
|
public void RotateRight(int times){
|
||||||
while(times > 0) {
|
while(times > 0) {
|
||||||
height = matrix.length;
|
|
||||||
width = matrix[0].length;
|
|
||||||
boolean[][] temp_matrix = new boolean[width][height];
|
boolean[][] temp_matrix = new boolean[width][height];
|
||||||
for (int i = 0; i < width; i++) {
|
for (int i = 0; i < width; i++) {
|
||||||
for (int j = 0; j < height; j++) {
|
for (int j = 0; j < height; j++) {
|
||||||
@ -65,6 +75,8 @@ public class Piece extends Shape{
|
|||||||
}
|
}
|
||||||
times--;
|
times--;
|
||||||
matrix = temp_matrix;
|
matrix = temp_matrix;
|
||||||
|
height = matrix.length;
|
||||||
|
width = matrix[0].length;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user