Changed sprites to always render as rectangle.

Also created Rectangle/Circle subclasses for sprite to handle
collision detection and moved collision detection from World to
CollisionModel (static methods).
This commit is contained in:
Felix Ableitner 2013-05-08 00:00:05 +02:00
parent 22bcd8cf13
commit aa6df65e97
20 changed files with 367 additions and 260 deletions

View file

@ -70,17 +70,17 @@ World::step(int elapsed) {
for (auto v = mDrawables.begin(); v != mDrawables.end(); v++) { for (auto v = mDrawables.begin(); v != mDrawables.end(); v++) {
for (auto it = v->second.begin(); it != v->second.end(); it++) { for (auto it = v->second.begin(); it != v->second.end(); it++) {
auto& spriteA = *it; auto& spriteA = *it;
sf::Vector2f speed = spriteA->getSpeed() * (elapsed / 1000.0f);
if (spriteA->getDelete()) { if (spriteA->getDelete()) {
v->second.erase(it); v->second.erase(it);
it--; it--;
} }
// Apply movement for movable sprites. else if (spriteA->getSpeed() != sf::Vector2f()) {
else if (spriteA->getSpeed() != sf::Vector2f() && sf::Vector2f speed = spriteA->getSpeed() * (elapsed / 1000.0f);
!doesOverlap(spriteA, elapsed)) if (!doesOverlap(spriteA, elapsed))
spriteA->setPosition(spriteA->getPosition() + speed); spriteA->setPosition(spriteA->getPosition() + speed);
} }
} }
}
} }
/** /**
@ -97,7 +97,7 @@ World::doesOverlap(std::shared_ptr<Sprite> spriteA, int elapsed) {
if (!spriteA->collisionEnabled(spriteB->getCategory()) || if (!spriteA->collisionEnabled(spriteB->getCategory()) ||
!spriteB->collisionEnabled(spriteA->getCategory())) !spriteB->collisionEnabled(spriteA->getCategory()))
continue; continue;
if (testCollision(spriteA, spriteB, elapsed)) { if (spriteA->testCollision(spriteB, elapsed)) {
spriteA->onCollide(spriteB); spriteA->onCollide(spriteB);
spriteB->onCollide(spriteA); spriteB->onCollide(spriteA);
return true; return true;
@ -128,112 +128,6 @@ World::think(int elapsed) {
} }
} }
/**
* Tests for collisions using Seperating Axis Theorem (SAT).
*
* http://www.metanetsoftware.com/technique/tutorialA.html
*
* @param spriteA, spriteB Pair of sprites which to test for collision/overlapping.
* @param elapsed Time elapsed in this step.
* @return True if both sprites will be overlapping after their current movement.
*/
bool
World::testCollision(std::shared_ptr<Sprite> spriteA,
std::shared_ptr<Sprite> spriteB, int elapsed) const {
// circle-circle collision
if ((spriteA->mShape.type == Sprite::Shape::Type::CIRCLE) &&
(spriteB->mShape.type == Sprite::Shape::Type::CIRCLE)) {
sf::Vector2f axis = spriteA->getPosition() - spriteB->getPosition();
// If both objects are at the exact same position, allow any movement for unstucking.
if (axis == sf::Vector2f())
return true;
axis = thor::unitVector(axis);
float centerA = thor::dotProduct(axis, spriteA->getPosition());
float radiusA = spriteA->getRadius();
float movementA = thor::dotProduct(axis, spriteA->getSpeed() * (elapsed / 1000.0f));
float centerB = thor::dotProduct(axis, spriteB->getPosition());
float radiusB = spriteB->getRadius();
float movementB = thor::dotProduct(axis, spriteB->getSpeed() * (elapsed / 1000.0f));
// Allow movement if sprites are moving apart.
return Interval::IntervalFromRadius(centerA, radiusA).getOverlap(
Interval::IntervalFromRadius(centerB, radiusB)).getLength() <
Interval::IntervalFromRadius(centerA + movementA, radiusA).getOverlap(
Interval::IntervalFromRadius(centerB + movementB, radiusB)).getLength();
}
// circle-rect collision
if (((spriteA->mShape.type == Sprite::Shape::Type::CIRCLE) &&
(spriteB->mShape.type == Sprite::Shape::Type::RECTANGLE)) ||
((spriteA->mShape.type == Sprite::Shape::Type::RECTANGLE) &&
(spriteB->mShape.type == Sprite::Shape::Type::CIRCLE))) {
std::shared_ptr<Sprite> circle = spriteA;
std::shared_ptr<Sprite> rect = spriteB;
if (circle->mShape.type != Sprite::Shape::Type::CIRCLE)
std::swap(circle, rect);
float radius = circle->getRadius();
sf::Vector2f halfsize = rect->getSize() / 2.0f;
sf::Vector2f circlePos = circle->getPosition();
sf::Vector2f rectPos = rect->getPosition();
// Only circle movement as rectangles don't move.
sf::Vector2f circleMovement = circle->getSpeed() * (elapsed / 1000.0f);
// We assume that rectangles are always axis aligned.
float overlapNoMovementX = Interval::IntervalFromRadius(circlePos.x, radius)
.getOverlap(Interval::IntervalFromRadius(rectPos.x, halfsize.x)).getLength();
float overlapMovementX = Interval::IntervalFromRadius(circlePos.x + circleMovement.x, radius)
.getOverlap(Interval::IntervalFromRadius(rectPos.x, halfsize.x)).getLength();
float overlapNoMovementY = Interval::IntervalFromRadius(circlePos.y, radius)
.getOverlap(Interval::IntervalFromRadius(rectPos.y, halfsize.y)).getLength();
float overlapMovementY = Interval::IntervalFromRadius(circlePos.y + circleMovement.y, radius)
.getOverlap(Interval::IntervalFromRadius(rectPos.y, halfsize.y)).getLength();
bool xyCollisionResult = (((overlapNoMovementX < overlapMovementX) &&
(overlapNoMovementY > 0)) ||
((overlapNoMovementY < overlapMovementY) && (overlapNoMovementX > 0)));
// If circle center is overlapping rectangle on x or y axis, we can take xyCollisionResult.
if (Interval::IntervalFromRadius(rectPos.x, halfsize.x).isInside(circlePos.x) ||
Interval::IntervalFromRadius(rectPos.y, halfsize.y).isInside(circlePos.y))
return xyCollisionResult;
// Test if the circle is colliding with a corner of the rectangle.
else if (xyCollisionResult) {
// This is the same as circle-circle collision.
sf::Vector2f axis = circle->getPosition() - rect->getPosition();
// If both objects are at the exact same position, allow any
// movement for unstucking.
if (axis == sf::Vector2f())
return true;
axis = thor::unitVector(axis);
float circlePosProjected = thor::dotProduct(axis, circlePos);
float movementProjected = thor::dotProduct(axis, circleMovement);
float rectPosProjected = thor::dotProduct(axis, rectPos);
// For corner projections, those on the same line with the rect
// center are equal by value, so we only need one on each axis
// and take the maximum.
float rectHalfWidthProjected = std::max(
abs(thor::dotProduct(axis, halfsize)),
abs(thor::dotProduct(axis,
sf::Vector2f(halfsize.x, -halfsize.y))));
// Allow movement if sprites are moving apart.
return Interval::IntervalFromRadius(circlePosProjected, radius)
.getOverlap(Interval::IntervalFromRadius(rectPosProjected,
rectHalfWidthProjected))
.getLength() <
Interval::IntervalFromRadius(circlePosProjected + movementProjected, radius)
.getOverlap(Interval::IntervalFromRadius(rectPosProjected,
rectHalfWidthProjected))
.getLength();
}
// If there is no collision on x and y axis, there can't be one at all.
else {
return false;
}
}
// Rectangles can't move and thus not collide.
return false;
}
/** /**
* Draws all elements in the group. * Draws all elements in the group.
*/ */

View file

@ -21,9 +21,10 @@ const float Character::VISION_DISTANCE = 500.0f;
/** /**
* Saves pointer to this instance in static var for think(). * Saves pointer to this instance in static var for think().
*/ */
Character::Character(World& world, Pathfinder& pathfinder, const Data& data, Character::Character(const sf::Vector2f& position, Category category,
const Yaml& config) : unsigned short mask, const Yaml& config, World& world,
Sprite(data, config), Pathfinder& pathfinder) :
Circle(position, category, mask, config),
mWorld(world), mWorld(world),
mPathfinder(pathfinder), mPathfinder(pathfinder),
mMaxHealth(config.get(YAML_KEY::HEALTH, YAML_DEFAULT::HEALTH)), mMaxHealth(config.get(YAML_KEY::HEALTH, YAML_DEFAULT::HEALTH)),

View file

@ -8,7 +8,7 @@
#ifndef DG_ACTOR_H_ #ifndef DG_ACTOR_H_
#define DG_ACTOR_H_ #define DG_ACTOR_H_
#include "Sprite.h" #include "Circle.h"
class Pathfinder; class Pathfinder;
class World; class World;
@ -18,10 +18,11 @@ class Yaml;
/** /**
* Provides think function for AI, manages health, drops body on death. * Provides think function for AI, manages health, drops body on death.
*/ */
class Character : public Sprite { class Character : public Circle {
public: public:
explicit Character(World& world, Pathfinder& pathfinder, const Data& data, explicit Character(const sf::Vector2f& position, Category category,
const Yaml& config); unsigned short mask, const Yaml& config, World& world,
Pathfinder& pathfinder);
virtual ~Character() = 0; virtual ~Character() = 0;
void onDamage(int damage); void onDamage(int damage);

View file

@ -0,0 +1,46 @@
/*
* Circle.cpp
*
* Created on: 04.05.2013
* Author: Felix
*/
#include "Circle.h"
#include "Rectangle.h"
#include "../util/Yaml.h"
Circle::Circle(const sf::Vector2f& position, Category category,
unsigned short mask, const Yaml& config,
const sf::Vector2f& direction) :
Sprite(position, category, mask,
sf::Vector2f(config.get(YAML_KEY::RADIUS, 0.0f),
config.get(YAML_KEY::RADIUS, 0.0f)) * 2.0f,
config.get(YAML_KEY::TEXTURE, std::string()), direction) {
}
/**
* Returns true if a collision between this and other occured. It does not
* matter which object is this or other.
*/
bool
Circle::testCollision(std::shared_ptr<Sprite> other, int elapsed) {
Rectangle* rect = dynamic_cast<Rectangle*>(other.get());
Circle* circle = dynamic_cast<Circle*>(other.get());
if (circle != nullptr)
return CollisionModel::testCollision(*this, *circle, elapsed);
else if (rect != nullptr)
return CollisionModel::testCollision(*this, *rect, elapsed);
else {
assert(false);
return false;
}
}
/**
* Returns the radius of the circle used as a collision model for this object.
*/
float
Circle::getRadius() const {
return getSize().x / 2;
}

30
source/abstract/Circle.h Normal file
View file

@ -0,0 +1,30 @@
/*
* Circle.h
*
* Created on: 04.05.2013
* Author: Felix
*/
#ifndef DG_CIRCLE_H_
#define DG_CIRCLE_H_
#include "CollisionModel.h"
#include "Sprite.h"
class Yaml;
/**
* Shape that uses a circle as collision model.
*/
class Circle : public CollisionModel, public Sprite {
public:
explicit Circle(const sf::Vector2f& position, Category category,
unsigned short mask, const Yaml& config,
const sf::Vector2f& direction = sf::Vector2f(0, 0));
virtual ~Circle() = default;
bool testCollision(std::shared_ptr<Sprite> other, int elapsed);
float getRadius() const;
};
#endif /* DG_CIRCLE_H_ */

View file

@ -0,0 +1,127 @@
/*
* CollisionModel.cpp
*
* Created on: 07.05.2013
* Author: Felix
*/
#include "CollisionModel.h"
#include <SFML/System.hpp>
#include <Thor/Vectors.hpp>
#include "Circle.h"
#include "Rectangle.h"
#include "../util/Interval.h"
CollisionModel::~CollisionModel() {
}
/**
* Tests for collision between rectangle and circle.
*
* @return True if a collision occured.
*/
bool
CollisionModel::testCollision(const Circle& circle, const Rectangle& rect,
int elapsed) {
sf::Vector2f halfsize = rect.getSize() / 2.0f;
sf::Vector2f circlePos = circle.getPosition();
sf::Vector2f rectPos = rect.getPosition();
// Only circle movement as rectangles don't move.
sf::Vector2f circleMovement = circle.getSpeed() * (elapsed / 1000.0f);
// We assume that rectangles are always axis aligned.
float overlapNoMovementX = Interval::IntervalFromRadius(circlePos.x, circle.getRadius())
.getOverlap(Interval::IntervalFromRadius(rectPos.x, halfsize.x)).getLength();
float overlapMovementX = Interval::IntervalFromRadius(circlePos.x + circleMovement.x, circle.getRadius())
.getOverlap(Interval::IntervalFromRadius(rectPos.x, halfsize.x)).getLength();
float overlapNoMovementY = Interval::IntervalFromRadius(circlePos.y, circle.getRadius())
.getOverlap(Interval::IntervalFromRadius(rectPos.y, halfsize.y)).getLength();
float overlapMovementY = Interval::IntervalFromRadius(circlePos.y + circleMovement.y, circle.getRadius())
.getOverlap(Interval::IntervalFromRadius(rectPos.y, halfsize.y)).getLength();
bool xyCollisionResult = (((overlapNoMovementX < overlapMovementX) &&
(overlapNoMovementY > 0)) ||
((overlapNoMovementY < overlapMovementY) && (overlapNoMovementX > 0)));
// If circle center is overlapping rectangle on x or y axis, we can take xyCollisionResult.
if (Interval::IntervalFromRadius(rectPos.x, halfsize.x).isInside(circlePos.x) ||
Interval::IntervalFromRadius(rectPos.y, halfsize.y).isInside(circlePos.y))
return xyCollisionResult;
// Test if the circle is colliding with a corner of the rectangle.
else if (xyCollisionResult) {
// This is the same as circle-circle collision.
sf::Vector2f axis = circlePos - rectPos;
// If both objects are at the exact same position, allow any
// movement for unstucking.
if (axis == sf::Vector2f())
return true;
axis = thor::unitVector(axis);
float circlePosProjected = thor::dotProduct(axis, circlePos);
float movementProjected = thor::dotProduct(axis, circleMovement);
float rectPosProjected = thor::dotProduct(axis, rectPos);
// For corner projections, those on the same line with the rect
// center are equal by value, so we only need one on each axis
// and take the maximum.
float rectHalfWidthProjected = std::max(
abs(thor::dotProduct(axis, halfsize)),
abs(thor::dotProduct(axis,
sf::Vector2f(halfsize.x, -halfsize.y))));
// Allow movement if sprites are moving apart.
return Interval::IntervalFromRadius(circlePosProjected, circle.getRadius())
.getOverlap(Interval::IntervalFromRadius(rectPosProjected,
rectHalfWidthProjected))
.getLength() <
Interval::IntervalFromRadius(circlePosProjected + movementProjected, circle.getRadius())
.getOverlap(Interval::IntervalFromRadius(rectPosProjected,
rectHalfWidthProjected))
.getLength();
}
// If there is no collision on x and y axis, there can't be one at all.
else {
return false;
}
}
/**
* Tests for collision between two circles.
*
* @return True if a collision occured.
*/
bool
CollisionModel::testCollision(const Circle& first, const Circle& second,
int elapsed) {
sf::Vector2f axis = first.getPosition() - second.getPosition();
// If both objects are at the exact same position, allow any movement for unstucking.
if (axis == sf::Vector2f())
return true;
axis = thor::unitVector(axis);
float centerA = thor::dotProduct(axis, first.getPosition());
float radiusA = first.getRadius();
float movementA = thor::dotProduct(axis, first.getSpeed() * (elapsed / 1000.0f));
float centerB = thor::dotProduct(axis, second.getPosition());
float radiusB = second.getRadius();
float movementB = thor::dotProduct(axis, second.getSpeed() * (elapsed / 1000.0f));
// Allow movement if sprites are moving apart.
return Interval::IntervalFromRadius(centerA, radiusA).getOverlap(
Interval::IntervalFromRadius(centerB, radiusB)).getLength() <
Interval::IntervalFromRadius(centerA + movementA, radiusA).getOverlap(
Interval::IntervalFromRadius(centerB + movementB, radiusB)).getLength();
}
/**
* Tests for collision between two rectangles. Not implemented as these can't
* occur (rectangles can't move).
*
* @return True if a collision occured.
*/
bool
CollisionModel::testCollision(const Rectangle& first, const Rectangle& second,
int elapsed) {
return false;
}

View file

@ -0,0 +1,29 @@
/*
* CollisionModel.h
*
* Created on: 07.05.2013
* Author: Felix
*/
#ifndef DG_COLLISIONMODEL_H_
#define DG_COLLISIONMODEL_H_
class Circle;
class Rectangle;
/**
* Abstract class providing helper functions to test for collisions between shapes.
*/
class CollisionModel {
public:
virtual ~CollisionModel() = 0;
static bool testCollision(const Circle& circle, const Rectangle& rect,
int elapsed);
static bool testCollision(const Circle& first, const Circle& second,
int elapsed);
static bool testCollision(const Rectangle& first, const Rectangle& second,
int elapsed);
};
#endif /* DG_COLLISIONMODEL_H_ */

View file

@ -0,0 +1,37 @@
/*
* Rectangle.cpp
*
* Created on: 04.05.2013
* Author: Felix
*/
#include "Rectangle.h"
#include "Circle.h"
#include "../util/Yaml.h"
Rectangle::Rectangle(const sf::Vector2f& position, Category category,
unsigned short mask, const Yaml& config,
const sf::Vector2f& direction) :
Sprite(position, category, mask, config.get(YAML_KEY::SIZE, sf::Vector2f()),
config.get(YAML_KEY::TEXTURE, std::string()), direction) {
}
/**
* Returns true if a collision between this and other occured. It does not
* matter which object is this or other.
*/
bool
Rectangle::testCollision(std::shared_ptr<Sprite> other, int elapsed) {
Rectangle* rect = dynamic_cast<Rectangle*>(other.get());
Circle* circle = dynamic_cast<Circle*>(other.get());
if (circle != nullptr)
return CollisionModel::testCollision(*circle, *this, elapsed);
else if (rect != nullptr)
return CollisionModel::testCollision(*rect, *this, elapsed);
else {
assert(false);
return false;
}
}

View file

@ -0,0 +1,29 @@
/*
* Rectangle.h
*
* Created on: 04.05.2013
* Author: Felix
*/
#ifndef DG_RECTANGLE_H_
#define DG_RECTANGLE_H_
#include "CollisionModel.h"
#include "Sprite.h"
class Yaml;
/**
* Shape that uses an axis aligned Rectangle as a collision model.
*/
class Rectangle : public CollisionModel, public Sprite {
public:
explicit Rectangle(const sf::Vector2f& position, Category category,
unsigned short mask, const Yaml& config,
const sf::Vector2f& direction = sf::Vector2f(0, 0));
virtual ~Rectangle() = default;
bool testCollision(std::shared_ptr<Sprite> other, int elapsed);
};
#endif /* DG_RECTANGLE_H_ */

View file

@ -12,78 +12,27 @@
#include "../util/Loader.h" #include "../util/Loader.h"
#include "../util/Log.h" #include "../util/Log.h"
#include "../util/ResourceManager.h" #include "../util/ResourceManager.h"
#include "../util/Yaml.h"
/** Sprite::Sprite(const sf::Vector2f& position, Category category,
* Initializes sprite data. unsigned short mask, const sf::Vector2f& size,
* const std::string& texture, const sf::Vector2f& direction) :
* @param data Container holding construction parameters. mCategory(category),
* @param config Additional construction parameters mMask(mask),
*/
Sprite::Sprite(const Data& data, const Yaml& config) :
mCategory(data.category),
mMask(data.mask),
mDelete(false) { mDelete(false) {
// Init shape mShape.setSize(size);
float radius = config.get(YAML_KEY::RADIUS, 0.0f); mShape.setOrigin(size / 2.0f);
sf::Vector2f size = config.get(YAML_KEY::SIZE, sf::Vector2f()); mShape.setTextureRect(sf::IntRect(sf::Vector2i(), sf::Vector2i(size)));
if (radius != 0.0f) { setPosition(position);
mShape.type = Shape::Type::CIRCLE; setDirection(direction);
mShape.shape = std::unique_ptr<sf::Shape>(new sf::CircleShape(radius));
mShape.shape->setOrigin(radius, radius);
mShape.shape->setTextureRect(sf::IntRect(sf::Vector2i(0, 0),
sf::Vector2i(radius * 2, radius * 2)));
}
else if (size == sf::Vector2f()) {
LOG_E("Failed to read size or radius from " << config.getFilename() <<
", using texture size.");
size = sf::Vector2f(mTexture->getSize());
}
else if (size != sf::Vector2f()) {
mShape.type = Shape::Type::RECTANGLE;
mShape.shape = std::unique_ptr<sf::Shape>(new sf::RectangleShape(size));
mShape.shape->setOrigin(size / 2.0f);
mShape.shape->setTextureRect(sf::IntRect(sf::Vector2i(0, 0), sf::Vector2i(size)));
}
// Init texture
std::string texture = config.get<std::string>(YAML_KEY::TEXTURE, "");
if (texture != "") {
try { try {
mTexture = ResourceManager::i().acquire(Loader::i() mTexture = ResourceManager::i().acquire(Loader::i()
.fromFile<sf::Texture>(texture)); .fromFile<sf::Texture>(texture));
mShape.shape->setTexture(&*mTexture, false); mShape.setTexture(&*mTexture, false);
} }
catch (thor::ResourceLoadingException&) { catch (thor::ResourceLoadingException&) {
LOG_W("Failed to load texture " << texture << ", coloring red."); LOG_W("Failed to load texture " << texture << ", coloring red.");
mShape.shape->setFillColor(sf::Color(255, 0, 0)); mShape.setFillColor(sf::Color(255, 0, 0));
} }
}
else {
LOG_W("Failed to read texture file name from YAML file " <<
config.getFilename() << ", coloring red.");
mShape.shape->setFillColor(sf::Color(255, 0, 0));
}
setPosition(data.position);
setDirection(data.direction);
}
/**
* Used to make this class pure virtual without any pure virtual function.
*/
Sprite::~Sprite() {
}
/**
* Initializes container.
*/
Sprite::Data::Data(const sf::Vector2f& position, Category category,
unsigned short mask, const sf::Vector2f& direction) :
position(position),
direction(direction),
category(category),
mask(mask) {
} }
/** /**
@ -91,7 +40,7 @@ Sprite::Data::Data(const sf::Vector2f& position, Category category,
*/ */
sf::Vector2f sf::Vector2f
Sprite::getPosition() const { Sprite::getPosition() const {
return mShape.shape->getPosition(); return mShape.getPosition();
} }
/** /**
@ -107,7 +56,7 @@ Sprite::getSpeed() const {
*/ */
sf::Vector2f sf::Vector2f
Sprite::getDirection() const { Sprite::getDirection() const {
return thor::rotatedVector(sf::Vector2f(1, 0), mShape.shape->getRotation()); return thor::rotatedVector(sf::Vector2f(1, 0), mShape.getRotation());
} }
/** /**
@ -132,13 +81,13 @@ Sprite::getCategory() const {
*/ */
sf::Vector2f sf::Vector2f
Sprite::getSize() const { Sprite::getSize() const {
sf::FloatRect bounds = mShape.shape->getLocalBounds(); sf::FloatRect bounds = mShape.getLocalBounds();
return sf::Vector2f(bounds.width, bounds.height); return sf::Vector2f(bounds.width, bounds.height);
} }
void void
Sprite::draw(sf::RenderTarget& target, sf::RenderStates states) const { Sprite::draw(sf::RenderTarget& target, sf::RenderStates states) const {
target.draw(*mShape.shape, states); target.draw(mShape, states);
} }
/** /**
@ -151,7 +100,7 @@ Sprite::collisionEnabled(Category category) const {
bool bool
Sprite::isInside(const sf::FloatRect& rect) const { Sprite::isInside(const sf::FloatRect& rect) const {
return rect.intersects(mShape.shape->getGlobalBounds()); return rect.intersects(mShape.getGlobalBounds());
} }
/** /**
* Called when a collision with another Sprite occured. Override this method * Called when a collision with another Sprite occured. Override this method
@ -187,7 +136,7 @@ Sprite::setSpeed(sf::Vector2f direction, float speed) {
void void
Sprite::setDirection(const sf::Vector2f& direction) { Sprite::setDirection(const sf::Vector2f& direction) {
if (direction != sf::Vector2f()) if (direction != sf::Vector2f())
mShape.shape->setRotation(thor::polarAngle(direction) + 90); mShape.setRotation(thor::polarAngle(direction) + 90);
} }
/** /**
@ -195,18 +144,5 @@ Sprite::setDirection(const sf::Vector2f& direction) {
*/ */
void void
Sprite::setPosition(const sf::Vector2f& position) { Sprite::setPosition(const sf::Vector2f& position) {
mShape.shape->setPosition(position); mShape.setPosition(position);
}
/**
* Returns the radius of this sprite. Will fail if this is not a circle.
*
* @return The radius of this sprite.
*/
float
Sprite::getRadius() const {
std::shared_ptr<sf::CircleShape> circleShape =
std::dynamic_pointer_cast<sf::CircleShape>(mShape.shape);
assert(circleShape);
return circleShape->getRadius();
} }

View file

@ -12,8 +12,6 @@
#include <SFML/Graphics.hpp> #include <SFML/Graphics.hpp>
class Yaml;
/** /**
* An sprite that is rendered in the world. * An sprite that is rendered in the world.
*/ */
@ -30,21 +28,6 @@ public:
CATEGORY_ACTOR = 1 << 4 CATEGORY_ACTOR = 1 << 4
}; };
/**
* Container that carries all data required to construct an object of
* this class.
*/
class Data {
public:
explicit Data(const sf::Vector2f& position,
Category category, unsigned short mask,
const sf::Vector2f& direction = sf::Vector2f(0, 0));
const sf::Vector2f& position;
const sf::Vector2f& direction;
Category category;
unsigned short mask;
};
/** /**
* Common collision masking values. * Common collision masking values.
*/ */
@ -55,8 +38,10 @@ public:
// Public functions. // Public functions.
public: public:
explicit Sprite(const Data& data, const Yaml& config); explicit Sprite(const sf::Vector2f& position, Category category,
virtual ~Sprite() = 0; unsigned short mask, const sf::Vector2f& size,
const std::string& texture, const sf::Vector2f& direction);
virtual ~Sprite() = default;
sf::Vector2f getPosition() const; sf::Vector2f getPosition() const;
sf::Vector2f getSpeed() const; sf::Vector2f getSpeed() const;
@ -68,6 +53,7 @@ public:
bool collisionEnabled(Category category) const; bool collisionEnabled(Category category) const;
bool isInside(const sf::FloatRect& rect) const; bool isInside(const sf::FloatRect& rect) const;
virtual bool testCollision(std::shared_ptr<Sprite> other, int elapsed) = 0;
virtual void onCollide(std::shared_ptr<Sprite> other); virtual void onCollide(std::shared_ptr<Sprite> other);
protected: protected:
@ -75,24 +61,11 @@ protected:
void setSpeed(sf::Vector2f direction, float speed); void setSpeed(sf::Vector2f direction, float speed);
void setDirection(const sf::Vector2f& direction); void setDirection(const sf::Vector2f& direction);
void setPosition(const sf::Vector2f& position); void setPosition(const sf::Vector2f& position);
float getRadius() const;
private:
class Shape {
public:
enum class Type {
CIRCLE,
RECTANGLE
};
Type type;
std::shared_ptr<sf::Shape> shape;
};
private: private:
friend class World; friend class World;
Shape mShape; sf::RectangleShape mShape;
std::shared_ptr<sf::Texture> mTexture; std::shared_ptr<sf::Texture> mTexture;
sf::Vector2f mSpeed; sf::Vector2f mSpeed;
Category mCategory; Category mCategory;

View file

@ -22,12 +22,12 @@
*/ */
Bullet::Bullet(const sf::Vector2f& position, Sprite& shooter, Bullet::Bullet(const sf::Vector2f& position, Sprite& shooter,
sf::Vector2f direction, const Yaml& config) : sf::Vector2f direction, const Yaml& config) :
Particle(config, Data(position, CATEGORY_PARTICLE, ~CATEGORY_PARTICLE, Particle(position, CATEGORY_PARTICLE, ~CATEGORY_PARTICLE,
thor::rotatedVector(direction, -90.0f))), config, thor::rotatedVector(direction, -90.0f)),
mShooter(shooter), mShooter(shooter),
mDamage(config.get(YAML_KEY::DAMAGE, YAML_DEFAULT::DAMAGE)), mDamage(config.get(YAML_KEY::DAMAGE, YAML_DEFAULT::DAMAGE)),
mSpeed(config.get(YAML_KEY::SPEED, YAML_DEFAULT::SPEED)) { mSpeed(config.get(YAML_KEY::SPEED, YAML_DEFAULT::SPEED)) {
setSpeed(direction, mSpeed); setSpeed(thor::rotatedVector(direction, -90.0f), mSpeed);
} }
/** /**

View file

@ -7,8 +7,10 @@
#include "Particle.h" #include "Particle.h"
Particle::Particle(const Yaml& config, const Data& data) : Particle::Particle(const sf::Vector2f& position, Category category,
Sprite(data, config) { unsigned short mask, const Yaml& config,
const sf::Vector2f& direction) :
Circle(position, category, mask, config, direction) {
} }
Particle::~Particle() { Particle::~Particle() {

View file

@ -8,16 +8,18 @@
#ifndef DG_PARTICLE_H_ #ifndef DG_PARTICLE_H_
#define DG_PARTICLE_H_ #define DG_PARTICLE_H_
#include "../abstract/Sprite.h" #include "../abstract/Circle.h"
class Yaml; class Yaml;
/** /**
* Prototype for a particle. * Prototype for a particle.
*/ */
class Particle : public Sprite { class Particle : public Circle {
public: public:
explicit Particle(const Yaml& config, const Data& data); explicit Particle(const sf::Vector2f& position, Category category,
unsigned short mask, const Yaml& config,
const sf::Vector2f& direction);
virtual ~Particle(); virtual ~Particle();
}; };

View file

@ -12,6 +12,6 @@
const std::string Corpse::CONFIG = "corpse.yaml"; const std::string Corpse::CONFIG = "corpse.yaml";
Corpse::Corpse(const sf::Vector2f& position) : Corpse::Corpse(const sf::Vector2f& position) :
Sprite(Data(position, CATEGORY_NONSOLID, MASK_NONE), Yaml(CONFIG)) { Circle(position, CATEGORY_NONSOLID, MASK_NONE, Yaml(CONFIG)) {
} }

View file

@ -8,9 +8,9 @@
#ifndef DG_CORPSE_H_ #ifndef DG_CORPSE_H_
#define DG_CORPSE_H_ #define DG_CORPSE_H_
#include "../abstract/Sprite.h" #include "../abstract/Circle.h"
class Corpse : public Sprite { class Corpse : public Circle {
public: public:
explicit Corpse(const sf::Vector2f& position); explicit Corpse(const sf::Vector2f& position);

View file

@ -15,8 +15,8 @@ const std::string Enemy::CONFIG = "enemy.yaml";
Enemy::Enemy(World& world, Pathfinder& pathfinder, Enemy::Enemy(World& world, Pathfinder& pathfinder,
const sf::Vector2f& position) : const sf::Vector2f& position) :
Character(world, pathfinder, Data(position, CATEGORY_ACTOR, MASK_ALL), Character(position, CATEGORY_ACTOR, MASK_ALL, Yaml(CONFIG), world,
Yaml(CONFIG)) { pathfinder) {
} }
void void

View file

@ -18,8 +18,8 @@ const std::string Player::CONFIG = "player.yaml";
*/ */
Player::Player(World& world, Pathfinder& pathfinder, Player::Player(World& world, Pathfinder& pathfinder,
const sf::Vector2f& position) : const sf::Vector2f& position) :
Character(world, pathfinder, Character(position, CATEGORY_ACTOR, MASK_ALL, Yaml(CONFIG), world,
Data(position, CATEGORY_ACTOR, MASK_ALL), Yaml(CONFIG)), pathfinder),
mDirection(0) { mDirection(0) {
} }

View file

@ -24,8 +24,8 @@ const sf::Vector2i Tile::TILE_SIZE = sf::Vector2i(75, 75);
* @param world Box2D world object. * @param world Box2D world object.
*/ */
Tile::Tile(Type type, int x, int y) : Tile::Tile(Type type, int x, int y) :
Sprite(Data(sf::Vector2f(x * TILE_SIZE.x, y * TILE_SIZE.y), Rectangle(sf::Vector2f(x * TILE_SIZE.x, y * TILE_SIZE.y),
CATEGORY_WORLD, (isSolid(type)) ? 0xffff : 0), CATEGORY_WORLD, (isSolid(type)) ? 0xffff : 0,
Yaml(getConfig(type))), Yaml(getConfig(type))),
mType(type) { mType(type) {
} }

View file

@ -8,12 +8,12 @@
#ifndef DG_TILE_H_ #ifndef DG_TILE_H_
#define DG_TILE_H_ #define DG_TILE_H_
#include "../abstract/Sprite.h" #include "../abstract/Rectangle.h"
/** /**
* Holds information about a single tile. * Holds information about a single tile.
*/ */
class Tile : public Sprite { class Tile : public Rectangle {
public: public:
enum class Type : char { enum class Type : char {
FLOOR, FLOOR,