This repository has been archived on 2019-12-07. You can view files and clone it, but cannot push or open issues or pull requests.
dungeon-gunner/source/items/Weapon.cpp

72 lines
1.7 KiB
C++
Raw Normal View History

/*
* Weapon.cpp
*
* Created on: 12.08.2012
* Author: Felix
*/
#include "Weapon.h"
2013-05-20 10:07:05 +00:00
#include <Thor/Vectors.hpp>
2012-09-12 12:21:57 +00:00
2012-12-22 13:56:17 +00:00
#include "../World.h"
#include "../effects/Bullet.h"
2013-03-29 17:34:51 +00:00
#include "../util/Yaml.h"
2013-05-26 18:44:59 +00:00
Weapon::Weapon(World& world, Character& holder, const Yaml& config) :
2012-12-22 13:56:17 +00:00
Emitter(world),
mHolder(holder),
mBullet(config.get(YAML_KEY::BULLET, YAML_DEFAULT::BULLET)),
2013-03-09 15:25:04 +00:00
mLastShotWaitInterval(0),
mFireInterval(config.get(YAML_KEY::INTERVAL, YAML_DEFAULT::INTERVAL)),
2012-12-24 00:14:22 +00:00
mFire(false),
2013-05-26 18:44:59 +00:00
mAutomatic(config.get(YAML_KEY::AUTOMATIC, YAML_DEFAULT::AUTOMATIC)) {
}
/**
* Pull the trigger.
*/
void
2012-12-24 00:14:22 +00:00
Weapon::pullTrigger() {
mFire = true;
}
/**
* Release the trigger.
*/
void
Weapon::releaseTrigger() {
mFire = false;
}
/**
* Fire if the trigger has been pulled, time between bullets is over, has ammo etc.
2013-03-09 15:25:04 +00:00
*
* @param elapsed Amount of time to simulate.
2012-12-24 00:14:22 +00:00
*/
void
2013-03-09 15:25:04 +00:00
Weapon::onThink(int elapsed) {
// Waiting for next shot, subtract time since last onThink.
2013-04-04 21:13:08 +00:00
if (mLastShotWaitInterval > 0)
2013-03-09 15:25:04 +00:00
mLastShotWaitInterval -= elapsed;
// Only reset to zero if we didn't recently fire (allow catching up for missed bullets).
2013-04-04 21:13:08 +00:00
else
2013-03-09 15:25:04 +00:00
mLastShotWaitInterval = 0;
// Loop just in case we miss a bullet to fire.
while (mFire && mLastShotWaitInterval <= 0) {
mLastShotWaitInterval += mFireInterval;
emit();
2013-04-04 21:13:08 +00:00
if (!mAutomatic)
2012-12-24 00:14:22 +00:00
mFire = false;
}
}
2013-03-29 17:34:51 +00:00
std::shared_ptr<Sprite>
2012-09-12 12:21:57 +00:00
Weapon::createParticle() {
// Minus to account for positive y-axis going downwards in SFML.
2013-05-26 18:44:59 +00:00
sf::Vector2f offset(0, - mHolder.getRadius());
thor::rotate(offset, thor::polarAngle(mHolder.getDirection()));
2013-03-29 17:34:51 +00:00
return std::shared_ptr<Sprite>(new Bullet(mHolder.getPosition() + offset,
mHolder, mHolder.getDirection(), Yaml(mBullet)));
}