Added YAML option to limit firing interval of weapons.

This commit is contained in:
Felix Ableitner 2012-10-19 15:44:45 +02:00
parent fe7909ea7b
commit 38d97abd5c
4 changed files with 95 additions and 3 deletions

View file

@ -15,12 +15,14 @@
#include "../effects/Bullet.h" #include "../effects/Bullet.h"
const String Weapon::KEY_BULLET = "bullet"; const String Weapon::KEY_BULLET = "bullet";
const String Weapon::KEY_INTERVAL = "interval";
Weapon::Weapon(const Instances& instances, Physical& holder, const Yaml& config) : Weapon::Weapon(const Instances& instances, Physical& 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<String>(KEY_BULLET)),
mTimer(sf::milliseconds(config.get<int>(KEY_INTERVAL))) {
Yaml bullet(mBullet); Yaml bullet(mBullet);
Vector2i bulletSize = bullet.get<Vector2i>(Physical::KEY_SIZE); Vector2i bulletSize = bullet.get<Vector2i>(Physical::KEY_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 +
@ -34,7 +36,10 @@ Weapon::Weapon(const Instances& instances, Physical& holder, const Yaml& config)
void void
Weapon::fire() { Weapon::fire() {
// Only call if has ammo, consider firing rate etc. // Only call if has ammo, consider firing rate etc.
if (mTimer.isExpired()) {
emit(); emit();
mTimer.start();
}
} }
std::shared_ptr<Particle> std::shared_ptr<Particle>

View file

@ -13,11 +13,13 @@
#include "../abstract/Physical.h" #include "../abstract/Physical.h"
#include "../particle/Emitter.h" #include "../particle/Emitter.h"
#include "../types/Instances.h" #include "../types/Instances.h"
#include "../util/Timer.h"
#include "../util/Yaml.h" #include "../util/Yaml.h"
class Emitter; class Emitter;
class Instances; class Instances;
class Physical; class Physical;
class Timer;
class Yaml; class Yaml;
/** /**
@ -39,12 +41,15 @@ protected:
// Private variables. // Private variables.
private: private:
static const String KEY_BULLET; static const String KEY_BULLET;
static const String KEY_INTERVAL;
Physical& mHolder; Physical& mHolder;
b2World& mWorld; b2World& mWorld;
Vector2f mOffset; //< Offset to the point where bullets are inserted (from holder center). 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_ */ #endif /* DG_WEAPON_H_ */

48
source/util/Timer.cpp Normal file
View file

@ -0,0 +1,48 @@
/*
* Timer.cpp
*
* Created on: 19.10.2012
* Author: Felix
*/
#include "Timer.h"
#include <assert.h>
/**
* 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;
}

34
source/util/Timer.h Normal file
View file

@ -0,0 +1,34 @@
/*
* Timer.h
*
* Created on: 19.10.2012
* Author: Felix
*/
#ifndef DG_TIMER_H_
#define DG_TIMER_H_
#include <SFML/System.hpp>
/**
* 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_ */