Fixed firing.

This commit is contained in:
Felix Ableitner 2013-05-26 20:44:59 +02:00
parent 0f5d0597fc
commit b9dc5b90d2
4 changed files with 14 additions and 19 deletions

View file

@ -20,7 +20,7 @@
* @param world Box2d world. * @param world Box2d world.
* @param texture Texture to display for bullet. * @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) : sf::Vector2f direction, const Yaml& config) :
Particle(position, CATEGORY_PARTICLE, ~CATEGORY_PARTICLE, Particle(position, CATEGORY_PARTICLE, ~CATEGORY_PARTICLE,
config, thor::rotatedVector(direction, -90.0f)), 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 void
Bullet::onCollide(std::shared_ptr<Sprite> other) { Bullet::onCollide(std::shared_ptr<Sprite> other) {
// Make sure we do not damage twice. // Make sure we do not damage twice.
if (!getDelete()) { if (!getDelete() && (&*other != &mShooter)) {
// Call onShot on other, with damage as param. // Call onShot on other, with damage as param.
if (other->getCategory() == CATEGORY_ACTOR) { if (other->getCategory() == CATEGORY_ACTOR) {
std::shared_ptr<Character> character = std::static_pointer_cast<Character>(other); std::shared_ptr<Character> character = std::static_pointer_cast<Character>(other);

View file

@ -10,6 +10,7 @@
#include "../particle/Particle.h" #include "../particle/Particle.h"
class Character;
class Yaml; class Yaml;
/** /**
@ -17,13 +18,13 @@ class Yaml;
*/ */
class Bullet : public Particle { class Bullet : public Particle {
public: public:
explicit Bullet(const sf::Vector2f& position, Sprite& shooter, explicit Bullet(const sf::Vector2f& position, Character& shooter,
sf::Vector2f direction, const Yaml& config); sf::Vector2f direction, const Yaml& config);
void onCollide(std::shared_ptr<Sprite> other); void onCollide(std::shared_ptr<Sprite> other);
private: private:
Sprite& mShooter; const Character& mShooter;
const int mDamage; const int mDamage;
const float mSpeed; const float mSpeed;
}; };

View file

@ -13,20 +13,14 @@
#include "../effects/Bullet.h" #include "../effects/Bullet.h"
#include "../util/Yaml.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), Emitter(world),
mHolder(holder), mHolder(holder),
mBullet(config.get(YAML_KEY::BULLET, YAML_DEFAULT::BULLET)), mBullet(config.get(YAML_KEY::BULLET, YAML_DEFAULT::BULLET)),
mLastShotWaitInterval(0), mLastShotWaitInterval(0),
mFireInterval(config.get(YAML_KEY::INTERVAL, YAML_DEFAULT::INTERVAL)), mFireInterval(config.get(YAML_KEY::INTERVAL, YAML_DEFAULT::INTERVAL)),
mFire(false), mFire(false),
mAutomatic(config.get(YAML_KEY::AUTOMATIC, YAML_DEFAULT::AUTOMATIC)) { 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);
} }
/** /**
@ -70,7 +64,7 @@ Weapon::onThink(int elapsed) {
std::shared_ptr<Sprite> std::shared_ptr<Sprite>
Weapon::createParticle() { Weapon::createParticle() {
// Minus to account for positive y-axis going downwards in SFML. // 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())); thor::rotate(offset, thor::polarAngle(mHolder.getDirection()));
return std::shared_ptr<Sprite>(new Bullet(mHolder.getPosition() + offset, return std::shared_ptr<Sprite>(new Bullet(mHolder.getPosition() + offset,
mHolder, mHolder.getDirection(), Yaml(mBullet))); mHolder, mHolder.getDirection(), Yaml(mBullet)));

View file

@ -14,10 +14,10 @@
#include "../particle/Emitter.h" #include "../particle/Emitter.h"
class Sprite; class Character;
class Yaml;
class World; class World;
class Particle; class Particle;
class Yaml;
/** /**
* Loading mechanism: * Loading mechanism:
@ -26,7 +26,7 @@ class Particle;
*/ */
class Weapon : public Emitter { class Weapon : public Emitter {
public: public:
explicit Weapon(World& world, Sprite& holder, const Yaml& config); explicit Weapon(World& world, Character& holder, const Yaml& config);
void pullTrigger(); void pullTrigger();
void releaseTrigger(); void releaseTrigger();
@ -36,9 +36,8 @@ protected:
std::shared_ptr<Sprite> createParticle(); std::shared_ptr<Sprite> createParticle();
private: 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. const std::string mBullet; //< Bullet config filename.
int mLastShotWaitInterval; //< Remaining time left after firing last bullet before firing next one. int mLastShotWaitInterval; //< Remaining time left after firing last bullet before firing next one.
const int mFireInterval; //< Time between firing bullets. const int mFireInterval; //< Time between firing bullets.