From b9dc5b90d2d334811c537bf04211ad100197b23c Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Sun, 26 May 2013 20:44:59 +0200 Subject: [PATCH] Fixed firing. --- source/effects/Bullet.cpp | 7 ++++--- source/effects/Bullet.h | 5 +++-- source/items/Weapon.cpp | 12 +++--------- source/items/Weapon.h | 9 ++++----- 4 files changed, 14 insertions(+), 19 deletions(-) diff --git a/source/effects/Bullet.cpp b/source/effects/Bullet.cpp index a87cf97..6dee287 100755 --- a/source/effects/Bullet.cpp +++ b/source/effects/Bullet.cpp @@ -20,7 +20,7 @@ * @param world Box2d world. * @param texture Texture to display for bullet. */ -Bullet::Bullet(const sf::Vector2f& position, Sprite& shooter, +Bullet::Bullet(const sf::Vector2f& position, Character& shooter, sf::Vector2f direction, const Yaml& config) : Particle(position, CATEGORY_PARTICLE, ~CATEGORY_PARTICLE, config, thor::rotatedVector(direction, -90.0f)), @@ -31,12 +31,13 @@ Bullet::Bullet(const sf::Vector2f& position, Sprite& shooter, } /** - * @copydoc Physical::onCollide + * Deletes this and calls onDamage if other is a character. Does not + * damage shooter. */ void Bullet::onCollide(std::shared_ptr other) { // Make sure we do not damage twice. - if (!getDelete()) { + if (!getDelete() && (&*other != &mShooter)) { // Call onShot on other, with damage as param. if (other->getCategory() == CATEGORY_ACTOR) { std::shared_ptr character = std::static_pointer_cast(other); diff --git a/source/effects/Bullet.h b/source/effects/Bullet.h index e1bd78b..e9531c2 100755 --- a/source/effects/Bullet.h +++ b/source/effects/Bullet.h @@ -10,6 +10,7 @@ #include "../particle/Particle.h" +class Character; class Yaml; /** @@ -17,13 +18,13 @@ class Yaml; */ class Bullet : public Particle { public: - explicit Bullet(const sf::Vector2f& position, Sprite& shooter, + explicit Bullet(const sf::Vector2f& position, Character& shooter, sf::Vector2f direction, const Yaml& config); void onCollide(std::shared_ptr other); private: - Sprite& mShooter; + const Character& mShooter; const int mDamage; const float mSpeed; }; diff --git a/source/items/Weapon.cpp b/source/items/Weapon.cpp index 1679fdc..00950de 100755 --- a/source/items/Weapon.cpp +++ b/source/items/Weapon.cpp @@ -13,20 +13,14 @@ #include "../effects/Bullet.h" #include "../util/Yaml.h" -Weapon::Weapon(World& world, Sprite& holder, const Yaml& config) : +Weapon::Weapon(World& world, Character& holder, const Yaml& config) : Emitter(world), mHolder(holder), mBullet(config.get(YAML_KEY::BULLET, YAML_DEFAULT::BULLET)), mLastShotWaitInterval(0), mFireInterval(config.get(YAML_KEY::INTERVAL, YAML_DEFAULT::INTERVAL)), mFire(false), - mAutomatic(config.get(YAML_KEY::AUTOMATIC, YAML_DEFAULT::AUTOMATIC)) { - sf::Vector2f holderSize = mHolder.getSize(); - Yaml bullet(mBullet); - sf::Vector2f bulletSize = bullet.get(YAML_KEY::SIZE, sf::Vector2f()); - mOffset = sf::Vector2f(0, - std::max(holderSize.x, holderSize.y) / 2 + - std::max(bulletSize.x, bulletSize.y) / 2); + mAutomatic(config.get(YAML_KEY::AUTOMATIC, YAML_DEFAULT::AUTOMATIC)) { } /** @@ -70,7 +64,7 @@ Weapon::onThink(int elapsed) { std::shared_ptr Weapon::createParticle() { // Minus to account for positive y-axis going downwards in SFML. - sf::Vector2f offset(- mOffset); + sf::Vector2f offset(0, - mHolder.getRadius()); thor::rotate(offset, thor::polarAngle(mHolder.getDirection())); return std::shared_ptr(new Bullet(mHolder.getPosition() + offset, mHolder, mHolder.getDirection(), Yaml(mBullet))); diff --git a/source/items/Weapon.h b/source/items/Weapon.h index 7f3f55c..2315836 100755 --- a/source/items/Weapon.h +++ b/source/items/Weapon.h @@ -14,10 +14,10 @@ #include "../particle/Emitter.h" -class Sprite; -class Yaml; +class Character; class World; class Particle; +class Yaml; /** * Loading mechanism: @@ -26,7 +26,7 @@ class Particle; */ class Weapon : public Emitter { public: - explicit Weapon(World& world, Sprite& holder, const Yaml& config); + explicit Weapon(World& world, Character& holder, const Yaml& config); void pullTrigger(); void releaseTrigger(); @@ -36,9 +36,8 @@ protected: std::shared_ptr createParticle(); private: - Sprite& mHolder; + Character& mHolder; - sf::Vector2f mOffset; //< Offset to the point where bullets are inserted (from holder center). const std::string mBullet; //< Bullet config filename. int mLastShotWaitInterval; //< Remaining time left after firing last bullet before firing next one. const int mFireInterval; //< Time between firing bullets.