-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChefScreen.java
More file actions
181 lines (140 loc) · 6.54 KB
/
Copy pathChefScreen.java
File metadata and controls
181 lines (140 loc) · 6.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class ChefScreen extends JFrame {
private static final int NUM_PLAYERS = 4;
protected GamePanel aGamePanel;
private JComboBox<String>[] RacesBoxes;
private JComboBox<String>[] chefBoxes;
private JButton startButton;
private Map<String, java.util.List<Character>> RacesChefs = new HashMap<>();
private Character[] selectedChefs = new Character[NUM_PLAYERS];
public ChefScreen(Map<String, Races> allRacess) {
setTitle("Sélection des chefs");
setSize(650, 500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setLayout(new BorderLayout(10, 10));
// 🧩 Étape 1 : extraire uniquement les chefs de chaque Races
for (String RacesName : allRacess.keySet()) {
Races Races = allRacess.get(RacesName);
java.util.List<Character> chefs = new ArrayList<>();
for (Character c : Races.getCharacters()) {
// Vérifie si c’est un chef (basé sur le dossier ou le nom)
String charName = c.getCharacterName().toLowerCase();
if (charName.contains("chef")) {
chefs.add(c);
}
}
if (!chefs.isEmpty()) {
RacesChefs.put(RacesName, chefs);
}
}
// 🧩 Étape 2 : interface joueurs
JPanel playersPanel = new JPanel(new GridLayout(NUM_PLAYERS, 3, 10, 10));
playersPanel.setBorder(BorderFactory.createTitledBorder("Choisissez la Races et le chef pour chaque joueur"));
RacesBoxes = new JComboBox[NUM_PLAYERS];
chefBoxes = new JComboBox[NUM_PLAYERS];
String[] RacesNames = RacesChefs.keySet().toArray(new String[0]);
String[] forcedRaces = {"Elfes", "Nains", "Goblins_Orcs_Trolls", "Morts_vivants"};
for (int i = 0; i < NUM_PLAYERS; i++) {
JLabel playerLabel = new JLabel("Joueur " + (i + 1) + " :", JLabel.RIGHT);
RacesBoxes[i] = new JComboBox<>(RacesNames);
chefBoxes[i] = new JComboBox<>();
// 🔒 Forcer la race pour chaque joueur
RacesBoxes[i].setSelectedItem(forcedRaces[i]);
RacesBoxes[i].setEnabled(false); // désactive la sélection pour empêcher le changement
int playerIndex = i;
RacesBoxes[i].addActionListener(e -> updateChefList(playerIndex));
playersPanel.add(playerLabel);
playersPanel.add(RacesBoxes[i]);
playersPanel.add(chefBoxes[i]);
updateChefList(i); // init liste chefs
// 🔒 Optionnel : sélectionner automatiquement le premier chef disponible
if (chefBoxes[i].getItemCount() > 0) {
chefBoxes[i].setSelectedIndex(0);
}
}
add(playersPanel, BorderLayout.CENTER);
// 🧩 Étape 3 : bouton "Commencer"
startButton = new JButton("Commencer la partie");
startButton.addActionListener(e -> startGame());
add(startButton, BorderLayout.SOUTH);
setVisible(true);
}
private void updateChefList(int playerIndex) {
String selectedRaces = (String) RacesBoxes[playerIndex].getSelectedItem();
JComboBox<String> chefBox = chefBoxes[playerIndex];
chefBox.removeAllItems();
if (selectedRaces != null && RacesChefs.containsKey(selectedRaces)) {
for (Character c : RacesChefs.get(selectedRaces)) {
chefBox.addItem(c.getCharacterName());
}
}
}
private void startGame(){
for (int i = 0; i < NUM_PLAYERS; i++) {
String Races = (String) RacesBoxes[i].getSelectedItem();
String chefName = (String) chefBoxes[i].getSelectedItem();
if (Races == null || chefName == null) {
JOptionPane.showMessageDialog(this, "Le joueur " + (i + 1) + " doit choisir un chef !");
return;
}
for (Character c : RacesChefs.get(Races)) {
if (c.getCharacterName().equals(chefName)) {
selectedChefs[i] = c;
break;
}
}
}
// ✅ Vérifie que tous les chefs sont bien choisis
for (int i = 0; i < selectedChefs.length; i++) {
if (selectedChefs[i] == null) {
JOptionPane.showMessageDialog(this, "Le joueur " + (i + 1) + " n’a pas de chef sélectionné !");
return;
}
}
// ✅ Ferme l’écran de sélection
dispose();
// --- Lancement de la carte ---
JFrame frame = new JFrame("War World - Carte");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1200, 800);
frame.setLayout(new BorderLayout());
GameEngine engine = new GameEngine();
Map<String, Character> selectedChefsMap = new LinkedHashMap<>();
for (int i = 0; i < NUM_PLAYERS; i++) {
String race = (String) RacesBoxes[i].getSelectedItem();
Character chef = selectedChefs[i];
if (race == null || chef == null) {
JOptionPane.showMessageDialog(this,
"Erreur : chaque joueur doit avoir une race et un chef.");
return;
}
selectedChefsMap.put(race, chef);
}
// ✅ Crée le panneau d'infos
InfoPanel infoPanel = new InfoPanel(aGamePanel);
// ✅ Crée le panneau de jeu
aGamePanel = new GamePanel(selectedChefsMap, infoPanel);
infoPanel.setGamePanel(aGamePanel);
aGamePanel.setInfoPanel(infoPanel);
// ✅ Scroll pour la carte
JScrollPane scrollPane = new JScrollPane(aGamePanel);
scrollPane.getVerticalScrollBar().setUnitIncrement(16);
scrollPane.getHorizontalScrollBar().setUnitIncrement(16);
// ✅ Place les composants
frame.add(scrollPane, BorderLayout.CENTER);
frame.add(infoPanel, BorderLayout.EAST);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
System.out.println("✅ Partie lancée avec les chefs sélectionnés :");
for (Character c : selectedChefs) {
if (c != null)
System.out.println(" - " + c.getCharacterName() + " (" + c.getName() + ")");
}
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1600, 1000);
}
}