possibilité d'ajouté des tuiles sur le plateau mais problème de position des tuiles
This commit is contained in:
33
TestV1/TestEnAttendantResolutionBug/Board.java
Normal file
33
TestV1/TestEnAttendantResolutionBug/Board.java
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
import java.awt.*;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public class Board {
|
||||||
|
private Map<Point, Tile> tiles;
|
||||||
|
|
||||||
|
public Board() {
|
||||||
|
tiles = new HashMap<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Vérifie si la position est déjà occupée
|
||||||
|
public boolean isPositionOccupied(Point position) {
|
||||||
|
return tiles.containsKey(position);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ajoute une tuile à une position donnée
|
||||||
|
public void addTile(Point position, Tile tile) {
|
||||||
|
if (!isPositionOccupied(position)) {
|
||||||
|
tiles.put(position, tile);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Récupère la tuile à une position donnée
|
||||||
|
public Tile getTile(Point position) {
|
||||||
|
return tiles.get(position);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Récupère toutes les tuiles
|
||||||
|
public Map<Point, Tile> getTiles() {
|
||||||
|
return tiles;
|
||||||
|
}
|
||||||
|
}
|
@@ -6,6 +6,6 @@ public class GameController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void startGame() {
|
public void startGame() {
|
||||||
view.showTile();
|
view.showBoard();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -1,19 +1,33 @@
|
|||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
|
import java.awt.event.MouseAdapter;
|
||||||
|
import java.awt.event.MouseEvent;
|
||||||
|
|
||||||
public class GameView extends JFrame {
|
public class GameView extends JFrame {
|
||||||
private Tile tile;
|
private Board board;
|
||||||
|
private int hexRadius = 30; // Rayon du grand hexagone
|
||||||
|
|
||||||
// Constructeur
|
// Dimensions calculées des hexagones
|
||||||
public GameView(Tile tile) {
|
private int hexWidth = (int) (1.5 * hexRadius); // Largeur de l'hexagone
|
||||||
this.tile = tile;
|
private int hexHeight = (int) (Math.sqrt(3) * hexRadius); // Hauteur de l'hexagone
|
||||||
setTitle("Dorfromantik - Affichage de tuile");
|
|
||||||
setSize(600, 600);
|
public GameView(Board board) {
|
||||||
|
this.board = board;
|
||||||
|
setTitle("Dorfromantik - Plateau");
|
||||||
|
setSize(800, 800);
|
||||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
setLocationRelativeTo(null);
|
setLocationRelativeTo(null);
|
||||||
|
|
||||||
|
// Gestion des clics de souris pour placer une tuile
|
||||||
|
addMouseListener(new MouseAdapter() {
|
||||||
|
@Override
|
||||||
|
public void mouseClicked(MouseEvent e) {
|
||||||
|
handleMouseClick(e.getX(), e.getY());
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public void showTile() {
|
public void showBoard() {
|
||||||
setVisible(true);
|
setVisible(true);
|
||||||
repaint();
|
repaint();
|
||||||
}
|
}
|
||||||
@@ -23,40 +37,49 @@ public class GameView extends JFrame {
|
|||||||
super.paint(g);
|
super.paint(g);
|
||||||
Graphics2D g2d = (Graphics2D) g;
|
Graphics2D g2d = (Graphics2D) g;
|
||||||
|
|
||||||
int hexRadius = 50; // Rayon des hexagones
|
// Dessiner toutes les tuiles du plateau
|
||||||
int hexHeight = (int) (Math.sqrt(3) * hexRadius); // Hauteur de l'hexagone
|
for (Point position : board.getTiles().keySet()) {
|
||||||
int xOffset = 150, yOffset = 150; // Décalage initial pour centrer
|
Tile tile = board.getTile(position);
|
||||||
|
drawLargeHexagon(g2d, position.x, position.y, tile);
|
||||||
Terrain[][] matrix = tile.getHexMatrix();
|
|
||||||
|
|
||||||
// Dessiner les hexagones en fonction de la matrice
|
|
||||||
for (int i = 0; i < matrix.length; i++) {
|
|
||||||
for (int j = 0; j < matrix[i].length; j++) {
|
|
||||||
if (matrix[i][j] != Terrain.VIDE) {
|
|
||||||
// Calcul des coordonnées
|
|
||||||
int x = xOffset + j * (3 * hexRadius / 2); // Décalage horizontal
|
|
||||||
int y = yOffset + i * hexHeight + (j % 2) * (hexHeight / 2); // Décalage vertical
|
|
||||||
|
|
||||||
drawHexagon(g2d, x, y, hexRadius, getColorForTerrain(matrix[i][j]));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void drawHexagon(Graphics2D g2d, int x, int y, int radius, Color color) {
|
private void drawLargeHexagon(Graphics2D g2d, int centerX, int centerY, Tile tile) {
|
||||||
int[] xPoints = new int[6];
|
// Couleurs des triangles
|
||||||
int[] yPoints = new int[6];
|
Color color1 = getColorForTerrain(tile.getTerrain());
|
||||||
|
Color color2 = color1.darker(); // Variante plus sombre pour contraste
|
||||||
|
|
||||||
|
// Dessiner les 6 triangles pour former l'hexagone
|
||||||
for (int i = 0; i < 6; i++) {
|
for (int i = 0; i < 6; i++) {
|
||||||
xPoints[i] = x + (int) (radius * Math.cos(i * Math.PI / 3));
|
// Dessiner chaque triangle équilatéral avec sa couleur associée
|
||||||
yPoints[i] = y + (int) (radius * Math.sin(i * Math.PI / 3));
|
drawTriangle(g2d, centerX, centerY, i, hexRadius, i % 2 == 0 ? color1 : color2);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
g2d.setColor(color);
|
private void drawTriangle(Graphics2D g2d, int centerX, int centerY, int triangleIndex, int radius, Color color) {
|
||||||
g2d.fillPolygon(xPoints, yPoints, 6);
|
// Orientation de chaque triangle (décalage de 60° par rapport à chaque triangle)
|
||||||
|
double angle = Math.toRadians(60 * triangleIndex);
|
||||||
|
|
||||||
g2d.setColor(Color.BLACK); // Contour noir
|
// Calcul des points de chaque triangle autour du centre du grand hexagone
|
||||||
g2d.drawPolygon(xPoints, yPoints, 6);
|
int x1 = centerX + (int) (Math.cos(angle) * radius);
|
||||||
|
int y1 = centerY + (int) (Math.sin(angle) * radius);
|
||||||
|
|
||||||
|
int x2 = centerX + (int) (Math.cos(angle + Math.toRadians(60)) * radius);
|
||||||
|
int y2 = centerY + (int) (Math.sin(angle + Math.toRadians(60)) * radius);
|
||||||
|
|
||||||
|
int x3 = centerX + (int) (Math.cos(angle + Math.toRadians(120)) * radius);
|
||||||
|
int y3 = centerY + (int) (Math.sin(angle + Math.toRadians(120)) * radius);
|
||||||
|
|
||||||
|
// Créer un polygone pour dessiner le triangle
|
||||||
|
Polygon triangle = new Polygon();
|
||||||
|
triangle.addPoint(centerX, centerY); // Centre de l'hexagone
|
||||||
|
triangle.addPoint(x1, y1); // Premier sommet
|
||||||
|
triangle.addPoint(x2, y2); // Deuxième sommet
|
||||||
|
triangle.addPoint(x3, y3); // Troisième sommet
|
||||||
|
|
||||||
|
// Remplir le triangle avec la couleur
|
||||||
|
g2d.setColor(color);
|
||||||
|
g2d.fillPolygon(triangle);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Color getColorForTerrain(Terrain terrain) {
|
private Color getColorForTerrain(Terrain terrain) {
|
||||||
@@ -75,4 +98,38 @@ public class GameView extends JFrame {
|
|||||||
return Color.WHITE;
|
return Color.WHITE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void handleMouseClick(int mouseX, int mouseY) {
|
||||||
|
// Calcul des coordonnées de la grille de manière à respecter la structure hexagonale
|
||||||
|
|
||||||
|
// Calcul de la colonne et de la ligne en fonction de la taille des hexagones
|
||||||
|
int gridX = (int) (mouseX / (hexWidth * 0.75)); // Calcul de la colonne
|
||||||
|
|
||||||
|
// Calcul de la ligne en fonction de la hauteur des hexagones
|
||||||
|
int gridY = (int) (mouseY / hexHeight); // Calcul de la ligne
|
||||||
|
|
||||||
|
// Détecter si c'est une rangée paire ou impaire et appliquer un décalage
|
||||||
|
if (gridY % 2 != 0) { // Si la ligne est impaire
|
||||||
|
gridX += 1; // Décalage horizontal pour les rangées impaires
|
||||||
|
}
|
||||||
|
|
||||||
|
// Calculer les coordonnées X et Y en pixels
|
||||||
|
int finalX = (int) (gridX * hexWidth * 0.75); // Calcul des coordonnées X
|
||||||
|
int finalY = gridY * hexHeight; // Calcul des coordonnées Y
|
||||||
|
|
||||||
|
// Créer un objet Point avec les coordonnées finales
|
||||||
|
Point position = new Point(finalX, finalY);
|
||||||
|
|
||||||
|
// Vérifier si la position est déjà occupée par une autre tuile
|
||||||
|
if (!board.isPositionOccupied(position)) {
|
||||||
|
// Créer une nouvelle tuile (ici, une tuile de forêt)
|
||||||
|
Tile newTile = new Tile(Terrain.FORET);
|
||||||
|
|
||||||
|
// Ajouter la tuile sur le plateau
|
||||||
|
board.addTile(position, newTile);
|
||||||
|
|
||||||
|
// Rafraîchir l'affichage
|
||||||
|
repaint();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
@@ -1,10 +1,10 @@
|
|||||||
public class Main {
|
public class Main {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
// Exemple : une tuile avec deux terrains
|
// Création d'un plateau
|
||||||
Tile tile = new Tile(Terrain.MER, Terrain.FORET, 1);
|
Board board = new Board();
|
||||||
|
|
||||||
// Vue
|
// Vue avec le plateau
|
||||||
GameView view = new GameView(tile);
|
GameView view = new GameView(board);
|
||||||
|
|
||||||
// Contrôleur
|
// Contrôleur
|
||||||
GameController controller = new GameController(view);
|
GameController controller = new GameController(view);
|
||||||
|
@@ -1,48 +1,11 @@
|
|||||||
public class Tile {
|
public class Tile {
|
||||||
private Terrain[][] hexMatrix;
|
private Terrain terrain; // Un seul terrain pour toute la tuile
|
||||||
|
|
||||||
// Constructeur pour une tuile avec un seul terrain
|
|
||||||
public Tile(Terrain terrain) {
|
public Tile(Terrain terrain) {
|
||||||
hexMatrix = new Terrain[3][3];
|
this.terrain = terrain;
|
||||||
for (int i = 0; i < 3; i++) {
|
|
||||||
for (int j = 0; j < 3; j++) {
|
|
||||||
hexMatrix[i][j] = terrain;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Constructeur pour une tuile avec deux terrains
|
public Terrain getTerrain() {
|
||||||
public Tile(Terrain terrain1, Terrain terrain2, int configuration) {
|
return terrain;
|
||||||
hexMatrix = new Terrain[3][3];
|
|
||||||
switch (configuration) {
|
|
||||||
case 1:
|
|
||||||
fillSides(terrain1, terrain2, 1, 5);
|
|
||||||
break;
|
|
||||||
case 2:
|
|
||||||
fillSides(terrain1, terrain2, 2, 4);
|
|
||||||
break;
|
|
||||||
case 3:
|
|
||||||
fillSides(terrain1, terrain2, 3, 3);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
throw new IllegalArgumentException("Configuration invalide");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void fillSides(Terrain terrain1, Terrain terrain2, int side1, int side2) {
|
|
||||||
// Exemple simplifié
|
|
||||||
for (int i = 0; i < 3; i++) {
|
|
||||||
for (int j = 0; j < 3; j++) {
|
|
||||||
if ((i + j) % 2 == 0) { // Terrain1 sur certaines cases
|
|
||||||
hexMatrix[i][j] = terrain1;
|
|
||||||
} else {
|
|
||||||
hexMatrix[i][j] = terrain2;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public Terrain[][] getHexMatrix() {
|
|
||||||
return hexMatrix;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user