-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbattlewindow.cpp
More file actions
286 lines (232 loc) · 9.93 KB
/
battlewindow.cpp
File metadata and controls
286 lines (232 loc) · 9.93 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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#include "battlewindow.h"
#include "gamedata.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QMovie>
#include <QDebug>
BattleWindow::BattleWindow(QWidget *parent, const QString &enemyType)
: QWidget(parent), player(nullptr), enemy(nullptr), enemyType(enemyType)
{
// Inicia a quantidade total das poções
playerPotions = 20;
// Configuração da tela de batalha
setFixedSize(640, 480);
QPixmap backgroundImage(":/images/assets/backgrounds/12.png");
QPalette palette;
palette.setBrush(backgroundRole(), backgroundImage);
setPalette(palette);
// Rótulo para exibir informações sobre a batalha
infoLabel = new QLabel("Você iniciou uma batalha", this);
infoLabel->setAlignment(Qt::AlignBottom | Qt::AlignCenter);
infoLabel->setFont(QFont("SansSerif", 10, QFont::Bold));
infoLabel->setStyleSheet("color: white;");
// Botão para iniciar a batalha
startButton = new QPushButton("Iniciar Batalha", this);
connect(startButton, &QPushButton::clicked, this, &BattleWindow::startBattle);
// Botões para ações durante a batalha
attackButton = new QPushButton("Atacar", this);
potionButton = new QPushButton("Usar Potion", this);
// Conecta os botões às respectivas funções
connect(attackButton, &QPushButton::clicked, this, &BattleWindow::attack);
connect(potionButton, &QPushButton::clicked, this, &BattleWindow::usePotion);
// Criação dos rótulos de saúde
playerHealthLabel = new QLabel(this);
enemyHealthLabel = new QLabel(this);
playerHealthLabel->setAlignment(Qt::AlignTop);
enemyHealthLabel->setAlignment(Qt::AlignTop);
playerHealthLabel->setFont(QFont("SansSerif", 10, QFont::Bold));
playerHealthLabel->setStyleSheet("color: white;");
enemyHealthLabel->setFont(QFont("SansSerif", 10, QFont::Bold));
enemyHealthLabel->setStyleSheet("color: white;");
// Criação de um QLabel para exibir mensagens dinâmicas
hpMessage = new QLabel(this);
hpMessage->setAlignment(Qt::AlignTop);
hpMessage->setFont(QFont("SansSerif", 10, QFont::Bold));
hpMessage->setStyleSheet("color: white;");
resultAttackLabel = new QLabel(this);
resultAttackLabel->setAlignment(Qt::AlignTop);
resultAttackLabel->setFont(QFont("SansSerif", 10, QFont::Bold));
resultAttackLabel->setStyleSheet("color: white;");
// Layout horizontal para os rótulos de saúde
QHBoxLayout *healthLabelLayout = new QHBoxLayout;
healthLabelLayout->addWidget(playerHealthLabel);
healthLabelLayout->addStretch(); // Adiciona um espaço flexível entre os rótulos
healthLabelLayout->addWidget(hpMessage);
healthLabelLayout->addWidget(resultAttackLabel);
healthLabelLayout->addStretch();
healthLabelLayout->addWidget(enemyHealthLabel);
// Layout horizontal para os botões (lado a lado)
QHBoxLayout *buttonLayout = new QHBoxLayout;
buttonLayout->addWidget(startButton);
buttonLayout->addWidget(attackButton);
buttonLayout->addWidget(potionButton);
// Layout vertical para organizar os elementos
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addWidget(infoLabel);
mainLayout->addLayout(healthLabelLayout); // Adiciona o layout dos rótulos de saúde
mainLayout->addLayout(buttonLayout); // Adiciona o layout dos botões
// Configura o layout principal
setLayout(mainLayout);
// Inicializa a QLabel do inimigo
enemyImgLabel = new QLabel(this);
enemyImgLabel->setAlignment(Qt::AlignCenter);
enemyImgLabel->hide(); // Esconde a QLabel do inimigo inicialmente
// Layout vertical para os botões de ação (inicialmente vazio)
actionButtonLayout = new QVBoxLayout;
mainLayout->addLayout(actionButtonLayout);
// Configura o jogador no GameData
GameData *gameData = GameData::getInstance();
gameData->getPlayer();
// Cria o inimigo com base no tipo especificado
if (enemyType == "InimigoPeludo") {
enemy = new Character("Inimigo Peludo", 80, 15, 10);
} else if (enemyType == "GuerreiroGigante") {
enemy = new Character("Guerreiro Gigante", 150, 20, 35);
} else if (enemyType == "Mimico") {
enemy = new Character("Mimico", 50, 50, 10);
} else if (enemyType == "GrupoDeInimigos") {
enemy = new Character("Grupo de Inimigos", 400, 50, 30);
}
gameData->setEnemy(enemy);
// Inicializa a primeira etapa
initStep();
}
void BattleWindow::startBattle()
{
// Lógica para ação após iniciar a batalha
startButton->hide();
attackButton->show();
potionButton->show();
infoLabel->hide();
// Recupera o número de poções do GameData
playerPotions = GameData::getPlayerPotions();
// Cria a imagemdo inimigo com base no tipo especificado
if (enemyType == "InimigoPeludo") {
QMovie *enemyMovie = new QMovie(":/images/assets/monster/reptile/idle.gif");
enemyImgLabel->setMovie(enemyMovie);
enemyMovie->start();
enemyImgLabel->setAlignment(Qt::AlignCenter);
} else if (enemyType == "GuerreiroGigante") {
QMovie *enemyMovie = new QMovie(":/images/assets/monster/giant/idle.gif");
enemyImgLabel->setMovie(enemyMovie);
enemyMovie->start();
enemyImgLabel->setAlignment(Qt::AlignCenter);
} else if (enemyType == "Mimico") {
QMovie *enemyMovie = new QMovie(":/images/assets/monster/mimic/idle.gif");
enemyImgLabel->setMovie(enemyMovie);
enemyMovie->start();
enemyImgLabel->setAlignment(Qt::AlignCenter);
} else if (enemyType == "GrupoDeInimigos") {
QMovie *enemyMovie = new QMovie(":/images/assets/group-enemy.gif");
enemyImgLabel->setMovie(enemyMovie);
enemyMovie->start();
enemyImgLabel->setAlignment(Qt::AlignCenter);
}
// Exibe a imagem abaixo doa botões
enemyImgLabel->setGeometry(0, 0, width(), height());
enemyImgLabel->show();
enemyImgLabel->lower();
}
void BattleWindow::attack()
{
// Esconde a mensagem do Hp
hpMessage->hide();
// Obtém as instâncias dos personagens do GameData
GameData* gameData = GameData::getInstance();
player = gameData->getPlayer();
enemy = gameData->getEnemy();
// Lógica para o ataque durante a batalha
AttackResult playerResult = player->attackEnemy(*enemy);
updateHealthLabels(); // Atualiza os rótulos de saúde
// Exibe mensagem de resultado do ataque do jogador
resultAttackLabel->setText(playerResult.message);
resultAttackLabel->show();
// Lógica para a resposta do inimigo
if (enemy->getHealth() > 0) {
AttackResult enemyResult = enemy->attackEnemy(*player, false);
updateHealthLabels(); // Atualiza os rótulos de saúde após o ataque do inimigo
// Adiciona a mensagem de resultado do ataque do inimigo ao texto existente
resultAttackLabel->setText(resultAttackLabel->text() + "\n" + enemyResult.message);
resultAttackLabel->show();
}
// Verifica se a batalha terminou
checkBattleResult();
}
void BattleWindow::usePotion()
{
GameData* gameData = GameData::getInstance();
player = gameData->getPlayer();
resultAttackLabel->hide();
// Lógica para o uso de poção durante a batalha
if (playerPotions > 0) {
// Recupera 20 de saúde
player->setHealth(player->getHealth() + 20);
// Atualiza o número de poções e os rótulos de saúde
playerPotions--;
updateHealthLabels();
// Exibe mensagem de recuperação de saúde
QString message = QString("Você recuperou 20 de HP. Poções sobrando: %1").arg(playerPotions);
hpMessage->setText(message);
hpMessage->show();
// Atualiza o número de poções no GameData
GameData::setPlayerPotions(playerPotions);
// Verifica se o jogador ainda tem poções disponíveis
if (playerPotions == 0) {
potionButton->setEnabled(false); // Desativa o botão se não houver mais poções
hpMessage->setText("Você já usou todas as poções!");
hpMessage->show();
}
} else {
hpMessage->setText("Você já usou todas as poções!");
hpMessage->show();
}
}
void BattleWindow::initStep()
{
// Mostra o texto inicial e o botão de iniciar
infoLabel->setText("Você iniciou uma batalha");
startButton->show();
// Esconde a QLabel do inimigo e os botões de ação
enemyImgLabel->hide();
attackButton->hide();
potionButton->hide();
}
void BattleWindow::updateHealthLabels()
{
// Atualiza os rótulos com informações de saúde dos personagens
GameData* gameData = GameData::getInstance();
Character* playerCharacter = gameData->getPlayer();
Character* enemyCharacter = gameData->getEnemy();
if (playerCharacter && enemyCharacter) {
// Posiciona o rótulo de saúde do jogador
playerHealthLabel->setText(QString("Jogador:\nSaúde: %1").arg(playerCharacter->getHealth()));
// Posiciona o rótulo de saúde do inimigo
enemyHealthLabel->setText(QString("Inimigo:\nSaúde: %1").arg(enemyCharacter->getHealth()));
} else {
qDebug() << "player and enemy not working";
}
}
void BattleWindow::checkBattleResult()
{
// Verifica se a batalha terminou (saúde de jogador ou inimigo <= 0)
GameData* gameData = GameData::getInstance();
player = gameData->getPlayer();
enemy = gameData->getEnemy();
if (player->getHealth() <= 0 || enemy->getHealth() <= 0) {
QString resultMessage;
if (player->getHealth() <= 0) {
resultMessage = "Você foi derrotado!";
} else {
resultMessage = "Você venceu!";
}
QMessageBox::information(this, "Fim da Batalha", resultMessage);
// Emite o sinal indicando que a batalha terminou
emit battleFinished();
// Fecha a janela de batalha
close();
}
}
void Character::setHealth(int newHealth) {
// Método da classe Character para configurar a saúde após o uso da potion
health = newHealth;
}