Piece overlap detection
there is 3 addition: - `ArrayList getOccupation()` to get a list of all spot occupied by a piece - Fixing a bug to rotate right where width and height where inverted - Check if a piece is overlapping another when placing and refusing the placement if so
This commit is contained in:
		@ -46,13 +46,25 @@ public class Map extends Shape{
 | 
			
		||||
                || 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);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        System.out.println(occupation);
 | 
			
		||||
 | 
			
		||||
        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] && piece.getShape()[x - pos.x][y - pos.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;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@ -3,6 +3,7 @@ package school_project;
 | 
			
		||||
import javafx.scene.paint.Color;
 | 
			
		||||
import javafx.scene.paint.Paint;
 | 
			
		||||
 | 
			
		||||
import java.util.ArrayList;
 | 
			
		||||
import java.util.Random;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
@ -39,6 +40,17 @@ public class Piece extends Shape{
 | 
			
		||||
        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
 | 
			
		||||
@ -55,8 +67,6 @@ public class Piece extends Shape{
 | 
			
		||||
     */
 | 
			
		||||
    public void RotateRight(int times){
 | 
			
		||||
        while(times > 0) {
 | 
			
		||||
            height = matrix.length;
 | 
			
		||||
            width = matrix[0].length;
 | 
			
		||||
            boolean[][] temp_matrix = new boolean[width][height];
 | 
			
		||||
            for (int i = 0; i < width; i++) {
 | 
			
		||||
                for (int j = 0; j < height; j++) {
 | 
			
		||||
@ -65,6 +75,8 @@ public class Piece extends Shape{
 | 
			
		||||
            }
 | 
			
		||||
            times--;
 | 
			
		||||
            matrix = temp_matrix;
 | 
			
		||||
            height = matrix.length;
 | 
			
		||||
            width = matrix[0].length;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user