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-07-14 19:56:44 +00:00
|
|
|
Weapon::Weapon(World& world, Character& holder, const Yaml& config) :
|
2013-08-07 15:39:43 +00:00
|
|
|
Item(Vector2f(80, 30), "weapon.png"),
|
2013-06-25 18:59:03 +00:00
|
|
|
mWorld(world),
|
2013-07-21 08:52:50 +00:00
|
|
|
mHolder(&holder),
|
2013-07-06 11:38:15 +00:00
|
|
|
mName(config.get("name", std::string())),
|
2013-06-25 16:28:18 +00:00
|
|
|
mProjectile(config.get("bullet", std::string("bullet.yaml"))),
|
|
|
|
mDamage(config.get("damage", 0)),
|
|
|
|
mProjectileSpeed(config.get("projectile_speed", 0.0f)),
|
2013-06-25 15:44:40 +00:00
|
|
|
mFireInterval(config.get("fire_interval", 0)),
|
|
|
|
mReloadTime(config.get("reload_time", 0)),
|
2013-06-25 18:59:03 +00:00
|
|
|
mFiring(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)),
|
2013-06-25 18:59:03 +00:00
|
|
|
mTotalAmmo(mMaxTotalAmmo),
|
2013-06-25 19:28:22 +00:00
|
|
|
mPellets(config.get("pellets", 0)),
|
2013-06-25 18:59:03 +00:00
|
|
|
mPelletSpread(config.get("pellet_spread", 0.0f)),
|
2013-06-25 19:28:22 +00:00
|
|
|
mReloadSingle(config.get("reload_single", false)),
|
2013-06-25 19:35:58 +00:00
|
|
|
mSpread(config.get("spread", 0.0f)),
|
2013-07-06 11:22:55 +00:00
|
|
|
mSpreadMoving(config.get("spread_moving", 0.0f)),
|
|
|
|
mMaxRange(config.get("max_range", 0.0f)),
|
|
|
|
mRequiresAmmo(!config.get("requires_no_ammo", false)) {
|
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() {
|
2013-06-25 18:59:03 +00:00
|
|
|
mFiring = true;
|
2012-12-24 00:14:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Release the trigger.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
Weapon::releaseTrigger() {
|
2013-06-25 18:59:03 +00:00
|
|
|
mFiring = false;
|
2012-12-24 00:14:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
2013-06-25 18:59:03 +00:00
|
|
|
|
2013-06-25 15:44:40 +00:00
|
|
|
if (mIsReloading) {
|
2013-06-25 18:59:03 +00:00
|
|
|
if (!mReloadSingle) {
|
|
|
|
mMagazineAmmo = (mTotalAmmo >= mMagazineSize)
|
|
|
|
? mMagazineSize
|
|
|
|
: mTotalAmmo;
|
|
|
|
mTotalAmmo -= mMagazineAmmo;
|
|
|
|
mIsReloading = false;
|
|
|
|
}
|
|
|
|
else if (mTotalAmmo > 0) {
|
|
|
|
mMagazineAmmo++;
|
|
|
|
mTotalAmmo--;
|
|
|
|
if (mMagazineAmmo == mMagazineSize)
|
|
|
|
mIsReloading = false;
|
|
|
|
else
|
|
|
|
reload();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
mIsReloading = false;
|
2013-06-25 15:44:40 +00:00
|
|
|
}
|
|
|
|
|
2013-07-06 11:22:55 +00:00
|
|
|
if (mFiring && (!mRequiresAmmo || mMagazineAmmo != 0)) {
|
2013-06-25 18:59:03 +00:00
|
|
|
fire();
|
2013-04-04 21:13:08 +00:00
|
|
|
if (!mAutomatic)
|
2013-06-25 18:59:03 +00:00
|
|
|
mFiring = false;
|
2012-10-19 13:44:45 +00:00
|
|
|
}
|
2013-06-25 15:44:40 +00:00
|
|
|
|
2013-07-06 11:22:55 +00:00
|
|
|
if (mRequiresAmmo && mMagazineAmmo == 0 && mTotalAmmo != 0)
|
2013-06-25 15:44:40 +00:00
|
|
|
reload();
|
2012-09-09 20:50:15 +00:00
|
|
|
}
|
|
|
|
|
2013-06-25 15:44:40 +00:00
|
|
|
/**
|
|
|
|
* Creates and fires a projectile.
|
|
|
|
*/
|
2013-06-25 18:59:03 +00:00
|
|
|
void
|
|
|
|
Weapon::fire() {
|
2013-06-25 15:44:40 +00:00
|
|
|
mTimer.restart(sf::milliseconds(mFireInterval));
|
2013-07-06 11:22:55 +00:00
|
|
|
if (mRequiresAmmo)
|
|
|
|
mMagazineAmmo--;
|
2013-06-25 18:59:03 +00:00
|
|
|
|
|
|
|
|
2013-06-25 19:28:22 +00:00
|
|
|
if (mPellets == 0)
|
|
|
|
insertProjectile(0.0f);
|
2013-06-25 18:59:03 +00:00
|
|
|
else
|
|
|
|
for (int i = - mPellets / 2; i < mPellets / 2; i++) {
|
|
|
|
insertProjectile(i * mPelletSpread);
|
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
2013-07-06 11:38:15 +00:00
|
|
|
std::string
|
|
|
|
Weapon::getName() const {
|
|
|
|
return mName;
|
|
|
|
}
|
|
|
|
|
2013-06-25 15:44:40 +00:00
|
|
|
void
|
|
|
|
Weapon::reload() {
|
2013-06-25 18:59:03 +00:00
|
|
|
if (mMagazineAmmo == mMagazineSize)
|
|
|
|
return;
|
2013-06-25 15:44:40 +00:00
|
|
|
mIsReloading = true;
|
|
|
|
mTimer.restart(sf::milliseconds(mReloadTime));
|
|
|
|
}
|
2013-06-25 18:59:03 +00:00
|
|
|
|
2013-06-25 20:00:40 +00:00
|
|
|
void
|
|
|
|
Weapon::cancelReload() {
|
|
|
|
mIsReloading = false;
|
|
|
|
// To make sure time isn't skipped.
|
|
|
|
mTimer.restart(sf::milliseconds(mFireInterval));
|
|
|
|
}
|
|
|
|
|
2013-07-21 08:52:50 +00:00
|
|
|
void
|
|
|
|
Weapon::setHolder(Character& holder) {
|
|
|
|
mHolder = &holder;
|
|
|
|
}
|
|
|
|
|
2013-06-25 18:59:03 +00:00
|
|
|
/**
|
|
|
|
* Creates a new projectile and inserts it into the world.
|
|
|
|
*
|
|
|
|
* @param angle Inaccuracy of the projectile, 0 is straight forward.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
Weapon::insertProjectile(float angle) {
|
2013-08-07 15:39:43 +00:00
|
|
|
Vector2f offset(mHolder->getDirection() * mHolder->getRadius());
|
2013-06-25 19:28:22 +00:00
|
|
|
|
2013-08-07 15:39:43 +00:00
|
|
|
float spread = (mHolder->getSpeed() == Vector2f())
|
2013-06-25 19:35:58 +00:00
|
|
|
? mSpread
|
|
|
|
: mSpreadMoving;
|
|
|
|
std::uniform_real_distribution<float> distribution(- spread, spread);
|
2013-07-10 21:25:53 +00:00
|
|
|
angle += distribution(mGenerator) + 90.0f;
|
2013-06-25 19:28:22 +00:00
|
|
|
|
2013-08-07 15:39:43 +00:00
|
|
|
Vector2f direction(thor::rotatedVector(mHolder->getDirection(), angle));
|
2013-06-25 19:28:22 +00:00
|
|
|
|
2013-07-21 08:52:50 +00:00
|
|
|
std::shared_ptr<Sprite> projectile(new Bullet(mHolder->getPosition() + offset,
|
|
|
|
*mHolder, direction, mProjectile, mProjectileSpeed,
|
2013-07-06 11:22:55 +00:00
|
|
|
mDamage, mMaxRange));
|
2013-06-25 18:59:03 +00:00
|
|
|
mWorld.insert(projectile);
|
|
|
|
}
|