Files
SAE31_2024/TestV1/TestEnAttendantResolutionBug/src/view/GameView.java

150 lines
5.0 KiB
Java
Raw Normal View History

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;
import model.TileGenerator;
public class GameView extends JFrame {
private final Board board;
private final TileGenerator tileGenerator;
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, long seed) {
this.board = board;
this.tileGenerator = new TileGenerator(seed); // Initialisation avec la seed
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)) {
Tile newTile = tileGenerator.generateRandomTile();
board.addTile(hexPosition, newTile);
repaint();
}
}
@Override
public void paint(Graphics g) {
super.paint(g);
for (Point position : board.getTiles().keySet()) {
Tile tile = board.getTile(position);
int x = calculateScreenX(position);
int y = calculateScreenY(position);
drawLargeHexagon(g, x, y, tile);
}
}
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());
// 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
);
// 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
);
}
// Contour de l'hexagone
g.setColor(Color.BLACK);
g.drawPolygon(xPoints, yPoints, 6);
}
// 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
}
// 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
}
}
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);
}
private int calculateScreenX(Point position) {
int col = position.x;
return col * (int) (1.5 * hexRadius); // Espacement horizontal
}
private int calculateScreenY(Point position) {
int col = position.x;
int row = position.y;
return row * (int) (Math.sqrt(3) * hexRadius) + (col % 2) * (int) (Math.sqrt(3) / 2 * hexRadius);
// Décalage vertical pour les colonnes impaires
}
}