From 38d97abd5c6fb2684449366601a9c017da35638f Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Fri, 19 Oct 2012 15:44:45 +0200 Subject: [PATCH] Added YAML option to limit firing interval of weapons. --- source/items/Weapon.cpp | 9 ++++++-- source/items/Weapon.h | 7 +++++- source/util/Timer.cpp | 48 +++++++++++++++++++++++++++++++++++++++++ source/util/Timer.h | 34 +++++++++++++++++++++++++++++ 4 files changed, 95 insertions(+), 3 deletions(-) create mode 100644 source/util/Timer.cpp create mode 100644 source/util/Timer.h diff --git a/source/items/Weapon.cpp b/source/items/Weapon.cpp index e6f8ada..0798bc7 100755 --- a/source/items/Weapon.cpp +++ b/source/items/Weapon.cpp @@ -15,12 +15,14 @@ #include "../effects/Bullet.h" const String Weapon::KEY_BULLET = "bullet"; +const String Weapon::KEY_INTERVAL = "interval"; Weapon::Weapon(const Instances& instances, Physical& holder, const Yaml& config) : Emitter(instances.collection), mHolder(holder), mWorld(instances.world), - mBullet(config.get(KEY_BULLET)) { + mBullet(config.get(KEY_BULLET)), + mTimer(sf::milliseconds(config.get(KEY_INTERVAL))) { Yaml bullet(mBullet); Vector2i bulletSize = bullet.get(Physical::KEY_SIZE); mOffset = Vector2f(0, std::max(mHolder.getSize().x, mHolder.getSize().y) / 2 + @@ -34,7 +36,10 @@ Weapon::Weapon(const Instances& instances, Physical& holder, const Yaml& config) void Weapon::fire() { // Only call if has ammo, consider firing rate etc. - emit(); + if (mTimer.isExpired()) { + emit(); + mTimer.start(); + } } std::shared_ptr diff --git a/source/items/Weapon.h b/source/items/Weapon.h index 15f9be8..27bda91 100755 --- a/source/items/Weapon.h +++ b/source/items/Weapon.h @@ -13,11 +13,13 @@ #include "../abstract/Physical.h" #include "../particle/Emitter.h" #include "../types/Instances.h" +#include "../util/Timer.h" #include "../util/Yaml.h" class Emitter; class Instances; class Physical; +class Timer; class Yaml; /** @@ -39,12 +41,15 @@ protected: // Private variables. private: static const String KEY_BULLET; + static const String KEY_INTERVAL; Physical& mHolder; b2World& mWorld; Vector2f mOffset; //< Offset to the point where bullets are inserted (from holder center). - const String mBullet; + const String mBullet; + Timer mTimer; + }; #endif /* DG_WEAPON_H_ */ diff --git a/source/util/Timer.cpp b/source/util/Timer.cpp new file mode 100644 index 0000000..096235e --- /dev/null +++ b/source/util/Timer.cpp @@ -0,0 +1,48 @@ +/* + * Timer.cpp + * + * Created on: 19.10.2012 + * Author: Felix + */ + +#include "Timer.h" + +#include + +/** + * INitializes time limit to zero. + */ +Timer::Timer() { +} + +/** + * Initializes time limit to parameter value. + */ +Timer::Timer(sf::Time timeLimit) : + mLimit(timeLimit) { +} + +/** + * Returns true if the time set in setLimit() has passed since calling start(). + */ +bool +Timer::isExpired() const { + return mClock.getElapsedTime() >= mLimit; +} + +/** + * Starts the timer. + */ +void +Timer::start() { + mClock.restart(); +} + +/** + * Sets the time limit to run to. + */ +void +Timer::setLimit(sf::Time timeLimit) { + assert(timeLimit > sf::Time::Zero); + mLimit = timeLimit; +} diff --git a/source/util/Timer.h b/source/util/Timer.h new file mode 100644 index 0000000..529b5fa --- /dev/null +++ b/source/util/Timer.h @@ -0,0 +1,34 @@ +/* + * Timer.h + * + * Created on: 19.10.2012 + * Author: Felix + */ + +#ifndef DG_TIMER_H_ +#define DG_TIMER_H_ + +#include + +/** + * A small timer utility that allows testing whether a certain amount of time is over. + * + * Replacement for thor::Timer as it causes crashes. + */ +class Timer { + // Public functions. + public: + Timer(); + Timer(sf::Time timeLimit); + + void start(); + void setLimit(sf::Time timeLimit); + bool isExpired() const; + + // Private variables. + private: + sf::Clock mClock; + sf::Time mLimit; +}; + +#endif /* DG_TIMER_H_ */