Added automatic weapons.

This commit is contained in:
Felix Ableitner 2012-12-24 01:14:22 +01:00
parent 5433e14f51
commit f0a9c3320d
8 changed files with 75 additions and 14 deletions

View file

@ -104,6 +104,9 @@ Game::input() {
case sf::Event::KeyReleased: case sf::Event::KeyReleased:
keyUp(event); keyUp(event);
break; break;
case sf::Event::MouseButtonPressed:
mouseDown(event);
break;
case sf::Event::MouseButtonReleased: case sf::Event::MouseButtonReleased:
mouseUp(event); mouseUp(event);
break; break;
@ -176,6 +179,16 @@ Game::convertCoordinates(int x, int y) {
return mWindow.convertCoords(sf::Vector2i(x, y), mView); return mWindow.convertCoords(sf::Vector2i(x, y), mView);
} }
void
Game::mouseDown(const sf::Event& event) {
switch(event.mouseButton.button) {
case sf::Mouse::Left:
mPlayer->pullTrigger();
break;
default:
break;
}
}
/** /**
* Handles mouse key up events. * Handles mouse key up events.
*/ */
@ -183,7 +196,7 @@ void
Game::mouseUp(const sf::Event& event) { Game::mouseUp(const sf::Event& event) {
switch (event.mouseButton.button) { switch (event.mouseButton.button) {
case sf::Mouse::Left: case sf::Mouse::Left:
mPlayer->fire(); mPlayer->releaseTrigger();
break; break;
case sf::Mouse::Right: case sf::Mouse::Right:
mPlayer->move(convertCoordinates(event.mouseButton.x, event.mouseButton.y)); mPlayer->move(convertCoordinates(event.mouseButton.x, event.mouseButton.y));

View file

@ -41,6 +41,7 @@ private:
void keyDown(const sf::Event& event); void keyDown(const sf::Event& event);
void keyUp(const sf::Event& event); void keyUp(const sf::Event& event);
void mouseDown(const sf::Event& event);
void mouseUp(const sf::Event& event); void mouseUp(const sf::Event& event);
void generate(); void generate();

View file

@ -59,6 +59,7 @@ Character::~Character() {
void void
Character::think(float elapsedTime) { Character::think(float elapsedTime) {
for (auto i : mCharacterInstances) { for (auto i : mCharacterInstances) {
i->mWeapon.think();
i->onThink(elapsedTime); i->onThink(elapsedTime);
} }
} }
@ -104,11 +105,19 @@ Character::getMovementSpeed() const {
} }
/** /**
* Fire the attached weapon. * Pull the trigger on the attached weapon.
*/ */
void void
Character::fire() { Character::pullTrigger() {
mWeapon.fire(); mWeapon.pullTrigger();
}
/**
* Release the trigger on the attached weapon.
*/
void
Character::releaseTrigger() {
mWeapon.releaseTrigger();
} }
/** /**

View file

@ -43,7 +43,8 @@ protected:
virtual void onThink(float elapsedTime); virtual void onThink(float elapsedTime);
virtual void onDeath(); virtual void onDeath();
float getMovementSpeed() const; float getMovementSpeed() const;
void fire(); void pullTrigger();
void releaseTrigger();
bool setDestination(const sf::Vector2f& destination); bool setDestination(const sf::Vector2f& destination);
void move(); void move();

View file

@ -18,13 +18,17 @@ const std::string Weapon::KEY_BULLET = "bullet";
const std::string Weapon::DEFAULT_BULLET = "bullet.yaml"; const std::string Weapon::DEFAULT_BULLET = "bullet.yaml";
const std::string Weapon::KEY_INTERVAL = "interval"; const std::string Weapon::KEY_INTERVAL = "interval";
const int Weapon::DEFAULT_INTERVAL = 250; 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) : Weapon::Weapon(World& world, Sprite& holder, const Yaml& config) :
Emitter(world), Emitter(world),
mWorld(world), mWorld(world),
mHolder(holder), mHolder(holder),
mBullet(config.get(KEY_BULLET, DEFAULT_BULLET)), mBullet(config.get(KEY_BULLET, DEFAULT_BULLET)),
mTimer(sf::milliseconds(config.get(KEY_INTERVAL, DEFAULT_INTERVAL))) { mTimer(sf::milliseconds(config.get(KEY_INTERVAL, DEFAULT_INTERVAL))),
mFire(false),
mAutomatic(config.get(KEY_AUTOMATIC, DEFAULT_AUTOMATIC)) {
sf::Vector2f holderSize = mHolder.getSize(); sf::Vector2f holderSize = mHolder.getSize();
Yaml bullet(mBullet); Yaml bullet(mBullet);
sf::Vector2f bulletSize = bullet.get(Sprite::KEY_SIZE, sf::Vector2f()); sf::Vector2f bulletSize = bullet.get(Sprite::KEY_SIZE, sf::Vector2f());
@ -37,11 +41,29 @@ Weapon::Weapon(World& world, Sprite& holder, const Yaml& config) :
* Pull the trigger. * Pull the trigger.
*/ */
void void
Weapon::fire() { Weapon::pullTrigger() {
// Only call if has ammo, consider firing rate etc. mFire = true;
if (mTimer.isExpired()) { }
/**
* Release the trigger.
*/
void
Weapon::releaseTrigger() {
mFire = false;
}
/**
* Fire if the trigger has been pulled, time between bullets is over, has ammo etc.
*/
void
Weapon::think() {
if (mFire && mTimer.isExpired()) {
emit(); emit();
mTimer.start(); mTimer.start();
if (!mAutomatic) {
mFire = false;
}
} }
} }

View file

@ -31,7 +31,9 @@ class Weapon : public Emitter {
public: public:
Weapon(World& world, Sprite& holder, const Yaml& config); Weapon(World& world, Sprite& holder, const Yaml& config);
void fire(); void pullTrigger();
void releaseTrigger();
void think();
// Protected functions. // Protected functions.
protected: protected:
@ -43,6 +45,8 @@ private:
static const std::string DEFAULT_BULLET; static const std::string DEFAULT_BULLET;
static const std::string KEY_INTERVAL; static const std::string KEY_INTERVAL;
static const int DEFAULT_INTERVAL; static const int DEFAULT_INTERVAL;
static const std::string KEY_AUTOMATIC;
static const bool DEFAULT_AUTOMATIC;
World& mWorld; World& mWorld;
Sprite& mHolder; Sprite& mHolder;
@ -50,6 +54,8 @@ private:
sf::Vector2f mOffset; //< Offset to the point where bullets are inserted (from holder center). sf::Vector2f mOffset; //< Offset to the point where bullets are inserted (from holder center).
const std::string mBullet; const std::string mBullet;
Timer mTimer; Timer mTimer;
bool mFire;
bool mAutomatic;
}; };

View file

@ -35,11 +35,19 @@ Player::setCrosshairPosition(const sf::Vector2f& position) {
} }
/** /**
* Fires the attached Weapon, emitting a Bullet object. * Pull the trigger on the attached weapon.
*/ */
void void
Player::fire() { Player::pullTrigger() {
Character::fire(); Character::pullTrigger();
}
/**
* Release the trigger on the attached weapon.
*/
void
Player::releaseTrigger() {
Character::releaseTrigger();
} }
/** /**

View file

@ -44,7 +44,8 @@ public:
const sf::Vector2f& position, const Yaml& config); const sf::Vector2f& position, const Yaml& config);
void setCrosshairPosition(const sf::Vector2f& position); void setCrosshairPosition(const sf::Vector2f& position);
void fire(); void pullTrigger();
void releaseTrigger();
void move(const sf::Vector2f& destination); void move(const sf::Vector2f& destination);
void setDirection(Direction direction, bool unset); void setDirection(Direction direction, bool unset);