mirror of
https://github.com/guezoloic/racing-game.git
synced 2026-03-28 18:03:50 +00:00
feat(Track.java): ajout commentaire
This commit is contained in:
131
src/Track.java
131
src/Track.java
@@ -1,3 +1,4 @@
|
|||||||
|
import java.awt.BorderLayout;
|
||||||
import java.awt.Color;
|
import java.awt.Color;
|
||||||
import java.awt.Font;
|
import java.awt.Font;
|
||||||
import java.awt.FontMetrics;
|
import java.awt.FontMetrics;
|
||||||
@@ -5,46 +6,73 @@ import java.awt.Graphics;
|
|||||||
import java.awt.Point;
|
import java.awt.Point;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <code>Track</code> est une vue graphique représentant le circuit de course
|
||||||
|
* ainsi que les voitures qui y circulent.
|
||||||
|
* Cette classe hérite de <code>GameView</code> pour être intégrée au système de vues.
|
||||||
|
* <p>
|
||||||
|
* Le rendu est basé sur une grille définie par <code>Map</code> et chaque cellule
|
||||||
|
* est dessinée selon son type. Les voitures sont superposées sur la grille.
|
||||||
|
* </p>
|
||||||
|
*/
|
||||||
public class Track extends GameView {
|
public class Track extends GameView {
|
||||||
|
/** La carte du circuit */
|
||||||
private Map map;
|
private Map map;
|
||||||
|
/** Liste des voitures à dessiner */
|
||||||
private ArrayList<Car> cars;
|
private ArrayList<Car> cars;
|
||||||
|
/** Échelle utilisée pour ajuster la taille des voitures dans les cellules */
|
||||||
|
private final int scale = 80;
|
||||||
|
|
||||||
private final int scale = 75;
|
/**
|
||||||
|
* Construit la vue Track avec une carte et une liste de voitures.
|
||||||
|
*
|
||||||
|
* @param map La carte du circuit
|
||||||
|
* @param cars Liste des voitures
|
||||||
|
* @param title Titre de la fenêtre
|
||||||
|
* @param width Largeur de la fenêtre
|
||||||
|
* @param height Hauteur de la fenêtre
|
||||||
|
* @param x Position X de la fenêtre
|
||||||
|
* @param y Position Y de la fenêtre
|
||||||
|
*/
|
||||||
protected Track(Map map, ArrayList<Car> cars, String title, int width, int height, int x, int y) {
|
protected Track(Map map, ArrayList<Car> cars, String title, int width, int height, int x, int y) {
|
||||||
super(title, width, height, x, y);
|
super(title, width, height, x, y);
|
||||||
this.map = map;
|
this.map = map;
|
||||||
this.cars = cars;
|
this.cars = cars;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Méthode de dessin appelée automatiquement par Swing.
|
||||||
|
* Dessine chaque cellule de la carte ainsi que les voitures.
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
protected void paintComponent(Graphics g)
|
protected void paintComponent(Graphics g) {
|
||||||
{
|
|
||||||
super.paintComponent(g);
|
super.paintComponent(g);
|
||||||
|
|
||||||
int rows = map.getHeight();
|
int rows = map.getHeight();
|
||||||
int cols = map.getWidth();
|
int cols = map.getWidth();
|
||||||
|
|
||||||
|
// Calcul de la taille d'une cellule en pixels selon la taille de la fenêtre
|
||||||
int[] cellSize = new int[] {
|
int[] cellSize = new int[] {
|
||||||
(getWidth() + 10) / cols, // width
|
(getWidth() + 10) / cols,
|
||||||
(getHeight() + 10) / rows // height
|
(getHeight() + 10) / rows
|
||||||
};
|
};
|
||||||
|
|
||||||
// Caractere bold de la taille de la longueur de l'ecran /2
|
// 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, cellSize[1] / 2));
|
||||||
|
|
||||||
for (int y = 0; y < rows; y++)
|
// Parcours de toutes les cellules du circuit
|
||||||
{
|
for (int y = 0; y < rows; y++) {
|
||||||
for (int x = 0; x < cols; x++)
|
for (int x = 0; x < cols; x++) {
|
||||||
{
|
|
||||||
Circuit cell = this.map.getElement(x, y);
|
Circuit cell = this.map.getElement(x, y);
|
||||||
drawCell(g, cell, new int[] {x, y}, cellSize);
|
drawCell(g, cell, new int[] {x, y}, cellSize);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private Color getCellColor(Circuit.Cell cell)
|
/**
|
||||||
{
|
* Retourne la couleur d'une cellule selon son type.
|
||||||
|
*/
|
||||||
|
private Color getCellColor(Circuit.Cell cell) {
|
||||||
return switch (cell) {
|
return switch (cell) {
|
||||||
case ROAD -> Color.GRAY;
|
case ROAD -> Color.GRAY;
|
||||||
case START -> Color.YELLOW;
|
case START -> Color.YELLOW;
|
||||||
@@ -54,46 +82,54 @@ public class Track extends GameView {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
private char getCellChar(Circuit cell)
|
/**
|
||||||
{
|
* Retourne le caractère à afficher pour une cellule du circuit.
|
||||||
|
*/
|
||||||
|
private char getCellChar(Circuit cell) {
|
||||||
return switch (cell.getType()) {
|
return switch (cell.getType()) {
|
||||||
case Circuit.Cell.ROAD -> '\0';
|
case ROAD -> '\0';
|
||||||
case Circuit.Cell.START -> 'D';
|
case START -> 'D';
|
||||||
case Circuit.Cell.FINISH -> 'A';
|
case FINISH -> 'A';
|
||||||
case Circuit.Cell.EMPTY -> '\0';
|
case EMPTY -> '\0';
|
||||||
case Circuit.Cell.YROAD -> (char)('0' + cell.getValue());
|
case YROAD -> (char)('0' + cell.getValue());
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
private void drawCars(Graphics g, int[] cellCoord)
|
/**
|
||||||
{
|
* 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;
|
int size = 1;
|
||||||
for (Car c : cars)
|
for (Car c : cars) {
|
||||||
{
|
|
||||||
// index de pathMap
|
|
||||||
int i = c.getPos();
|
int i = c.getPos();
|
||||||
Point p = this.map.getPath(i);
|
Point p = this.map.getPath(i);
|
||||||
int[] ncoord = new int[] {p.x, p.y};
|
int[] ncoord = new int[] {p.x, p.y};
|
||||||
drawInnerBlock(g, ncoord, cellCoord, c.getColor(), scale - size);
|
drawInnerBlock(g, ncoord, cellCoord, c.getColor(), scale - size);
|
||||||
// ne jamais avoir la meme taille pour les voitures
|
size += 3; // pour différencier visuellement les voitures
|
||||||
size += 3;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void drawCell(Graphics g, Circuit cell, int[] coord, int[] cellCoord)
|
/**
|
||||||
{
|
* 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);
|
drawBlock(g, cell, coord, cellCoord);
|
||||||
|
|
||||||
char c = getCellChar(cell);
|
char c = getCellChar(cell);
|
||||||
if (c != '\0')
|
if (c != '\0') {
|
||||||
drawCharacter(g, coord, cellCoord, c);
|
drawCharacter(g, coord, cellCoord, c);
|
||||||
|
}
|
||||||
|
|
||||||
drawCars(g, cellCoord);
|
drawCars(g, cellCoord);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void drawCharacter(Graphics g, int[] coord, int[] cellCoord, char c)
|
/**
|
||||||
{
|
* Dessine un caractère centré dans une cellule.
|
||||||
int x = coord[0]; int y = coord[1];
|
*/
|
||||||
|
private void drawCharacter(Graphics g, int[] coord, int[] cellCoord, char c) {
|
||||||
|
int x = coord[0];
|
||||||
|
int y = coord[1];
|
||||||
|
|
||||||
int cellWidth = cellCoord[0];
|
int cellWidth = cellCoord[0];
|
||||||
int cellHeight = cellCoord[1];
|
int cellHeight = cellCoord[1];
|
||||||
@@ -102,7 +138,6 @@ public class Track extends GameView {
|
|||||||
g.setColor(Color.BLACK);
|
g.setColor(Color.BLACK);
|
||||||
|
|
||||||
FontMetrics fm = g.getFontMetrics();
|
FontMetrics fm = g.getFontMetrics();
|
||||||
|
|
||||||
// taille du string + font
|
// taille du string + font
|
||||||
int textWidth = fm.stringWidth(s);
|
int textWidth = fm.stringWidth(s);
|
||||||
// hauteur haute par exemple: h depasse en haut
|
// hauteur haute par exemple: h depasse en haut
|
||||||
@@ -116,8 +151,10 @@ public class Track extends GameView {
|
|||||||
g.drawString(s, cx, cy);
|
g.drawString(s, cx, cy);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void drawInnerBlock(Graphics g, int[] coord, int[] cellCoord, Color c, int scale)
|
/**
|
||||||
{
|
* 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
|
// scale de la taille du bloc
|
||||||
int w = cellCoord[0] * scale / 100;
|
int w = cellCoord[0] * scale / 100;
|
||||||
int h = cellCoord[1] * scale / 100;
|
int h = cellCoord[1] * scale / 100;
|
||||||
@@ -131,9 +168,12 @@ public class Track extends GameView {
|
|||||||
g.fillRect(ox, oy, w, h);
|
g.fillRect(ox, oy, w, h);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void drawBlock(Graphics g, Circuit cell, int[] coord, int[] cellCoord)
|
/**
|
||||||
{
|
* Dessine une cellule de base selon son type et ajoute un contour noir.
|
||||||
int x = coord[0]; int y = coord[1];
|
*/
|
||||||
|
private void drawBlock(Graphics g, Circuit cell, int[] coord, int[] cellCoord) {
|
||||||
|
int x = coord[0];
|
||||||
|
int y = coord[1];
|
||||||
|
|
||||||
int cellWidth = cellCoord[0];
|
int cellWidth = cellCoord[0];
|
||||||
int cellHeight = cellCoord[1];
|
int cellHeight = cellCoord[1];
|
||||||
@@ -143,7 +183,8 @@ public class Track extends GameView {
|
|||||||
// dessine le bloc de route
|
// dessine le bloc de route
|
||||||
g.setColor(getCellColor(Circuit.Cell.ROAD));
|
g.setColor(getCellColor(Circuit.Cell.ROAD));
|
||||||
g.fillRect(x * cellWidth, y * cellHeight, cellWidth, cellHeight);
|
g.fillRect(x * cellWidth, y * cellHeight, cellWidth, cellHeight);
|
||||||
|
|
||||||
|
// dessine le sous bloc de route
|
||||||
drawInnerBlock(g, coord, cellCoord, getCellColor(cell.getType()), scale);
|
drawInnerBlock(g, coord, cellCoord, getCellColor(cell.getType()), scale);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
@@ -152,14 +193,16 @@ public class Track extends GameView {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// contour noir
|
||||||
g.setColor(Color.BLACK);
|
g.setColor(Color.BLACK);
|
||||||
g.drawRect(x * cellWidth, y * cellHeight, cellWidth, cellHeight);
|
g.drawRect(x * cellWidth, y * cellHeight, cellWidth, cellHeight);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Méthode appelée automatiquement pour mettre à jour l'affichage.
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
protected void run()
|
protected void run() {
|
||||||
{
|
|
||||||
repaint();
|
repaint();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user