diff --git a/src/visual/Track.java b/src/visual/Track.java index f4d43ff..b64c686 100644 --- a/src/visual/Track.java +++ b/src/visual/Track.java @@ -66,152 +66,119 @@ public class Track extends GameView { int rows = map.getHeight(); int cols = map.getWidth(); - // Calcul de la taille d'une cellule en pixels selon la taille de la fenêtre - int[] cellSize = new int[] { + // Taille d'un cube sur la fenetre + Point size = new Point( (getWidth() + 10) / cols, - (getHeight() + 10) / rows - }; + (getHeight() + 10) / rows); // Définir une police monospace en gras pour les caractères du circuit - g.setFont(new Font("Monospaced", Font.BOLD, cellSize[1] / 2)); + g.setFont(new Font("Monospaced", Font.BOLD, size.y / 2)); - // Parcours de toutes les cellules du circuit - for (int y = 0; y < rows; y++) { - for (int x = 0; x < cols; x++) { - Circuit cell = this.map.getElement(x, y); - drawCell(g, cell, new int[] { x, y }, cellSize); + drawCells(g, size); + drawCars(g, size); + } + + /** + * Dessine une cellule complète avec son contenu (caractère et voitures). + */ + private void drawCells(Graphics g, Point squareSize) { + for (int y = 0; y < map.getHeight(); y++) { + for (int x = 0; x < map.getWidth(); x++) { + Point pos = new Point(x, y); + Circuit element = map.getElement(x, y); + drawBlock(g, element, pos, squareSize); } } } - /** - * Retourne la couleur d'une cellule selon son type. - */ - private Color getCellColor(Circuit.Cell cell) { - return switch (cell) { - case ROAD -> Color.GRAY; - case START -> Color.YELLOW; - case FINISH -> Color.YELLOW; - case EMPTY -> Color.GREEN; - case YROAD -> Color.YELLOW; - }; - } - - /** - * Retourne le caractère à afficher pour une cellule du circuit. - */ - private char getCellChar(Circuit cell) { - return switch (cell.getType()) { - case ROAD -> '\0'; - case START -> 'D'; - case FINISH -> 'A'; - case EMPTY -> '\0'; - case YROAD -> (char) ('0' + cell.getValue()); - }; - } - /** * Dessine toutes les voitures sur une cellule donnée. * Les voitures sont légèrement décalées pour ne pas avoir exactement la même * taille. */ - private void drawCars(Graphics g, int[] cellCoord) { - int size = 1; - for (Car c : cars) { - int i = c.getPosition(); - Point p = this.map.getPath(i); - int[] ncoord = new int[] { p.x, p.y }; - drawInnerBlock(g, ncoord, cellCoord, c.getColor(), scale - size); - size += 3; // pour différencier visuellement les voitures + private void drawCars(Graphics g, Point squaresize) { + int size = cars.size(); + + for (int i = 0; i < size; i++) { + Car car = cars.get(i); + Point p = map.getPath(car.getPosition()); + drawInnerBlock(g, p, squaresize, car.getColor(), scale / size, i * (squaresize.y / size)); } } - /** - * Dessine une cellule complète avec son contenu (caractère et voitures). - */ - private void drawCell(Graphics g, Circuit cell, int[] coord, int[] cellCoord) { - drawBlock(g, cell, coord, cellCoord); - - char c = getCellChar(cell); - if (c != '\0') { - drawCharacter(g, coord, cellCoord, c); - } - - drawCars(g, cellCoord); - } - - /** - * Dessine un caractère centré dans une cellule. - */ - private void drawCharacter(Graphics g, int[] coord, int[] cellCoord, char c) { - int x = coord[0]; - int y = coord[1]; - - int cellWidth = cellCoord[0]; - int cellHeight = cellCoord[1]; - - String s = c + ""; - g.setColor(Color.BLACK); - - FontMetrics fm = g.getFontMetrics(); - // taille du string + font - int textWidth = fm.stringWidth(s); - // hauteur haute par exemple: h depasse en haut - int textHeight = fm.getAscent(); - // hauteur basse par exemple: g depasse en bas - int descent = fm.getDescent(); - - int cx = x * cellWidth + (cellWidth - textWidth) / 2; - int cy = y * cellHeight + (cellHeight + textHeight - descent) / 2; - - g.drawString(s, cx, cy); - } - - /** - * Dessine un bloc intérieur plus petit, utilisé pour représenter une voiture - */ - private void drawInnerBlock(Graphics g, int[] coord, int[] cellCoord, Color c, int scale) { - // scale de la taille du bloc - int w = cellCoord[0] * scale / 100; - int h = cellCoord[1] * scale / 100; - - // calcul de la position avec la nouvelle taille - int ox = coord[0] * cellCoord[0] + (cellCoord[0] - w) / 2; - int oy = coord[1] * cellCoord[1] + (cellCoord[1] - h) / 2; - - // dessine le bloc plus petit - g.setColor(c); - g.fillRect(ox, oy, w, h); - } - /** * Dessine une cellule de base selon son type et ajoute un contour noir. */ - private void drawBlock(Graphics g, Circuit cell, int[] coord, int[] cellCoord) { - int x = coord[0]; - int y = coord[1]; - - int cellWidth = cellCoord[0]; - int cellHeight = cellCoord[1]; - - switch (cell.getType()) { - case YROAD: - // dessine le bloc de route - g.setColor(getCellColor(Circuit.Cell.ROAD)); - g.fillRect(x * cellWidth, y * cellHeight, cellWidth, cellHeight); - - // dessine le sous bloc de route - drawInnerBlock(g, coord, cellCoord, getCellColor(cell.getType()), scale); - break; - default: - g.setColor(getCellColor(cell.getType())); - g.fillRect(x * cellWidth, y * cellHeight, cellWidth, cellHeight); - break; - } + private void drawBlock(Graphics g, Circuit element, Point coord, Point squareSize) { + g.setColor(switch (element.getType()) { + case ROAD -> Color.GRAY; + case EMPTY -> Color.GREEN; + default -> Color.YELLOW; + }); + g.fillRect( + coord.x * squareSize.x, + coord.y * squareSize.y, + squareSize.x, + squareSize.y); // contour noir g.setColor(Color.BLACK); - g.drawRect(x * cellWidth, y * cellHeight, cellWidth, cellHeight); + g.drawRect(coord.x * squareSize.x, + coord.y * squareSize.y, + squareSize.x, + squareSize.y); + + drawCharacter(g, element, coord, squareSize); + } + + /** + * Dessine un caractère centré dans une cellule. + */ + private void drawCharacter(Graphics g, Circuit element, Point coord, Point squareSize) { + String character = switch (element.getType()) { + case START -> 'D'; + case FINISH -> 'A'; + case YROAD -> (char) ('0' + element.getValue()); + default -> ""; + } + ""; + + if (character.isEmpty()) + return; + + g.setColor(Color.BLACK); + + FontMetrics fm = g.getFontMetrics(); + // taille du string + font + int textWidth = fm.stringWidth(character); + // hauteur haute par exemple: h depasse en haut + int textHeight = fm.getAscent(); + // hauteur basse par exemple: g depasse en bas + int descent = fm.getDescent(); + + Point cp = new Point( + coord.x * squareSize.x + (squareSize.x - textWidth) / 2, + coord.y * squareSize.y + (squareSize.y + textHeight - descent) / 2); + + g.drawString(character, cp.x, cp.y); + } + + /** + * Dessine un bloc intérieur plus petit, utilisé pour représenter une voiture + */ + private void drawInnerBlock(Graphics g, Point coord, Point squareSize, Color c, int scale, int y) { + // scale de la taille du bloc + Point size = new Point( + squareSize.x * scale / 100, + squareSize.y * scale / 100); + + // calcul de la position avec la nouvelle taille + Point pos = new Point( + coord.x * squareSize.x + (squareSize.x - size.x) / 2, + coord.y * squareSize.y + y); + + // dessine le bloc plus petit + g.setColor(c); + g.fillRect(pos.x, pos.y, size.x, size.y); } /**