From 16103baba12bcc64432e92296387f5d3f004f077 Mon Sep 17 00:00:00 2001 From: Lenny FOULOU Date: Fri, 6 Dec 2024 22:38:09 +0100 Subject: [PATCH] =?UTF-8?q?Am=C3=A9lioration=20du=20code?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/fr/monkhanny/dorfromantik/game/Tile.java | 107 ++++++------------ .../dorfromantik/utils/HexagonDrawer.java | 51 +++++++++ 2 files changed, 87 insertions(+), 71 deletions(-) create mode 100644 src/fr/monkhanny/dorfromantik/utils/HexagonDrawer.java diff --git a/src/fr/monkhanny/dorfromantik/game/Tile.java b/src/fr/monkhanny/dorfromantik/game/Tile.java index bba0a14..d922648 100644 --- a/src/fr/monkhanny/dorfromantik/game/Tile.java +++ b/src/fr/monkhanny/dorfromantik/game/Tile.java @@ -12,6 +12,7 @@ import java.util.Arrays; import fr.monkhanny.dorfromantik.enums.Biome; import fr.monkhanny.dorfromantik.enums.TileOrientation; import fr.monkhanny.dorfromantik.utils.Hexagon; +import fr.monkhanny.dorfromantik.utils.HexagonDrawer; public class Tile extends Cell { @@ -78,7 +79,7 @@ public class Tile extends Cell { } - private Biome getDominantBiome() { + public Biome getDominantBiome() { TileOrientation[] sides = TileOrientation.values(); int firstBiomeCount = 0; @@ -170,29 +171,22 @@ public Tile getNeighbor(TileOrientation orientation) { int neighborX = this.getXCoord(); int neighborY = this.getYCoord(); - switch (orientation) { - case NORTH: - neighborY -= (int) (Math.sqrt(3) * radius); - break; - case NORTH_EAST: - neighborX += (int) (1.5 * radius); - neighborY -= (int) (Math.sqrt(3) / 2 * radius); - break; - case SOUTH_EAST: - neighborX += (int) (1.5 * radius); - neighborY += (int) (Math.sqrt(3) / 2 * radius); - break; - case SOUTH: - neighborY += (int) (Math.sqrt(3) * radius); - break; - case SOUTH_WEST: - neighborX -= (int) (1.5 * radius); - neighborY += (int) (Math.sqrt(3) / 2 * radius); - break; - case NORTH_WEST: - neighborX -= (int) (1.5 * radius); - neighborY -= (int) (Math.sqrt(3) / 2 * radius); - break; + if (orientation == TileOrientation.NORTH) { + neighborY -= (int) (Math.sqrt(3) * radius); + } else if (orientation == TileOrientation.NORTH_EAST) { + neighborX += (int) (1.5 * radius); + neighborY -= (int) (Math.sqrt(3) / 2 * radius); + } else if (orientation == TileOrientation.SOUTH_EAST) { + neighborX += (int) (1.5 * radius); + neighborY += (int) (Math.sqrt(3) / 2 * radius); + } else if (orientation == TileOrientation.SOUTH) { + neighborY += (int) (Math.sqrt(3) * radius); + } else if (orientation == TileOrientation.SOUTH_WEST) { + neighborX -= (int) (1.5 * radius); + neighborY += (int) (Math.sqrt(3) / 2 * radius); + } else if (orientation == TileOrientation.NORTH_WEST) { + neighborX -= (int) (1.5 * radius); + neighborY -= (int) (Math.sqrt(3) / 2 * radius); } // Rechercher la tuile à la position calculée @@ -200,7 +194,8 @@ public Tile getNeighbor(TileOrientation orientation) { } - private TileOrientation determineSide(int x, int y) { + + public TileOrientation determineSide(int x, int y) { int radius = this.getRadius(); TileOrientation[] sides = TileOrientation.values(); double angle = Cell.to360Degrees(Math.toDegrees(Math.atan2(y - radius, x - radius)) + 120); @@ -226,27 +221,6 @@ public Tile getNeighbor(TileOrientation orientation) { return floorBiome.equals(dominantBiome) ? sides[ceilSide] : sides[floorSide]; } - - private void drawHexagonRow(Graphics2D g2d, double rowX, double rowY, double radius, int rowLength) { - int gRadius = this.getRadius(); - - for (int i = 0; i < rowLength; i++) { - Color[] colors; - int x = (int) Math.round(rowX + radius * Math.sqrt(3) * i); - int y = (int) Math.round(rowY); - - if (x == Math.round(gRadius) && y == Math.round(gRadius)) { - Biome dominantBiome = this.getDominantBiome(); - colors = (dominantBiome != null) ? dominantBiome.getBiomeColors() : this.getBiome(TileOrientation.SOUTH).getBiomeColors(); - } else { - colors = this.getBiome(this.determineSide(x, y)).getBiomeColors(); - } - - g2d.setColor(colors[i % colors.length]); - g2d.fillPolygon(new Hexagon(x, y, (int) Math.ceil(radius), 90)); - } - } - protected void drawTileAt(Graphics g, int x, int y, float scale) { // Sauvegarde de l'état actuel du graphique Graphics2D g2d = (Graphics2D) g.create(); @@ -267,34 +241,25 @@ public Tile getNeighbor(TileOrientation orientation) { * @param scale L'échelle de la tuile */ protected void paintTile(Graphics g, float scale) { - super.paintComponent(g); - Graphics2D g2d = (Graphics2D) g.create(); - int radius = this.getRadius(); - Point center = new Point(radius, radius); - radius = (int) (radius * scale); - Hexagon hexagon = new Hexagon(center, radius); + super.paintComponent(g); + Graphics2D g2d = (Graphics2D) g.create(); + int radius = this.getRadius(); + Point center = new Point(radius, radius); + radius = (int) (radius * scale); + Hexagon hexagon = new Hexagon(center, radius); - g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); - g2d.setClip(hexagon); + g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); + g2d.setClip(hexagon); - double hexRadius = radius / Math.sqrt(3) / 3; - double paddingX = center.x - radius; - double paddingY = center.y - radius; + HexagonDrawer hexDrawer = new HexagonDrawer(this); + hexDrawer.drawHexagon(g2d, radius, center); - this.drawHexagonRow(g2d, paddingX + radius * 0.5, paddingY + radius - radius * Math.sqrt(3) / 2, hexRadius, 4); - this.drawHexagonRow(g2d, paddingX, paddingY + radius - radius * Math.sqrt(3) / 3, hexRadius, 6); - this.drawHexagonRow(g2d, paddingX - radius * 0.5, paddingY + radius - radius * Math.sqrt(3) / 6, hexRadius, 8); - this.drawHexagonRow(g2d, paddingX - radius, paddingY + radius, hexRadius, 10); - this.drawHexagonRow(g2d, paddingX - radius * 0.5, paddingY + radius + radius * Math.sqrt(3) / 6, hexRadius, 8); - this.drawHexagonRow(g2d, paddingX, paddingY + radius + radius * Math.sqrt(3) / 3, hexRadius, 6); - this.drawHexagonRow(g2d, paddingX + radius * 0.5, paddingY + radius + radius * Math.sqrt(3) / 2, hexRadius, 4); + g2d.setClip(null); + g2d.setStroke(new BasicStroke((int) radius / 15)); + g2d.setColor(Color.BLACK); + g2d.drawPolygon(hexagon); - g2d.setClip(null); - g2d.setStroke(new BasicStroke((int) radius / 15)); - g2d.setColor(Color.BLACK); - g2d.drawPolygon(hexagon); - - g2d.dispose(); - } + g2d.dispose(); + } } diff --git a/src/fr/monkhanny/dorfromantik/utils/HexagonDrawer.java b/src/fr/monkhanny/dorfromantik/utils/HexagonDrawer.java new file mode 100644 index 0000000..4652ec0 --- /dev/null +++ b/src/fr/monkhanny/dorfromantik/utils/HexagonDrawer.java @@ -0,0 +1,51 @@ +package fr.monkhanny.dorfromantik.utils; + +import java.awt.Color; +import java.awt.Graphics2D; +import java.awt.Point; + +import fr.monkhanny.dorfromantik.enums.Biome; +import fr.monkhanny.dorfromantik.game.Tile; +import fr.monkhanny.dorfromantik.enums.TileOrientation; + +public class HexagonDrawer { + private Tile tile; + + public HexagonDrawer(Tile tile) { + this.tile = tile; + } + + public void drawHexagon(Graphics2D g2d, double radius, Point center) { + double hexRadius = radius / Math.sqrt(3) / 3; + double paddingX = center.x - radius; + double paddingY = center.y - radius; + + drawHexagonRow(g2d, paddingX + radius * 0.5, paddingY + radius - radius * Math.sqrt(3) / 2, hexRadius, 4); + drawHexagonRow(g2d, paddingX, paddingY + radius - radius * Math.sqrt(3) / 3, hexRadius, 6); + drawHexagonRow(g2d, paddingX - radius * 0.5, paddingY + radius - radius * Math.sqrt(3) / 6, hexRadius, 8); + drawHexagonRow(g2d, paddingX - radius, paddingY + radius, hexRadius, 10); + drawHexagonRow(g2d, paddingX - radius * 0.5, paddingY + radius + radius * Math.sqrt(3) / 6, hexRadius, 8); + drawHexagonRow(g2d, paddingX, paddingY + radius + radius * Math.sqrt(3) / 3, hexRadius, 6); + drawHexagonRow(g2d, paddingX + radius * 0.5, paddingY + radius + radius * Math.sqrt(3) / 2, hexRadius, 4); + } + + private void drawHexagonRow(Graphics2D g2d, double rowX, double rowY, double radius, int rowLength) { + int gRadius = tile.getRadius(); + + for (int i = 0; i < rowLength; i++) { + Color[] colors; + int x = (int) Math.round(rowX + radius * Math.sqrt(3) * i); + int y = (int) Math.round(rowY); + + if (x == Math.round(gRadius) && y == Math.round(gRadius)) { + Biome dominantBiome = tile.getDominantBiome(); + colors = (dominantBiome != null) ? dominantBiome.getBiomeColors() : tile.getBiome(TileOrientation.SOUTH).getBiomeColors(); + } else { + colors = tile.getBiome(tile.determineSide(x, y)).getBiomeColors(); + } + + g2d.setColor(colors[i % colors.length]); + g2d.fillPolygon(new Hexagon(x, y, (int) Math.ceil(radius), 90)); + } + } +}