mirror of
https://github.com/guezoloic/racing-game.git
synced 2026-03-28 18:03:50 +00:00
feat(SelectionView): ajout commentaire
This commit is contained in:
@@ -11,73 +11,96 @@ import model.car.Car;
|
|||||||
import model.car.Selection;
|
import model.car.Selection;
|
||||||
|
|
||||||
public class SelectionView extends GameView {
|
public class SelectionView extends GameView {
|
||||||
|
/** Builder du jeu associé */
|
||||||
private Game.Builder gameBuilder = new Game.Builder();
|
private Game.Builder gameBuilder = new Game.Builder();
|
||||||
|
|
||||||
|
/** Sélection de voitures et décorateurs */
|
||||||
private Selection selection = new Selection(gameBuilder);
|
private Selection selection = new Selection(gameBuilder);
|
||||||
|
|
||||||
|
/** Panel contenant tous les boutons de sélection */
|
||||||
private JPanel buttonPanel = new JPanel();
|
private JPanel buttonPanel = new JPanel();
|
||||||
|
|
||||||
|
/** Accesseur vers la sélection */
|
||||||
public Selection getSelection() {
|
public Selection getSelection() {
|
||||||
return selection;
|
return selection;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructeur principal
|
||||||
|
*
|
||||||
|
* @param ncar nombre de voitures à créer et personnaliser
|
||||||
|
*/
|
||||||
public SelectionView(int ncar) {
|
public SelectionView(int ncar) {
|
||||||
super(null, "Menu de sélection", 500, 300, 400, 400);
|
super(null, "Menu de sélection", 500, 300, 400, 400);
|
||||||
init(null); // forcer le init
|
init(null); // forcer l'initialisation du JFrame
|
||||||
|
|
||||||
|
// Layout des boutons : grille avec 3 colonnes (Hybrid, Drunk, Boost)
|
||||||
buttonPanel.setLayout(new GridLayout(0, 3, 5, 5));
|
buttonPanel.setLayout(new GridLayout(0, 3, 5, 5));
|
||||||
frame.add(buttonPanel, BorderLayout.CENTER);
|
frame.add(buttonPanel, BorderLayout.CENTER);
|
||||||
|
|
||||||
|
// Boucle pour créer chaque voiture et ses boutons de décorateurs
|
||||||
for (int i = 1; i <= ncar; i++) {
|
for (int i = 1; i <= ncar; i++) {
|
||||||
Car car = selection.addCar("Car " + i, Color.getHSBColor(i / 3f, 0.8f, 0.8f));
|
Car car = selection.addCar(
|
||||||
// bouton Hybrid
|
"Car " + i,
|
||||||
|
Color.getHSBColor(i / 3f, 0.8f, 0.8f) // couleur différente pour chaque voiture
|
||||||
|
);
|
||||||
|
|
||||||
|
// Boutons de décorateurs
|
||||||
JButton hybridBtn = new JButton("Hybrid " + car.getName());
|
JButton hybridBtn = new JButton("Hybrid " + car.getName());
|
||||||
// bouton Drunk
|
|
||||||
JButton drunkBtn = new JButton("Drunk " + car.getName());
|
JButton drunkBtn = new JButton("Drunk " + car.getName());
|
||||||
// bouton boost
|
|
||||||
JButton boostBtn = new JButton("Boost " + car.getName());
|
JButton boostBtn = new JButton("Boost " + car.getName());
|
||||||
|
|
||||||
final int I = i;
|
final int I = i; // variable finale pour lambda
|
||||||
|
// Décorateur Hybrid
|
||||||
hybridBtn.addActionListener((ActionEvent e) -> {
|
hybridBtn.addActionListener((ActionEvent e) -> {
|
||||||
try {
|
try {
|
||||||
Car current = selection.getCars().get(I-1);
|
Car current = selection.getCars().get(I - 1);
|
||||||
selection.addHybridCarDecorator(current);
|
selection.addHybridCarDecorator(current);
|
||||||
hybridBtn.setEnabled(false);
|
hybridBtn.setEnabled(false);
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
ex.printStackTrace();
|
ex.printStackTrace();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Décorateur Drunk
|
||||||
drunkBtn.addActionListener((ActionEvent e) -> {
|
drunkBtn.addActionListener((ActionEvent e) -> {
|
||||||
try {
|
try {
|
||||||
Car current = selection.getCars().get(I-1);
|
Car current = selection.getCars().get(I - 1);
|
||||||
selection.addDrunkCarDecorator(current);
|
selection.addDrunkCarDecorator(current);
|
||||||
drunkBtn.setEnabled(false);
|
drunkBtn.setEnabled(false);
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
ex.printStackTrace();
|
ex.printStackTrace();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Décorateur Boost
|
||||||
boostBtn.addActionListener((ActionEvent e) -> {
|
boostBtn.addActionListener((ActionEvent e) -> {
|
||||||
try {
|
try {
|
||||||
Car current = selection.getCars().get(I-1);
|
Car current = selection.getCars().get(I - 1);
|
||||||
selection.addBoostCarDecorator(current);
|
selection.addBoostCarDecorator(current);
|
||||||
boostBtn.setEnabled(false);
|
boostBtn.setEnabled(false);
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
ex.printStackTrace();
|
ex.printStackTrace();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Ajouter les boutons au panel
|
||||||
buttonPanel.add(hybridBtn);
|
buttonPanel.add(hybridBtn);
|
||||||
buttonPanel.add(drunkBtn);
|
buttonPanel.add(drunkBtn);
|
||||||
buttonPanel.add(boostBtn);
|
buttonPanel.add(boostBtn);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Bouton Start pour lancer la partie
|
||||||
JButton startBtn = new JButton("Start");
|
JButton startBtn = new JButton("Start");
|
||||||
startBtn.addActionListener((ActionEvent e) -> {
|
startBtn.addActionListener((ActionEvent e) -> {
|
||||||
frame.dispose();
|
frame.dispose(); // ferme le menu
|
||||||
selection.run();
|
selection.run(); // lance le jeu avec les voitures sélectionnées
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Ajout du bouton Start en bas de la fenêtre
|
||||||
this.frame.add(startBtn, BorderLayout.SOUTH);
|
this.frame.add(startBtn, BorderLayout.SOUTH);
|
||||||
|
|
||||||
|
// Repaint et validation pour afficher correctement les boutons
|
||||||
frame.revalidate();
|
frame.revalidate();
|
||||||
frame.repaint();
|
frame.repaint();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user