-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcombatroundcontroller.cpp
More file actions
264 lines (207 loc) · 8.08 KB
/
combatroundcontroller.cpp
File metadata and controls
264 lines (207 loc) · 8.08 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
#include "combatroundcontroller.h"
#include "hitrolldialog.h"
#include "enginioclient.h"
#include "playereditdialog.h"
#include <QFile>
CombatRoundController::CombatRoundController(QObject *parent) : QObject(parent)
{
_model = new CombatRoundModel(this);
_playerModel = new PlayerListModel();
}
CombatRoundController::~CombatRoundController()
{
}
void CombatRoundController::generateWeaponList()
{
QMap<QString, Weapon*>::iterator i;
for (i = _model->weapons().begin(); i != _model->weapons().end(); ++i)
{
//qDebug() << i.key() << ": " << i.value()->toString() << endl;
_combatRoundWidget->addWeapon(i.key());
}
}
void CombatRoundController::setCombatRoundWidget(CombatRoundWidget *widget)
{
_combatRoundWidget = widget;
_combatRoundWidget->setPlayerListModel(_playerModel);
_currentAttacker = NULL;
_currentDefender = NULL;
connect(_combatRoundWidget,SIGNAL(attackerHPUpdated(int)),this,SLOT(attackerHPUpdated(int)));
connect(_combatRoundWidget,SIGNAL(defenderHPUpdated(int)),this,SLOT(defenderHPUpdated(int)));
connect(_combatRoundWidget,SIGNAL(defenderShieldUpdated(bool)),this,SLOT(defenderShieldUpdated(bool)));
connect(_combatRoundWidget,SIGNAL(bonusUpdated(int)),this,SLOT(bonusUpdated(int)));
connect(_combatRoundWidget,SIGNAL(playRoundClicked()),this,SLOT(playRound()));
connect(_combatRoundWidget,SIGNAL(editPlayer(QModelIndex)),this,SLOT(editPlayer(QModelIndex)));
connect(_combatRoundWidget,SIGNAL(newAttackerAdded(QString)),this,SLOT(newAttackerAdded(QString)));
connect(_combatRoundWidget,SIGNAL(weaponSelected(QString)),this,SLOT(weaponSelected(QString)));
connect(_combatRoundWidget,SIGNAL(attackUpdated(int)),this,SLOT(attackUpdated(int)));
connect(_combatRoundWidget,SIGNAL(defenderSelected(QModelIndex)),this,SLOT(defenderSelected(QModelIndex)));
connect(_combatRoundWidget,SIGNAL(attackerSelected(QModelIndex)),this,SLOT(attackerSelected(QModelIndex)));
connect(_combatRoundWidget,SIGNAL(defenceUpdated(int)),this,SLOT(defenceUpdated(int)));
connect(_combatRoundWidget,SIGNAL(clearClicked()),this,SLOT(clearDeadPeople()));
connect(_model,SIGNAL(finished()),this,SLOT(generateWeaponList()));
_model->fetchData();
}
void CombatRoundController::editPlayer(QModelIndex index)
{
PlayerEditDialog dialog(_combatRoundWidget);
Player *p = _playerModel->player(index);
dialog.setPlayer(p);
dialog.exec();
}
void CombatRoundController::defenderShieldUpdated(bool s)
{
qDebug() << "set shield to " << s;
//if (_currentDefender)
this->_currentDefender->setShield(s);
}
void CombatRoundController::clearDeadPeople()
{
//Memory leak
_currentAttacker = NULL;
_currentDefender = NULL;
_currentAttackerWeapon = NULL;
_combatRoundWidget->clearPlayers();
_playerModel->clearDeads();
return;
}
void CombatRoundController::attackerHPUpdated(int hp)
{
if (!_currentAttacker)
return;
_currentAttacker->setHealthPoints(hp);
}
void CombatRoundController::bonusUpdated(int bonus)
{
if (!_currentAttacker || !_currentAttackerWeapon)
return;
qDebug() << "Bonus updated:" << bonus;
_currentAttacker->setBonus(_currentAttackerWeapon->toString(),bonus);
}
void CombatRoundController::defenderHPUpdated(int hp)
{
if (!_currentDefender)
return;
_currentDefender->setHealthPoints(hp);
}
void CombatRoundController::playRound()
{
HitRollDialog hitdialog(this->_combatRoundWidget);
if (!_currentAttacker || !_currentDefender || !_currentAttackerWeapon)
return;
hitdialog.setAttack(_currentAttack);
hitdialog.setDefence(_currentDefence);
hitdialog.setParticipants(_currentAttacker, _currentDefender);
if (hitdialog.exec())
{
ArmourBypassDialog armourdialog(this->_combatRoundWidget);
armourdialog.setWeapon(_currentAttackerWeapon);
armourdialog.setArmour(_currentDefender->armour());
armourdialog.setParticipants(_currentAttacker,_currentDefender);
if (armourdialog.exec())
{
_currentDefender->takeHit(_currentAttackerWeapon->_dammage+_currentAttacker->bonus(_currentAttackerWeapon->toString()));
}
}
}
void CombatRoundController::attackerSelected(const QModelIndex &index)
{
if (!index.isValid())
return;
//qDebug() << "Attacker selected: "<< attacker;
_currentAttacker = _playerModel->player(index);
if (!_currentAttacker)
return;
_combatRoundWidget->setHealthPointsAttacker(_currentAttacker->healthPoints());
QString lastWeapon = _currentAttacker->lastWeaponUsed();
qDebug() << "Last weapon used for player " << _currentAttacker->name() << " is " << lastWeapon;
_combatRoundWidget->setCurrentWeapon(lastWeapon);
_combatRoundWidget->setCurrentBonus(_currentAttacker->bonus(lastWeapon));
this->weaponSelected(lastWeapon);//force update if lastWeapon is already the selected weapon but code executed twice then
_currentAttackerWeapon = _model->weapons()[lastWeapon];
assert(_currentAttackerWeapon);
/** @todo this will add a new entry for given lastDefender with an empty Player pointer to change */
_currentDefender = _playerModel->player(_currentAttacker->lastDefenderName());
if (_currentDefender)
{
QModelIndex index = _playerModel->find(_currentDefender->name());
_combatRoundWidget->setCurrentDefender(index);
this->defenderSelected(_playerModel->find(_currentDefender->name()));
}
_combatRoundWidget->enableWeapons(true);
_combatRoundWidget->enableHitRoll(true);
//_combatRoundWidget->enableHealthPointsAttacker(true);
}
void CombatRoundController::newAttackerAdded(QString newAttacker)
{
//qDebug() << "newAttacker Added";
if (!_playerModel->exists(newAttacker))
{
Player *p = new Player(newAttacker);
_playerModel->addPlayer(p);
qDebug() << "newAttacker added: " << newAttacker;
_currentAttacker = p;
}
else
{
qDebug() << "Attacker already exists:" << newAttacker;
}
}
void CombatRoundController::newDefenderAdded(QString newDefender)
{
if (!_playerModel->exists(newDefender))
{
Player *p = new Player(newDefender);
_playerModel->addPlayer(p);
qDebug() << "newDefender added: " << newDefender;
}
else
{
qDebug() << "Attacker already exists:" << newDefender;
}
}
void CombatRoundController::weaponSelected(const QString &weapon)
{
qDebug() << "weapon selected: "<< weapon;
_currentAttackerWeapon = _model->weapons()[weapon];
if (!_currentAttacker)
return;
assert(_currentAttackerWeapon);
_currentAttack = _currentAttacker->attack(_currentAttackerWeapon->toString());
QString str = QString("Attack for player %1 with weapon %2 is %3").arg(_currentAttacker->name()).arg(_currentAttackerWeapon->toString()).arg(_currentAttack);
_combatRoundWidget->setCurrentAttack(_currentAttack);
}
void CombatRoundController::weaponBonusUpdated(const int &bonus)
{
qDebug() << "Bonus Updated: "<< bonus;
}
void CombatRoundController::attackUpdated(int attack)
{
qDebug() << "Attack Updated: "<< attack;
_currentAttack = attack;
if (!_currentAttacker || !_currentAttackerWeapon)
return;
_currentAttacker->setAttack(_currentAttackerWeapon->toString(),_currentAttack);
}
void CombatRoundController::defenderSelected(const QModelIndex &index)
{
if(!index.isValid())
return;
_currentDefender = _playerModel->player(index);
if (!_currentDefender || !_currentAttacker)
return;
qDebug() << "Defender Selected: "<< _currentDefender->name();
_currentAttacker->setDefenderName(_currentDefender->name());
_currentDefence = _currentDefender->defence();
_combatRoundWidget->setHealthPointsDefender(_currentDefender->healthPoints());
_combatRoundWidget->setShield(_currentDefender->hasShield());
_combatRoundWidget->setCurrentDefence(_currentDefence);
}
void CombatRoundController::defenceUpdated(int defence)
{
qDebug() << "Defence Updated: "<< defence;
_currentDefence = defence;
if (!_currentDefender)
return;
_currentDefender->setDefence(defence);
}