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;
// Private variables.
private:
static const float SPEED;
static const float SPEED; //< If speed is too low, bullets push each other away on insert.
Physical& mShooter;
const int mDamage;
};

View file

@ -7,18 +7,27 @@
#include "Weapon.h"
#include <algorithm>
#include <thor/Vectors.hpp>
#include "../util/Collection.h"
#include "../effects/Bullet.h"
#include "../util/Loader.h"
#include "../util/ResourceManager.h"
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),
mHolder(holder),
mBulletTexture(ResourceManager::i().acquire(Loader::i().fromFile<sf::Texture>("bullet.png"))),
mWorld(world) {
mBulletTexture(ResourceManager::i()
.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() {
@ -35,6 +44,9 @@ Weapon::fire() {
std::shared_ptr<Particle>
Weapon::createParticle() {
return std::shared_ptr<Particle>(new Bullet(mHolder.getPosition(), mWorld, mBulletTexture,
mHolder, mHolder.getAngle(), BULLET_DAMAGE));
// Minus to account for positive y-axis going downwards in SFML.
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 {
// Public functions.
public:
Weapon(Physical& holder, Collection& collection, b2World& world);
Weapon(Physical& holder, Collection& collection, b2World& world, const Vector2i& holderSize);
~Weapon();
void fire();
@ -37,6 +37,7 @@ private:
Physical& mHolder;
std::shared_ptr<sf::Texture> mBulletTexture;
b2World& mWorld;
const Vector2f mOffset; //< Offset to the point where bullets are inserted (from holder center).
};
#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,
CATEGORY_ACTOR, MASK_ALL, true)),
Actor(100),
mWeapon(*this, collection, world),
mWeapon(*this, collection, world, SIZE),
mDestination(Vector2i(50, 50)) {
}