-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtower.cpp
More file actions
145 lines (127 loc) · 4.36 KB
/
tower.cpp
File metadata and controls
145 lines (127 loc) · 4.36 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
#include "tower.h"
#include "bullet.h"
#include "globals.h"
#include <QPainter>
#include <QGraphicsScene>
#include <cmath>
#include <QGraphicsSceneMouseEvent>
#include <QPixmap>
#include <QDebug>
#include <QTimer>
#include <QGraphicsOpacityEffect>
#include<mainwindow.h>
Tower::Tower(const QString &imagePath, QGraphicsItem *parent)
: QObject(), QGraphicsItem(parent),
attackInterval(1000),
damageOfTower(10),
type(0),
pathOfTower(0),
level(1),
maxLevel(3),
attackRange(250),
attackTimer(new QTimer(this)),
showAttackRange(false),
imagePath(imagePath) { // 初始化图片路径
connect(attackTimer, &QTimer::timeout, this, &Tower::attackEnemy);
attackTimer->start(attackInterval); // 使用 attackInterval 进行攻击
}
QRectF Tower::boundingRect() const {
return QRectF(0, 0, 120, 110);
}
void Tower::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *) {
if (!imagePath.isEmpty()) {
QPixmap pixmap(imagePath);
if (!pixmap.isNull()) {
painter->drawPixmap(boundingRect().toRect(), pixmap);
} else {
qDebug() << "Failed to load tower image:" << imagePath;
}
} else {
painter->setBrush(Qt::blue);
painter->drawRect(boundingRect());
}
if (showAttackRange) {
QColor rangeColor(255, 0, 0, 50); // 红色,透明度为50
painter->setBrush(rangeColor);
painter->setPen(Qt::NoPen);
painter->drawEllipse(QPointF(60, 55), attackRange, attackRange); // 以塔中心为圆心绘制攻击范围
}
painter->setPen(Qt::red);
painter->drawText(boundingRect(), Qt::AlignCenter, QString("Lv %1").arg(level));
}
void Tower::setAttackInterval(int interval) {
attackInterval = interval;
attackTimer->setInterval(attackInterval); // 更新攻击定时器的间隔
}
void Tower::setDamage(int dmg) {
damageOfTower = dmg;
}
void Tower::mousePressEvent(QGraphicsSceneMouseEvent *event) {
if (canUpgrade()) {
upgrade(0); // 默认升级路径
}
showAttackRange = !showAttackRange; // 切换攻击范围显示状态
update(); // 重新绘制塔
QGraphicsItem::mousePressEvent(event); // 保持默认事件处理
}
void Tower::attackEnemy() {
Enemy *target = findClosestEnemy();
if (target && stopnow == 0) {
shootAt(target);
}
}
void Tower::upgrade(int path) {
if (level < maxLevel) {
level++; // 增加等级
// 提升塔的属性
setDamage(damageOfTower + 5); // 每次升级增加5点攻击力
setAttackInterval(qMax(200, attackInterval - 200)); // 减少攻击间隔,每次减少200毫秒,最小到200毫秒
attackRange += 20; // 增加攻击范围
// 刷新塔的显示
update(); // 立即触发 paint() 方法
}
}
Enemy* Tower::findClosestEnemy() {
QList<QGraphicsItem *> items = scene()->items();
Enemy *closestEnemy = nullptr;
qreal closestDistance = attackRange; // 以攻击范围作为初始最大距离
for (QGraphicsItem *item : items) {
Enemy *enemy = dynamic_cast<Enemy *>(item);
if (enemy) {
QPointF theCenter = enemy->pos() + enemy->boundingRect().center();
qreal distance = QLineF(pos(), theCenter).length();
if (distance < closestDistance) {
closestDistance = distance;
closestEnemy = enemy;
}
}
}
return closestEnemy; // 返回在攻击范围内的最近敌人
}
void Tower::shootAt(Enemy *target) {
Bullet *bullet = new Bullet(target);
bullet->damageOfBullet = damageOfTower;
bullet->bulletPath = pathOfTower;
bullet->poison = false;
bullet->slowdown = false;
if (type == 1) {
bullet->poison = true;
} else if (type == 2) {
bullet->slowdown = true;
} else if (type == 3) {
bullet->fast = true;
}
bullet->setPos(boundingRect().center() + pos()); // 从塔的中心发射
scene()->addItem(bullet);
}
bool Tower::canUpgrade() const {
if(level < maxLevel && globalMoney >= this->upgradeCost()){
return true;
}
else{
return false;
}
}
int Tower::upgradeCost() const {
return 50 + level * 50; // 假设每次升级的费用增加
}