-
Notifications
You must be signed in to change notification settings - Fork 0
/
boss.cpp
91 lines (81 loc) · 2.52 KB
/
boss.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
#include "boss.h"
#include "gamecontroller.h"
#include <QPainter>
#include <QDebug>
Boss::Boss(GameController &controller):
controller(controller)
{
setData(GD_type, GO_Boss);
pixMap.load("://images/Boss.png");
gradient = new QLinearGradient(QPoint(-50, -50), QPoint(50, 50));
gradient->setColorAt(0, Qt::red);
gradient->setColorAt(0.2, Qt::yellow);
gradient->setColorAt(0.7, Qt::blue);
gradient->setColorAt(1, Qt::green);
setPos(viewWidth/2, 150);
life = fullLife = 30 * difficulty;
rad = cnt = 0;
}
QRectF Boss::boundingRect() const
{
int w = pixMap.width(), h = pixMap.height();
return QRectF(-w/2, -h/2 - 10, w, h + 10);
}
void Boss::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
Q_UNUSED(option); Q_UNUSED(widget);
if(!pixMap.isNull()) {
painter->save();
painter->scale(1.3, 1.3);
int w = pixMap.width(), h = pixMap.height();
painter->drawPixmap(QPoint(-w/2, -h/2), pixMap);
painter->setBrush(*gradient);
painter->drawRect(-w/2, -h/2 - 10, w * life / fullLife, 8);
painter->restore();
}
}
void Boss::advance(int phace)
{
if(!phace) return;
if(++cnt == 10) {
rad = (rad + 18) % 360;
controller.shootBossBall(pos(), rad + 0);
controller.shootBossBall(pos(), rad + 72);
controller.shootBossBall(pos(), rad + 144);
controller.shootBossBall(pos(), rad + 216);
controller.shootBossBall(pos(), rad + 288);
cnt = 0;
}
handleCollisions();
}
void Boss::handleCollisions()
{
if(life <= 0){
handleDie();
return;
}
QList<QGraphicsItem *> collisions = collidingItems();
foreach (QGraphicsItem *item, collisions) {
auto t = item->data(GD_type);
if(t == GO_Bullet || t == GO_WingBullet) {
controller.ariseCollision(pos());
controller.removeItem(item);
if(--life <= 0)
handleDie();
return;
}
}
}
void Boss::changeLife(const int d)
{
life += d;
}
void Boss::handleDie()
{
controller.updateText(2000 + qrand() % 100);
controller.removeItem(this);
controller.addLifeAdder(QPointF(pos().x()-100, pos().y()+30)); //出现补给箱
controller.addLifeAdder(QPointF(pos().x(), pos().y()+30));
controller.addLifeAdder(QPointF(pos().x()+100, pos().y()+30));
QTimer::singleShot(70000, &controller, SLOT(addBoss()));
}