Added Yaml config for bullet.
This commit is contained in:
parent
28681c7eb3
commit
f23f2225db
5 changed files with 29 additions and 24 deletions
|
@ -8,10 +8,12 @@
|
|||
#include "Bullet.h"
|
||||
|
||||
#include "../abstract/Character.h"
|
||||
#include "../util/Loader.h"
|
||||
#include "../util/ResourceManager.h"
|
||||
|
||||
const Vector2i Bullet::SIZE = Vector2i(20, 20);
|
||||
|
||||
const float Bullet::SPEED = 500.0f;
|
||||
const String Bullet::KEY_DAMAGE = "damage";
|
||||
const String Bullet::KEY_SPEED = "speed";
|
||||
|
||||
/**
|
||||
* Places a bullet in the world.
|
||||
|
@ -20,13 +22,15 @@ const float Bullet::SPEED = 500.0f;
|
|||
* @param world Box2d world.
|
||||
* @param texture Texture to display for bullet.
|
||||
*/
|
||||
Bullet::Bullet(const Vector2f& position, b2World& world,
|
||||
const std::shared_ptr<sf::Texture>& texture, Physical& shooter, float direction, int damage) :
|
||||
Particle(texture, PhysicalData(position, SIZE, world, CATEGORY_PARTICLE,
|
||||
CATEGORY_PARTICLE, true, true, true)),
|
||||
Bullet::Bullet(const Vector2f& position, b2World& world, Physical& shooter, float direction,
|
||||
const Yaml& config) :
|
||||
Particle(ResourceManager::i().acquire(Loader::i().fromFile<sf::Texture>("bullet.png")),
|
||||
PhysicalData(position, SIZE, world, CATEGORY_PARTICLE, CATEGORY_PARTICLE,
|
||||
true, true, true)),
|
||||
mShooter(shooter),
|
||||
mDamage(damage) {
|
||||
setSpeed(angle(direction), SPEED);
|
||||
mDamage(config.get<int>(KEY_DAMAGE)),
|
||||
mSpeed(config.get<int>(KEY_SPEED)) {
|
||||
setSpeed(angle(direction), mSpeed);
|
||||
setAngle(direction);
|
||||
}
|
||||
|
||||
|
|
|
@ -9,8 +9,11 @@
|
|||
#define DG_BULLET_H_
|
||||
|
||||
#include "../particle/Particle.h"
|
||||
#include "../util/String.h"
|
||||
#include "../util/Yaml.h"
|
||||
|
||||
class Particle;
|
||||
class Yaml;
|
||||
|
||||
/**
|
||||
* Bullet particle fired by a weapon, may damage actors.
|
||||
|
@ -18,8 +21,8 @@ class Particle;
|
|||
class Bullet : public Particle {
|
||||
// Public functions.
|
||||
public:
|
||||
Bullet(const Vector2f& position, b2World& world, const std::shared_ptr<sf::Texture>& texture,
|
||||
Physical& shooter, float direction, int damage);
|
||||
Bullet(const Vector2f& position, b2World& world, Physical& shooter, float direction,
|
||||
const Yaml& config);
|
||||
|
||||
void onCollide(Physical& other, uint16 category);
|
||||
bool doesCollide(Physical& other);
|
||||
|
@ -30,9 +33,12 @@ public:
|
|||
|
||||
// Private variables.
|
||||
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;
|
||||
const int mDamage;
|
||||
const float mSpeed;
|
||||
};
|
||||
|
||||
#endif /* DG_BULLET_H_ */
|
||||
|
|
|
@ -13,21 +13,17 @@
|
|||
|
||||
#include "../util/Collection.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) :
|
||||
Emitter(instances.collection),
|
||||
mHolder(holder),
|
||||
mBulletTexture(ResourceManager::i()
|
||||
.acquire(Loader::i().fromFile<sf::Texture>("bullet.png"))),
|
||||
mWorld(instances.world),
|
||||
mOffset(0, std::max(mHolder.getSize().x, mHolder.getSize().y) / 2 +
|
||||
b2_linearSlop +
|
||||
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.
|
||||
Vector2f offset(- mOffset);
|
||||
thor::rotate(offset, mHolder.getAngle());
|
||||
return std::shared_ptr<Particle>(new Bullet(mHolder.getPosition() + offset,
|
||||
mWorld, mBulletTexture, mHolder, mHolder.getAngle(), mDamage));
|
||||
return std::shared_ptr<Particle>(new Bullet(mHolder.getPosition() + offset, mWorld,
|
||||
mHolder, mHolder.getAngle(), Yaml(mBullet)));
|
||||
}
|
||||
|
|
|
@ -18,6 +18,7 @@
|
|||
class Emitter;
|
||||
class Instances;
|
||||
class Physical;
|
||||
class Yaml;
|
||||
|
||||
/**
|
||||
* Loading mechanism:
|
||||
|
@ -37,14 +38,13 @@ protected:
|
|||
|
||||
// Private variables.
|
||||
private:
|
||||
static const String KEY_DAMAGE;
|
||||
static const String KEY_BULLET;
|
||||
|
||||
Physical& mHolder;
|
||||
std::shared_ptr<sf::Texture> mBulletTexture;
|
||||
b2World& mWorld;
|
||||
|
||||
const Vector2f mOffset; //< Offset to the point where bullets are inserted (from holder center).
|
||||
const int mDamage;
|
||||
const String mBullet;
|
||||
};
|
||||
|
||||
#endif /* DG_WEAPON_H_ */
|
||||
|
|
|
@ -7,7 +7,6 @@
|
|||
|
||||
#include "Game.h"
|
||||
#include "util/Loader.h"
|
||||
|
||||
#include "util/Yaml.h"
|
||||
|
||||
/**
|
||||
|
|
Reference in a new issue