Added hardcoded default values for YAML values.
This commit is contained in:
parent
88db5d5324
commit
fa5c64d043
12 changed files with 57 additions and 18 deletions
|
@ -12,6 +12,7 @@
|
||||||
#include <Thor/Vectors.hpp>
|
#include <Thor/Vectors.hpp>
|
||||||
|
|
||||||
const String Body::KEY_SIZE = "size";
|
const String Body::KEY_SIZE = "size";
|
||||||
|
const Vector2i Body::DEFAULT_SIZE = Vector2i(50, 50);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initializes Box2D body.
|
* Initializes Box2D body.
|
||||||
|
@ -21,7 +22,7 @@ const String Body::KEY_SIZE = "size";
|
||||||
Body::Body(const PhysicalData& data, const Yaml& config, const Vector2i& pSize) :
|
Body::Body(const PhysicalData& data, const Yaml& config, const Vector2i& pSize) :
|
||||||
mDelete(false) {
|
mDelete(false) {
|
||||||
Vector2i size = (pSize == Vector2i())
|
Vector2i size = (pSize == Vector2i())
|
||||||
? config.get<Vector2i>(KEY_SIZE)
|
? config.get(KEY_SIZE, DEFAULT_SIZE)
|
||||||
: pSize;
|
: pSize;
|
||||||
assert(size != Vector2i());
|
assert(size != Vector2i());
|
||||||
|
|
||||||
|
|
|
@ -92,6 +92,7 @@ public:
|
||||||
// Public variables.
|
// Public variables.
|
||||||
public:
|
public:
|
||||||
static const String KEY_SIZE;
|
static const String KEY_SIZE;
|
||||||
|
static const Vector2i DEFAULT_SIZE;
|
||||||
|
|
||||||
// Protected functions.
|
// Protected functions.
|
||||||
protected:
|
protected:
|
||||||
|
|
|
@ -16,7 +16,9 @@
|
||||||
#include "../util/Log.h"
|
#include "../util/Log.h"
|
||||||
|
|
||||||
const String Character::KEY_HEALTH = "health";
|
const String Character::KEY_HEALTH = "health";
|
||||||
|
const int Character::DEFAULT_HEALTH = 100;
|
||||||
const String Character::KEY_SPEED = "speed";
|
const String Character::KEY_SPEED = "speed";
|
||||||
|
const float Character::DEFAULT_SPEED = 100;
|
||||||
const float Character::POINT_REACHED_DISTANCE = 1.0f;
|
const float Character::POINT_REACHED_DISTANCE = 1.0f;
|
||||||
std::vector<Character*> Character::mCharacterInstances = std::vector<Character*>();
|
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,
|
Character::Character(const Instances& instances, const String& texturePath,
|
||||||
const PhysicalData& data, const Yaml& config) :
|
const PhysicalData& data, const Yaml& config) :
|
||||||
Sprite(config, data),
|
Sprite(config, data),
|
||||||
mMaxHealth(config.get<int>(KEY_HEALTH)),
|
mMaxHealth(config.get(KEY_HEALTH, DEFAULT_HEALTH)),
|
||||||
mCurrentHealth(mMaxHealth),
|
mCurrentHealth(mMaxHealth),
|
||||||
mMovementSpeed(config.get<float>(KEY_SPEED)),
|
mMovementSpeed(config.get(KEY_SPEED, DEFAULT_SPEED)),
|
||||||
mWeapon(instances, *this, Yaml("weapon.yaml")),
|
mWeapon(instances, *this, Yaml("weapon.yaml")),
|
||||||
mInstances(instances),
|
mInstances(instances),
|
||||||
mStartPathfinding(false) {
|
mStartPathfinding(false) {
|
||||||
|
|
|
@ -47,7 +47,9 @@ protected:
|
||||||
// Private variables.
|
// Private variables.
|
||||||
private:
|
private:
|
||||||
static const String KEY_HEALTH;
|
static const String KEY_HEALTH;
|
||||||
|
static const int DEFAULT_HEALTH;
|
||||||
static const String KEY_SPEED;
|
static const String KEY_SPEED;
|
||||||
|
static const float DEFAULT_SPEED;
|
||||||
/// The distance to a point where it is considered reached.
|
/// The distance to a point where it is considered reached.
|
||||||
static const float POINT_REACHED_DISTANCE;
|
static const float POINT_REACHED_DISTANCE;
|
||||||
|
|
||||||
|
|
|
@ -9,8 +9,10 @@
|
||||||
|
|
||||||
#include "../util/Loader.h"
|
#include "../util/Loader.h"
|
||||||
#include "../util/ResourceManager.h"
|
#include "../util/ResourceManager.h"
|
||||||
|
#include "../util/Log.h"
|
||||||
|
|
||||||
const String Sprite::KEY_TEXTURE = "texture";
|
const String Sprite::KEY_TEXTURE = "texture";
|
||||||
|
const String Sprite::DEFAULT_TEXTURE = "";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Loads sprite from ResourceManager, sets world position.
|
* 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) :
|
Sprite::Sprite(const Yaml& config, const PhysicalData& data, const Vector2i& size) :
|
||||||
Body(data, config, size),
|
Body(data, config, size),
|
||||||
mTexture(ResourceManager::i().acquire(Loader::i()
|
|
||||||
.fromFile<sf::Texture>(config.get<String>(KEY_TEXTURE)))),
|
|
||||||
mSize(Vector2i(getSize())) {
|
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));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -39,6 +39,7 @@ protected:
|
||||||
// Private variables.
|
// Private variables.
|
||||||
private:
|
private:
|
||||||
static const String KEY_TEXTURE;
|
static const String KEY_TEXTURE;
|
||||||
|
static const String DEFAULT_TEXTURE;
|
||||||
|
|
||||||
std::shared_ptr<sf::Texture> mTexture;
|
std::shared_ptr<sf::Texture> mTexture;
|
||||||
Vector2i mSize;
|
Vector2i mSize;
|
||||||
|
|
|
@ -12,7 +12,9 @@
|
||||||
#include "../util/ResourceManager.h"
|
#include "../util/ResourceManager.h"
|
||||||
|
|
||||||
const String Bullet::KEY_DAMAGE = "damage";
|
const String Bullet::KEY_DAMAGE = "damage";
|
||||||
|
const int Bullet::DEFAULT_DAMAGE = 10;
|
||||||
const String Bullet::KEY_SPEED = "speed";
|
const String Bullet::KEY_SPEED = "speed";
|
||||||
|
const float Bullet::DEFAULT_SPEED = 500;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Places a bullet in the world.
|
* 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,
|
Particle(config, PhysicalData(position, world, CATEGORY_PARTICLE, CATEGORY_PARTICLE,
|
||||||
true, true, true)),
|
true, true, true)),
|
||||||
mShooter(shooter),
|
mShooter(shooter),
|
||||||
mDamage(config.get<int>(KEY_DAMAGE)),
|
mDamage(config.get(KEY_DAMAGE, DEFAULT_DAMAGE)),
|
||||||
mSpeed(config.get<int>(KEY_SPEED)) {
|
mSpeed(config.get(KEY_SPEED, DEFAULT_SPEED)) {
|
||||||
setSpeed(angle(direction), mSpeed);
|
setSpeed(angle(direction), mSpeed);
|
||||||
setAngle(direction);
|
setAngle(direction);
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,7 +30,9 @@ public:
|
||||||
// Private variables.
|
// Private variables.
|
||||||
private:
|
private:
|
||||||
static const String KEY_DAMAGE;
|
static const String KEY_DAMAGE;
|
||||||
|
static const int DEFAULT_DAMAGE;
|
||||||
static const String KEY_SPEED;
|
static const String KEY_SPEED;
|
||||||
|
static const float DEFAULT_SPEED;
|
||||||
|
|
||||||
Body& mShooter;
|
Body& mShooter;
|
||||||
const int mDamage;
|
const int mDamage;
|
||||||
|
|
|
@ -15,16 +15,18 @@
|
||||||
#include "../effects/Bullet.h"
|
#include "../effects/Bullet.h"
|
||||||
|
|
||||||
const String Weapon::KEY_BULLET = "bullet";
|
const String Weapon::KEY_BULLET = "bullet";
|
||||||
|
const String Weapon::DEFAULT_BULLET = "bullet.yaml";
|
||||||
const String Weapon::KEY_INTERVAL = "interval";
|
const String Weapon::KEY_INTERVAL = "interval";
|
||||||
|
const int Weapon::DEFAULT_INTERVAL = 250;
|
||||||
|
|
||||||
Weapon::Weapon(const Instances& instances, Body& holder, const Yaml& config) :
|
Weapon::Weapon(const Instances& instances, Body& holder, const Yaml& config) :
|
||||||
Emitter(instances.collection),
|
Emitter(instances.collection),
|
||||||
mHolder(holder),
|
mHolder(holder),
|
||||||
mWorld(instances.world),
|
mWorld(instances.world),
|
||||||
mBullet(config.get<String>(KEY_BULLET)),
|
mBullet(config.get(KEY_BULLET, DEFAULT_BULLET)),
|
||||||
mTimer(sf::milliseconds(config.get<int>(KEY_INTERVAL))) {
|
mTimer(sf::milliseconds(config.get(KEY_INTERVAL, DEFAULT_INTERVAL))) {
|
||||||
Yaml bullet(mBullet);
|
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 +
|
mOffset = Vector2f(0, std::max(mHolder.getSize().x, mHolder.getSize().y) / 2 +
|
||||||
b2_linearSlop +
|
b2_linearSlop +
|
||||||
std::max(bulletSize.x, bulletSize.y) / 2);
|
std::max(bulletSize.x, bulletSize.y) / 2);
|
||||||
|
|
|
@ -41,7 +41,9 @@ protected:
|
||||||
// Private variables.
|
// Private variables.
|
||||||
private:
|
private:
|
||||||
static const String KEY_BULLET;
|
static const String KEY_BULLET;
|
||||||
|
static const String DEFAULT_BULLET;
|
||||||
static const String KEY_INTERVAL;
|
static const String KEY_INTERVAL;
|
||||||
|
static const int DEFAULT_INTERVAL;
|
||||||
|
|
||||||
Body& mHolder;
|
Body& mHolder;
|
||||||
b2World& mWorld;
|
b2World& mWorld;
|
||||||
|
|
|
@ -16,7 +16,8 @@ String Yaml::mFolder = "";
|
||||||
* set in setFolder().
|
* set in setFolder().
|
||||||
*/
|
*/
|
||||||
Yaml::Yaml(const String& filename) :
|
Yaml::Yaml(const String& filename) :
|
||||||
mFile(mFolder+filename) {
|
mFilename(mFolder+filename),
|
||||||
|
mFile(mFilename) {
|
||||||
if (mFile.fail()) {
|
if (mFile.fail()) {
|
||||||
LOG_W("Failed to open YAML file: " << mFolder << filename);
|
LOG_W("Failed to open YAML file: " << mFolder << filename);
|
||||||
}
|
}
|
||||||
|
@ -28,6 +29,14 @@ Yaml::~Yaml() {
|
||||||
mFile.close();
|
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
|
* 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.
|
* shorter strings as this does not have to be added everywhere.
|
||||||
|
|
|
@ -24,14 +24,18 @@ public:
|
||||||
Yaml(const String& filename);
|
Yaml(const String& filename);
|
||||||
~Yaml();
|
~Yaml();
|
||||||
|
|
||||||
|
String getFilename() const;
|
||||||
|
|
||||||
static void setFolder(const String& folder);
|
static void setFolder(const String& folder);
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
T get(const String& key) const;
|
T get(const String& key, const T& defaultValue) const;
|
||||||
|
|
||||||
// Private variables.
|
// Private variables.
|
||||||
private:
|
private:
|
||||||
static String mFolder;
|
static String mFolder;
|
||||||
|
|
||||||
|
String mFilename;
|
||||||
std::ifstream mFile;
|
std::ifstream mFile;
|
||||||
YAML::Node mNode;
|
YAML::Node mNode;
|
||||||
};
|
};
|
||||||
|
@ -59,10 +63,15 @@ namespace {
|
||||||
* @return The value of the specified key.
|
* @return The value of the specified key.
|
||||||
*/
|
*/
|
||||||
template <typename T>
|
template <typename T>
|
||||||
T Yaml::get(const String& key) const {
|
T Yaml::get(const String& key, const T& defaultValue) const {
|
||||||
T tmp;
|
if (const YAML::Node* node = mNode.FindValue(key)) {
|
||||||
mNode[key] >> tmp;
|
T value;
|
||||||
return tmp;
|
*node >> value;
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return defaultValue;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif /* DG_YAML_H_ */
|
#endif /* DG_YAML_H_ */
|
||||||
|
|
Reference in a new issue