Added hardcoded default values for YAML values.

This commit is contained in:
Felix Ableitner 2012-12-20 14:59:05 +01:00
parent 88db5d5324
commit fa5c64d043
12 changed files with 57 additions and 18 deletions

View file

@ -12,6 +12,7 @@
#include <Thor/Vectors.hpp>
const String Body::KEY_SIZE = "size";
const Vector2i Body::DEFAULT_SIZE = Vector2i(50, 50);
/**
* Initializes Box2D body.
@ -21,7 +22,7 @@ const String Body::KEY_SIZE = "size";
Body::Body(const PhysicalData& data, const Yaml& config, const Vector2i& pSize) :
mDelete(false) {
Vector2i size = (pSize == Vector2i())
? config.get<Vector2i>(KEY_SIZE)
? config.get(KEY_SIZE, DEFAULT_SIZE)
: pSize;
assert(size != Vector2i());

View file

@ -92,6 +92,7 @@ public:
// Public variables.
public:
static const String KEY_SIZE;
static const Vector2i DEFAULT_SIZE;
// Protected functions.
protected:

View file

@ -16,7 +16,9 @@
#include "../util/Log.h"
const String Character::KEY_HEALTH = "health";
const int Character::DEFAULT_HEALTH = 100;
const String Character::KEY_SPEED = "speed";
const float Character::DEFAULT_SPEED = 100;
const float Character::POINT_REACHED_DISTANCE = 1.0f;
std::vector<Character*> Character::mCharacterInstances = std::vector<Character*>();
@ -26,9 +28,9 @@ std::vector<Character*> Character::mCharacterInstances = std::vector<Character*>
Character::Character(const Instances& instances, const String& texturePath,
const PhysicalData& data, const Yaml& config) :
Sprite(config, data),
mMaxHealth(config.get<int>(KEY_HEALTH)),
mMaxHealth(config.get(KEY_HEALTH, DEFAULT_HEALTH)),
mCurrentHealth(mMaxHealth),
mMovementSpeed(config.get<float>(KEY_SPEED)),
mMovementSpeed(config.get(KEY_SPEED, DEFAULT_SPEED)),
mWeapon(instances, *this, Yaml("weapon.yaml")),
mInstances(instances),
mStartPathfinding(false) {

View file

@ -47,7 +47,9 @@ protected:
// Private variables.
private:
static const String KEY_HEALTH;
static const int DEFAULT_HEALTH;
static const String KEY_SPEED;
static const float DEFAULT_SPEED;
/// The distance to a point where it is considered reached.
static const float POINT_REACHED_DISTANCE;

View file

@ -9,8 +9,10 @@
#include "../util/Loader.h"
#include "../util/ResourceManager.h"
#include "../util/Log.h"
const String Sprite::KEY_TEXTURE = "texture";
const String Sprite::DEFAULT_TEXTURE = "";
/**
* Loads sprite from ResourceManager, sets world position.
@ -19,9 +21,13 @@ const String Sprite::KEY_TEXTURE = "texture";
*/
Sprite::Sprite(const Yaml& config, const PhysicalData& data, const Vector2i& size) :
Body(data, config, size),
mTexture(ResourceManager::i().acquire(Loader::i()
.fromFile<sf::Texture>(config.get<String>(KEY_TEXTURE)))),
mSize(Vector2i(getSize())) {
String texture = config.get(KEY_TEXTURE, DEFAULT_TEXTURE);
if (texture == "") {
LOG_E("Failed to read texture from YAML file " << config.getFilename());
}
mTexture = ResourceManager::i().acquire(Loader::i()
.fromFile<sf::Texture>(texture));
}
/**

View file

@ -39,6 +39,7 @@ protected:
// Private variables.
private:
static const String KEY_TEXTURE;
static const String DEFAULT_TEXTURE;
std::shared_ptr<sf::Texture> mTexture;
Vector2i mSize;

View file

@ -12,7 +12,9 @@
#include "../util/ResourceManager.h"
const String Bullet::KEY_DAMAGE = "damage";
const int Bullet::DEFAULT_DAMAGE = 10;
const String Bullet::KEY_SPEED = "speed";
const float Bullet::DEFAULT_SPEED = 500;
/**
* Places a bullet in the world.
@ -26,8 +28,8 @@ Bullet::Bullet(const Vector2f& position, b2World& world, Body& shooter, float di
Particle(config, PhysicalData(position, world, CATEGORY_PARTICLE, CATEGORY_PARTICLE,
true, true, true)),
mShooter(shooter),
mDamage(config.get<int>(KEY_DAMAGE)),
mSpeed(config.get<int>(KEY_SPEED)) {
mDamage(config.get(KEY_DAMAGE, DEFAULT_DAMAGE)),
mSpeed(config.get(KEY_SPEED, DEFAULT_SPEED)) {
setSpeed(angle(direction), mSpeed);
setAngle(direction);
}

View file

@ -30,7 +30,9 @@ public:
// Private variables.
private:
static const String KEY_DAMAGE;
static const int DEFAULT_DAMAGE;
static const String KEY_SPEED;
static const float DEFAULT_SPEED;
Body& mShooter;
const int mDamage;

View file

@ -15,16 +15,18 @@
#include "../effects/Bullet.h"
const String Weapon::KEY_BULLET = "bullet";
const String Weapon::DEFAULT_BULLET = "bullet.yaml";
const String Weapon::KEY_INTERVAL = "interval";
const int Weapon::DEFAULT_INTERVAL = 250;
Weapon::Weapon(const Instances& instances, Body& holder, const Yaml& config) :
Emitter(instances.collection),
mHolder(holder),
mWorld(instances.world),
mBullet(config.get<String>(KEY_BULLET)),
mTimer(sf::milliseconds(config.get<int>(KEY_INTERVAL))) {
mBullet(config.get(KEY_BULLET, DEFAULT_BULLET)),
mTimer(sf::milliseconds(config.get(KEY_INTERVAL, DEFAULT_INTERVAL))) {
Yaml bullet(mBullet);
Vector2i bulletSize = bullet.get<Vector2i>(Body::KEY_SIZE);
Vector2i bulletSize = bullet.get(Body::KEY_SIZE, Body::DEFAULT_SIZE);
mOffset = Vector2f(0, std::max(mHolder.getSize().x, mHolder.getSize().y) / 2 +
b2_linearSlop +
std::max(bulletSize.x, bulletSize.y) / 2);

View file

@ -41,7 +41,9 @@ protected:
// Private variables.
private:
static const String KEY_BULLET;
static const String DEFAULT_BULLET;
static const String KEY_INTERVAL;
static const int DEFAULT_INTERVAL;
Body& mHolder;
b2World& mWorld;

View file

@ -16,7 +16,8 @@ String Yaml::mFolder = "";
* set in setFolder().
*/
Yaml::Yaml(const String& filename) :
mFile(mFolder+filename) {
mFilename(mFolder+filename),
mFile(mFilename) {
if (mFile.fail()) {
LOG_W("Failed to open YAML file: " << mFolder << filename);
}
@ -28,6 +29,14 @@ Yaml::~Yaml() {
mFile.close();
}
/**
* Return path and name of the file opened in this object.
*/
String
Yaml::getFilename() const {
return mFilename;
}
/**
* Sets the folder where YAML files are placed. Is added in front of each file name. Allows
* shorter strings as this does not have to be added everywhere.

View file

@ -24,14 +24,18 @@ public:
Yaml(const String& filename);
~Yaml();
String getFilename() const;
static void setFolder(const String& folder);
template <typename T>
T get(const String& key) const;
T get(const String& key, const T& defaultValue) const;
// Private variables.
private:
static String mFolder;
String mFilename;
std::ifstream mFile;
YAML::Node mNode;
};
@ -59,10 +63,15 @@ namespace {
* @return The value of the specified key.
*/
template <typename T>
T Yaml::get(const String& key) const {
T tmp;
mNode[key] >> tmp;
return tmp;
T Yaml::get(const String& key, const T& defaultValue) const {
if (const YAML::Node* node = mNode.FindValue(key)) {
T value;
*node >> value;
return value;
}
else {
return defaultValue;
}
};
#endif /* DG_YAML_H_ */