forked from Haroooold/motaGroup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbattle.cpp
174 lines (150 loc) · 5.4 KB
/
battle.cpp
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
/*
* File: battle.cpp
* ----------------
* This file implements battle.h interface.
*/
#include "battle.h"
using namespace std;
extern Game *game;
battle::battle(Monster * monster, Hero * hero)
{
// if (!checkBattle()) delete this; // bug
// get Monster hp & atk & def
monsterId = monster->getId();
monsterHp = monster->getHp();
monsterAtk = monster->getAtk();
monsterDef = monster->getDef();
monsterHpInfo = "HP:\t" + QString::number(monsterHp);
monsterAtkInfo = "ATK:\t" + QString::number(monsterAtk);
monsterDefInfo = "DEF:\t" + QString::number(monsterDef);
// get Hero hp & atk & def
heroHp = hero->getHp();
heroAtk = hero->getAtk();
heroDef = hero->getDef();
heroHpInfo = "HP:\t" + QString::number(heroHp);
heroAtkInfo = "ATK:\t" + QString::number(heroAtk);
heroDefInfo = "DEF:\t" + QString::number(heroDef);
// get harm value (>=0)
monsterHarm = max(0, monsterAtk-heroDef);
heroHarm = max(0, heroAtk-monsterDef);
battleFrameShow(monster, hero); // show battle frame
}
/*
* Implementation notes: battleFrameShow(Monster* monster, Hero* hero)
* ---------------------------------------------------------------------
* This function creates a <code>battleFrame</code> and displays information
* relevant to battle.
*/
void battle::battleFrameShow(Monster * monster, Hero * hero)
{
// create battle frame
battleFrame = new QGraphicsRectItem();
battleFrame->setRect(100, 50, 440, 220);
QBrush brush;
brush.setStyle(Qt::SolidPattern);
brush.setColor(Qt::darkCyan);
battleFrame->setBrush(brush);
game->scene->addItem(battleFrame);
battleFrame->setFlag(QGraphicsItem::ItemIsFocusable); // avoid events' influence
battleFrame->setFocus();
// create battle information & image
monsterHpText = new QGraphicsTextItem(monsterHpInfo);
monsterAtkText = new QGraphicsTextItem(monsterAtkInfo);
monsterDefText = new QGraphicsTextItem(monsterDefInfo);
monsterHpText->setPos(200,50);
monsterAtkText->setPos(200,100);
monsterDefText->setPos(200,150);
heroHpText = new QGraphicsTextItem(heroHpInfo);
heroAtkText = new QGraphicsTextItem(heroAtkInfo);
heroDefText = new QGraphicsTextItem(heroDefInfo);
heroHpText->setPos(330,50);
heroAtkText->setPos(330,100);
heroDefText->setPos(330,150);
game->scene->addItem(monsterHpText);
game->scene->addItem(monsterAtkText);
game->scene->addItem(monsterDefText);
game->scene->addItem(heroHpText);
game->scene->addItem(heroAtkText);
game->scene->addItem(heroDefText);
// add monster & hero image to battle frame
hero_img_battle = new QGraphicsPixmapItem();
QString hero_img_path = ":/images/down1.png";
QImage hero_img;
hero_img.load(hero_img_path);
QPixmap hero_pic = QPixmap::fromImage(hero_img.scaled(80, 80));
hero_img_battle->setPixmap(hero_pic);
hero_img_battle->setPos(450,80);
game->scene->addItem(hero_img_battle);
monster_img_battle = new QGraphicsPixmapItem();
QString monster_img_path = QString(":/images/") + QString::number(monsterId) + QString(".png");
QImage monster_img;
monster_img.load(monster_img_path);
QPixmap monster_pic = QPixmap::fromImage(monster_img.scaled(80, 80));
monster_img_battle->setPixmap(monster_pic);
monster_img_battle->setPos(110,80);
game->scene->addItem(monster_img_battle);
// TO DO: add name to battle frame
// battle
timer = new QTimer(this);
timer->start(300);
connect(timer,SIGNAL(timeout()),this,SLOT(battleOnce()));
// update information
hero->setHp(heroHp);
hero->setMoney(hero->getMoney()+monster->getMoney());
hero->setExperience(hero->getExperience()+monster->getExperience());
game->updateInfo();
hero->setFocusToSelf(); // focus back
}
/*
* Implementation notes: checkBattle(Monster* monster, Hero* hero)
* ---------------------------------------------------------------
* Check whether hero is stronger than monster. Only hp, atk & def would
* be considered.
*/
bool battle::checkBattle()
{
int numOfRounds = monsterHp / heroHarm;
if (numOfRounds*monsterHarm >= heroHp) return false;
return true;
}
/*
* Implemenetation notes: updateText()
* -----------------------------------
* Upate HP text after every round of battle.
*/
void battle::updateText()
{
monsterHpInfo = "HP:\t" + QString::number(monsterHp);
heroHpInfo = "HP:\t" + QString::number(heroHp);
monsterHpText->setPlainText(monsterHpInfo);
heroHpText->setPlainText(heroHpInfo);
}
/*
* Implementation notes: battleOnce()
* ----------------------------------
* If <code>monsterHp</code> is no smaller than 0, battle once;
* otherwise, delete the battle frame. <code>timer</code> should
* be stopped, or it would continue all the time.
*/
void battle::battleOnce()
{
monsterHp -= heroHarm;
if (monsterHp <= 0)
{
monsterHp = 0;
updateText();
game->scene->removeItem(battleFrame);
game->scene->removeItem(monsterHpText);
game->scene->removeItem(monsterAtkText);
game->scene->removeItem(monsterDefText);
game->scene->removeItem(heroHpText);
game->scene->removeItem(heroAtkText);
game->scene->removeItem(heroDefText);
game->scene->removeItem(hero_img_battle);
game->scene->removeItem(monster_img_battle);
timer->stop(); // stop timer
}
updateText();
heroHp -= monsterHarm;
updateText();
}