From 439231727a5b806bdc923477fab9c223263ac11c Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Sat, 27 Apr 2013 18:54:04 +0200 Subject: [PATCH] Moved all YAML keys and defaults to Yaml.h. --- source/abstract/Character.cpp | 12 +++--------- source/abstract/Character.h | 6 ------ source/abstract/Sprite.cpp | 10 +++------- source/abstract/Sprite.h | 10 ---------- source/effects/Bullet.cpp | 8 ++------ source/effects/Bullet.h | 5 ----- source/items/Weapon.cpp | 15 ++++----------- source/items/Weapon.h | 7 ------- source/util/Yaml.h | 30 ++++++++++++++++++++++++++++++ 9 files changed, 42 insertions(+), 61 deletions(-) diff --git a/source/abstract/Character.cpp b/source/abstract/Character.cpp index 68e1737..20a6db0 100644 --- a/source/abstract/Character.cpp +++ b/source/abstract/Character.cpp @@ -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()){ } diff --git a/source/abstract/Character.h b/source/abstract/Character.h index eb4c9a0..5bd1d5c 100644 --- a/source/abstract/Character.h +++ b/source/abstract/Character.h @@ -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; diff --git a/source/abstract/Sprite.cpp b/source/abstract/Sprite.cpp index b29003f..d4d5d2e 100755 --- a/source/abstract/Sprite.cpp +++ b/source/abstract/Sprite.cpp @@ -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(new sf::CircleShape(radius)); @@ -51,7 +47,7 @@ Sprite::Sprite(const Data& data, const Yaml& config) : } // Init texture - std::string texture = config.get(KEY_TEXTURE, ""); + std::string texture = config.get(YAML_KEY::TEXTURE, ""); if (texture != "") { try { mTexture = ResourceManager::i().acquire(Loader::i() diff --git a/source/abstract/Sprite.h b/source/abstract/Sprite.h index 5f98b64..e6b5b7b 100755 --- a/source/abstract/Sprite.h +++ b/source/abstract/Sprite.h @@ -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 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 shape; }; -// Private variables. private: friend class World; diff --git a/source/effects/Bullet.cpp b/source/effects/Bullet.cpp index 97eb337..9b830e5 100755 --- a/source/effects/Bullet.cpp +++ b/source/effects/Bullet.cpp @@ -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); diff --git a/source/effects/Bullet.h b/source/effects/Bullet.h index 2c7e728..e1bd78b 100755 --- a/source/effects/Bullet.h +++ b/source/effects/Bullet.h @@ -23,11 +23,6 @@ public: void onCollide(std::shared_ptr 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; diff --git a/source/items/Weapon.cpp b/source/items/Weapon.cpp index 4b3ea16..7bf98be 100755 --- a/source/items/Weapon.cpp +++ b/source/items/Weapon.cpp @@ -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); diff --git a/source/items/Weapon.h b/source/items/Weapon.h index ca98387..7f3f55c 100755 --- a/source/items/Weapon.h +++ b/source/items/Weapon.h @@ -36,13 +36,6 @@ protected: std::shared_ptr 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). diff --git a/source/util/Yaml.h b/source/util/Yaml.h index 2691fa8..1f5b1b3 100644 --- a/source/util/Yaml.h +++ b/source/util/Yaml.h @@ -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_ */