Added YAML option to limit firing interval of weapons.
This commit is contained in:
parent
fe7909ea7b
commit
38d97abd5c
4 changed files with 95 additions and 3 deletions
|
@ -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<String>(KEY_BULLET)) {
|
||||
mBullet(config.get<String>(KEY_BULLET)),
|
||||
mTimer(sf::milliseconds(config.get<int>(KEY_INTERVAL))) {
|
||||
Yaml bullet(mBullet);
|
||||
Vector2i bulletSize = bullet.get<Vector2i>(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.
|
||||
if (mTimer.isExpired()) {
|
||||
emit();
|
||||
mTimer.start();
|
||||
}
|
||||
}
|
||||
|
||||
std::shared_ptr<Particle>
|
||||
|
|
|
@ -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;
|
||||
Timer mTimer;
|
||||
|
||||
};
|
||||
|
||||
#endif /* DG_WEAPON_H_ */
|
||||
|
|
48
source/util/Timer.cpp
Normal file
48
source/util/Timer.cpp
Normal 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
34
source/util/Timer.h
Normal 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_ */
|
Reference in a new issue