From 28681c7eb31d5e3108d1f96008e9063f0c373f7e Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Fri, 12 Oct 2012 19:04:34 +0200 Subject: [PATCH] Allow setting weapon damage via yaml config. --- source/items/Weapon.cpp | 20 +++++++++----------- source/items/Weapon.h | 10 ++++++---- source/sprite/Player.cpp | 3 ++- 3 files changed, 17 insertions(+), 16 deletions(-) diff --git a/source/items/Weapon.cpp b/source/items/Weapon.cpp index 338d26f..792217d 100755 --- a/source/items/Weapon.cpp +++ b/source/items/Weapon.cpp @@ -14,26 +14,24 @@ #include "../util/Collection.h" #include "../effects/Bullet.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 Vector2i& holderSize) : +Weapon::Weapon(const Instances& instances, Physical& holder, const Yaml& config) : Emitter(instances.collection), mHolder(holder), mBulletTexture(ResourceManager::i() .acquire(Loader::i().fromFile("bullet.png"))), 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 + - std::max(Bullet::SIZE.x, Bullet::SIZE.y) / 2) { -} - -Weapon::~Weapon() { + std::max(Bullet::SIZE.x, Bullet::SIZE.y) / 2), + mDamage(config.get(KEY_DAMAGE)) { } /** - * Call on any button press/refire. + * Pull the trigger. */ void Weapon::fire() { @@ -47,5 +45,5 @@ Weapon::createParticle() { Vector2f offset(- mOffset); thor::rotate(offset, mHolder.getAngle()); return std::shared_ptr(new Bullet(mHolder.getPosition() + offset, - mWorld, mBulletTexture, mHolder, mHolder.getAngle(), BULLET_DAMAGE)); + mWorld, mBulletTexture, mHolder, mHolder.getAngle(), mDamage)); } diff --git a/source/items/Weapon.h b/source/items/Weapon.h index 07a1e70..edd82b3 100755 --- a/source/items/Weapon.h +++ b/source/items/Weapon.h @@ -13,6 +13,7 @@ #include "../Instances.h" #include "../abstract/Physical.h" #include "../particle/Emitter.h" +#include "../util/Yaml.h" class Emitter; class Instances; @@ -26,8 +27,7 @@ class Physical; class Weapon : public Emitter { // Public functions. public: - Weapon(const Instances& instances, Physical& holder, const Vector2i& holderSize); - ~Weapon(); + Weapon(const Instances& instances, Physical& holder, const Yaml& config); void fire(); @@ -37,12 +37,14 @@ protected: // Private variables. private: - static const int BULLET_DAMAGE; + static const String KEY_DAMAGE; Physical& mHolder; std::shared_ptr mBulletTexture; 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_ */ diff --git a/source/sprite/Player.cpp b/source/sprite/Player.cpp index 2c95098..da6568b 100644 --- a/source/sprite/Player.cpp +++ b/source/sprite/Player.cpp @@ -12,6 +12,7 @@ #include "../util/Vector.h" #include "../items/Weapon.h" #include "../util/String.h" +#include "../util/Yaml.h" const float Player::SPEED = 100.0f; 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) : Character(instances, "player.png", PhysicalData(position, SIZE, instances.world, CATEGORY_ACTOR, MASK_ALL, true, false, true), 100), - mWeapon(instances, *this, SIZE), + mWeapon(instances, *this, Yaml("weapon.yaml")), mDirection(0), mPathfinder(instances.pathfinder) { }