-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshooter.cpp
183 lines (161 loc) · 4.54 KB
/
shooter.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
#include "shooter.h"
#include <math.h>
#include <QGraphicsScene>
static const double Pi = 3.14159265358979323846264338327950288419717;
static double TwoPi = 2.0 * Pi;
static qreal normalizeAngle(qreal angle)
{
while (angle < 0)
angle += TwoPi;
while (angle > TwoPi)
angle -= TwoPi;
return angle;
}
Shooter::Shooter(QObject *parent)
: Target(parent)
{
setRotation(0);
target = QPointF(0,0);
shot = false;
gameTimer = new QTimer();
connect(gameTimer, &QTimer::timeout, this, &Shooter::slotGameTimer);
gameTimer->start(10);
target = QPointF(0,0);
health = 7;
maxHealth = health;
this->bulletTimer->start(1000/6);
}
Shooter::~Shooter()
{
qDebug() << "Shooter destructor was called";
delete gameTimer;
}
QRectF Shooter::boundingRect() const
{
return QRectF(-15,-15,35,35);
}
void Shooter::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
QPolygon polygon;
polygon << QPoint(0,-15) << QPoint(10,15) << QPoint(-10,15);
painter->setBrush(Qt::blue);
painter->drawPolygon(polygon);
painter->setPen(Qt::NoPen);
painter->setBrush(Qt::green);
painter->drawRect(-10,18, 20 * health/maxHealth ,2);
Q_UNUSED(option);
Q_UNUSED(widget);
}
void Shooter::slotTarget(QPointF point)
{
target = point;
QLineF lineToTarget(QPointF(0, 0), mapFromScene(target));
qreal angleToTarget = ::acos(lineToTarget.dx() / lineToTarget.length());
if (lineToTarget.dy() < 0)
angleToTarget = TwoPi - angleToTarget;
angleToTarget = normalizeAngle((Pi - angleToTarget) + Pi / 2);
if (angleToTarget >= 0 && angleToTarget < Pi) {
setRotation(rotation() - angleToTarget * 180 /Pi);
} else if (angleToTarget <= TwoPi && angleToTarget > Pi) {
setRotation(rotation() + (angleToTarget - TwoPi )* (-180) /Pi);
}
}
void Shooter::slotGameTimer()
{
if(GetAsyncKeyState('A')){
this->setX(this->x() - 1);
if(!scene()->collidingItems(this).isEmpty()){
this->setX(this->x() + 1);
}
}
if(GetAsyncKeyState('D')){
this->setX(this->x() + 1);
if(!scene()->collidingItems(this).isEmpty()){
this->setX(this->x() - 1);
}
}
if(GetAsyncKeyState('W')){
this->setY(this->y() - 1);
if(!scene()->collidingItems(this).isEmpty()){
this->setY(this->y() + 1);
}
}
if(GetAsyncKeyState('S')){
this->setY(this->y() + 1);
if(!scene()->collidingItems(this).isEmpty()){
this->setY(this->y() - 1);
}
}
if(GetAsyncKeyState(VK_LEFT)){
this->setX(this->x() - 1);
if(!scene()->collidingItems(this).isEmpty()){
this->setX(this->x() + 1);
}
}
if(GetAsyncKeyState(VK_RIGHT)){
this->setX(this->x() + 1);
if(!scene()->collidingItems(this).isEmpty()){
this->setX(this->x() - 1);
}
}
if(GetAsyncKeyState(VK_UP)){
this->setY(this->y() - 1);
if(!scene()->collidingItems(this).isEmpty()){
this->setY(this->y() + 1);
}
}
if(GetAsyncKeyState(VK_DOWN)){
this->setY(this->y() + 1);
if(!scene()->collidingItems(this).isEmpty()){
this->setY(this->y() - 1);
}
}
if(this->x() - 30 < 0){
this->setX(30);
}
if(this->x() + 30 > 500){
this->setX(500 - 30);
}
if(this->y() - 30 < 0){
this->setY(30);
}
if(this->y() + 30 > 500){
this->setY(500 - 30);
}
QLineF lineToTarget(QPointF(0, 0), mapFromScene(target));
qreal angleToTarget = ::acos(lineToTarget.dx() / lineToTarget.length());
if (lineToTarget.dy() < 0)
angleToTarget = TwoPi - angleToTarget;
angleToTarget = normalizeAngle((Pi - angleToTarget) + Pi / 2);
if (angleToTarget >= 0 && angleToTarget < Pi) {
setRotation(rotation() - angleToTarget * 180 /Pi);
} else if (angleToTarget <= TwoPi && angleToTarget > Pi) {
setRotation(rotation() + (angleToTarget - TwoPi )* (-180) /Pi);
}
}
void Shooter::slotBulletTimer()
{
if(shot) emit signalBullet(mapToScene(0,-25), target, this);
}
void Shooter::slotShot(bool shot)
{
this->shot = shot;
}
QPainterPath Shooter::shape() const
{
QPainterPath path;
path.addEllipse(boundingRect());
return path;
}
void Shooter::hit(int damage)
{
health -= damage;
this->update(QRectF(-15,-15,35,35));
if(health <= 0) {
this->deleteLater();
emit signalDie();
}
}
ObjectType Shooter::getType(){
return SHOOTER;
}