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"
|
2013-03-29 17:34:51 +00:00
|
|
|
#include "../util/Yaml.h"
|
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.
|
|
|
|
*/
|
2013-08-07 15:39:43 +00:00
|
|
|
Bullet::Bullet(const Vector2f& position, Character& shooter,
|
|
|
|
Vector2f direction, const Yaml& config, float speed,
|
2013-07-06 11:22:55 +00:00
|
|
|
float damage, float maxRange) :
|
2013-06-25 18:59:03 +00:00
|
|
|
Circle(position, CATEGORY_PARTICLE, ~CATEGORY_PARTICLE,
|
2013-05-07 22:00:05 +00:00
|
|
|
config, thor::rotatedVector(direction, -90.0f)),
|
2012-09-11 14:55:04 +00:00
|
|
|
mShooter(shooter),
|
2013-06-25 16:28:18 +00:00
|
|
|
mDamage(damage),
|
2013-07-06 11:22:55 +00:00
|
|
|
mSpeed(speed),
|
|
|
|
mMaxRangeSquared((maxRange == 0) ? std::numeric_limits<float>::max() : maxRange * maxRange),
|
|
|
|
mStartPoint(getPosition()) {
|
2013-05-07 22:00:05 +00:00
|
|
|
setSpeed(thor::rotatedVector(direction, -90.0f), mSpeed);
|
2012-09-09 20:50:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-05-26 18:44:59 +00:00
|
|
|
* Deletes this and calls onDamage if other is a character. Does not
|
|
|
|
* damage shooter.
|
2012-09-09 20:50:15 +00:00
|
|
|
*/
|
|
|
|
void
|
2013-03-03 20:28:12 +00:00
|
|
|
Bullet::onCollide(std::shared_ptr<Sprite> other) {
|
2013-07-06 11:22:55 +00:00
|
|
|
if (thor::squaredLength(getPosition() - mStartPoint) >= mMaxRangeSquared) {
|
|
|
|
setDelete(true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-09-09 20:50:15 +00:00
|
|
|
// Make sure we do not damage twice.
|
2013-07-06 11:22:55 +00:00
|
|
|
if (!getDelete() && (&*other != &mShooter)) {
|
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
|
|
|
}
|
2012-09-09 20:50:15 +00:00
|
|
|
setDelete(true);
|
|
|
|
}
|
|
|
|
}
|