-
-
Notifications
You must be signed in to change notification settings - Fork 103
/
ranged_weapon.cpp
77 lines (65 loc) · 2.54 KB
/
ranged_weapon.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
/* Copyright (C) 2013-2014 Michal Brzozowski (rusolis@poczta.fm)
This file is part of KeeperRL.
KeeperRL is free software; you can redistribute it and/or modify it under the terms of the
GNU General Public License as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
KeeperRL is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program.
If not, see http://www.gnu.org/licenses/ . */
#include "stdafx.h"
#include "ranged_weapon.h"
#include "creature.h"
#include "level.h"
#include "attack.h"
#include "view.h"
#include "game.h"
#include "sound.h"
#include "attack_level.h"
#include "attack_type.h"
#include "skill.h"
#include "creature_attributes.h"
#include "player_message.h"
#include "view_id.h"
#include "event_listener.h"
#include "vision.h"
SERIALIZE_DEF(RangedWeapon, damageAttr, projectileName, projectileViewId, maxDistance)
SERIALIZATION_CONSTRUCTOR_IMPL(RangedWeapon)
RangedWeapon::RangedWeapon(AttrType attr, const string& name, ViewId id, int dist)
: damageAttr(attr), projectileName(name), projectileViewId(id), maxDistance(dist) {}
void RangedWeapon::fire(WCreature c, Vec2 dir) const {
CHECK(dir.length8() == 1);
c->getGame()->getView()->addSound(SoundId::SHOOT_BOW);
int damage = c->getAttr(damageAttr);
Attack attack(c, Random.choose(AttackLevel::LOW, AttackLevel::MIDDLE, AttackLevel::HIGH),
AttackType::SHOOT, damage, damageAttr, none);
const auto position = c->getPosition();
auto vision = c->getVision().getId();
Position lastPos;
int distance = 0;
for (Position pos = position.plus(dir);; pos = pos.plus(dir)) {
lastPos = pos;
distance++;
if (auto c = pos.getCreature()) {
c->you(MsgType::HIT_THROWN_ITEM, "the " + projectileName);
c->takeDamage(attack);
break;
}
if (pos.stopsProjectiles(vision)) {
pos.globalMessage("the " + projectileName + " hits the " + pos.getName());
break;
}
if (distance >= maxDistance) {
pos.globalMessage("the " + projectileName + " falls short.");
break;
}
}
c->getGame()->addEvent(EventInfo::Projectile{projectileViewId, position, lastPos});
}
AttrType RangedWeapon::getDamageAttr() const {
return damageAttr;
}
int RangedWeapon::getMaxDistance() const {
return maxDistance;
}