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

78 lines
1.8 KiB
C++
Raw Normal View History

/*
* Weapon.cpp
*
* Created on: 12.08.2012
* Author: Felix
*/
#include "Weapon.h"
2012-09-12 12:21:57 +00:00
#include <algorithm>
#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"
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";
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-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),
2012-12-22 00:44:36 +00:00
mWorld(world),
2012-12-22 13:56:17 +00:00
mHolder(holder),
mBullet(config.get(KEY_BULLET, DEFAULT_BULLET)),
2012-12-24 00:14:22 +00:00
mTimer(sf::milliseconds(config.get(KEY_INTERVAL, DEFAULT_INTERVAL))),
mFire(false),
mAutomatic(config.get(KEY_AUTOMATIC, DEFAULT_AUTOMATIC)) {
2012-12-23 14:50:49 +00:00
sf::Vector2f holderSize = mHolder.getSize();
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,
std::max(holderSize.x, holderSize.y) / 2 +
std::max(bulletSize.x, bulletSize.y) / 2);
}
/**
* 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.
*/
void
Weapon::think() {
if (mFire && mTimer.isExpired()) {
emit();
mTimer.start();
2012-12-24 00:14:22 +00:00
if (!mAutomatic) {
mFire = false;
}
}
}
std::shared_ptr<Particle>
2012-09-12 12:21:57 +00:00
Weapon::createParticle() {
// Minus to account for positive y-axis going downwards in SFML.
2012-12-22 14:10:26 +00:00
sf::Vector2f offset(- mOffset);
thor::rotate(offset, mHolder.getAngle());
2012-12-22 13:56:17 +00:00
return std::shared_ptr<Particle>(new Bullet(mHolder.getPosition() + offset,
2012-10-12 17:22:53 +00:00
mHolder, mHolder.getAngle(), Yaml(mBullet)));
}