Added Yaml config for bullet.

This commit is contained in:
Felix Ableitner 2012-10-12 19:22:53 +02:00
parent 28681c7eb3
commit f23f2225db
5 changed files with 29 additions and 24 deletions

View file

@ -8,10 +8,12 @@
#include "Bullet.h" #include "Bullet.h"
#include "../abstract/Character.h" #include "../abstract/Character.h"
#include "../util/Loader.h"
#include "../util/ResourceManager.h"
const Vector2i Bullet::SIZE = Vector2i(20, 20); const Vector2i Bullet::SIZE = Vector2i(20, 20);
const String Bullet::KEY_DAMAGE = "damage";
const float Bullet::SPEED = 500.0f; const String Bullet::KEY_SPEED = "speed";
/** /**
* Places a bullet in the world. * Places a bullet in the world.
@ -20,13 +22,15 @@ const float Bullet::SPEED = 500.0f;
* @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 Vector2f& position, b2World& world, Bullet::Bullet(const Vector2f& position, b2World& world, Physical& shooter, float direction,
const std::shared_ptr<sf::Texture>& texture, Physical& shooter, float direction, int damage) : const Yaml& config) :
Particle(texture, PhysicalData(position, SIZE, world, CATEGORY_PARTICLE, Particle(ResourceManager::i().acquire(Loader::i().fromFile<sf::Texture>("bullet.png")),
CATEGORY_PARTICLE, true, true, true)), PhysicalData(position, SIZE, world, CATEGORY_PARTICLE, CATEGORY_PARTICLE,
true, true, true)),
mShooter(shooter), mShooter(shooter),
mDamage(damage) { mDamage(config.get<int>(KEY_DAMAGE)),
setSpeed(angle(direction), SPEED); mSpeed(config.get<int>(KEY_SPEED)) {
setSpeed(angle(direction), mSpeed);
setAngle(direction); setAngle(direction);
} }

View file

@ -9,8 +9,11 @@
#define DG_BULLET_H_ #define DG_BULLET_H_
#include "../particle/Particle.h" #include "../particle/Particle.h"
#include "../util/String.h"
#include "../util/Yaml.h"
class Particle; class Particle;
class Yaml;
/** /**
* Bullet particle fired by a weapon, may damage actors. * Bullet particle fired by a weapon, may damage actors.
@ -18,8 +21,8 @@ class Particle;
class Bullet : public Particle { class Bullet : public Particle {
// Public functions. // Public functions.
public: public:
Bullet(const Vector2f& position, b2World& world, const std::shared_ptr<sf::Texture>& texture, Bullet(const Vector2f& position, b2World& world, Physical& shooter, float direction,
Physical& shooter, float direction, int damage); const Yaml& config);
void onCollide(Physical& other, uint16 category); void onCollide(Physical& other, uint16 category);
bool doesCollide(Physical& other); bool doesCollide(Physical& other);
@ -30,9 +33,12 @@ public:
// Private variables. // Private variables.
private: private:
static const float SPEED; //< If speed is too low, bullets push each other away on insert. static const String KEY_DAMAGE;
static const String KEY_SPEED;
Physical& mShooter; Physical& mShooter;
const int mDamage; const int mDamage;
const float mSpeed;
}; };
#endif /* DG_BULLET_H_ */ #endif /* DG_BULLET_H_ */

View file

@ -13,21 +13,17 @@
#include "../util/Collection.h" #include "../util/Collection.h"
#include "../effects/Bullet.h" #include "../effects/Bullet.h"
#include "../util/Loader.h"
#include "../util/ResourceManager.h"
const String Weapon::KEY_DAMAGE = "damage"; const String Weapon::KEY_BULLET = "bullet";
Weapon::Weapon(const Instances& instances, Physical& holder, const Yaml& config) : Weapon::Weapon(const Instances& instances, Physical& holder, const Yaml& config) :
Emitter(instances.collection), Emitter(instances.collection),
mHolder(holder), mHolder(holder),
mBulletTexture(ResourceManager::i()
.acquire(Loader::i().fromFile<sf::Texture>("bullet.png"))),
mWorld(instances.world), mWorld(instances.world),
mOffset(0, std::max(mHolder.getSize().x, mHolder.getSize().y) / 2 + mOffset(0, std::max(mHolder.getSize().x, mHolder.getSize().y) / 2 +
b2_linearSlop + b2_linearSlop +
std::max(Bullet::SIZE.x, Bullet::SIZE.y) / 2), std::max(Bullet::SIZE.x, Bullet::SIZE.y) / 2),
mDamage(config.get<int>(KEY_DAMAGE)) { mBullet(config.get<String>(KEY_BULLET)) {
} }
/** /**
@ -44,6 +40,6 @@ Weapon::createParticle() {
// Minus to account for positive y-axis going downwards in SFML. // Minus to account for positive y-axis going downwards in SFML.
Vector2f offset(- mOffset); Vector2f offset(- mOffset);
thor::rotate(offset, mHolder.getAngle()); thor::rotate(offset, mHolder.getAngle());
return std::shared_ptr<Particle>(new Bullet(mHolder.getPosition() + offset, return std::shared_ptr<Particle>(new Bullet(mHolder.getPosition() + offset, mWorld,
mWorld, mBulletTexture, mHolder, mHolder.getAngle(), mDamage)); mHolder, mHolder.getAngle(), Yaml(mBullet)));
} }

View file

@ -18,6 +18,7 @@
class Emitter; class Emitter;
class Instances; class Instances;
class Physical; class Physical;
class Yaml;
/** /**
* Loading mechanism: * Loading mechanism:
@ -37,14 +38,13 @@ protected:
// Private variables. // Private variables.
private: private:
static const String KEY_DAMAGE; static const String KEY_BULLET;
Physical& mHolder; Physical& mHolder;
std::shared_ptr<sf::Texture> mBulletTexture;
b2World& mWorld; b2World& mWorld;
const Vector2f mOffset; //< Offset to the point where bullets are inserted (from holder center). const Vector2f mOffset; //< Offset to the point where bullets are inserted (from holder center).
const int mDamage; const String mBullet;
}; };
#endif /* DG_WEAPON_H_ */ #endif /* DG_WEAPON_H_ */

View file

@ -7,7 +7,6 @@
#include "Game.h" #include "Game.h"
#include "util/Loader.h" #include "util/Loader.h"
#include "util/Yaml.h" #include "util/Yaml.h"
/** /**