-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathboard.cpp
315 lines (267 loc) · 8.69 KB
/
board.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
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
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
* Copyright (C) 2015 Simon Stuerz <[email protected]> *
* *
* This file is part of Monster Wars. *
* *
* Monster Wars is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, version 3 of the License. *
* *
* Monster Wars is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with Monster Wars. If not, see <http://www.gnu.org/licenses/>. *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include "board.h"
#include "level.h"
#include "gameengine.h"
#include "monster.h"
#include "player.h"
#include "attack.h"
#include "math.h"
#include <QJsonDocument>
#include <QSettings>
Board::Board(GameEngine *engine):
QObject(engine),
m_level(0),
m_engine(engine),
m_levelId(1)
{
m_monsters = new MonsterModel(this);
m_attack = new Attack(this);
connect(m_attack, &Attack::attackFinished, this, &Board::attackFinished);
}
void Board::setLevel(Level *level)
{
resetBoard();
m_level = level;
m_levelId = level->levelId();
emit levelIdChanged();
qDebug() << "loaded " << m_level->name() << "on board...";
// add player 1 (the human player)
Player *player = new Player(1, this);
player->setColorString("blue");
player->setPlayerType(Player::PlayerTypeHuman);
player->setReproduction(m_engine->playerSettings()->reproductionPoints());
player->setDefence(m_engine->playerSettings()->defensePoints());
player->setStrength(m_engine->playerSettings()->strengthPoints());
player->setSpeed(m_engine->playerSettings()->speedPoints());
m_players.append(player);
qDebug() << " -> Create Player" << player->id() << player->playerTypeString();
// add the dummy player from type PlayerTypeNone (id 0)
Player *dummyPlayer = new Player(0, this);
m_players.append(dummyPlayer);
foreach (const QVariant &playerJson, m_level->playersVariant()) {
m_players.append(createPlayer(playerJson.toMap()));
}
emit playersChanged();
foreach (const QVariant &monsterJson, m_level->monstersVariant()) {
m_monsters->addMonster(createMonster(monsterJson.toMap()));
}
emit monstersChanged();
resetSelections();
}
Level *Board::level() const
{
return m_level;
}
int Board::levelId() const
{
return m_levelId;
}
QList<Player *> Board::playersList()
{
return m_players;
}
QList<Monster *> Board::monstersList()
{
return m_monsters->monsters();
}
QList<Monster *> Board::freeMonsters()
{
QList<Monster *> freeMonsters;
foreach (Monster *monster, m_monsters->monsters()) {
if (monster->player()->id() == 0) {
freeMonsters.append(monster);
}
}
return freeMonsters;
}
QList<Monster *> Board::myMonsters(Player *player)
{
QList<Monster *> myMonsters;
foreach (Monster *monster, m_monsters->monsters()) {
if (monster->player()->id() == player->id()) {
myMonsters.append(monster);
}
}
return myMonsters;
}
QList<Monster *> Board::enemyMonsters(Player *player)
{
QList<Monster *> enemyMonsters;
foreach (Monster *monster, m_monsters->monsters()) {
if (monster->player()->id() != player->id() && monster->player()->id() !=0 ) {
enemyMonsters.append(monster);
}
}
return enemyMonsters;
}
QQmlListProperty<Player> Board::players()
{
return QQmlListProperty<Player>(this, m_players);
}
MonsterModel *Board::monsters()
{
return m_monsters;
}
int Board::rows() const
{
return m_engine->rows();
}
int Board::columns() const
{
return m_engine->columns();
}
int Board::monsterCount() const
{
return m_monsters->monsters().count();
}
Monster *Board::monster(int id) const
{
return m_monsters->monsterWithId(id);
}
Player *Board::player(int id) const
{
foreach (Player *player, m_players) {
if(player->id() == id){
return player;
}
}
return NULL;
}
void Board::evaluateReleased(const int &monsterId)
{
Monster* m = monster(monsterId);
m->select(true);
if (m_attack->sourceIds().isEmpty()) {
m_attack->beginnAttack(m->id());
} else {
// check if the source monsters are still my monster
foreach (int sourceId, m_attack->sourceIds()) {
Monster* sourceMonster = monster(sourceId);
if (sourceMonster->player()->id() != 1) {
qDebug() << "remove" << sourceId << "from attack, no longer my monster";
m_attack->removeSourceId(sourceId);
}
}
m_attack->endAttack(m->id());
}
}
void Board::evaluateHovered(const bool &hovering, const int &monsterId)
{
if (!m_engine->running())
return;
Monster* m = monster(monsterId);
if (!hovering) {
if (m->player()->id() != 1) {
m->select(false);
}
}
if (hovering) {
if (m_attack->sourceIds().isEmpty() && m->player()->id() != 1) {
return;
}
m->select(true);
if (m_attack->sourceIds().isEmpty() && m->player()->id() == 1) {
m_attack->beginnAttack(monsterId);
} else if (m->player()->id() == 1) {
m_attack->addMonsterId(monsterId);
}
}
}
void Board::resetSelections()
{
foreach (Monster* monster, m_monsters->monsters()) {
monster->select(false);
}
m_attack->reset();
}
void Board::resetBoard()
{
qDebug() << "Clean up board.";
// delete monsters
foreach (Monster *monster, m_monsters->monsters()) {
monster->deleteLater();
}
// delete players
foreach (Player *player, m_players) {
player->deleteLater();
}
m_level = 0;
m_players.clear();
emit playersChanged();
m_monsters->clearModel();
emit monstersChanged();
}
void Board::tick()
{
foreach (Monster *monster, m_monsters->monsters()) {
monster->tick();
}
}
Monster *Board::createMonster(QVariantMap monsterJson)
{
int id = monsterJson.value("id").toInt();
QString monsterTypeString = monsterJson.value("monsterType").toString();
QPoint position(monsterJson.value("x").toInt(), monsterJson.value("y").toInt());
int startValue = monsterJson.value("startValue").toInt();
int size = monsterJson.value("size").toInt();
int playerId = monsterJson.value("player").toInt();
qDebug() << " -> Create Monster" << id << monsterTypeString;
Monster *monster = new Monster(m_engine, Monster::MonsterTypeNormal, -1, startValue, QPoint(), "white", this);
monster->setId(id);
monster->setMonsterType(monsterTypeString);
monster->setPosition(position);
monster->setSize(size);
foreach (Player* player, m_players) {
if (player->id() == playerId) {
monster->setPlayer(player);
}
}
return monster;
}
Player *Board::createPlayer(QVariantMap playerJson)
{
int id = playerJson.value("id").toInt();
QString color = playerJson.value("color").toString();
QString playerType = playerJson.value("playerType").toString();
int strength = playerJson.value("strength").toInt();
int speed = playerJson.value("speed").toInt();
int reproduction = playerJson.value("reproduction").toInt();
int defense = playerJson.value("defense").toInt();
int reaction = playerJson.value("reaction").toInt();
qDebug() << " -> Create Player" << id << playerType;
Player* player = new Player(id, m_level);
player->setColorString(color);
player->setPlayerType(playerType);
player->setStrength(strength);
player->setSpeed(speed);
player->setDefence(defense);
player->setReproduction(reproduction);
player->setReaction(reaction);
return player;
}
void Board::attackFinished()
{
if (m_attack->sourceIds().isEmpty())
return;
m_engine->startAttack(m_attack);
m_attack->reset();
resetSelections();
}