Allow setting weapon damage via yaml config.

This commit is contained in:
Felix Ableitner 2012-10-12 19:04:34 +02:00
parent 76b074220d
commit 28681c7eb3
3 changed files with 17 additions and 16 deletions

View file

@ -14,26 +14,24 @@
#include "../util/Collection.h" #include "../util/Collection.h"
#include "../effects/Bullet.h" #include "../effects/Bullet.h"
#include "../util/Loader.h" #include "../util/Loader.h"
#include "../util/ResourceManager.h" #include "../util/ResourceManager.h"
const String Weapon::KEY_DAMAGE = "damage";
const int Weapon::BULLET_DAMAGE = 10; Weapon::Weapon(const Instances& instances, Physical& holder, const Yaml& config) :
Weapon::Weapon(const Instances& instances, Physical& holder, const Vector2i& holderSize) :
Emitter(instances.collection), Emitter(instances.collection),
mHolder(holder), mHolder(holder),
mBulletTexture(ResourceManager::i() mBulletTexture(ResourceManager::i()
.acquire(Loader::i().fromFile<sf::Texture>("bullet.png"))), .acquire(Loader::i().fromFile<sf::Texture>("bullet.png"))),
mWorld(instances.world), mWorld(instances.world),
mOffset(0, std::max(holderSize.x, holderSize.y) / 2 + mOffset(0, std::max(mHolder.getSize().x, mHolder.getSize().y) / 2 +
b2_linearSlop + b2_linearSlop +
std::max(Bullet::SIZE.x, Bullet::SIZE.y) / 2) { std::max(Bullet::SIZE.x, Bullet::SIZE.y) / 2),
} mDamage(config.get<int>(KEY_DAMAGE)) {
Weapon::~Weapon() {
} }
/** /**
* Call on any button press/refire. * Pull the trigger.
*/ */
void void
Weapon::fire() { Weapon::fire() {
@ -47,5 +45,5 @@ Weapon::createParticle() {
Vector2f offset(- mOffset); Vector2f offset(- mOffset);
thor::rotate(offset, mHolder.getAngle()); thor::rotate(offset, mHolder.getAngle());
return std::shared_ptr<Particle>(new Bullet(mHolder.getPosition() + offset, return std::shared_ptr<Particle>(new Bullet(mHolder.getPosition() + offset,
mWorld, mBulletTexture, mHolder, mHolder.getAngle(), BULLET_DAMAGE)); mWorld, mBulletTexture, mHolder, mHolder.getAngle(), mDamage));
} }

View file

@ -13,6 +13,7 @@
#include "../Instances.h" #include "../Instances.h"
#include "../abstract/Physical.h" #include "../abstract/Physical.h"
#include "../particle/Emitter.h" #include "../particle/Emitter.h"
#include "../util/Yaml.h"
class Emitter; class Emitter;
class Instances; class Instances;
@ -26,8 +27,7 @@ class Physical;
class Weapon : public Emitter { class Weapon : public Emitter {
// Public functions. // Public functions.
public: public:
Weapon(const Instances& instances, Physical& holder, const Vector2i& holderSize); Weapon(const Instances& instances, Physical& holder, const Yaml& config);
~Weapon();
void fire(); void fire();
@ -37,12 +37,14 @@ protected:
// Private variables. // Private variables.
private: private:
static const int BULLET_DAMAGE; static const String KEY_DAMAGE;
Physical& mHolder; Physical& mHolder;
std::shared_ptr<sf::Texture> mBulletTexture; std::shared_ptr<sf::Texture> mBulletTexture;
b2World& mWorld; b2World& mWorld;
const Vector2f mOffset; //< Offset to the point where bullets are inserted (from holder center).
const Vector2f mOffset; //< Offset to the point where bullets are inserted (from holder center).
const int mDamage;
}; };
#endif /* DG_WEAPON_H_ */ #endif /* DG_WEAPON_H_ */

View file

@ -12,6 +12,7 @@
#include "../util/Vector.h" #include "../util/Vector.h"
#include "../items/Weapon.h" #include "../items/Weapon.h"
#include "../util/String.h" #include "../util/String.h"
#include "../util/Yaml.h"
const float Player::SPEED = 100.0f; const float Player::SPEED = 100.0f;
const Vector2i Player::SIZE = Vector2i(50, 50); const Vector2i Player::SIZE = Vector2i(50, 50);
@ -23,7 +24,7 @@ const float Player::POINT_REACHED_DISTANCE = 1.0f;
Player::Player(const Instances& instances, const Vector2f& position) : Player::Player(const Instances& instances, const Vector2f& position) :
Character(instances, "player.png", PhysicalData(position, SIZE, instances.world, Character(instances, "player.png", PhysicalData(position, SIZE, instances.world,
CATEGORY_ACTOR, MASK_ALL, true, false, true), 100), CATEGORY_ACTOR, MASK_ALL, true, false, true), 100),
mWeapon(instances, *this, SIZE), mWeapon(instances, *this, Yaml("weapon.yaml")),
mDirection(0), mDirection(0),
mPathfinder(instances.pathfinder) { mPathfinder(instances.pathfinder) {
} }