2024-11-17 12:00:36 +01:00
|
|
|
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;
|
2024-11-17 20:25:32 +01:00
|
|
|
import model.TileGenerator;
|
2024-11-17 12:00:36 +01:00
|
|
|
|
|
|
|
|
|
|
|
public class GameView extends JFrame {
|
|
|
|
private final Board board;
|
2024-11-17 20:25:32 +01:00
|
|
|
private final TileGenerator tileGenerator;
|
2024-11-17 12:00:36 +01:00
|
|
|
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-17 20:25:32 +01:00
|
|
|
public GameView(Board board, long seed) {
|
2024-11-17 12:00:36 +01:00
|
|
|
this.board = board;
|
2024-11-17 20:25:32 +01:00
|
|
|
this.tileGenerator = new TileGenerator(seed); // Initialisation avec la seed
|
2024-11-17 12:00:36 +01:00
|
|
|
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) {
|
|
|
|
Point hexPosition = calculateHexCoordinates(clickPoint);
|
|
|
|
|
|
|
|
if (!board.isPositionOccupied(hexPosition)) {
|
2024-11-17 20:25:32 +01:00
|
|
|
Tile newTile = tileGenerator.generateRandomTile();
|
2024-11-17 12:00:36 +01:00
|
|
|
board.addTile(hexPosition, newTile);
|
|
|
|
repaint();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void paint(Graphics g) {
|
|
|
|
super.paint(g);
|
2024-11-17 20:25:32 +01:00
|
|
|
|
2024-11-17 12:00:36 +01:00
|
|
|
for (Point position : board.getTiles().keySet()) {
|
|
|
|
Tile tile = board.getTile(position);
|
|
|
|
int x = calculateScreenX(position);
|
|
|
|
int y = calculateScreenY(position);
|
2024-11-17 20:25:32 +01:00
|
|
|
drawLargeHexagon(g, x, y, tile);
|
2024-11-17 12:00:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-17 20:25:32 +01:00
|
|
|
public void drawLargeHexagon(Graphics g, int x, int y, Tile tile) {
|
|
|
|
int r = hexRadius;
|
|
|
|
int h = (int) (Math.sqrt(3) / 2 * r);
|
|
|
|
|
|
|
|
// Coordonnées des sommets
|
|
|
|
int[] xPoints = {x + r, x + r / 2, x - r / 2, x - r, x - r / 2, x + r / 2};
|
|
|
|
int[] yPoints = {y, y + h, y + h, y, y - h, y - h};
|
|
|
|
|
|
|
|
if (tile.hasSingleTerrain()) {
|
|
|
|
// Une seule couleur : remplir tout l'hexagone
|
|
|
|
g.setColor(getColorForTerrain(tile.getTerrain1()));
|
|
|
|
g.fillPolygon(xPoints, yPoints, 6);
|
|
|
|
} else {
|
|
|
|
// Deux terrains : Diviser l'hexagone en deux moitiés
|
|
|
|
Color color1 = getColorForTerrain(tile.getTerrain1());
|
|
|
|
Color color2 = getColorForTerrain(tile.getTerrain2());
|
2024-11-17 12:00:36 +01:00
|
|
|
|
2024-11-17 20:25:32 +01:00
|
|
|
// Premier terrain (moitié supérieure)
|
|
|
|
g.setColor(color1);
|
|
|
|
g.fillPolygon(
|
|
|
|
new int[]{xPoints[0], xPoints[1], xPoints[2], xPoints[3]},
|
|
|
|
new int[]{yPoints[0], yPoints[1], yPoints[2], yPoints[3]},
|
|
|
|
4
|
|
|
|
);
|
2024-11-17 12:00:36 +01:00
|
|
|
|
2024-11-17 20:25:32 +01:00
|
|
|
// Deuxième terrain (moitié inférieure)
|
|
|
|
g.setColor(color2);
|
|
|
|
g.fillPolygon(
|
|
|
|
new int[]{xPoints[3], xPoints[4], xPoints[5], xPoints[0]},
|
|
|
|
new int[]{yPoints[3], yPoints[4], yPoints[5], yPoints[0]},
|
|
|
|
4
|
|
|
|
);
|
2024-11-17 12:00:36 +01:00
|
|
|
}
|
|
|
|
|
2024-11-17 20:25:32 +01:00
|
|
|
// Contour de l'hexagone
|
|
|
|
g.setColor(Color.BLACK);
|
|
|
|
g.drawPolygon(xPoints, yPoints, 6);
|
2024-11-17 12:00:36 +01:00
|
|
|
}
|
|
|
|
|
2024-11-17 20:25:32 +01:00
|
|
|
|
|
|
|
|
|
|
|
// Dans la classe GameView.java
|
|
|
|
private Color getColorForTerrain(Terrain terrain) {
|
|
|
|
if (terrain == null) {
|
|
|
|
return Color.GRAY; // Retourner une couleur par défaut si le terrain est null
|
2024-11-17 12:00:36 +01:00
|
|
|
}
|
|
|
|
|
2024-11-17 20:25:32 +01:00
|
|
|
// Mappage des couleurs pour chaque terrain
|
|
|
|
switch (terrain) {
|
|
|
|
case MER:
|
|
|
|
return new Color(0, 0, 255); // Bleu pour MER
|
|
|
|
case CHAMP:
|
|
|
|
return new Color(0, 255, 0); // Vert pour CHAMP
|
|
|
|
case FORET:
|
|
|
|
return new Color(34, 139, 34); // Vert foncé pour FORET
|
|
|
|
case PRE:
|
|
|
|
return new Color(255, 255, 0); // Jaune pour PRE
|
|
|
|
case MONTAGNE:
|
|
|
|
return new Color(139, 69, 19); // Marron pour MONTAGNE
|
|
|
|
default:
|
|
|
|
return Color.GRAY; // Couleur par défaut pour les cas non gérés
|
2024-11-17 12:00:36 +01:00
|
|
|
}
|
2024-11-17 20:25:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private Point calculateHexCoordinates(Point clickPoint) {
|
|
|
|
// Rayon et espacement vertical/hexagonal
|
|
|
|
int r = hexRadius;
|
|
|
|
double h = Math.sqrt(3) * r / 2; // Hauteur effective entre deux lignes
|
|
|
|
|
|
|
|
// Calcul approximatif de la colonne et de la ligne
|
|
|
|
int col = (int) Math.round((double) clickPoint.x / (1.5 * r));
|
|
|
|
int row = (int) Math.round((double) (clickPoint.y - (col % 2) * h) / (Math.sqrt(3) * r));
|
|
|
|
|
|
|
|
return new Point(col, row);
|
|
|
|
}
|
|
|
|
|
2024-11-17 12:00:36 +01:00
|
|
|
|
|
|
|
private int calculateScreenX(Point position) {
|
|
|
|
int col = position.x;
|
2024-11-17 20:25:32 +01:00
|
|
|
return col * (int) (1.5 * hexRadius); // Espacement horizontal
|
2024-11-17 12:00:36 +01:00
|
|
|
}
|
2024-11-17 20:25:32 +01:00
|
|
|
|
2024-11-17 12:00:36 +01:00
|
|
|
private int calculateScreenY(Point position) {
|
|
|
|
int col = position.x;
|
|
|
|
int row = position.y;
|
2024-11-17 20:25:32 +01:00
|
|
|
return row * (int) (Math.sqrt(3) * hexRadius) + (col % 2) * (int) (Math.sqrt(3) / 2 * hexRadius);
|
|
|
|
// Décalage vertical pour les colonnes impaires
|
2024-11-17 12:00:36 +01:00
|
|
|
}
|
2024-11-17 20:25:32 +01:00
|
|
|
|
2024-11-17 12:00:36 +01:00
|
|
|
}
|