Fixed firing.
This commit is contained in:
parent
0f5d0597fc
commit
b9dc5b90d2
4 changed files with 14 additions and 19 deletions
|
@ -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<Sprite> 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> character = std::static_pointer_cast<Character>(other);
|
||||
|
|
|
@ -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<Sprite> other);
|
||||
|
||||
private:
|
||||
Sprite& mShooter;
|
||||
const Character& mShooter;
|
||||
const int mDamage;
|
||||
const float mSpeed;
|
||||
};
|
||||
|
|
|
@ -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<Sprite>
|
||||
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<Sprite>(new Bullet(mHolder.getPosition() + offset,
|
||||
mHolder, mHolder.getDirection(), Yaml(mBullet)));
|
||||
|
|
|
@ -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<Sprite> 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.
|
||||
|
|
Reference in a new issue