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

48 lines
1.2 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
#include "../util/Collection.h"
#include "../effects/Bullet.h"
2012-10-12 17:22:53 +00:00
const String Weapon::KEY_BULLET = "bullet";
Weapon::Weapon(const Instances& instances, Physical& holder, const Yaml& config) :
Emitter(instances.collection),
mHolder(holder),
mWorld(instances.world),
mBullet(config.get<String>(KEY_BULLET)) {
Yaml bullet(mBullet);
Vector2i bulletSize = bullet.get<Vector2i>(Physical::KEY_SIZE);
mOffset = Vector2f(0, std::max(mHolder.getSize().x, mHolder.getSize().y) / 2 +
b2_linearSlop +
std::max(bulletSize.x, bulletSize.y) / 2);
}
/**
* Pull the trigger.
*/
void
Weapon::fire() {
// Only call if has ammo, consider firing rate etc.
emit();
}
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.
Vector2f offset(- mOffset);
thor::rotate(offset, mHolder.getAngle());
return std::shared_ptr<Particle>(new Bullet(mHolder.getPosition() + offset, mWorld,
2012-10-12 17:22:53 +00:00
mHolder, mHolder.getAngle(), Yaml(mBullet)));
}