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/effects/Bullet.cpp

54 lines
1.4 KiB
C++
Raw Normal View History

/*
* Bullet.cpp
*
* Created on: 12.08.2012
* Author: Felix
*/
#include "Bullet.h"
#include <Thor/Vectors.hpp>
2012-09-12 12:21:57 +00:00
2012-10-01 09:02:44 +00:00
#include "../abstract/Character.h"
2013-03-29 17:34:51 +00:00
#include "../util/Yaml.h"
2012-12-22 14:10:26 +00:00
const std::string Bullet::KEY_DAMAGE = "damage";
const int Bullet::DEFAULT_DAMAGE = 10;
2012-12-22 14:10:26 +00:00
const std::string Bullet::KEY_SPEED = "speed";
const float Bullet::DEFAULT_SPEED = 500;
/**
* Places a bullet in the world.
*
* @param position World position of the bullet.
* @param world Box2d world.
* @param texture Texture to display for bullet.
*/
Bullet::Bullet(const sf::Vector2f& position, Sprite& shooter,
sf::Vector2f direction, const Yaml& config) :
Particle(config, Data(position, CATEGORY_PARTICLE,
~CATEGORY_PARTICLE)),
2012-09-11 14:55:04 +00:00
mShooter(shooter),
mDamage(config.get(KEY_DAMAGE, DEFAULT_DAMAGE)),
mSpeed(config.get(KEY_SPEED, DEFAULT_SPEED)) {
thor::rotate(direction, - 90.0f);
setSpeed(direction, mSpeed);
setDirection(direction);
}
/**
* @copydoc Physical::onCollide
*/
void
2013-03-03 20:28:12 +00:00
Bullet::onCollide(std::shared_ptr<Sprite> other) {
// Make sure we do not damage twice.
if (!getDelete()) {
2012-09-12 12:21:57 +00:00
// Call onShot on other, with damage as param.
2013-03-03 20:28:12 +00:00
if (other->getCategory() == CATEGORY_ACTOR) {
std::shared_ptr<Character> character = std::static_pointer_cast<Character>(other);
character->onDamage(mDamage);
2012-09-12 12:21:57 +00:00
}
setDelete(true);
}
}