-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgun.cpp
49 lines (44 loc) · 1.04 KB
/
gun.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
#include "gun.h"
#include "global_consts.h"
#include <QtDebug>
Gun::Gun(bool automatic, int fireFrequency)
: automatic(automatic), fireFrequency(fireFrequency)
{
}
void Gun::reload()
{
reloading = true;
reloaded();
QTimer::singleShot(RELOADING_TIME, [this](){ reloading = false; });
}
void Gun::addAmmo(int ammo)
{
if (currentLoadedAmmo + ammo > capacityAmmo)
{
currentOwnedAmmo += capacityAmmo - currentLoadedAmmo;
currentLoadedAmmo = capacityAmmo;
}
else
currentLoadedAmmo += ammo;
}
void Gun::reset()
{
currentLoadedAmmo = capacityAmmo;
currentOwnedAmmo = maxAmmo;
}
void Gun::reloaded()
{
if (currentOwnedAmmo > 0 && currentLoadedAmmo < capacityAmmo)
{
if (capacityAmmo - currentLoadedAmmo < currentOwnedAmmo)
{
currentOwnedAmmo -= capacityAmmo - currentLoadedAmmo;
currentLoadedAmmo = capacityAmmo;
}
else
{
currentLoadedAmmo += currentOwnedAmmo;
currentOwnedAmmo = 0;
}
}
}