Added removed pieces set and animation for changing Pawn
This commit is contained in:
parent
824f26ac67
commit
e3d3a93c5c
|
@ -166,7 +166,6 @@ public abstract class AbstractPiece implements IPiece {
|
|||
chessboard.addPiece(this, x, y);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isFloating() {
|
||||
return overrideX != 0 && overrideY != 0;
|
||||
}
|
||||
|
@ -260,7 +259,6 @@ public abstract class AbstractPiece implements IPiece {
|
|||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean[][] getPossibleMoves() {
|
||||
return getPossibleMoves(false);
|
||||
}
|
||||
|
@ -289,7 +287,6 @@ public abstract class AbstractPiece implements IPiece {
|
|||
int i = 1;
|
||||
double currentX = 0;
|
||||
double currentY = 0;
|
||||
@Override
|
||||
public void run() {
|
||||
chessboard.repaint(getRepaintRectangle(currentX, currentY, chessboard.boardScale*chessboard.pieceScale));
|
||||
if(i > 100) {
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.geom.AffineTransform;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Chessboard class
|
||||
|
@ -52,6 +54,8 @@ public class Chessboard extends JPanel {
|
|||
*/
|
||||
public IPiece[][] pieces = new IPiece[SQUARE_COUNT][SQUARE_COUNT];
|
||||
|
||||
public Set<IPiece> removedPieces = new HashSet<>();
|
||||
|
||||
public boolean[][] possibleMoves = new boolean[SQUARE_COUNT][SQUARE_COUNT];
|
||||
|
||||
public boolean[][] lastMove = new boolean[SQUARE_COUNT][SQUARE_COUNT];
|
||||
|
@ -142,6 +146,9 @@ public class Chessboard extends JPanel {
|
|||
*/
|
||||
public void addPiece(IPiece piece, int x, int y) {
|
||||
if(!isOnBoard(x, y)) return;
|
||||
IPiece removedPiece = pieces[y][x];
|
||||
if(removedPiece != null) removedPieces.add(removedPiece);
|
||||
removedPieces.remove(piece);
|
||||
pieces[y][x] = piece;
|
||||
}
|
||||
|
||||
|
@ -154,6 +161,7 @@ public class Chessboard extends JPanel {
|
|||
public IPiece removePiece(int x, int y) {
|
||||
if(!isOnBoard(x, y)) return null;
|
||||
IPiece piece = pieces[y][x];
|
||||
if(piece != null) removedPieces.add(piece);
|
||||
pieces[y][x] = null;
|
||||
return piece;
|
||||
}
|
||||
|
@ -182,32 +190,16 @@ public class Chessboard extends JPanel {
|
|||
|
||||
boolean isBlack = true;
|
||||
|
||||
g2.setStroke(new BasicStroke(10));
|
||||
|
||||
for(int i=0; i<SQUARE_COUNT; i++) {
|
||||
if(SQUARE_COUNT % 2 == 0) isBlack = !isBlack;
|
||||
for(int j=0; j<SQUARE_COUNT; j++) {
|
||||
if(isBlack) g2.setColor(Color.LIGHT_GRAY);
|
||||
else g2.setColor(Color.WHITE);
|
||||
g2.setTransform(beforeSquares);
|
||||
g2.translate(i*SQUARE_SIZE, j*SQUARE_SIZE);
|
||||
paintSquare(g2, isBlack);
|
||||
paintHighlights(g2, i, j);
|
||||
isBlack = !isBlack;
|
||||
g2.fillRect(i*SQUARE_SIZE, j*SQUARE_SIZE, SQUARE_SIZE, SQUARE_SIZE);
|
||||
}
|
||||
}
|
||||
|
||||
g2.setStroke(new BasicStroke(10));
|
||||
for(int i=0; i<SQUARE_COUNT; i++) {
|
||||
for(int j=0; j<SQUARE_COUNT; j++) {
|
||||
if(lastMove != null && lastMove[j][i]) {
|
||||
g2.setTransform(beforeSquares);
|
||||
g2.translate(i*SQUARE_SIZE, j*SQUARE_SIZE);
|
||||
g2.setColor(new Color(50, 50, 250, 30));
|
||||
g2.fillRect(0, 0, SQUARE_SIZE, SQUARE_SIZE);
|
||||
}
|
||||
if(possibleMoves != null && possibleMoves[j][i]) {
|
||||
g2.setTransform(beforeSquares);
|
||||
g2.translate(i*SQUARE_SIZE, j*SQUARE_SIZE);
|
||||
g2.setColor(new Color(0, 0, 0, 40));
|
||||
if(pieces[j][i] == null) g2.fillOval(35, 35, 30, 30);
|
||||
else g2.drawRect(5, 5, 90, 90);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -240,10 +232,34 @@ public class Chessboard extends JPanel {
|
|||
}
|
||||
}
|
||||
|
||||
g2.setTransform(beforeBoard);
|
||||
for (IPiece piece : removedPieces) {
|
||||
if(!piece.isFloating()) continue;
|
||||
g2.setTransform(beforeBoard);
|
||||
g2.translate(piece.getOverrideX(), piece.getOverrideY());
|
||||
g2.scale(pieceScale * boardScale, pieceScale * boardScale);
|
||||
piece.paint(g2);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void paintSquare(Graphics g, boolean isBlack) {
|
||||
if(isBlack) g.setColor(Color.LIGHT_GRAY);
|
||||
else g.setColor(Color.WHITE);
|
||||
g.fillRect(0, 0, SQUARE_SIZE+1, SQUARE_SIZE+1);
|
||||
}
|
||||
|
||||
public void paintHighlights(Graphics g, int x, int y) {
|
||||
if(lastMove != null && lastMove[y][x]) {
|
||||
g.setColor(new Color(50, 50, 250, 30));
|
||||
g.fillRect(0, 0, SQUARE_SIZE, SQUARE_SIZE);
|
||||
}
|
||||
if(possibleMoves != null && possibleMoves[y][x]) {
|
||||
g.setColor(new Color(0, 0, 0, 40));
|
||||
if(pieces[y][x] == null) g.fillOval(35, 35, 30, 30);
|
||||
else g.drawRect(5, 5, 90, 90);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Setup Graphics2D with anti-aliasing
|
||||
* @param g Graphics instance
|
||||
|
|
|
@ -1,4 +1,6 @@
|
|||
import java.awt.*;
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
|
||||
/**
|
||||
* Pawn piece class
|
||||
|
@ -68,7 +70,14 @@ public class Pawn extends AbstractPiece {
|
|||
super.move(pos, animate);
|
||||
|
||||
int changeY = color == PieceColor.WHITE ? 0 : chessboard.SQUARE_COUNT-1;
|
||||
if(y == changeY) new Queen(player, x, y);
|
||||
if(y == changeY) {
|
||||
Timer timer = new Timer();
|
||||
timer.schedule(new TimerTask() {
|
||||
public void run() {
|
||||
new Queen(player, x, y);
|
||||
}
|
||||
}, animate ? 500 : 0);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue