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 {
|
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,25 +19,24 @@ public class WindowMouseListener implements MouseListener {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void mouseClicked(MouseEvent e) {
|
public void mouseClicked(MouseEvent e) {
|
||||||
int x = e.getX() - 7;
|
if(requestInput) {
|
||||||
int y = e.getY() - 30;
|
int x = e.getX() - 7;
|
||||||
int initialHeight = window.height / 12;
|
int y = e.getY() - 30;
|
||||||
int initialWidth = window.width / 23;
|
int initialHeight = window.height / 12;
|
||||||
if(y >= initialHeight * 2 && y <= window.height) {
|
int initialWidth = window.width / 23;
|
||||||
y -= initialHeight * 2;
|
if(y >= initialHeight * 2 && y <= window.height) {
|
||||||
if(x >= initialWidth && x <= initialWidth * 11) {
|
y -= initialHeight * 2;
|
||||||
x -= initialWidth;
|
if(x >= initialWidth && x <= initialWidth * 11) {
|
||||||
System.out.println("Player 1");
|
x -= initialWidth;
|
||||||
Pair<Integer, Integer> location = new Pair<>(y / initialHeight, x / initialWidth);
|
lastInput = new Pair<>(y / initialHeight, x / initialWidth);
|
||||||
System.out.println(location);
|
playerIdLastInput = 1;
|
||||||
} 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
|
||||||
|
@ -65,8 +65,14 @@ public class Game {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void Play(AbstractView view){
|
public void Play(AbstractView view){
|
||||||
view.setShips(players[0]);
|
try {
|
||||||
view.setShips(players[1]);
|
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;
|
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());
|
||||||
|
@ -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();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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
|
||||||
|
@ -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();
|
||||||
|
|
||||||
|
@ -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,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() {
|
public void displayBoard() {
|
||||||
frame.paintComponents(frame.getGraphics());
|
frame.paintComponents(frame.getGraphics());
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user