From 081228750288737e394018f82f70092598c7852c Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Tue, 3 Sep 2013 01:17:03 +0200 Subject: [PATCH] Moved Gadget parameters to yaml. --- res/yaml/heal.yaml | 10 ++++++++++ res/yaml/ring_of_fire.yaml | 18 ++++++++++++++++++ res/yaml/rotating_shield.yaml | 6 +++++- src/sprites/items/Gadget.cpp | 10 +++++++--- src/sprites/items/Gadget.h | 6 +++--- src/sprites/items/Heal.cpp | 22 +++++++++++----------- src/sprites/items/Heal.h | 18 ++++++++++++------ src/sprites/items/RingOfFire.cpp | 22 ++++++++++++---------- src/sprites/items/RingOfFire.h | 10 +++++++--- src/sprites/items/Shield.cpp | 9 +++------ src/sprites/items/Shield.h | 4 +++- 11 files changed, 91 insertions(+), 44 deletions(-) create mode 100644 res/yaml/heal.yaml create mode 100644 res/yaml/ring_of_fire.yaml diff --git a/res/yaml/heal.yaml b/res/yaml/heal.yaml new file mode 100644 index 0000000..bf4f6ce --- /dev/null +++ b/res/yaml/heal.yaml @@ -0,0 +1,10 @@ +name: Heal + +# Milliseconds between each use. +cooldown: 5000 + +# Hit points restored with each use (total). +amount_healed: 50 + +# Time in milliseconds to restore one hitpoint. +time_per_point: 75 \ No newline at end of file diff --git a/res/yaml/ring_of_fire.yaml b/res/yaml/ring_of_fire.yaml new file mode 100644 index 0000000..63ae4d6 --- /dev/null +++ b/res/yaml/ring_of_fire.yaml @@ -0,0 +1,18 @@ +name: Ring of Fire + +# Milliseconds between each use. +cooldown: 5000 + +bullet: bullet.yaml + +# Number of times a new ring of bullets is unleashed (per item use). +waves_per_use: 3 + +# Degrees by which each new wave is offset from the previous one. +angle_offset: 10 + +# Number of bullets in each ring. +bullets_per_wave: 12 + +# Milliseconds between two waves. +delay: 500 \ No newline at end of file diff --git a/res/yaml/rotating_shield.yaml b/res/yaml/rotating_shield.yaml index f4d30c7..d5ddf15 100644 --- a/res/yaml/rotating_shield.yaml +++ b/res/yaml/rotating_shield.yaml @@ -1,3 +1,7 @@ name: Shield + texture: shield.png -size: [75, 5] \ No newline at end of file + +size: [75, 5] + +cooldown: 5000 \ No newline at end of file diff --git a/src/sprites/items/Gadget.cpp b/src/sprites/items/Gadget.cpp index 3d8777f..60f79c6 100644 --- a/src/sprites/items/Gadget.cpp +++ b/src/sprites/items/Gadget.cpp @@ -12,9 +12,13 @@ #include "Shield.h" #include "RingOfFire.h" -Gadget::Gadget(std::string name) : +/** + * @param cooldown Time in milliseconds after which the gadget can be used again. + */ +Gadget::Gadget(std::string name, int cooldown) : Item(sf::Vector2f(32, 32), "item.png"), - mName(name) { + mName(name), + mCooldown(sf::milliseconds(cooldown)) { } /** @@ -38,7 +42,7 @@ void Gadget::use(Character& character) { if (mCooldownTimer.isExpired()) { onUse(character); - mCooldownTimer.restart(getCooldownTime()); + mCooldownTimer.restart(mCooldown); } } diff --git a/src/sprites/items/Gadget.h b/src/sprites/items/Gadget.h index 376c0c5..de96ce3 100644 --- a/src/sprites/items/Gadget.h +++ b/src/sprites/items/Gadget.h @@ -28,7 +28,7 @@ public: }; public: - Gadget(std::string name); + Gadget(std::string name, int cooldown); static std::shared_ptr getGadget(World& world, GadgetType type); void use(Character& character); virtual void onThink(int elapsed) = 0; @@ -37,13 +37,13 @@ public: protected: virtual void onUse(Character& character) = 0; - virtual sf::Time getCooldownTime() const = 0; protected: thor::Timer mCooldownTimer; private: - std::string mName; + const std::string mName; + const sf::Time mCooldown; }; #endif /* DG_GADGET_H_ */ diff --git a/src/sprites/items/Heal.cpp b/src/sprites/items/Heal.cpp index 21dc2c7..5ec04ab 100644 --- a/src/sprites/items/Heal.cpp +++ b/src/sprites/items/Heal.cpp @@ -1,5 +1,5 @@ /* - * SlowHeal.cpp + * Heal.cpp * * Created on: 06.07.2013 * Author: Felix @@ -9,30 +9,30 @@ #include "../abstract/Character.h" +const Yaml Heal::CONFIG("res/yaml/heal.yaml"); + Heal::Heal() : - Gadget("Heal") { + Gadget(CONFIG.get("name", std::string()), CONFIG.get("cooldown", 0)), + mHealedTotal(CONFIG.get("amount_healed", 0)), + mTimePerPoint(sf::milliseconds(CONFIG.get("time_healing", 0))), + mHealed(mHealedTotal + 1) { } void Heal::onUse(Character& character) { mCharacter = &character; - mHealedTotal = 0; + mHealed = 0; } void Heal::onThink(int elapsed) { - if (mHealedTotal < 50 && mTimer.isExpired()) { + if (mHealed < mHealedTotal && mTimer.isExpired()) { mCharacter->onDamage(-1); - mHealedTotal += 1; - mTimer.restart(sf::milliseconds(75)); + mHealed++; + mTimer.restart(mTimePerPoint); } } -sf::Time -Heal::getCooldownTime() const { - return sf::seconds(5); -} - Gadget::GadgetType Heal::getType() const { return GadgetType::HEAL; diff --git a/src/sprites/items/Heal.h b/src/sprites/items/Heal.h index 2af5647..fb87f22 100644 --- a/src/sprites/items/Heal.h +++ b/src/sprites/items/Heal.h @@ -1,15 +1,17 @@ /* - * SlowHeal.h + * Heal.h * * Created on: 06.07.2013 * Author: Felix */ -#ifndef DG_SLOWHEAL_H_ -#define DG_SLOWHEAL_H_ +#ifndef DG_HEAL_H_ +#define DG_HEAL_H_ #include "Gadget.h" +#include "../../util/Yaml.h" + class Heal : public Gadget { public: Heal(); @@ -17,13 +19,17 @@ public: protected: void onUse(Character& character) override; void onThink(int elapsed) override; - sf::Time getCooldownTime() const override; GadgetType getType() const override; private: + static const Yaml CONFIG; + const int mHealedTotal; + const sf::Time mTimePerPoint; + + int mHealed; + Character* mCharacter; thor::Timer mTimer; - int mHealedTotal = 50; }; -#endif /* DG_SLOWHEAL_H_ */ +#endif /* DG_HEAL_H_ */ diff --git a/src/sprites/items/RingOfFire.cpp b/src/sprites/items/RingOfFire.cpp index 0b31933..7c13115 100644 --- a/src/sprites/items/RingOfFire.cpp +++ b/src/sprites/items/RingOfFire.cpp @@ -12,10 +12,17 @@ #include "../effects/Bullet.h" #include "../../World.h" +const Yaml RingOfFire::CONFIG("res/yaml/ring_of_fire.yaml"); + RingOfFire::RingOfFire(World& world) : - Gadget("Ring of Fire"), + Gadget(CONFIG.get("name", std::string()), CONFIG.get("cooldown", 0)), + mWavesPerUse(CONFIG.get("waves_per_use", 0)), + mAngleOffset(CONFIG.get("angle_offset", 0.0f)), + mBulletsPerWave(CONFIG.get("bullets_per_wave", 0)), + mDelay(sf::milliseconds(CONFIG.get("delay", 0))), + mCurrentWave(mWavesPerUse + 1), mWorld(world), - mBullet("bullet.yaml") { + mBullet(CONFIG.get("bullet", std::string())) { } void @@ -26,8 +33,8 @@ RingOfFire::onUse(Character& character) { void RingOfFire::onThink(int elapsed) { - if (mCurrentWave < WAVES_PER_USE && mTimer.isExpired()) { - for (int angle = mCurrentWave * 10; angle <= 360; angle += 30) { + if (mCurrentWave < mWavesPerUse && mTimer.isExpired()) { + for (int angle = mCurrentWave * 10; angle <= 360; angle += 360 / mBulletsPerWave) { Vector2f direction(thor::rotatedVector(mCharacter->getDirection(), (float) angle) * mCharacter->getRadius()); @@ -36,16 +43,11 @@ RingOfFire::onThink(int elapsed) { mWorld.insert(projectile); } - mTimer.restart(sf::milliseconds(500)); + mTimer.restart(mDelay); mCurrentWave++; } } -sf::Time -RingOfFire::getCooldownTime() const { - return sf::seconds(5); -} - Gadget::GadgetType RingOfFire::getType() const { return GadgetType::RINGOFFIRE; diff --git a/src/sprites/items/RingOfFire.h b/src/sprites/items/RingOfFire.h index 30a5efc..3ff52e5 100644 --- a/src/sprites/items/RingOfFire.h +++ b/src/sprites/items/RingOfFire.h @@ -22,12 +22,16 @@ public: protected: void onUse(Character& character) override; void onThink(int elapsed) override; - sf::Time getCooldownTime() const override; GadgetType getType() const override; private: - static constexpr int WAVES_PER_USE = 3; - int mCurrentWave = WAVES_PER_USE + 1; + static const Yaml CONFIG; + const int mWavesPerUse; + const int mAngleOffset; + const int mBulletsPerWave; + const sf::Time mDelay; + + int mCurrentWave; Character* mCharacter; thor::Timer mTimer; diff --git a/src/sprites/items/Shield.cpp b/src/sprites/items/Shield.cpp index 59f2595..555febf 100644 --- a/src/sprites/items/Shield.cpp +++ b/src/sprites/items/Shield.cpp @@ -11,8 +11,10 @@ #include "../RotatingShield.h" #include "../../World.h" +const Yaml Shield::CONFIG("res/yaml/rotating_shield.yaml"); + Shield::Shield() : - Gadget("Shield") { + Gadget(CONFIG.get("name", std::string()), CONFIG.get("cooldown", 0)) { } void @@ -34,11 +36,6 @@ Shield::onThink(int elapsed) { } } -sf::Time -Shield::getCooldownTime() const { - return sf::seconds(10); -} - Shield::GadgetType Shield::getType() const { return GadgetType::SHIELD; diff --git a/src/sprites/items/Shield.h b/src/sprites/items/Shield.h index 99568ac..d5f59da 100644 --- a/src/sprites/items/Shield.h +++ b/src/sprites/items/Shield.h @@ -10,6 +10,8 @@ #include "Gadget.h" +#include "../../util/Yaml.h" + class RotatingShield; class Sprite; @@ -20,10 +22,10 @@ public: protected: void onUse(Character& character) override; void onThink(int elapsed) override; - sf::Time getCooldownTime() const override; GadgetType getType() const override; private: + static const Yaml CONFIG; Character* mCharacter; std::shared_ptr mRotatingShield; };