Moved all YAML keys and defaults to Yaml.h.

This commit is contained in:
Felix Ableitner 2013-04-27 18:54:04 +02:00
parent ca6943f37d
commit 439231727a
9 changed files with 42 additions and 61 deletions

View file

@ -16,12 +16,6 @@
#include "../util/Yaml.h"
#include "../World.h"
const std::string Character::KEY_HEALTH = "health";
const int Character::DEFAULT_HEALTH = 100;
const std::string Character::KEY_SPEED = "speed";
const float Character::DEFAULT_SPEED = 100;
const std::string Character::KEY_WEAPON = "weapon";
const std::string Character::DEFAULT_WEAPON = "weapon.yaml";
const float Character::VISION_DISTANCE = 500.0f;
/**
@ -32,11 +26,11 @@ Character::Character(World& world, TileManager& tileManager, const Data& data,
Sprite(data, config),
mWorld(world),
mTileManager(tileManager),
mMaxHealth(config.get(KEY_HEALTH, DEFAULT_HEALTH)),
mMaxHealth(config.get(YAML_KEY::HEALTH, YAML_DEFAULT::HEALTH)),
mCurrentHealth(mMaxHealth),
mMovementSpeed(config.get(KEY_SPEED, DEFAULT_SPEED)),
mMovementSpeed(config.get(YAML_KEY::SPEED, YAML_DEFAULT::SPEED)),
mWeapon(new Weapon(world, *this,
Yaml(config.get(KEY_WEAPON, DEFAULT_WEAPON)))),
Yaml(config.get(YAML_KEY::WEAPON, YAML_DEFAULT::WEAPON)))),
mLastPosition(getPosition()){
}

View file

@ -41,12 +41,6 @@ private:
void move();
private:
static const std::string KEY_HEALTH;
static const int DEFAULT_HEALTH;
static const std::string KEY_SPEED;
static const float DEFAULT_SPEED;
static const std::string KEY_WEAPON;
static const std::string DEFAULT_WEAPON;
/// Maximum distance where an enemy will be detected.
static const float VISION_DISTANCE;

View file

@ -14,10 +14,6 @@
#include "../util/ResourceManager.h"
#include "../util/Yaml.h"
const std::string Sprite::KEY_SIZE = "size";
const std::string Sprite::KEY_RADIUS = "radius";
const std::string Sprite::KEY_TEXTURE = "texture";
/**
* Initializes sprite data.
*
@ -29,8 +25,8 @@ Sprite::Sprite(const Data& data, const Yaml& config) :
mMask(data.mask),
mDelete(false) {
// Init shape
float radius = config.get(KEY_RADIUS, 0.0f);
sf::Vector2f size = config.get(KEY_SIZE, sf::Vector2f());
float radius = config.get(YAML_KEY::RADIUS, 0.0f);
sf::Vector2f size = config.get(YAML_KEY::SIZE, sf::Vector2f());
if (radius != 0.0f) {
mShape.type = Shape::Type::CIRCLE;
mShape.shape = std::unique_ptr<sf::Shape>(new sf::CircleShape(radius));
@ -51,7 +47,7 @@ Sprite::Sprite(const Data& data, const Yaml& config) :
}
// Init texture
std::string texture = config.get<std::string>(KEY_TEXTURE, "");
std::string texture = config.get<std::string>(YAML_KEY::TEXTURE, "");
if (texture != "") {
try {
mTexture = ResourceManager::i().acquire(Loader::i()

View file

@ -18,7 +18,6 @@ class Yaml;
* An sprite that is rendered in the world.
*/
class Sprite : public sf::Drawable {
// Public types.
public:
/**
* Categories of objects for filtering.
@ -71,13 +70,6 @@ public:
virtual void onCollide(std::shared_ptr<Sprite> other);
// Public variables.
public:
static const std::string KEY_SIZE;
static const std::string KEY_RADIUS;
static const std::string KEY_TEXTURE;
// Protected functions.
protected:
void setDelete(bool value);
void setSpeed(sf::Vector2f direction, float speed);
@ -85,7 +77,6 @@ protected:
void setPosition(const sf::Vector2f& position);
float getRadius() const;
// Private types.
private:
class Shape {
public:
@ -98,7 +89,6 @@ private:
std::shared_ptr<sf::Shape> shape;
};
// Private variables.
private:
friend class World;

View file

@ -12,10 +12,6 @@
#include "../abstract/Character.h"
#include "../util/Yaml.h"
const std::string Bullet::KEY_DAMAGE = "damage";
const int Bullet::DEFAULT_DAMAGE = 10;
const std::string Bullet::KEY_SPEED = "speed";
const float Bullet::DEFAULT_SPEED = 500;
/**
* Places a bullet in the world.
@ -29,8 +25,8 @@ Bullet::Bullet(const sf::Vector2f& position, Sprite& shooter,
Particle(config, Data(position, CATEGORY_PARTICLE,
~CATEGORY_PARTICLE)),
mShooter(shooter),
mDamage(config.get(KEY_DAMAGE, DEFAULT_DAMAGE)),
mSpeed(config.get(KEY_SPEED, DEFAULT_SPEED)) {
mDamage(config.get(YAML_KEY::DAMAGE, YAML_DEFAULT::DAMAGE)),
mSpeed(config.get(YAML_KEY::SPEED, YAML_DEFAULT::SPEED)) {
thor::rotate(direction, - 90.0f);
setSpeed(direction, mSpeed);
setDirection(direction);

View file

@ -23,11 +23,6 @@ public:
void onCollide(std::shared_ptr<Sprite> other);
private:
static const std::string KEY_DAMAGE;
static const int DEFAULT_DAMAGE;
static const std::string KEY_SPEED;
static const float DEFAULT_SPEED;
Sprite& mShooter;
const int mDamage;
const float mSpeed;

View file

@ -12,25 +12,18 @@
#include "../World.h"
#include "../effects/Bullet.h"
#include "../util/Yaml.h"
const std::string Weapon::KEY_BULLET = "bullet";
const std::string Weapon::DEFAULT_BULLET = "bullet.yaml";
const std::string Weapon::KEY_INTERVAL = "interval";
const int Weapon::DEFAULT_INTERVAL = 250;
const std::string Weapon::KEY_AUTOMATIC = "automatic";
const bool Weapon::DEFAULT_AUTOMATIC = false;
Weapon::Weapon(World& world, Sprite& holder, const Yaml& config) :
Emitter(world),
mHolder(holder),
mBullet(config.get(KEY_BULLET, DEFAULT_BULLET)),
mBullet(config.get(YAML_KEY::BULLET, YAML_DEFAULT::BULLET)),
mLastShotWaitInterval(0),
mFireInterval(config.get(KEY_INTERVAL, DEFAULT_INTERVAL)),
mFireInterval(config.get(YAML_KEY::INTERVAL, YAML_DEFAULT::INTERVAL)),
mFire(false),
mAutomatic(config.get(KEY_AUTOMATIC, DEFAULT_AUTOMATIC)) {
mAutomatic(config.get(YAML_KEY::AUTOMATIC, YAML_DEFAULT::AUTOMATIC)) {
sf::Vector2f holderSize = mHolder.getSize();
Yaml bullet(mBullet);
sf::Vector2f bulletSize = bullet.get(Sprite::KEY_SIZE, sf::Vector2f());
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);

View file

@ -36,13 +36,6 @@ protected:
std::shared_ptr<Sprite> createParticle();
private:
static const std::string KEY_BULLET;
static const std::string DEFAULT_BULLET;
static const std::string KEY_INTERVAL;
static const int DEFAULT_INTERVAL;
static const std::string KEY_AUTOMATIC;
static const bool DEFAULT_AUTOMATIC;
Sprite& mHolder;
sf::Vector2f mOffset; //< Offset to the point where bullets are inserted (from holder center).

View file

@ -79,4 +79,34 @@ T Yaml::get(const std::string& key, const T& defaultValue) const {
}
};
namespace YAML_KEY {
// Sprite
const std::string SIZE = "size";
const std::string RADIUS = "radius";
const std::string TEXTURE = "texture";
// Character
const std::string HEALTH = "health";
const std::string SPEED = "speed";
const std::string WEAPON = "weapon";
// Bullet
const std::string DAMAGE = "damage";
// Weapon
const std::string BULLET = "bullet";
const std::string INTERVAL = "interval";
const std::string AUTOMATIC = "automatic";
}
namespace YAML_DEFAULT {
const int HEALTH = 100;
const float SPEED = 100;
const std::string WEAPON = "weapon.yaml";
const int DAMAGE = 10;
const int INTERVAL = 250;
const std::string BULLET = "bullet.yaml";
const bool AUTOMATIC = false;
}
#endif /* DG_YAML_H_ */