Amélioration du code
This commit is contained in:
@@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
|
51
src/fr/monkhanny/dorfromantik/utils/HexagonDrawer.java
Normal file
51
src/fr/monkhanny/dorfromantik/utils/HexagonDrawer.java
Normal 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));
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user