Repository navigation
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
313 lines (256 loc) · 6.85 KB
/
Copy pathmainwindow.cpp
File metadata and controls
313 lines (256 loc) · 6.85 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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "waypoint.h"
#include "enemy.h"
#include "bullet.h"
#include "audioplayer.h"
#include "plistreader.h"
#include <QPainter>
#include <QMouseEvent>
#include <QtGlobal>
#include <QMessageBox>
#include <QTimer>
#include <QXmlStreamReader>
#include <QtDebug>
static const int TowerCost = 300;
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
, m_waves(0)
, m_playerHp(5)
, m_playrGold(1000)
, m_gameEnded(false)
, m_gameWin(false)
{
//此处开始构造MainWindow
ui->setupUi(this);
preLoadWavesInfo();
loadTowerPositions(); //调用位置函数
addWayPoints();
m_audioPlayer = new AudioPlayer(this);
m_audioPlayer->startBGM();
//每30ms发送一个更新信号
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(updateMap()));
timer->start(30);
// 设置300ms后游戏启动
QTimer::singleShot(300, this, SLOT(gameStart()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::loadTowerPositions()
{
//读取文件中的炮塔位置信息,可以不用深究,但是可以修改位置【可改】
//这里可以改变炮塔的位置,文件是html格式
QFile file(":/config/TowersPosition.plist");
if (!file.open(QFile::ReadOnly | QFile::Text))
{
QMessageBox::warning(this, "TowerDefense", "Cannot Open TowersPosition.plist");
return;
}
PListReader reader;
reader.read(&file);
QList<QVariant> array = reader.data();
foreach (QVariant dict, array)
{
QMap<QString, QVariant> point = dict.toMap();
int x = point.value("x").toInt();
int y = point.value("y").toInt();
m_towerPositionsList.push_back(QPoint(x, y));
}
file.close();
}
void MainWindow::paintEvent(QPaintEvent *)
{
if (m_gameEnded || m_gameWin)
{
QString text = m_gameEnded ? "YOU LOST!!!" : "YOU WIN!!!";
QPainter painter(this);
painter.setPen(QPen(Qt::red));
painter.drawText(rect(), Qt::AlignCenter, text);
return;
}
QPixmap cachePix(":/image/Bg.png"); //背景图片【可改】!!
//先在背景图片QPixmap上绘制,最后统一绘制QPixmap
QPainter cachePainter(&cachePix); //缓存,避免直接在界面上绘制而效率低下
//foreach手法,讲究
foreach (const TowerPosition &towerPos, m_towerPositionsList)
towerPos.draw(&cachePainter);
foreach (const Tower *tower, m_towersList)
tower->draw(&cachePainter);
foreach (const WayPoint *wayPoint, m_wayPointsList)
wayPoint->draw(&cachePainter);
foreach (const Enemy *enemy, m_enemyList)
enemy->draw(&cachePainter);
foreach (const Bullet *bullet, m_bulletList)
bullet->draw(&cachePainter);
drawWave(&cachePainter);
drawHP(&cachePainter);
drawPlayerGold(&cachePainter);
//初始化画笔
QPainter painter(this);
//画背景图片
painter.drawPixmap(0, 0, cachePix);//QPixmap cachePix(":/image/Bg.png");
}
void MainWindow::mousePressEvent(QMouseEvent *event)
{
//单击鼠标后的处理
QPoint pressPos = event->pos();
auto it = m_towerPositionsList.begin();
while (it != m_towerPositionsList.end())
{
if (canBuyTower() && it->containPoint(pressPos) && !it->hasTower())
{
m_audioPlayer->playSound(TowerPlaceSound);
m_playrGold -= TowerCost;
it->setHasTower();
Tower *tower = new Tower(it->centerPos(), this);
m_towersList.push_back(tower);
update(); //调用paintevent(),重绘画面
break;
}
++it;
}
}
bool MainWindow::canBuyTower() const
{
if (m_playrGold >= TowerCost)
return true;
return false;
}
void MainWindow::drawWave(QPainter *painter)
{
painter->setPen(QPen(Qt::red));
painter->drawText(QRect(400, 5, 100, 25), QString("WAVE : %1").arg(m_waves + 1));
}
void MainWindow::drawHP(QPainter *painter)
{
painter->setPen(QPen(Qt::red));
painter->drawText(QRect(30, 5, 100, 25), QString("HP : %1").arg(m_playerHp));
}
void MainWindow::drawPlayerGold(QPainter *painter)
{
painter->setPen(QPen(Qt::red));
painter->drawText(QRect(200, 5, 200, 25), QString("GOLD : %1").arg(m_playrGold));
}
void MainWindow::doGameOver()
{
if (!m_gameEnded)
{
m_gameEnded = true;
// 此处应该切换场景到结束场景
// 暂时以打印替代,见paintEvent处理
}
}
void MainWindow::awardGold(int gold)
{
m_playrGold += gold;
update();
}
AudioPlayer *MainWindow::audioPlayer() const
{
return m_audioPlayer;
}
void MainWindow::addWayPoints()
{
//敌人航点【可改】
WayPoint *wayPoint1 = new WayPoint(QPoint(420, 285));
m_wayPointsList.push_back(wayPoint1);
WayPoint *wayPoint2 = new WayPoint(QPoint(35, 285));
m_wayPointsList.push_back(wayPoint2);
wayPoint2->setNextWayPoint(wayPoint1);
WayPoint *wayPoint3 = new WayPoint(QPoint(35, 195));
m_wayPointsList.push_back(wayPoint3);
wayPoint3->setNextWayPoint(wayPoint2);
WayPoint *wayPoint4 = new WayPoint(QPoint(445, 195));
m_wayPointsList.push_back(wayPoint4);
wayPoint4->setNextWayPoint(wayPoint3);
WayPoint *wayPoint5 = new WayPoint(QPoint(445, 100));
m_wayPointsList.push_back(wayPoint5);
wayPoint5->setNextWayPoint(wayPoint4);
WayPoint *wayPoint6 = new WayPoint(QPoint(35, 100));
m_wayPointsList.push_back(wayPoint6);
wayPoint6->setNextWayPoint(wayPoint5);
}
void MainWindow::getHpDamage(int damage/* = 1*/)
{
m_audioPlayer->playSound(LifeLoseSound);
m_playerHp -= damage;
if (m_playerHp <= 0)
doGameOver();
}
void MainWindow::removedEnemy(Enemy *enemy)
{
Q_ASSERT(enemy);
m_enemyList.removeOne(enemy);
delete enemy;
if (m_enemyList.empty())
{
++m_waves;
if (!loadWave())
{
m_gameWin = true;
// 游戏胜利转到游戏胜利场景
// 这里暂时以打印处理
}
}
}
void MainWindow::removedBullet(Bullet *bullet)
{
Q_ASSERT(bullet);
m_bulletList.removeOne(bullet);
delete bullet;
}
void MainWindow::addBullet(Bullet *bullet)
{
Q_ASSERT(bullet);
m_bulletList.push_back(bullet);
}
void MainWindow::updateMap()
{
foreach (Enemy *enemy, m_enemyList)
enemy->move();
foreach (Tower *tower, m_towersList)
tower->checkEnemyInRange();
update();
}
void MainWindow::preLoadWavesInfo()
{
QFile file(":/config/Waves.plist");
if (!file.open(QFile::ReadOnly | QFile::Text))
{
QMessageBox::warning(this, "TowerDefense", "Cannot Open TowersPosition.plist");
return;
}
PListReader reader;
reader.read(&file);
// 获取波数信息
m_wavesInfo = reader.data();
file.close();
}
bool MainWindow::loadWave()
{
if (m_waves >= m_wavesInfo.size())
return false;
WayPoint *startWayPoint = m_wayPointsList.back();
QList<QVariant> curWavesInfo = m_wavesInfo[m_waves].toList();
for (int i = 0; i < curWavesInfo.size(); ++i)
{
QMap<QString, QVariant> dict = curWavesInfo[i].toMap();
int spawnTime = dict.value("spawnTime").toInt();
Enemy *enemy = new Enemy(startWayPoint, this);
m_enemyList.push_back(enemy);
QTimer::singleShot(spawnTime, enemy, SLOT(doActivate()));
}
return true;
}
QList<Enemy *> MainWindow::enemyList() const
{
return m_enemyList;
}
void MainWindow::gameStart()
{
loadWave();
}