2024-11-16 10:12:24 +01:00
|
|
|
import javax.swing.*;
|
|
|
|
import java.awt.*;
|
2024-11-16 14:12:55 +01:00
|
|
|
import java.awt.event.MouseAdapter;
|
|
|
|
import java.awt.event.MouseEvent;
|
2024-11-16 10:12:24 +01:00
|
|
|
|
|
|
|
public class GameView extends JFrame {
|
2024-11-16 17:28:09 +01:00
|
|
|
private final Board board;
|
|
|
|
private final int hexRadius = 60;
|
|
|
|
private final int hexWidth = (int) (Math.sqrt(3) * hexRadius); // Largeur d'un hexagone
|
|
|
|
private final int hexHeight = 2 * hexRadius; // Hauteur d'un hexagone
|
2024-11-16 14:12:55 +01:00
|
|
|
|
|
|
|
public GameView(Board board) {
|
|
|
|
this.board = board;
|
|
|
|
setTitle("Dorfromantik - Plateau");
|
|
|
|
setSize(800, 800);
|
2024-11-16 10:12:24 +01:00
|
|
|
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
|
|
|
setLocationRelativeTo(null);
|
2024-11-16 14:12:55 +01:00
|
|
|
|
|
|
|
addMouseListener(new MouseAdapter() {
|
|
|
|
@Override
|
|
|
|
public void mouseClicked(MouseEvent e) {
|
2024-11-16 17:28:09 +01:00
|
|
|
handleMouseClick(e.getPoint());
|
2024-11-16 14:12:55 +01:00
|
|
|
}
|
|
|
|
});
|
2024-11-16 10:12:24 +01:00
|
|
|
}
|
|
|
|
|
2024-11-16 17:28:09 +01:00
|
|
|
private void handleMouseClick(Point clickPoint) {
|
|
|
|
// Calcul de la position en coordonnées hexagonales
|
|
|
|
Point hexPosition = calculateHexCoordinates(clickPoint);
|
|
|
|
|
|
|
|
// Vérifie si la position est libre
|
|
|
|
if (!board.isPositionOccupied(hexPosition)) {
|
|
|
|
Tile newTile = new Tile(Terrain.FORET); // Exemple : tuile de forêt
|
|
|
|
board.addTile(hexPosition, newTile);
|
|
|
|
repaint();
|
|
|
|
}
|
2024-11-16 10:12:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void paint(Graphics g) {
|
|
|
|
super.paint(g);
|
|
|
|
Graphics2D g2d = (Graphics2D) g;
|
|
|
|
|
2024-11-16 17:28:09 +01:00
|
|
|
// Dessiner toutes les tuiles placées
|
2024-11-16 14:12:55 +01:00
|
|
|
for (Point position : board.getTiles().keySet()) {
|
|
|
|
Tile tile = board.getTile(position);
|
2024-11-16 17:28:09 +01:00
|
|
|
int x = calculateScreenX(position);
|
|
|
|
int y = calculateScreenY(position);
|
|
|
|
drawLargeHexagon(g2d, x, y, tile);
|
2024-11-16 10:12:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-16 17:28:09 +01:00
|
|
|
private void drawLargeHexagon(Graphics2D g2d, int x, int y, Tile tile) {
|
|
|
|
Polygon hexagon = new Polygon();
|
2024-11-16 10:12:24 +01:00
|
|
|
for (int i = 0; i < 6; i++) {
|
2024-11-16 17:28:09 +01:00
|
|
|
double angle = Math.toRadians(60 * i);
|
|
|
|
int px = x + (int) (Math.cos(angle) * hexRadius);
|
|
|
|
int py = y + (int) (Math.sin(angle) * hexRadius);
|
|
|
|
hexagon.addPoint(px, py);
|
2024-11-16 10:12:24 +01:00
|
|
|
}
|
2024-11-16 14:12:55 +01:00
|
|
|
|
2024-11-16 17:28:09 +01:00
|
|
|
g2d.setColor(getColorForTerrain(tile.getTerrain()));
|
|
|
|
g2d.fillPolygon(hexagon);
|
|
|
|
g2d.setColor(Color.BLACK);
|
|
|
|
g2d.drawPolygon(hexagon);
|
|
|
|
}
|
2024-11-16 14:12:55 +01:00
|
|
|
|
2024-11-16 17:28:09 +01:00
|
|
|
private Color getColorForTerrain(Terrain terrain) {
|
|
|
|
return switch (terrain) {
|
|
|
|
case MER -> Color.BLUE;
|
|
|
|
case CHAMP -> Color.YELLOW;
|
|
|
|
case FORET -> new Color(34, 139, 34);
|
|
|
|
case PRE -> Color.GREEN;
|
|
|
|
case MONTAGNE -> Color.GRAY;
|
|
|
|
};
|
|
|
|
}
|
2024-11-16 14:12:55 +01:00
|
|
|
|
2024-11-16 17:28:09 +01:00
|
|
|
private Point calculateHexCoordinates(Point clickPoint) {
|
|
|
|
int col = (int) Math.round((clickPoint.x - hexWidth / 2.0) / (1.5 * hexRadius));
|
|
|
|
int row = (int) Math.round((clickPoint.y - hexHeight / 2.0 - (col % 2) * hexHeight / 4.0) / hexHeight);
|
|
|
|
return new Point(col, row);
|
2024-11-16 10:12:24 +01:00
|
|
|
}
|
|
|
|
|
2024-11-16 17:28:09 +01:00
|
|
|
private int calculateScreenX(Point position) {
|
|
|
|
int col = position.x;
|
|
|
|
return (int) (col * 1.5 * hexRadius + hexWidth / 2.0);
|
2024-11-16 10:12:24 +01:00
|
|
|
}
|
2024-11-16 14:12:55 +01:00
|
|
|
|
2024-11-16 17:28:09 +01:00
|
|
|
private int calculateScreenY(Point position) {
|
|
|
|
int col = position.x;
|
|
|
|
int row = position.y;
|
|
|
|
return (int) (row * hexHeight + (col % 2) * hexHeight / 2.0 + hexHeight / 2.0);
|
|
|
|
}
|
|
|
|
}
|