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:
parent
3c03f01962
commit
1323286c93
@ -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; }
|
||||||
}
|
}
|
||||||
|
@ -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,16 +18,35 @@ 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 final OverlayComponent overlayComponent;
|
||||||
|
|
||||||
private boolean moveRequested;
|
private boolean moveRequested;
|
||||||
|
private Position pos;
|
||||||
|
|
||||||
public NaturalPlayer(Color color, OverlayComponent overlayComponent) {
|
public NaturalPlayer(Color color, OverlayComponent overlayComponent) {
|
||||||
super(color);
|
super(color);
|
||||||
|
this.overlayComponent = overlayComponent;
|
||||||
moveRequested = false;
|
moveRequested = false;
|
||||||
overlayComponent.addMouseListener(new MouseAdapter() {
|
|
||||||
|
|
||||||
private Position pos;
|
overlayComponent.addMouseListener(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void requestMove() {
|
||||||
|
moveRequested = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void cancelMove() {
|
||||||
|
moveRequested = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void disconnect() {
|
||||||
|
overlayComponent.removeMouseListener(this);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void mousePressed(MouseEvent evt) {
|
public void mousePressed(MouseEvent evt) {
|
||||||
@ -54,16 +73,16 @@ public class NaturalPlayer extends Player {
|
|||||||
pos = null;
|
pos = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void requestMove() {
|
public void mouseClicked(MouseEvent e) {}
|
||||||
moveRequested = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void cancelMove() {
|
public void mouseReleased(MouseEvent e) {}
|
||||||
moveRequested = false;
|
|
||||||
}
|
@Override
|
||||||
|
public void mouseEntered(MouseEvent e) {}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void mouseExited(MouseEvent e) {}
|
||||||
}
|
}
|
||||||
|
@ -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) {
|
||||||
|
@ -98,4 +98,7 @@ public class AIPlayer extends Player {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void disconnect() {}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user