2012-09-09 20:50:15 +00:00
|
|
|
/*
|
|
|
|
* Weapon.cpp
|
|
|
|
*
|
|
|
|
* Created on: 12.08.2012
|
|
|
|
* Author: Felix
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Weapon.h"
|
2012-09-12 12:21:57 +00:00
|
|
|
|
2012-09-11 20:02:46 +00:00
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
#include <thor/Vectors.hpp>
|
2012-09-12 12:21:57 +00:00
|
|
|
|
2012-09-09 20:50:15 +00:00
|
|
|
#include "../util/Collection.h"
|
|
|
|
#include "../effects/Bullet.h"
|
2012-10-12 17:04:34 +00:00
|
|
|
|
2012-10-12 17:22:53 +00:00
|
|
|
const String Weapon::KEY_BULLET = "bullet";
|
2012-12-20 13:59:05 +00:00
|
|
|
const String Weapon::DEFAULT_BULLET = "bullet.yaml";
|
2012-10-19 13:44:45 +00:00
|
|
|
const String Weapon::KEY_INTERVAL = "interval";
|
2012-12-20 13:59:05 +00:00
|
|
|
const int Weapon::DEFAULT_INTERVAL = 250;
|
2012-09-11 20:02:46 +00:00
|
|
|
|
2012-12-20 09:31:32 +00:00
|
|
|
Weapon::Weapon(const Instances& instances, Body& holder, const Yaml& config) :
|
2012-10-04 17:10:12 +00:00
|
|
|
Emitter(instances.collection),
|
2012-09-09 20:50:15 +00:00
|
|
|
mHolder(holder),
|
2012-10-04 17:10:12 +00:00
|
|
|
mWorld(instances.world),
|
2012-12-20 13:59:05 +00:00
|
|
|
mBullet(config.get(KEY_BULLET, DEFAULT_BULLET)),
|
|
|
|
mTimer(sf::milliseconds(config.get(KEY_INTERVAL, DEFAULT_INTERVAL))) {
|
2012-10-13 10:48:16 +00:00
|
|
|
Yaml bullet(mBullet);
|
2012-12-20 13:59:05 +00:00
|
|
|
Vector2i bulletSize = bullet.get(Body::KEY_SIZE, Body::DEFAULT_SIZE);
|
2012-10-13 10:48:16 +00:00
|
|
|
mOffset = Vector2f(0, std::max(mHolder.getSize().x, mHolder.getSize().y) / 2 +
|
2012-09-11 20:02:46 +00:00
|
|
|
b2_linearSlop +
|
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
|
|
|
|
Weapon::fire() {
|
|
|
|
// Only call if has ammo, consider firing rate etc.
|
2012-10-19 13:44:45 +00:00
|
|
|
if (mTimer.isExpired()) {
|
|
|
|
emit();
|
|
|
|
mTimer.start();
|
|
|
|
}
|
2012-09-09 20:50:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<Particle>
|
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.
|
|
|
|
Vector2f offset(- mOffset);
|
|
|
|
thor::rotate(offset, mHolder.getAngle());
|
2012-10-13 10:48:16 +00:00
|
|
|
return std::shared_ptr<Particle>(new Bullet(mHolder.getPosition() + offset, mWorld,
|
2012-10-12 17:22:53 +00:00
|
|
|
mHolder, mHolder.getAngle(), Yaml(mBullet)));
|
2012-09-09 20:50:15 +00:00
|
|
|
}
|