-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshooting.cpp
74 lines (64 loc) · 1.97 KB
/
shooting.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
#include "shooting.h"
#include "hero.h"
#include <QtDebug>
Shooting::Shooting(Hero *owner, Gun *gun, QGraphicsScene *mainScene, bool mainPlayer)
: mainPlayer(mainPlayer), owner(owner), gun(gun), mainScene(mainScene)
{
connect(&timer, SIGNAL(timeout()), this, SLOT(shot()));
}
void Shooting::start()
{
if (gun->ammoLoaded() > 0)
{
shot();
if (gun->isAutomatic())
timer.start(gun->getFireFrequency());
else
QTimer::singleShot(200, [this](){ owner->setCurrentlyShooting(false); });
}
else
{
gun->reload();
QTimer::singleShot(RELOADING_TIME, [this](){ owner->setCurrentlyShooting(false); });
if (mainPlayer)
emit (owner->ammoChangedReloading());
}
}
void Shooting::stop()
{
timer.stop();
}
void Shooting::shot()
{
lineAngle = owner->lineAngle();
if (owner->gunENUM() == SHOTGUN)
for (int i = -4; i <= 4; i+=2)
createBullet(i);
else
createBullet();
gun->decreaseAmmo();
if (gun->ammoLoaded() == 0)
{
timer.stop();
if (!mainPlayer)
gun->reload();
}
if (mainPlayer)
emit (owner->ammoChangedNoReloading());
}
void Shooting::createBullet()
{
Bullet * bullet = new Bullet(owner, gun->getDamage(), 5*qCos(qDegreesToRadians(lineAngle)),
-5*qSin(qDegreesToRadians(lineAngle)));
bullet->setPos(owner->x() + 5, owner->y() + 5);
mainScene->addItem(bullet);
}
void Shooting::createBullet(int xy_)
{
Bullet * bullet = new Bullet(owner, gun->getDamage() + QRandomGenerator::global()->bounded(3),
5*qCos(qDegreesToRadians(lineAngle + QRandomGenerator::global()->bounded(-7,7))),
-5*qSin(qDegreesToRadians(lineAngle + QRandomGenerator::global()->bounded(-7,7)))
);
bullet->setPos(owner->x() + 5 + xy_, owner->y() + 5 + xy_);
mainScene->addItem(bullet);
}