Amélioration du code

This commit is contained in:
2024-12-06 22:38:09 +01:00
parent f327128bb7
commit 16103baba1
2 changed files with 87 additions and 71 deletions

View File

@@ -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));
}
}
}