Commit 8ca7196e authored by Ciliste's avatar Ciliste
Browse files

Rotation reverse et déplacement de pièce buggé

parent b837c216
Loading
Loading
Loading
Loading
−188 B (5.29 KiB)

File changed.

No diff preview for this file type.

+8 −0
Changes for jeuAssemblage/src/main/Controller.java: 8 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -100,8 +100,16 @@ public class Controller {
	}

	public void addPieceOnBoard(int x, int y) {

		this.model.addSelectedPiece(x, y);
		this.view.repaint();
		this.eventManager.fireEvent();
	}

	public void removeSelectedPiece() {

		this.model.removeSelectedPiece();
		this.view.repaint();
		this.eventManager.fireEvent();
	}
}
+36 −4
Changes for jeuAssemblage/src/model/PlayBoard.java: 36 added lines, 4 removed lines.
Original line number Diff line number Diff line
@@ -65,12 +65,26 @@ public class PlayBoard {
    * @see          Piece
    */
    public void addSelectedPiece(int x, int y) {

        if (this.selectedPiece == null) return;

        this.addPieceOnBoard(this.selectedPiece, x, y);

		int index = this.alPieceOnBoard.indexOf(this.getPieceById(this.selectedPiece.getInstanceId()));
        this.alPieceOnBoard.remove(this.getPieceById(this.selectedPiece.getInstanceId()));
		this.alPieceOnBoard.add(index, this.selectedPiece);
        this.selectedPiece = null;

		printMatrice(playBoard);
    }

	public void removeSelectedPiece() {

		if (this.selectedPiece == null) return;

		this.removePieceOnBoard(this.selectedPiece);

		this.alPieceOnBoard.add(this.getPieceById(this.selectedPiece.getInstanceId()));
	}

    /**
@@ -167,19 +181,37 @@ public class PlayBoard {
        int[][] bounds = p.getBounds();
        int k = 0;
        int l = 0;
        for (int i = y; i < y + p.getHeight(); i++) {
            l = 0;
            for (int j = x; j < x + p.getWidth(); j++) {
                this.playBoard[i][j] = bounds[k][l] * (p.getInstanceId() + 1);

        for (int i = y; i < y + bounds.length; i++) {

			for (int j = x; j < x + bounds[i - y].length; j++) {

				if (bounds[k][l] == 1) {

					this.playBoard[i][j] = p.getInstanceId() + 1;
				}

				l++;
			}
			k++;
			l = 0;
		}

        p.setX(x);
        p.setY(y);
    }

	public void removePieceOnBoard(Piece p) {

		for (int i = p.getY(); i < p.getY() + p.getHeight(); i++) {

			for (int j = p.getX(); j < p.getX() + p.getWidth(); j++) {

				this.playBoard[i][j] = 0;
			}
		}
	}

    private boolean canBeAddedToBoard(int x, int y, int width, int height) {
        if ( x + width  > this.playBoard.length   ) return false;
        if ( y + height > this.playBoard[0].length) return false;
+69 −8
Changes for jeuAssemblage/src/view/component/board/Grid.java: 69 added lines, 8 removed lines.
Original line number Diff line number Diff line
@@ -12,7 +12,6 @@ import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;

public class Grid extends JPanel {

@@ -33,6 +32,7 @@ public class Grid extends JPanel {
        this.setVisible(true);

		this.addMouseListener(new GridClickListener());
		this.addMouseMotionListener(new GridMouseMotionListener());
    }

	@Override
@@ -58,6 +58,7 @@ public class Grid extends JPanel {

        Graphics2D g2d = (Graphics2D) g;
        g2d.setColor(Color.WHITE);

		int[][] matrices = this.controller.getPlayBoard();

        for ( int i = 0; i < matrices.length; i++) {
@@ -75,8 +76,6 @@ public class Grid extends JPanel {

					if (p.equals(controller.getSelectedPiece())) {

						System.out.println("Piece selected");

						g2d.setColor(Color.RED);
						g2d.fill(shape);
						g2d.setColor(Color.WHITE);
@@ -101,14 +100,53 @@ public class Grid extends JPanel {
                }
            }
        }

		if (controller.getSelectedPiece() != null) {

			// Get the position of the mouse in the grid
			int x = (int) ((mousePosition.getX() - xGridDeb) / ((xGridFin - xGridDeb) / matrices[0].length));
			int y = (int) ((mousePosition.getY() - yGridDeb) / ((yGridFin - yGridDeb) / matrices.length));

			int[][] piece = controller.getSelectedPiece().getBounds();
			Image img = controller.getImageById(controller.getSelectedPiece().getInstanceId());

			for (int i = 0; i < piece.length; i++) {

				for (int j = 0; j < piece[i].length; j++) {

					if (piece[i][j] != 0) {

						int x2 = (x + j - 1);
						int y2 = (y + i - 1);

						if (x2 - j < 0) x2 = j;
						if (y2 - i < 0) y2 = i;

						if (x2 + (piece[i].length - j) > matrices[0].length) x2 = matrices[0].length - piece[i].length + j;
						if (y2 + (piece.length - i) > matrices.length) y2 = matrices.length - piece.length + i;

						g2d.drawImage(
							img,
							x2 * (int) componentSize + (int) paddingWidth / 2 + 1,
							y2 * (int) componentSize + (int) paddingHeight / 2 + 1,
							(int) componentSize,
							(int) componentSize,
							null
						);
					}
				}
			}
		}
	}

	private final Point mousePosition = new Point(0, 0);

	private class GridClickListener extends MouseAdapter {
	
		@Override
		public void mouseClicked(MouseEvent e) {

			System.out.println("Grid clicked");
			mousePosition.setLocation(e.getX(), e.getY());

			int[][] grille = controller.getPlayBoard();

@@ -122,20 +160,43 @@ public class Grid extends JPanel {
			int x = (int) ((e.getX() - xGridDeb) / ((xGridFin - xGridDeb) / grille[0].length));
			int y = (int) ((e.getY() - yGridDeb) / ((yGridFin - yGridDeb) / grille.length));

			System.out.println("x: " + x + " y: " + y);

			int pieceId = grille[y][x];

			if (pieceId == 0) {
				System.out.println("No piece here");

				if (controller.getSelectedPiece() == null) return;

				if (controller.canBeAddedToBoard(x, y)) {

					controller.addPieceOnBoard(x, y);
					controller.setPieceSelected(null);
					getParent().repaint();
				}
				else {

					System.out.println("Piece can't be added to the board");
				}

				return;
			}

			System.out.println("Piece id: " + pieceId);
			Piece piece = controller.getPieceById(pieceId);

			controller.setPieceSelected(piece);
			getParent().repaint();
		}
	}

	private class GridMouseMotionListener extends MouseAdapter {

		@Override
		public void mouseMoved(MouseEvent e) {

			if (controller.getSelectedPiece() != null) {

				mousePosition.setLocation(e.getX(), e.getY());
				repaint();
			}
		}
	}
}
+29 −17
Changes for jeuAssemblage/src/view/component/board/PieceBoard.java: 29 added lines, 17 removed lines.
Original line number Diff line number Diff line
@@ -7,6 +7,7 @@ import view.utils.SwingUtils;
import javax.swing.*;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.util.Arrays;

public class PieceBoard extends JPanel {

@@ -14,6 +15,7 @@ public class PieceBoard extends JPanel {

	private final JButton btnTurnLeft  = new JButton("<-");
	private final JButton btnTurnRight = new JButton("->");
	private final JButton btnReverse   = new JButton("<>");

	private final JButton btnCancel = new JButton("X");

@@ -25,19 +27,29 @@ public class PieceBoard extends JPanel {

		this.add(btnTurnLeft);
		this.add(btnTurnRight);
		this.add(btnReverse);

		this.add(btnCancel);

		btnTurnLeft.addActionListener(e -> {

			controller.getSelectedPiece().rotate(Piece.LEFT);
			repaint();
			System.out.println(Arrays.deepToString(controller.getSelectedPiece().getBounds()));
			getParent().repaint();
		});

		btnTurnRight.addActionListener(e -> {

			controller.getSelectedPiece().rotate(Piece.RIGHT);
			repaint();
			System.out.println(Arrays.deepToString(controller.getSelectedPiece().getBounds()));
			getParent().repaint();
		});

		btnReverse.addActionListener(e -> {

			controller.getSelectedPiece().reverse();
			System.out.println(Arrays.deepToString(controller.getSelectedPiece().getBounds()));
			getParent().repaint();
		});

		btnCancel.addActionListener(e -> {
@@ -52,7 +64,7 @@ public class PieceBoard extends JPanel {

		final int MARGIN_TOP = SwingUtils.getHeightTimesPourcent(this, 0.5f);

		final int BTN_WIDTH  = SwingUtils.getWidthTimesPourcent(this, 0.33f);
		final int BTN_WIDTH  = SwingUtils.getWidthTimesPourcent(this, 0.2f);
		final int BTN_HEIGHT = SwingUtils.getHeightTimesPourcent(this, 0.2f);

		final int BTN_MARGIN = SwingUtils.getWidthTimesPourcent(this, 0.11f);
@@ -64,13 +76,20 @@ public class PieceBoard extends JPanel {
			BTN_HEIGHT
		);

		btnTurnRight.setBounds(
		btnReverse.setBounds(
			BTN_MARGIN + BTN_WIDTH + BTN_MARGIN,
			MARGIN_TOP,
			BTN_WIDTH,
			BTN_HEIGHT
		);

		btnTurnRight.setBounds(
			BTN_MARGIN + BTN_WIDTH + BTN_MARGIN + BTN_WIDTH + BTN_MARGIN,
			MARGIN_TOP,
			BTN_WIDTH,
			BTN_HEIGHT
		);

		final int BTN_CANCEL_WIDTH  = SwingUtils.getWidthTimesPourcent(this, 0.5f);
		final int BTN_CANCEL_HEIGHT = BTN_HEIGHT;

@@ -93,18 +112,11 @@ public class PieceBoard extends JPanel {

        SwingUtils.drawDebugBounds(this, g);

		if (this.controller.getSelectedPiece() == null) {

			btnTurnLeft.setVisible(false);
			btnTurnRight.setVisible(false);
			btnCancel.setVisible(false);
		}
		else {

			btnTurnLeft.setVisible(true);
			btnTurnRight.setVisible(true);
			btnCancel.setVisible(true);
		}
		boolean hasSelectedPiece = controller.getSelectedPiece() != null;
		btnTurnLeft.setVisible(hasSelectedPiece);
		btnTurnRight.setVisible(hasSelectedPiece);
		btnReverse.setVisible(hasSelectedPiece);
		btnCancel.setVisible(hasSelectedPiece);

		final Piece p = this.controller.getSelectedPiece();
		if (p == null) return;
@@ -171,8 +183,8 @@ public class PieceBoard extends JPanel {

						g2d.drawImage(
							img,
							i * CELL_SIZE,
							j * CELL_SIZE,
							i * CELL_SIZE,
							CELL_SIZE,
							CELL_SIZE,
							null
Loading