Improve mouse support (untested)
This commit is contained in:
parent
c8d2d5ae25
commit
3d11240037
@ -9,6 +9,9 @@ import java.awt.event.MouseListener;
|
||||
public class WindowMouseListener implements MouseListener {
|
||||
|
||||
private final Window window;
|
||||
public boolean requestInput = false;
|
||||
public Pair<Integer, Integer> lastInput = null;
|
||||
public int playerIdLastInput = 0;
|
||||
|
||||
public WindowMouseListener(Window view) {
|
||||
this.window = view;
|
||||
@ -16,25 +19,24 @@ public class WindowMouseListener implements MouseListener {
|
||||
|
||||
@Override
|
||||
public void mouseClicked(MouseEvent e) {
|
||||
int x = e.getX() - 7;
|
||||
int y = e.getY() - 30;
|
||||
int initialHeight = window.height / 12;
|
||||
int initialWidth = window.width / 23;
|
||||
if(y >= initialHeight * 2 && y <= window.height) {
|
||||
y -= initialHeight * 2;
|
||||
if(x >= initialWidth && x <= initialWidth * 11) {
|
||||
x -= initialWidth;
|
||||
System.out.println("Player 1");
|
||||
Pair<Integer, Integer> location = new Pair<>(y / initialHeight, x / initialWidth);
|
||||
System.out.println(location);
|
||||
} else if(x >= initialHeight * 13 && x <= window.width) {
|
||||
x -= initialWidth * 13;
|
||||
System.out.println("Player 2");
|
||||
Pair<Integer, Integer> location = new Pair<>(y / initialHeight, x / initialWidth);
|
||||
System.out.println("location: " + location);
|
||||
if(requestInput) {
|
||||
int x = e.getX() - 7;
|
||||
int y = e.getY() - 30;
|
||||
int initialHeight = window.height / 12;
|
||||
int initialWidth = window.width / 23;
|
||||
if(y >= initialHeight * 2 && y <= window.height) {
|
||||
y -= initialHeight * 2;
|
||||
if(x >= initialWidth && x <= initialWidth * 11) {
|
||||
x -= initialWidth;
|
||||
lastInput = new Pair<>(y / initialHeight, x / initialWidth);
|
||||
playerIdLastInput = 1;
|
||||
} else if(x >= initialHeight * 13 && x <= window.width) {
|
||||
x -= initialWidth * 13;
|
||||
lastInput = new Pair<>(y / initialHeight, x / initialWidth);
|
||||
playerIdLastInput = 2;
|
||||
}
|
||||
}
|
||||
}
|
||||
System.out.println("(" + x + ", " + y + ")");
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -65,8 +65,14 @@ public class Game {
|
||||
}
|
||||
|
||||
public void Play(AbstractView view){
|
||||
view.setShips(players[0]);
|
||||
view.setShips(players[1]);
|
||||
try {
|
||||
view.setShips(players[0]);
|
||||
view.setShips(players[1]);
|
||||
} catch (InterruptedException e) {
|
||||
System.out.println("Une erreur est survenue");
|
||||
e.printStackTrace();
|
||||
System.exit(1);
|
||||
}
|
||||
Player winner = null;
|
||||
while(winner == null) {
|
||||
System.out.println("Au tour du joueur " + currentPlayer.getId());
|
||||
|
@ -71,7 +71,7 @@ public abstract class AbstractView implements View {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setShips(Player player) {
|
||||
public void setShips(Player player) throws InterruptedException {
|
||||
player.placeShips();
|
||||
}
|
||||
|
||||
|
@ -18,7 +18,7 @@ public class Terminal extends AbstractView {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setShips(Player player) {
|
||||
public void setShips(Player player) throws InterruptedException {
|
||||
System.out.println("Joueur " + player.getId() + ", placez vos navires");
|
||||
int x, y;
|
||||
String dir;
|
||||
@ -61,7 +61,7 @@ public class Terminal extends AbstractView {
|
||||
|
||||
@Override
|
||||
public void displayBoard() {
|
||||
System.out.println(toString());
|
||||
System.out.println(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -7,7 +7,7 @@ public interface View {
|
||||
|
||||
int[] shipsSize = { 5, 4, 3, 3, 2};
|
||||
|
||||
void setShips(Player player);
|
||||
void setShips(Player player) throws InterruptedException;
|
||||
|
||||
void displayBoard();
|
||||
|
||||
|
@ -16,6 +16,7 @@ public class Window extends AbstractView {
|
||||
|
||||
public final int height = 600;
|
||||
public final int width = 1200;
|
||||
private final WindowMouseListener mouseComponent;
|
||||
String upperText = "";
|
||||
|
||||
public Window(Game game) {
|
||||
@ -26,16 +27,20 @@ public class Window extends AbstractView {
|
||||
frame.setContentPane(new Draw(this));
|
||||
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
|
||||
frame.setVisible(true);
|
||||
frame.addMouseListener(new WindowMouseListener(this));
|
||||
this.mouseComponent = new WindowMouseListener(this);
|
||||
frame.addMouseListener(mouseComponent);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setShips(Player player) {
|
||||
public void setShips(Player player) throws InterruptedException {
|
||||
if(player instanceof Human) {
|
||||
for(int i : shipsSize) {
|
||||
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 {
|
||||
@ -43,7 +48,23 @@ public class Window extends AbstractView {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
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
|
||||
public void displayBoard() {
|
||||
frame.paintComponents(frame.getGraphics());
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user