Improve mouse support (untested)

This commit is contained in:
Quentin Legot 2021-04-27 14:50:47 +02:00
parent c8d2d5ae25
commit 3d11240037
6 changed files with 56 additions and 27 deletions

View File

@ -9,6 +9,9 @@ import java.awt.event.MouseListener;
public class WindowMouseListener implements MouseListener { public class WindowMouseListener implements MouseListener {
private final Window window; private final Window window;
public boolean requestInput = false;
public Pair<Integer, Integer> lastInput = null;
public int playerIdLastInput = 0;
public WindowMouseListener(Window view) { public WindowMouseListener(Window view) {
this.window = view; this.window = view;
@ -16,6 +19,7 @@ public class WindowMouseListener implements MouseListener {
@Override @Override
public void mouseClicked(MouseEvent e) { public void mouseClicked(MouseEvent e) {
if(requestInput) {
int x = e.getX() - 7; int x = e.getX() - 7;
int y = e.getY() - 30; int y = e.getY() - 30;
int initialHeight = window.height / 12; int initialHeight = window.height / 12;
@ -24,17 +28,15 @@ public class WindowMouseListener implements MouseListener {
y -= initialHeight * 2; y -= initialHeight * 2;
if(x >= initialWidth && x <= initialWidth * 11) { if(x >= initialWidth && x <= initialWidth * 11) {
x -= initialWidth; x -= initialWidth;
System.out.println("Player 1"); lastInput = new Pair<>(y / initialHeight, x / initialWidth);
Pair<Integer, Integer> location = new Pair<>(y / initialHeight, x / initialWidth); playerIdLastInput = 1;
System.out.println(location);
} else if(x >= initialHeight * 13 && x <= window.width) { } else if(x >= initialHeight * 13 && x <= window.width) {
x -= initialWidth * 13; x -= initialWidth * 13;
System.out.println("Player 2"); lastInput = new Pair<>(y / initialHeight, x / initialWidth);
Pair<Integer, Integer> location = new Pair<>(y / initialHeight, x / initialWidth); playerIdLastInput = 2;
System.out.println("location: " + location); }
} }
} }
System.out.println("(" + x + ", " + y + ")");
} }
@Override @Override

View File

@ -65,8 +65,14 @@ public class Game {
} }
public void Play(AbstractView view){ public void Play(AbstractView view){
try {
view.setShips(players[0]); view.setShips(players[0]);
view.setShips(players[1]); view.setShips(players[1]);
} catch (InterruptedException e) {
System.out.println("Une erreur est survenue");
e.printStackTrace();
System.exit(1);
}
Player winner = null; Player winner = null;
while(winner == null) { while(winner == null) {
System.out.println("Au tour du joueur " + currentPlayer.getId()); System.out.println("Au tour du joueur " + currentPlayer.getId());

View File

@ -71,7 +71,7 @@ public abstract class AbstractView implements View {
} }
@Override @Override
public void setShips(Player player) { public void setShips(Player player) throws InterruptedException {
player.placeShips(); player.placeShips();
} }

View File

@ -18,7 +18,7 @@ public class Terminal extends AbstractView {
} }
@Override @Override
public void setShips(Player player) { public void setShips(Player player) throws InterruptedException {
System.out.println("Joueur " + player.getId() + ", placez vos navires"); System.out.println("Joueur " + player.getId() + ", placez vos navires");
int x, y; int x, y;
String dir; String dir;
@ -61,7 +61,7 @@ public class Terminal extends AbstractView {
@Override @Override
public void displayBoard() { public void displayBoard() {
System.out.println(toString()); System.out.println(this);
} }
@Override @Override

View File

@ -7,7 +7,7 @@ public interface View {
int[] shipsSize = { 5, 4, 3, 3, 2}; int[] shipsSize = { 5, 4, 3, 3, 2};
void setShips(Player player); void setShips(Player player) throws InterruptedException;
void displayBoard(); void displayBoard();

View File

@ -16,6 +16,7 @@ public class Window extends AbstractView {
public final int height = 600; public final int height = 600;
public final int width = 1200; public final int width = 1200;
private final WindowMouseListener mouseComponent;
String upperText = ""; String upperText = "";
public Window(Game game) { public Window(Game game) {
@ -26,16 +27,20 @@ public class Window extends AbstractView {
frame.setContentPane(new Draw(this)); frame.setContentPane(new Draw(this));
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setVisible(true); frame.setVisible(true);
frame.addMouseListener(new WindowMouseListener(this)); this.mouseComponent = new WindowMouseListener(this);
frame.addMouseListener(mouseComponent);
} }
@Override @Override
public void setShips(Player player) { public void setShips(Player player) throws InterruptedException {
if(player instanceof Human) { if(player instanceof Human) {
for(int i : shipsSize) { for(int i : shipsSize) {
upperText = "joueur " + player.getId() + ", Placez votre premier navire de taille " + i + " à l'aide de la souris"; upperText = "joueur " + player.getId() + ", Placez votre premier navire de taille " + i + " à l'aide de la souris";
Pair<Integer, Integer> coords = waitingForMouseInput(player);
upperText = "joueur " + player.getId() + ", Choisissez la direction de votre navire avec le clavier\n" +
"H, B, G, D pour respectivement Haut, Bas, Gauche, Droite";
// TODO: 27/04/2021 implementer clavier
} }
} else { } else {
@ -43,6 +48,22 @@ public class Window extends AbstractView {
} }
} }
private Pair<Integer, Integer> waitingForMouseInput(Player player) throws InterruptedException {
mouseComponent.requestInput = true;
while(true) {
Thread.sleep(33);
if(mouseComponent.playerIdLastInput != 0) {
if(player.getId() == mouseComponent.playerIdLastInput) {
return mouseComponent.lastInput;
} else {
JOptionPane.showMessageDialog(frame, "Vous avez cliquer sur une zone de jeu qui n'est pas la votre");
mouseComponent.playerIdLastInput = 0;
}
}
}
}
@Override @Override
public void displayBoard() { public void displayBoard() {
frame.paintComponents(frame.getGraphics()); frame.paintComponents(frame.getGraphics());