-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunderworld.cpp
More file actions
383 lines (331 loc) · 10.9 KB
/
underworld.cpp
File metadata and controls
383 lines (331 loc) · 10.9 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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
#include "underworld.h"
#include <QGraphicsScene>
#include <QPainter>
#include "bullet.h"
#include "containingbox.cpp"
#include "player.h"
#include <QTimer>
#include "enemy.h"
#include <QDebug>
#include "healthbar.h"
#include <QGraphicsView>
#include <QGraphicsRectItem>
#include "gameview.h"
#include "inventory.h"
#include <QMediaPlayer>
Underworld::Underworld(QGraphicsScene * main_scene)
{
scene_ = main_scene;
}
/*
Function: DrawUnderworld
Params: Enemy, Player
Desc: Given an enemy and a player to work with, creates necessary assets for the underworld.
Returns: None
*/
void Underworld::DrawUnderworld(Enemy *enemy, Player *player) {
this->enemy_ = enemy;
this->player_= player;
music = new QMediaPlayer();
music->setMedia(QUrl(enemy_->getMusic()));
music->play();
//EventHandler
//Containing box coordinates (temp)
//containing box width and height
int cwidth = 300;
int cheight = 300;
//starting point for containing box
cx1_ = scene_->width()/2 - 200;
cy1_ = scene_->height()/2 - 200;
//other corner
cx2_ = cx1_ + cwidth;
cy2_ = cy1_ + cheight;
int player_sprite_size = 25;
//Set Background
QImage *img = new QImage(":/images/battlebackground.jpg");
*img = img->scaled(100,100,Qt::KeepAspectRatioByExpanding);
QBrush bg_brush(*img);
scene_ ->setBackgroundBrush(bg_brush);
//DRAW AND POSITION THE PLAYER
if (dynamic_cast<SecondPlayer*>(player)){
QPixmap sprite = QPixmap(":/images/heart.png");
sprite = sprite.scaled(player_sprite_size, player_sprite_size,Qt::KeepAspectRatio);
player_->setPixmap(sprite);
} else {
QPixmap sprite = QPixmap(":/images/p2.png");
sprite = sprite.scaled(player_sprite_size, player_sprite_size,Qt::KeepAspectRatio);
player_->setPixmap(sprite);
}
Bounds bound = {cx1_, cy1_ , cx2_ - player_sprite_size, cy2_ - player_sprite_size};
player_->setBound(bound);
//DRAW THE BOX THE PLAYER MAY MOVE AROUND IN
ContainingBox *b = new ContainingBox(cx1_, cy1_, cwidth, cheight, Qt::GlobalColor::white, "");
scene_->addItem(b);
connect(this, &Underworld::OnBulletFired, this, &Underworld::FireBullet);
//DRAW THE ENEMY
enemy_->setPos(cx1_,cy1_-enemy_->pixmap().height()-100);
scene_->addItem(enemy_);
StatsDisplay * s = player_->getStats();
s->setPos(cx2_ - 150, cy2_- 500);
scene_->addItem(s);
//Player 1 Health Bar
HealthBar *ph = new HealthBar(cx1_, cy2_ + 10, cwidth, 20, player_->getMaxHealth(), player_->getHealth());
scene_->addItem(ph);
connect(player_, &Player::HealthChanged, ph, &HealthBar::ChangeHealth);
connect(player_, &Player::PlayerDied, this, &Underworld::OnPlayerDeath);
//Health Bar Temporary text
//Enemy health bar
HealthBar *eh = new HealthBar(cx1_, cy1_-100, cwidth, 50, enemy_->getMaxHealth(), enemy_->getHealth());
scene_->addItem(eh);
connect(this, &Underworld::OnEnemyHit, eh, &HealthBar::ChangeHealth);
//Draw boxes for options
fight_box_ = new ContainingBox(cx1_, cy2_ + 50, 150, 50, Qt::GlobalColor::green, "Fight [F]");
scene_->addItem(fight_box_);
bribe_box_ = new ContainingBox(cx1_ + 150, cy2_ + 50, 150, 50, Qt::GlobalColor::green, "Bribe [B]");
scene_->addItem(bribe_box_);
player_->getInventory()->setPos(cx1_ - 250, cy1_);
scene_->addItem(player_->getInventory());
}
/*
Function: ProcessAttackPattern
Params: Vector of Attack Patterns
Desc: Given an instructional list of bullets to fire, fires those bullets.
Returns: none
*/
void Underworld::ProcessAttackPattern(std::vector<AttackPattern> s) {
for (size_t i = 0; i < s.size(); i ++) {
QTimer::singleShot(s[i].delay, [=](){
if (!fight_over_) {
FireBullet(s[i].x, s[i].y, s[i].dir);
}
});
}
}
/*
Function: FireBullet
Params: x starting x, y starting y, direction to travel in
Desc: Fires a bullet and travels it in that direction until it hits the boundries.
Returns: none
*/
void Underworld::FireBullet(int x, int y, Direction d) {
Bounds bound = {cx1_ - 30,cy1_ - 30, cx2_ + 10, cy2_ + 10};
Bullet *b = new Bullet(x, y, d, scene_, bound);
scene_->addItem(b);
}
/*
Function: OnFightClicked
Params: none
Desc: Carries out an enemies' attack turn and processes damage
Returns: none
*/
void Underworld::OnFightClicked() {
//Send damage to the enemy and health bars
emit OnEnemyHit(-1 * player_->getDamage());
enemy_->changeHealth(-1 * player_->getDamage());
scene_->update();
//Check if enemy is dead
if (enemy_->IsDead()) {
EnemyDeath();
return;
}
InitiateFightSequence();
}
/*
Function: onItemUsed
Params: none
Desc: Uses an item in the player's inventory and applies effects to the fight. Starts enemy attack turn
Returns: none
*/
void Underworld::OnItemUsed() {
//Check if item kills enemy
if (enemy_->IsDead()) {
EnemyDeath();
return;
}
scene_->update();
InitiateFightSequence();
}
/*
Function: InitiateFightSequence
Params: none
Desc: Starts the enemy attack pattern, adding the movable player to the screen and removing some functionality.
Returns: none
*/
void Underworld::InitiateFightSequence() {
//If the enemy is dead, don't initiate a fight sequence
scene_->removeItem(fight_box_);
scene_->removeItem(bribe_box_);
fighting_ = true;
//Add the player to the scene, do health calculations
player_->setPos(cx1_ + 150, cy1_ + 150);
scene_->addItem(player_);
std::vector<AttackPattern> random_pattern = enemy_->GetFightPattern();
int fight_duration = random_pattern.back().delay;
//After a 1 second delay, initiate the bullets
QTimer::singleShot(1000, [=]() {
ProcessAttackPattern(random_pattern);
});
//After all bullets have been fired plus a few seconds, remove the player from the battle.
QTimer::singleShot(fight_duration + 3000, [=] () {
if(!fight_over_) {
scene_->addItem(fight_box_);
scene_->addItem(bribe_box_);
scene_->removeItem(player_);
fighting_ = false;
}
});
}
/*
Function: SwitchToOverWorld
Params: none
Desc: Removes necessary objects from the underworld and passes control back to the overworld.
Returns: none
*/
void Underworld::SwitchToOverWorld() {
scene_->clear();
GameView &game = GameView::GetInstance();
Mode mode = game.get_game_mode();
//Different modes depending on how many people are in the game
if (mode == SinglePlayer) {
game.CreateSinglePlayerOverWorld();
} else if (mode == TwoPlayer) {
game.CreateTwoPlayerOverWorld();
}
//If you delete the underwold object some memory is gonna leak
delete this;
}
/*
Function: EndBattle
Params: QString s, the string to display in the after message box.
Desc: Displays the after-battle stats and gains, then after a certain time calls switch to overworld.
Returns: none
*/
void Underworld::EndBattle(QString s) {
scene_->removeItem(player_->getInventory());
scene_->removeItem(player_->getStats());
scene_->clear();
scene_->update();
music->stop();
//Draw end game review
int x = scene_->width()/2 - 350;
int y = scene_->height()/2 - 200;
ContainingBox *end = new ContainingBox(x,y, 600, 200, Qt::GlobalColor::white, "");
QGraphicsTextItem *text = new QGraphicsTextItem(s);
text->setDefaultTextColor(Qt::GlobalColor::white);
text->setPos(x+20, y+20);
//Switches back to overworld in 5 seconds
QTimer::singleShot(3000, [=]() {
SwitchToOverWorld();
});
scene_->addItem(text);
scene_->addItem(end);
}
/*
Function: Bribe
Params: none
Desc: Carries out the bribe action, where the player doesn't fight and instead pays a gold premium.
Returns: none
*/
void Underworld::Bribe() {
fight_over_ = true;
player_->changeGold(-10);
EndBattle("You ran like a coward and lost 10 gold!");
}
/*
Function: EnemyDeath
Params: none
Desc: When the player successfully kills an enemy, the player receives bountiful rewards.
Returns: none
*/
void Underworld::EnemyDeath() {
fight_over_ = true;
player_->changeGold(enemy_->getGold());
std::string message = "You killed " + enemy_->getName() + " and received " + std::to_string(enemy_->getGold()) + " gold. ";
if (enemy_->getItem()){
player_->getInventory()->AddItem(enemy_->getItem(), false);
message += "They were also carrying a " + enemy_->getItem()->getName() + "!";
if (enemy_->getItem()->getItemType() == itemtype::Equipable){
enemy_->getItem()->Use(player_);
}
scene_->update();
}
QString q = QString::fromStdString(message);
EndBattle(q);
}
/*
Function: OnKeyPress
Params: QKeyEvent * event, the key the user is pressing
Desc: Given a key press, determines what action the user will take.
Returns: none
*/
void Underworld::OnKeyPress(QKeyEvent *event) {
if (fighting_ || fight_over_)
return;
if (event->key() == Qt::Key::Key_F) {
OnFightClicked();
}
else if (event->key() == Qt::Key::Key_B) {
Bribe();
}
else if (event->key() == Qt::Key::Key_1) {
//I'm not sure if this can be simplified to reduce repeated code
if (player_->getInventory()->GetConsumableItemsCount() < 1) {
return;
}
player_->UseItem(0);
InitiateFightSequence();
}
else if (event->key() == Qt::Key::Key_2) {
if (player_->getInventory()->GetConsumableItemsCount() < 2) {
return;
}
player_->UseItem(1);
InitiateFightSequence();
}
else if (event->key() == Qt::Key::Key_3) {
if (player_->getInventory()->GetConsumableItemsCount() < 3) {
return;
}
player_->UseItem(2);
InitiateFightSequence();
}
else if (event->key() == Qt::Key::Key_4) {
if (player_->getInventory()->GetConsumableItemsCount() < 3) {
return;
}
player_->UseItem(3);
InitiateFightSequence();
}
else if (event->key() == Qt::Key::Key_5) {
if (player_->getInventory()->GetConsumableItemsCount() < 3) {
return;
}
player_->UseItem(4);
InitiateFightSequence();
}
else if (event->key() == Qt::Key::Key_6) {
if (player_->getInventory()->GetConsumableItemsCount() < 3) {
return;
}
player_->UseItem(5);
InitiateFightSequence();
}
scene_->update();
}
/*
Function: OnPlayerDeath
Params: none
Desc: Punishes the player for losing a battle with monitary value.
Returns: none
*/
void Underworld::OnPlayerDeath() {
fight_over_ = true;
int lose_amount = -20;
if(player_->IsDead()) {
scene_->removeItem(player_);
player_->changeGold(lose_amount);
player_->changeHealth(5);
}
std::string message = "You lost to " + enemy_->getName() + " and lost " + std::to_string(lose_amount) + " gold!";
EndBattle(QString::fromStdString(message));
}