-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnewgame.cpp
More file actions
239 lines (188 loc) · 7.28 KB
/
newgame.cpp
File metadata and controls
239 lines (188 loc) · 7.28 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#include "newgame.h"
#include "partie.h"
#include "mainmenu.h"
#include <QFont>
#include <QPushButton>
#include <QGridLayout>
#include <QLabel>
#include <QJsonObject>
#include <QJsonDocument>
#include <QJsonArray>
#include <QFile>
#include <QFileDialog>
#include <QDir>
#include <memory>
/**
* @file newgame.cpp
* @brief Implémentation de la classe NewGame.
*
* La classe NewGame représente l'écran permettant de configurer et de démarrer une nouvelle partie.
*/
/**
* @brief Constructeur de la classe NewGame.
*
* Initialise l'écran de configuration pour une nouvelle partie et configure ses composants.
*
* @param parent Pointeur vers le widget parent (par défaut nullptr).
*/
NewGame::NewGame(QWidget* parent) : QWidget(parent)
{
layout = new QGridLayout(this);
// Configuration de l'écran de nouvelle partie
setup_newgame();
}
/**
* @brief Configure les composants de l'écran Nouvelle Partie.
*
* Ajoute le titre, les boutons "Démarrer" et "Retour", et configure leur disposition dans le layout.
*/
void NewGame::setup_newgame()
{
// Ajout du titre
QLabel* title = new QLabel("Nouvelle Partie");
title->setAlignment(Qt::AlignCenter);
layout->addWidget(title, 1, 1);
QString label_style = "QLabel {"
"font-family: 'Arial', sans-serif;"
"font-size: 28px;"
"font-weight: bold;"
"color: white;"
"text-shadow: 2px 2px 10px rgba(0, 0, 0, 0.7);"
"padding: 10px;"
"background: rgba(0, 0, 0, 0.4); "
"border-radius: 5px;"
"}";
title->setStyleSheet(label_style);
layout->addItem(new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Expanding), 0, 0);
layout->addItem(new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Expanding), 2, 1);
// Bouton pour démarrer une nouvelle partie
QPushButton* start_button = new QPushButton("Démarrer");
layout->addWidget(start_button, 3, 1);
MainMenu::pimper(start_button);
layout->addItem(new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Expanding), 4, 1);
QPushButton* charger_bouton = new QPushButton("Charger une partie");
layout->addWidget(charger_bouton, 5, 1);
MainMenu::pimper(charger_bouton);
layout->addItem(new QSpacerItem(9, 0, QSizePolicy::Expanding, QSizePolicy::Expanding), 6, 1);
QPushButton* back_bouton = new QPushButton("Retour");
layout->addWidget(back_bouton, 7, 1);
MainMenu::pimper(back_bouton);
layout->addItem(new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Expanding), 8, 2);
// Configuration des proportions des colonnes et des lignes
layout->setColumnStretch(0, 1);
layout->setColumnStretch(1, 2);
layout->setColumnStretch(2, 1);
layout->setRowStretch(0, 2);
layout->setRowStretch(1, 1);
layout->setRowStretch(2, 1);
layout->setRowStretch(3, 1);
layout->setRowStretch(4, 1);
layout->setRowStretch(5, 1);
layout->setRowStretch(6, 1);
layout->setRowStretch(7, 1);
layout->setRowStretch(8, 2);
// Connexion des signaux aux slots
connect(start_button, &QPushButton::clicked, this, &NewGame::demarrer_nouvelle_partie);
connect(back_bouton, &QPushButton::clicked, this, &NewGame::retour_menu);
connect(charger_bouton, &QPushButton::clicked, this, &NewGame::load_game);
}
void NewGame::save_game(const QString &chemin_fichier, Partie* partie)
{
if (!partie)
return;
QJsonObject gameData;
// Settings
QJsonObject settings;
settings["joueur1"] = QString::fromStdString(partie->get_joueur1().get_pseudo());
settings["joueur2"] = QString::fromStdString(partie->get_joueur2().get_pseudo());
settings["nb_retours_possibles"] = QString::fromStdString(std::to_string(partie->nb_retours_possibles));
QJsonArray insectes;
std::vector<std::string> insectes_std = partie->get_joueur1().get_insectes();
for (const auto& i : insectes_std)
insectes.push_back(QString::fromStdString(i));
settings["insectes"] = insectes;
gameData["settings"] = settings;
QJsonArray actions_json;
for (auto action : partie->actions)
{
QJsonObject action_json;
QJsonObject debut;
QJsonObject arrivee;
debut["x"] = action.get_depart().x();
debut["y"] = action.get_depart().y();
arrivee["x"] = action.get_arrivee().x();
arrivee["y"] = action.get_arrivee().y();
action_json["debut"] = debut;
action_json["arrivee"] = arrivee;
action_json["type"] = QString::fromStdString(Type::type_to_str(action.get_type()));
action_json["est_placement"] = action.est_placement();
actions_json.append(action_json);
}
gameData["moves"] = actions_json;
// Write to file
QFile saveFile(chemin_fichier);
if (!saveFile.open(QIODevice::WriteOnly))
{
qWarning("Couldn't open save file.");
return;
}
QJsonDocument saveDoc(gameData);
saveFile.write(saveDoc.toJson());
}
void NewGame::load_game()
{
// Open the file dialog to select the file
QString chemin_fichier = QFileDialog::getOpenFileName(
nullptr,
"Open Game",
QDir::homePath(), // Default directory (you can change it)
"JSON Files (*.json);;All Files (*)"
);
// If the user cancels the dialog, filePath will be empty
if (chemin_fichier.isEmpty())
return;
QFile loadFile(chemin_fichier);
if (!loadFile.open(QIODevice::ReadOnly))
{
qWarning("Couldn't open save file.");
return;
}
// Read the file contents
QByteArray saveData = loadFile.readAll();
// Parse the JSON
QJsonDocument loadDoc(QJsonDocument::fromJson(saveData));
QJsonObject gameData = loadDoc.object();
// Load settings
QJsonObject settings = gameData["settings"].toObject();
unsigned int nb_retours_possibles = settings["nb_retours_possibles"].toString().toInt();
QJsonArray insectes_json = settings["insectes"].toArray();
std::vector<std::string> insectes;
for (const auto& insecte : insectes_json)
{
insectes.push_back(insecte.toString().toStdString());
}
// Load moves
QJsonArray actions_json = gameData["moves"].toArray();
std::vector<Action> actions;
for (const QJsonValue &actionValue : actions_json)
{
QJsonObject action_json = actionValue.toObject();
QJsonObject debut = action_json["debut"].toObject();
QPoint start(debut["x"].toInt(), debut["y"].toInt());
QJsonObject arrivee = action_json["arrivee"].toObject();
QPoint end(arrivee["x"].toInt(), arrivee["y"].toInt());
Type::Type action_type = Type::str_to_type(action_json["type"].toString().toStdString());
bool est_placement = action_json["est_placement"].toBool();
if (est_placement)
{
Action action = Action{end, action_type};
actions.push_back(action);
}
else
{
Action action = Action{start, end};
actions.push_back(action);
}
}
emit charger_nouvelle_partie(settings["joueur1"].toString().toStdString(), settings["joueur2"].toString().toStdString(), nb_retours_possibles, insectes, actions);
}