aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/cpp/RangedWeapon.cpp
blob: 13d4c7082b83a3f2ff89d84f4722e7c68a1d5810 (plain)
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 "RangedWeapon.hpp"
#include "Actor.hpp"
#include "Bullet.hpp"

RangedWeapon::RangedWeapon(Weapon::Class c, const std::string &name)
: Weapon(Item::Type::RANGEDWEAPON, c, name)
{
}

bool RangedWeapon::use(Actor &user, Actor &actor)
{
    //TODO implement attack probability
    if (_charged) {
        actor.damage(user.stat(Actor::Stat::ATTACK) 
            + _damage
            - actor.stat(Actor::Stat::DEFENCE)
        );

        _charged = false;
    }

    return true;
}

bool RangedWeapon::reload(Actor &actor)
{
    Bullet *bullet = nullptr;

    //TODO add bullet type
    std::list<Item*> inventory = actor.inventory();
    for (Item *item : inventory) {
        if (item->type == Item::Type::BULLET) {
            //TODO get the bullet
            bullet = dynamic_cast<Bullet*>(item);
            break;
        }
    }

    if (bullet == nullptr) {
        return false;
    }

    if (bullet->decreaseAmount(1)) {
        _charged = true;
        return true;
    }

    return false;
}