124 lines
4.4 KiB
Java
124 lines
4.4 KiB
Java
|
package view;
|
||
|
|
||
|
import javax.swing.*;
|
||
|
import java.awt.*;
|
||
|
import java.awt.event.MouseAdapter;
|
||
|
import java.awt.event.MouseEvent;
|
||
|
import model.Board;
|
||
|
import model.Tile;
|
||
|
import model.Terrain;
|
||
|
|
||
|
|
||
|
public class GameView extends JFrame {
|
||
|
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
|
||
|
|
||
|
public GameView(Board board) {
|
||
|
this.board = board;
|
||
|
setTitle("Dorfromantik - Plateau");
|
||
|
setSize(800, 800);
|
||
|
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||
|
setLocationRelativeTo(null);
|
||
|
|
||
|
addMouseListener(new MouseAdapter() {
|
||
|
@Override
|
||
|
public void mouseClicked(MouseEvent e) {
|
||
|
handleMouseClick(e.getPoint());
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
|
||
|
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();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void paint(Graphics g) {
|
||
|
super.paint(g);
|
||
|
Graphics2D g2d = (Graphics2D) g;
|
||
|
|
||
|
// Dessiner toutes les tuiles placées
|
||
|
for (Point position : board.getTiles().keySet()) {
|
||
|
Tile tile = board.getTile(position);
|
||
|
int x = calculateScreenX(position);
|
||
|
int y = calculateScreenY(position);
|
||
|
drawLargeHexagon(g2d, x, y, tile);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private void drawLargeHexagon(Graphics2D g2d, int x, int y, Tile tile) {
|
||
|
Polygon hexagon = new Polygon();
|
||
|
Point center = new Point(x, y); // Centre de l'hexagone
|
||
|
|
||
|
// Calculer les points des sommets de l'hexagone
|
||
|
for (int i = 0; i < 6; i++) {
|
||
|
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);
|
||
|
}
|
||
|
|
||
|
// Dessiner l'intérieur de l'hexagone en divisant en triangles
|
||
|
for (int i = 0; i < 6; i++) {
|
||
|
// Trouver les coordonnées des points du côté de l'hexagone
|
||
|
int x1 = hexagon.xpoints[i];
|
||
|
int y1 = hexagon.ypoints[i];
|
||
|
int x2 = hexagon.xpoints[(i + 1) % 6]; // Le point suivant (modulo 6)
|
||
|
int y2 = hexagon.ypoints[(i + 1) % 6];
|
||
|
|
||
|
// Dessiner un triangle avec le centre de l'hexagone
|
||
|
Polygon triangle = new Polygon();
|
||
|
triangle.addPoint(center.x, center.y); // Ajouter le centre
|
||
|
triangle.addPoint(x1, y1); // Ajouter le premier sommet
|
||
|
triangle.addPoint(x2, y2); // Ajouter le deuxième sommet
|
||
|
|
||
|
g2d.setColor(getColorForTerrain(tile.getTerrain()));
|
||
|
g2d.fillPolygon(triangle); // Remplir le triangle
|
||
|
g2d.setColor(Color.BLACK);
|
||
|
g2d.drawPolygon(triangle); // Dessiner les contours du triangle
|
||
|
}
|
||
|
|
||
|
// Dessiner l'extérieur de l'hexagone
|
||
|
g2d.setColor(Color.BLACK);
|
||
|
g2d.drawPolygon(hexagon);
|
||
|
}
|
||
|
|
||
|
|
||
|
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;
|
||
|
};
|
||
|
}
|
||
|
|
||
|
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);
|
||
|
}
|
||
|
|
||
|
private int calculateScreenX(Point position) {
|
||
|
int col = position.x;
|
||
|
return (int) (col * 1.5 * hexRadius + hexWidth / 2.0);
|
||
|
}
|
||
|
|
||
|
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);
|
||
|
}
|
||
|
}
|