2012-09-09 20:50:15 +00:00
|
|
|
/*
|
|
|
|
* Bullet.cpp
|
|
|
|
*
|
|
|
|
* Created on: 12.08.2012
|
|
|
|
* Author: Felix
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Bullet.h"
|
2012-12-22 00:14:30 +00:00
|
|
|
|
|
|
|
#include <Thor/Vectors.hpp>
|
2012-09-12 12:21:57 +00:00
|
|
|
|
2012-10-01 09:02:44 +00:00
|
|
|
#include "../abstract/Character.h"
|
2012-10-12 17:22:53 +00:00
|
|
|
#include "../util/Loader.h"
|
|
|
|
#include "../util/ResourceManager.h"
|
2012-09-09 20:50:15 +00:00
|
|
|
|
2012-12-22 14:10:26 +00:00
|
|
|
const std::string Bullet::KEY_DAMAGE = "damage";
|
2012-12-20 13:59:05 +00:00
|
|
|
const int Bullet::DEFAULT_DAMAGE = 10;
|
2012-12-22 14:10:26 +00:00
|
|
|
const std::string Bullet::KEY_SPEED = "speed";
|
2012-12-20 13:59:05 +00:00
|
|
|
const float Bullet::DEFAULT_SPEED = 500;
|
2012-09-09 20:50:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Places a bullet in the world.
|
|
|
|
*
|
|
|
|
* @param position World position of the bullet.
|
|
|
|
* @param world Box2d world.
|
|
|
|
* @param texture Texture to display for bullet.
|
|
|
|
*/
|
2012-12-22 14:10:26 +00:00
|
|
|
Bullet::Bullet(const sf::Vector2f& position, Body& shooter, float direction,
|
2012-10-12 17:22:53 +00:00
|
|
|
const Yaml& config) :
|
2012-12-22 13:56:17 +00:00
|
|
|
Particle(config, Data(position, 0, CATEGORY_PARTICLE, CATEGORY_PARTICLE)),
|
2012-09-11 14:55:04 +00:00
|
|
|
mShooter(shooter),
|
2012-12-20 13:59:05 +00:00
|
|
|
mDamage(config.get(KEY_DAMAGE, DEFAULT_DAMAGE)),
|
2012-12-22 00:14:30 +00:00
|
|
|
mSpeed(config.get(KEY_SPEED, DEFAULT_SPEED)) {
|
2012-12-22 14:10:26 +00:00
|
|
|
sf::Vector2f dir(1.0f, 0);
|
2012-12-22 15:44:07 +00:00
|
|
|
thor::setPolarAngle(dir, direction - 90);
|
2012-12-22 00:14:30 +00:00
|
|
|
setSpeed(dir, mSpeed);
|
2012-09-09 20:50:15 +00:00
|
|
|
setAngle(direction);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @copydoc Physical::onCollide
|
|
|
|
*/
|
|
|
|
void
|
2012-12-22 15:52:43 +00:00
|
|
|
Bullet::onCollide(Body& other) {
|
2012-09-09 20:50:15 +00:00
|
|
|
// 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.
|
2012-12-22 15:52:43 +00:00
|
|
|
if (other.getCategory() == CATEGORY_ACTOR) {
|
2012-10-01 09:02:44 +00:00
|
|
|
Character& a = dynamic_cast<Character&>(other);
|
2012-09-11 14:55:04 +00:00
|
|
|
a.onDamage(mDamage);
|
2012-09-12 12:21:57 +00:00
|
|
|
}
|
2012-09-09 20:50:15 +00:00
|
|
|
setDelete(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2012-12-20 09:31:32 +00:00
|
|
|
Bullet::doesCollide(Body& other) {
|
2012-09-09 20:50:15 +00:00
|
|
|
return &other != &mShooter;
|
|
|
|
}
|