Fixed input listening bug in NaturalPlayer

+ disconnect methods in Game and Player
+ NaturalPlayer removes its MouseListener from OverlayComponent after
the disconnect method is called
This commit is contained in:
Kai S. K. Engelbart 2019-07-18 15:01:15 +02:00
parent 3c03f01962
commit 1323286c93
5 changed files with 68 additions and 37 deletions

View File

@ -93,5 +93,12 @@ public class Game {
overlayComponent.clearArrow(); overlayComponent.clearArrow();
} }
/**
* Removed all connections between the game and the ui.
*/
public void disconnect() {
players.values().forEach(Player::disconnect);
}
public Board getBoard() { return board; } public Board getBoard() { return board; }
} }

View File

@ -1,7 +1,7 @@
package dev.kske.chess.game; package dev.kske.chess.game;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent; import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -18,43 +18,19 @@ import dev.kske.chess.ui.OverlayComponent;
* Created: <strong>06.07.2019</strong><br> * Created: <strong>06.07.2019</strong><br>
* Author: <strong>Kai S. K. Engelbart</strong> * Author: <strong>Kai S. K. Engelbart</strong>
*/ */
public class NaturalPlayer extends Player { public class NaturalPlayer extends Player implements MouseListener {
private boolean moveRequested; private final OverlayComponent overlayComponent;
private boolean moveRequested;
private Position pos;
public NaturalPlayer(Color color, OverlayComponent overlayComponent) { public NaturalPlayer(Color color, OverlayComponent overlayComponent) {
super(color); super(color);
moveRequested = false; this.overlayComponent = overlayComponent;
overlayComponent.addMouseListener(new MouseAdapter() { moveRequested = false;
private Position pos; overlayComponent.addMouseListener(this);
@Override
public void mousePressed(MouseEvent evt) {
if (!moveRequested) return;
if (pos == null) {
pos = new Position(evt.getPoint().x / overlayComponent.getTileSize(),
evt.getPoint().y / overlayComponent.getTileSize());
Board board = (Board) NaturalPlayer.this.board.clone();
if (board.get(pos) != null && board.get(pos).getColor() == color) {
List<Position> positions = board.getMoves(pos)
.stream()
.map(move -> move.dest)
.collect(Collectors.toList());
overlayComponent.displayDots(positions);
} else pos = null;
} else {
Position dest = new Position(evt.getPoint().x / overlayComponent.getTileSize(),
evt.getPoint().y / overlayComponent.getTileSize());
overlayComponent.clearDots();
moveRequested = false;
game.onMove(NaturalPlayer.this, new Move(pos, dest, Type.UNKNOWN));
pos = null;
}
}
});
} }
@Override @Override
@ -66,4 +42,47 @@ public class NaturalPlayer extends Player {
public void cancelMove() { public void cancelMove() {
moveRequested = false; moveRequested = false;
} }
@Override
public void disconnect() {
overlayComponent.removeMouseListener(this);
}
@Override
public void mousePressed(MouseEvent evt) {
if (!moveRequested) return;
if (pos == null) {
pos = new Position(evt.getPoint().x / overlayComponent.getTileSize(),
evt.getPoint().y / overlayComponent.getTileSize());
Board board = (Board) NaturalPlayer.this.board.clone();
if (board.get(pos) != null && board.get(pos).getColor() == color) {
List<Position> positions = board.getMoves(pos)
.stream()
.map(move -> move.dest)
.collect(Collectors.toList());
overlayComponent.displayDots(positions);
} else pos = null;
} else {
Position dest = new Position(evt.getPoint().x / overlayComponent.getTileSize(),
evt.getPoint().y / overlayComponent.getTileSize());
overlayComponent.clearDots();
moveRequested = false;
game.onMove(NaturalPlayer.this, new Move(pos, dest, Type.UNKNOWN));
pos = null;
}
}
@Override
public void mouseClicked(MouseEvent e) {}
@Override
public void mouseReleased(MouseEvent e) {}
@Override
public void mouseEntered(MouseEvent e) {}
@Override
public void mouseExited(MouseEvent e) {}
} }

View File

@ -23,6 +23,8 @@ public abstract class Player {
public abstract void cancelMove(); public abstract void cancelMove();
public abstract void disconnect();
public Game getGame() { return game; } public Game getGame() { return game; }
public void setGame(Game game) { public void setGame(Game game) {

View File

@ -98,4 +98,7 @@ public class AIPlayer extends Player {
} }
} }
} }
@Override
public void disconnect() {}
} }

View File

@ -16,12 +16,12 @@ public class MenuBar extends JMenuBar {
private static final long serialVersionUID = -7221583703531248228L; private static final long serialVersionUID = -7221583703531248228L;
private final MainWindow mainWindow; private final MainWindow mainWindow;
private final BoardPane boardPane; private final BoardPane boardPane;
public MenuBar(MainWindow mainWindow) { public MenuBar(MainWindow mainWindow) {
this.mainWindow = mainWindow; this.mainWindow = mainWindow;
boardPane = mainWindow.getBoardPane(); boardPane = mainWindow.getBoardPane();
initGameMenu(); initGameMenu();
} }