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
|
|
|
|
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"
|
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-09-11 20:02:46 +00:00
|
|
|
|
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),
|
2013-06-25 15:44:40 +00:00
|
|
|
mBullet(config.get("bullet", std::string("bullet.yaml"))),
|
|
|
|
mFireInterval(config.get("fire_interval", 0)),
|
|
|
|
mReloadTime(config.get("reload_time", 0)),
|
2012-12-24 00:14:22 +00:00
|
|
|
mFire(false),
|
2013-06-25 15:44:40 +00:00
|
|
|
mAutomatic(config.get("automatic", false)),
|
|
|
|
mMagazineSize(config.get("magazine_size", 0)),
|
|
|
|
mMagazineAmmo(mMagazineSize),
|
|
|
|
mMaxTotalAmmo(config.get("max_total_ammo", 0)),
|
|
|
|
mTotalAmmo(mMaxTotalAmmo) {
|
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) {
|
2013-06-25 15:44:40 +00:00
|
|
|
if (!mTimer.isExpired())
|
|
|
|
return;
|
|
|
|
if (mIsReloading) {
|
|
|
|
mMagazineAmmo = (mTotalAmmo >= mMagazineSize)
|
|
|
|
? mMagazineSize
|
|
|
|
: mTotalAmmo;
|
|
|
|
mTotalAmmo -= mMagazineAmmo;
|
|
|
|
mIsReloading = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mFire && mMagazineAmmo != 0) {
|
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
|
|
|
}
|
2013-06-25 15:44:40 +00:00
|
|
|
|
|
|
|
if (mMagazineAmmo == 0 && mTotalAmmo != 0)
|
|
|
|
reload();
|
2012-09-09 20:50:15 +00:00
|
|
|
}
|
|
|
|
|
2013-06-25 15:44:40 +00:00
|
|
|
/**
|
|
|
|
* Creates and fires a projectile.
|
|
|
|
*/
|
2013-03-29 17:34:51 +00:00
|
|
|
std::shared_ptr<Sprite>
|
2013-06-25 15:44:40 +00:00
|
|
|
Weapon::createParticle() {
|
|
|
|
mTimer.restart(sf::milliseconds(mFireInterval));
|
|
|
|
mMagazineAmmo--;
|
|
|
|
|
2012-09-11 20:02:46 +00:00
|
|
|
// 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());
|
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
|
|
|
}
|
2013-06-25 15:44:40 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
Weapon::getMagazineAmmo() const {
|
|
|
|
return mMagazineAmmo;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
Weapon::getTotalAmmo() const {
|
|
|
|
return mTotalAmmo;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Weapon::reload() {
|
|
|
|
mIsReloading = true;
|
|
|
|
mTimer.restart(sf::milliseconds(mReloadTime));
|
|
|
|
}
|