2012-09-09 20:50:15 +00:00
|
|
|
/*
|
|
|
|
* Weapon.cpp
|
|
|
|
*
|
|
|
|
* Created on: 12.08.2012
|
|
|
|
* Author: Felix
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Weapon.h"
|
2012-09-11 20:02:46 +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"
|
2012-09-09 20:50:15 +00:00
|
|
|
#include "../effects/Bullet.h"
|
2013-03-29 17:34:51 +00:00
|
|
|
#include "../util/Yaml.h"
|
2012-10-12 17:04:34 +00:00
|
|
|
|
2012-12-22 14:10:26 +00:00
|
|
|
const std::string Weapon::KEY_BULLET = "bullet";
|
|
|
|
const std::string Weapon::DEFAULT_BULLET = "bullet.yaml";
|
|
|
|
const std::string Weapon::KEY_INTERVAL = "interval";
|
2012-12-20 13:59:05 +00:00
|
|
|
const int Weapon::DEFAULT_INTERVAL = 250;
|
2012-12-24 00:14:22 +00:00
|
|
|
const std::string Weapon::KEY_AUTOMATIC = "automatic";
|
|
|
|
const bool Weapon::DEFAULT_AUTOMATIC = false;
|
2012-09-11 20:02:46 +00:00
|
|
|
|
2012-12-23 14:50:49 +00:00
|
|
|
Weapon::Weapon(World& world, Sprite& holder, const Yaml& config) :
|
2012-12-22 13:56:17 +00:00
|
|
|
Emitter(world),
|
|
|
|
mHolder(holder),
|
2012-12-20 13:59:05 +00:00
|
|
|
mBullet(config.get(KEY_BULLET, DEFAULT_BULLET)),
|
2013-03-09 15:25:04 +00:00
|
|
|
mLastShotWaitInterval(0),
|
|
|
|
mFireInterval(config.get(KEY_INTERVAL, DEFAULT_INTERVAL)),
|
2012-12-24 00:14:22 +00:00
|
|
|
mFire(false),
|
|
|
|
mAutomatic(config.get(KEY_AUTOMATIC, DEFAULT_AUTOMATIC)) {
|
2012-12-23 14:50:49 +00:00
|
|
|
sf::Vector2f holderSize = mHolder.getSize();
|
2012-10-13 10:48:16 +00:00
|
|
|
Yaml bullet(mBullet);
|
2012-12-23 14:50:49 +00:00
|
|
|
sf::Vector2f bulletSize = bullet.get(Sprite::KEY_SIZE, sf::Vector2f());
|
2012-12-22 14:10:26 +00:00
|
|
|
mOffset = sf::Vector2f(0,
|
2012-12-22 00:14:30 +00:00
|
|
|
std::max(holderSize.x, holderSize.y) / 2 +
|
2012-10-13 10:48:16 +00:00
|
|
|
std::max(bulletSize.x, bulletSize.y) / 2);
|
2012-09-09 20:50:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-10-12 17:04:34 +00:00
|
|
|
* Pull the trigger.
|
2012-09-09 20:50:15 +00:00
|
|
|
*/
|
|
|
|
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;
|
2012-10-19 13:44:45 +00:00
|
|
|
emit();
|
2013-04-04 21:13:08 +00:00
|
|
|
if (!mAutomatic)
|
2012-12-24 00:14:22 +00:00
|
|
|
mFire = false;
|
2012-10-19 13:44:45 +00:00
|
|
|
}
|
2012-09-09 20:50:15 +00:00
|
|
|
}
|
|
|
|
|
2013-03-29 17:34:51 +00:00
|
|
|
std::shared_ptr<Sprite>
|
2012-09-12 12:21:57 +00:00
|
|
|
Weapon::createParticle() {
|
2012-09-11 20:02:46 +00:00
|
|
|
// Minus to account for positive y-axis going downwards in SFML.
|
2012-12-22 14:10:26 +00:00
|
|
|
sf::Vector2f offset(- mOffset);
|
2013-03-30 01:30:11 +00:00
|
|
|
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,
|
2013-03-30 01:30:11 +00:00
|
|
|
mHolder, mHolder.getDirection(), Yaml(mBullet)));
|
2012-09-09 20:50:15 +00:00
|
|
|
}
|