Bullets are inserted at offset to avoid collisions on insert.

This commit is contained in:
Felix Ableitner 2012-09-11 22:02:46 +02:00
parent 59cd09ac16
commit c679a0ecaa
4 changed files with 23 additions and 10 deletions

View file

@ -27,7 +27,7 @@ public:
static const Vector2i SIZE; static const Vector2i SIZE;
// Private variables. // Private variables.
private: private:
static const float SPEED; static const float SPEED; //< If speed is too low, bullets push each other away on insert.
Physical& mShooter; Physical& mShooter;
const int mDamage; const int mDamage;
}; };

View file

@ -7,6 +7,10 @@
#include "Weapon.h" #include "Weapon.h"
#include <algorithm>
#include <thor/Vectors.hpp>
#include "../util/Collection.h" #include "../util/Collection.h"
#include "../effects/Bullet.h" #include "../effects/Bullet.h"
#include "../util/Loader.h" #include "../util/Loader.h"
@ -14,11 +18,16 @@
const int Weapon::BULLET_DAMAGE = 10; const int Weapon::BULLET_DAMAGE = 10;
Weapon::Weapon(Physical& holder, Collection& collection, b2World& world) : Weapon::Weapon(Physical& holder, Collection& collection, b2World& world,
const Vector2i& holderSize) :
Emitter(collection), Emitter(collection),
mHolder(holder), mHolder(holder),
mBulletTexture(ResourceManager::i().acquire(Loader::i().fromFile<sf::Texture>("bullet.png"))), mBulletTexture(ResourceManager::i()
mWorld(world) { .acquire(Loader::i().fromFile<sf::Texture>("bullet.png"))),
mWorld(world),
mOffset(0, std::max(holderSize.x, holderSize.y) / 2 +
b2_linearSlop +
std::max(Bullet::SIZE.x, Bullet::SIZE.y) / 2) {
} }
Weapon::~Weapon() { Weapon::~Weapon() {
@ -35,6 +44,9 @@ Weapon::fire() {
std::shared_ptr<Particle> std::shared_ptr<Particle>
Weapon::createParticle() { Weapon::createParticle() {
return std::shared_ptr<Particle>(new Bullet(mHolder.getPosition(), mWorld, mBulletTexture, // Minus to account for positive y-axis going downwards in SFML.
mHolder, mHolder.getAngle(), BULLET_DAMAGE)); Vector2f offset(- mOffset);
thor::rotate(offset, mHolder.getAngle());
return std::shared_ptr<Particle>(new Bullet(mHolder.getPosition() + offset,
mWorld, mBulletTexture, mHolder, mHolder.getAngle(), BULLET_DAMAGE));
} }

View file

@ -21,7 +21,7 @@
class Weapon : public Emitter { class Weapon : public Emitter {
// Public functions. // Public functions.
public: public:
Weapon(Physical& holder, Collection& collection, b2World& world); Weapon(Physical& holder, Collection& collection, b2World& world, const Vector2i& holderSize);
~Weapon(); ~Weapon();
void fire(); void fire();
@ -37,6 +37,7 @@ private:
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).
}; };
#endif /* DG_WEAPON_H_ */ #endif /* DG_WEAPON_H_ */

View file

@ -22,7 +22,7 @@ Player::Player(b2World& world, Collection& collection, const Vector2f& position)
Sprite("player.png", PhysicalData(position, SIZE, world, Sprite("player.png", PhysicalData(position, SIZE, world,
CATEGORY_ACTOR, MASK_ALL, true)), CATEGORY_ACTOR, MASK_ALL, true)),
Actor(100), Actor(100),
mWeapon(*this, collection, world), mWeapon(*this, collection, world, SIZE),
mDestination(Vector2i(50, 50)) { mDestination(Vector2i(50, 50)) {
} }