Renamed Body to Corpse, Physical to Body.

This commit is contained in:
Felix Ableitner 2012-12-20 10:31:32 +01:00
parent 1e0d005a1b
commit 88db5d5324
21 changed files with 83 additions and 83 deletions

View file

@ -251,8 +251,8 @@ Game::getFps() {
*/ */
void void
Game::BeginContact(b2Contact* contact) { Game::BeginContact(b2Contact* contact) {
Physical& first = *static_cast<Physical*>(contact->GetFixtureA()->GetBody()->GetUserData()); Body& first = *static_cast<Body*>(contact->GetFixtureA()->GetBody()->GetUserData());
Physical& second = *static_cast<Physical*>(contact->GetFixtureB()->GetBody()->GetUserData()); Body& second = *static_cast<Body*>(contact->GetFixtureB()->GetBody()->GetUserData());
if (!first.doesCollide(second) || !second.doesCollide(first)) { if (!first.doesCollide(second) || !second.doesCollide(first)) {
contact->SetEnabled(false); contact->SetEnabled(false);

View file

@ -1,24 +1,24 @@
/* /*
* Physical.cpp * Body.cpp
* *
* Created on: 11.08.2012 * Created on: 11.08.2012
* Author: Felix * Author: Felix
*/ */
#include "Physical.h" #include "Body.h"
#include <math.h> #include <math.h>
#include <Thor/Vectors.hpp> #include <Thor/Vectors.hpp>
const String Physical::KEY_SIZE = "size"; const String Body::KEY_SIZE = "size";
/** /**
* Initializes Box2D body. * Initializes Box2D body.
* *
* @param data Data needed for construction. * @param data Data needed for construction.
*/ */
Physical::Physical(const PhysicalData& data, const Yaml& config, const Vector2i& pSize) : Body::Body(const PhysicalData& data, const Yaml& config, const Vector2i& pSize) :
mDelete(false) { mDelete(false) {
Vector2i size = (pSize == Vector2i()) Vector2i size = (pSize == Vector2i())
? config.get<Vector2i>(KEY_SIZE) ? config.get<Vector2i>(KEY_SIZE)
@ -66,7 +66,7 @@ Physical::Physical(const PhysicalData& data, const Yaml& config, const Vector2i&
/** /**
* Removes body from world. * Removes body from world.
*/ */
Physical::~Physical() { Body::~Body() {
mBody->GetWorld()->DestroyBody(mBody); mBody->GetWorld()->DestroyBody(mBody);
} }
@ -75,7 +75,7 @@ Physical::~Physical() {
* *
* @link Physical::PhysicalData * @link Physical::PhysicalData
*/ */
Physical::PhysicalData::PhysicalData( const Vector2f& position, b2World& world, uint16 category, Body::PhysicalData::PhysicalData( const Vector2f& position, b2World& world, uint16 category,
uint16 maskExclude, bool moving, bool bullet, bool circle) : uint16 maskExclude, bool moving, bool bullet, bool circle) :
position(position), position(position),
world(world), world(world),
@ -90,7 +90,7 @@ Physical::PhysicalData::PhysicalData( const Vector2f& position, b2World& world,
* Returns the position of the sprite (center). * Returns the position of the sprite (center).
*/ */
Vector2f Vector2f
Physical::getPosition() const { Body::getPosition() const {
return vector(mBody->GetPosition()); return vector(mBody->GetPosition());
} }
@ -98,7 +98,7 @@ Physical::getPosition() const {
* Returns the movement speed of the body. * Returns the movement speed of the body.
*/ */
Vector2f Vector2f
Physical::getSpeed() const { Body::getSpeed() const {
return vector(mBody->GetLinearVelocity()); return vector(mBody->GetLinearVelocity());
} }
@ -106,7 +106,7 @@ Physical::getSpeed() const {
* Returns the rotation of the body (converted to an SFML angle). * Returns the rotation of the body (converted to an SFML angle).
*/ */
float float
Physical::getAngle() const { Body::getAngle() const {
return thor::toDegree(mBody->GetAngle()); return thor::toDegree(mBody->GetAngle());
} }
@ -114,15 +114,15 @@ Physical::getAngle() const {
* Returns true if this object should be deleted. * Returns true if this object should be deleted.
*/ */
bool bool
Physical::getDelete() const { Body::getDelete() const {
return mDelete; return mDelete;
} }
/** /**
* Returns the Physical::Category of this object. * Returns the Physical::Category of this object.
*/ */
Physical::Category Body::Category
Physical::getCategory() const { Body::getCategory() const {
return (Category) mBody->GetFixtureList()->GetFilterData().categoryBits; return (Category) mBody->GetFixtureList()->GetFilterData().categoryBits;
} }
@ -130,7 +130,7 @@ Physical::getCategory() const {
* Returns the size of the body as a vector. * Returns the size of the body as a vector.
*/ */
Vector2f Vector2f
Physical::getSize() const { Body::getSize() const {
b2AABB aabb(mBody->GetFixtureList()->GetAABB(0)); b2AABB aabb(mBody->GetFixtureList()->GetAABB(0));
return vector(aabb.upperBound - aabb.lowerBound); return vector(aabb.upperBound - aabb.lowerBound);
} }
@ -139,7 +139,7 @@ Physical::getSize() const {
* Returns true if collisions are enabled for the body. * Returns true if collisions are enabled for the body.
*/ */
bool bool
Physical::isSolid() const { Body::isSolid() const {
return mBody->GetFixtureList()->GetFilterData().maskBits != 0; return mBody->GetFixtureList()->GetFilterData().maskBits != 0;
} }
@ -147,7 +147,7 @@ Physical::isSolid() const {
* Returns true if the body is able to move. * Returns true if the body is able to move.
*/ */
bool bool
Physical::isMovable() const { Body::isMovable() const {
return mBody->GetType() != b2_staticBody; return mBody->GetType() != b2_staticBody;
} }
@ -159,7 +159,7 @@ Physical::isMovable() const {
* @return True if the objects should collide. * @return True if the objects should collide.
*/ */
bool bool
Physical::doesCollide(Physical& other) { Body::doesCollide(Body& other) {
return true; return true;
} }
@ -171,14 +171,14 @@ Physical::doesCollide(Physical& other) {
* @param category The Category of the other object (as passed in constructor). * @param category The Category of the other object (as passed in constructor).
*/ */
void void
Physical::onCollide(Physical& other, uint16 type) { Body::onCollide(Body& other, uint16 type) {
} }
/** /**
* Set to true to mark this object for deletion from the world. * Set to true to mark this object for deletion from the world.
*/ */
void void
Physical::setDelete(bool value) { Body::setDelete(bool value) {
mDelete = value; mDelete = value;
} }
@ -189,7 +189,7 @@ Physical::setDelete(bool value) {
* @param speed The value of the movement speed to be used. * @param speed The value of the movement speed to be used.
*/ */
void void
Physical::setSpeed(Vector2f direction, float speed) { Body::setSpeed(Vector2f direction, float speed) {
if (direction != Vector2f()) { if (direction != Vector2f()) {
direction = thor::unitVector<float>(direction); direction = thor::unitVector<float>(direction);
} }
@ -201,6 +201,6 @@ Physical::setSpeed(Vector2f direction, float speed) {
* Sets the angle of the body based on the direction of a vector. * Sets the angle of the body based on the direction of a vector.
*/ */
void void
Physical::setAngle(float angle) { Body::setAngle(float angle) {
mBody->SetTransform(mBody->GetPosition(), thor::toRadian(angle)); mBody->SetTransform(mBody->GetPosition(), thor::toRadian(angle));
} }

View file

@ -21,7 +21,7 @@ class Yaml;
* *
* @warning May only handle bodies with one fixture. * @warning May only handle bodies with one fixture.
*/ */
class Physical { class Body {
// Public types. // Public types.
public: public:
/** /**
@ -73,8 +73,8 @@ public:
// Public functions. // Public functions.
public: public:
Physical(const PhysicalData& data, const Yaml& config, const Vector2i& pSize = Vector2i()); Body(const PhysicalData& data, const Yaml& config, const Vector2i& pSize = Vector2i());
virtual ~Physical() = 0; virtual ~Body() = 0;
Vector2f getPosition() const; Vector2f getPosition() const;
Vector2f getSpeed() const; Vector2f getSpeed() const;
@ -86,8 +86,8 @@ public:
bool isSolid() const; bool isSolid() const;
bool isMovable() const; bool isMovable() const;
virtual bool doesCollide(Physical& other); virtual bool doesCollide(Body& other);
virtual void onCollide(Physical& other, uint16 category); virtual void onCollide(Body& other, uint16 category);
// Public variables. // Public variables.
public: public:

View file

@ -12,7 +12,7 @@
#include <Thor/Vectors.hpp> #include <Thor/Vectors.hpp>
#include "../sprites/Body.h" #include "../sprites/Corpse.h"
#include "../util/Log.h" #include "../util/Log.h"
const String Character::KEY_HEALTH = "health"; const String Character::KEY_HEALTH = "health";
@ -44,7 +44,7 @@ Character::~Character() {
assert(it != mCharacterInstances.end()); assert(it != mCharacterInstances.end());
mCharacterInstances.erase(it); mCharacterInstances.erase(it);
mInstances.collection.insert(std::shared_ptr<Sprite>(new Body(mInstances.world, mInstances.collection.insert(std::shared_ptr<Sprite>(new Corpse(mInstances.world,
getPosition(), Yaml("body.yaml")))); getPosition(), Yaml("body.yaml"))));
} }

View file

@ -18,7 +18,7 @@ const String Sprite::KEY_TEXTURE = "texture";
* @param texturePath Relative path to the texture file in the resource folder. * @param texturePath Relative path to the texture file in the resource folder.
*/ */
Sprite::Sprite(const Yaml& config, const PhysicalData& data, const Vector2i& size) : Sprite::Sprite(const Yaml& config, const PhysicalData& data, const Vector2i& size) :
Physical(data, config, size), Body(data, config, size),
mTexture(ResourceManager::i().acquire(Loader::i() mTexture(ResourceManager::i().acquire(Loader::i()
.fromFile<sf::Texture>(config.get<String>(KEY_TEXTURE)))), .fromFile<sf::Texture>(config.get<String>(KEY_TEXTURE)))),
mSize(Vector2i(getSize())) { mSize(Vector2i(getSize())) {

View file

@ -12,12 +12,12 @@
#include <Thor/Resources.hpp> #include <Thor/Resources.hpp>
#include "Physical.h" #include "Body.h"
#include "../types/String.h" #include "../types/String.h"
#include "../types/Vector.h" #include "../types/Vector.h"
#include "../util/Yaml.h" #include "../util/Yaml.h"
class Physical; class Body;
class Yaml; class Yaml;
/** /**
@ -25,7 +25,7 @@ class Yaml;
* *
* Handles drawing to world. * Handles drawing to world.
*/ */
class Sprite : public sf::Drawable, public Physical { class Sprite : public sf::Drawable, public Body {
// Public functions. // Public functions.
public: public:
Sprite(const Yaml& config, const PhysicalData& data, const Vector2i& size = Vector2i()); Sprite(const Yaml& config, const PhysicalData& data, const Vector2i& size = Vector2i());

View file

@ -21,7 +21,7 @@ const String Bullet::KEY_SPEED = "speed";
* @param world Box2d world. * @param world Box2d world.
* @param texture Texture to display for bullet. * @param texture Texture to display for bullet.
*/ */
Bullet::Bullet(const Vector2f& position, b2World& world, Physical& shooter, float direction, Bullet::Bullet(const Vector2f& position, b2World& world, Body& shooter, float direction,
const Yaml& config) : const Yaml& config) :
Particle(config, PhysicalData(position, world, CATEGORY_PARTICLE, CATEGORY_PARTICLE, Particle(config, PhysicalData(position, world, CATEGORY_PARTICLE, CATEGORY_PARTICLE,
true, true, true)), true, true, true)),
@ -36,7 +36,7 @@ Bullet::Bullet(const Vector2f& position, b2World& world, Physical& shooter, floa
* @copydoc Physical::onCollide * @copydoc Physical::onCollide
*/ */
void void
Bullet::onCollide(Physical& other, uint16 type) { Bullet::onCollide(Body& other, uint16 type) {
// Make sure we do not damage twice. // Make sure we do not damage twice.
if (!getDelete()) { if (!getDelete()) {
// Call onShot on other, with damage as param. // Call onShot on other, with damage as param.
@ -49,6 +49,6 @@ Bullet::onCollide(Physical& other, uint16 type) {
} }
bool bool
Bullet::doesCollide(Physical& other) { Bullet::doesCollide(Body& other) {
return &other != &mShooter; return &other != &mShooter;
} }

View file

@ -21,18 +21,18 @@ class Yaml;
class Bullet : public Particle { class Bullet : public Particle {
// Public functions. // Public functions.
public: public:
Bullet(const Vector2f& position, b2World& world, Physical& shooter, float direction, Bullet(const Vector2f& position, b2World& world, Body& shooter, float direction,
const Yaml& config); const Yaml& config);
void onCollide(Physical& other, uint16 category); void onCollide(Body& other, uint16 category);
bool doesCollide(Physical& other); bool doesCollide(Body& other);
// Private variables. // Private variables.
private: private:
static const String KEY_DAMAGE; static const String KEY_DAMAGE;
static const String KEY_SPEED; static const String KEY_SPEED;
Physical& mShooter; Body& mShooter;
const int mDamage; const int mDamage;
const float mSpeed; const float mSpeed;
}; };

View file

@ -17,14 +17,14 @@
const String Weapon::KEY_BULLET = "bullet"; const String Weapon::KEY_BULLET = "bullet";
const String Weapon::KEY_INTERVAL = "interval"; const String Weapon::KEY_INTERVAL = "interval";
Weapon::Weapon(const Instances& instances, Physical& holder, const Yaml& config) : Weapon::Weapon(const Instances& instances, Body& holder, const Yaml& config) :
Emitter(instances.collection), Emitter(instances.collection),
mHolder(holder), mHolder(holder),
mWorld(instances.world), mWorld(instances.world),
mBullet(config.get<String>(KEY_BULLET)), mBullet(config.get<String>(KEY_BULLET)),
mTimer(sf::milliseconds(config.get<int>(KEY_INTERVAL))) { mTimer(sf::milliseconds(config.get<int>(KEY_INTERVAL))) {
Yaml bullet(mBullet); Yaml bullet(mBullet);
Vector2i bulletSize = bullet.get<Vector2i>(Physical::KEY_SIZE); Vector2i bulletSize = bullet.get<Vector2i>(Body::KEY_SIZE);
mOffset = Vector2f(0, std::max(mHolder.getSize().x, mHolder.getSize().y) / 2 + mOffset = Vector2f(0, std::max(mHolder.getSize().x, mHolder.getSize().y) / 2 +
b2_linearSlop + b2_linearSlop +
std::max(bulletSize.x, bulletSize.y) / 2); std::max(bulletSize.x, bulletSize.y) / 2);

View file

@ -10,7 +10,7 @@
#include <Thor/Particles.hpp> #include <Thor/Particles.hpp>
#include "../abstract/Physical.h" #include "../abstract/Body.h"
#include "../particle/Emitter.h" #include "../particle/Emitter.h"
#include "../types/Instances.h" #include "../types/Instances.h"
#include "../util/Timer.h" #include "../util/Timer.h"
@ -18,7 +18,7 @@
class Emitter; class Emitter;
class Instances; class Instances;
class Physical; class Body;
class Timer; class Timer;
class Yaml; class Yaml;
@ -30,7 +30,7 @@ class Yaml;
class Weapon : public Emitter { class Weapon : public Emitter {
// Public functions. // Public functions.
public: public:
Weapon(const Instances& instances, Physical& holder, const Yaml& config); Weapon(const Instances& instances, Body& holder, const Yaml& config);
void fire(); void fire();
@ -43,7 +43,7 @@ private:
static const String KEY_BULLET; static const String KEY_BULLET;
static const String KEY_INTERVAL; static const String KEY_INTERVAL;
Physical& mHolder; Body& mHolder;
b2World& mWorld; b2World& mWorld;
Vector2f mOffset; //< Offset to the point where bullets are inserted (from holder center). Vector2f mOffset; //< Offset to the point where bullets are inserted (from holder center).

View file

@ -8,11 +8,11 @@
#ifndef DG_EMITTER_H_ #ifndef DG_EMITTER_H_
#define DG_EMITTER_H_ #define DG_EMITTER_H_
#include "../abstract/Physical.h" #include "../abstract/Body.h"
#include "../util/Collection.h" #include "../util/Collection.h"
#include "Particle.h" #include "Particle.h"
class Physical; class Body;
class Collection; class Collection;
class Particle; class Particle;

View file

@ -1,23 +0,0 @@
/*
* Body.h
*
* Created on: 13.09.2012
* Author: Felix
*/
#ifndef DG_BODY_H_
#define DG_BODY_H_
#include "../abstract/Sprite.h"
#include "../util/Yaml.h"
class Sprite;
class Yaml;
class Body : public Sprite {
// Public functions.
public:
Body(b2World& world, const Vector2f& position, const Yaml& config);
};
#endif /* DG_BODY_H_ */

View file

@ -1,13 +1,13 @@
/* /*
* Body.cpp * Corpse.cpp
* *
* Created on: 13.09.2012 * Created on: 13.09.2012
* Author: Felix * Author: Felix
*/ */
#include "Body.h" #include "Corpse.h"
Body::Body(b2World& world, const Vector2f& position, const Yaml& config) : Corpse::Corpse(b2World& world, const Vector2f& position, const Yaml& config) :
Sprite(config, PhysicalData(position, world, CATEGORY_NONSOLID, MASK_NONE, false)) { Sprite(config, PhysicalData(position, world, CATEGORY_NONSOLID, MASK_NONE, false)) {
} }

23
source/sprites/Corpse.h Normal file
View file

@ -0,0 +1,23 @@
/*
* Corpse.h
*
* Created on: 13.09.2012
* Author: Felix
*/
#ifndef DG_CORPSE_H_
#define DG_CORPSE_H_
#include "../abstract/Sprite.h"
#include "../util/Yaml.h"
class Sprite;
class Yaml;
class Corpse : public Sprite {
// Public functions.
public:
Corpse(b2World& world, const Vector2f& position, const Yaml& config);
};
#endif /* DG_CORPSE_H_ */

View file

@ -7,7 +7,7 @@
#include "Enemy.h" #include "Enemy.h"
#include "Body.h" #include "Corpse.h"
Enemy::Enemy(const Instances& instances, const Vector2f& position, const Yaml& config) : Enemy::Enemy(const Instances& instances, const Vector2f& position, const Yaml& config) :
Character(instances, "enemy.png", PhysicalData(position, instances.world, Character(instances, "enemy.png", PhysicalData(position, instances.world,

View file

@ -99,7 +99,7 @@ Player::onThink(float elapsedTime) {
* Stop movement if we collide with anything except bullets. * Stop movement if we collide with anything except bullets.
*/ */
void void
Player::onCollide(Physical& other, uint16 category) { Player::onCollide(Body& other, uint16 category) {
if (category != CATEGORY_PARTICLE) { if (category != CATEGORY_PARTICLE) {
setDestination(getPosition()); setDestination(getPosition());
} }

View file

@ -51,7 +51,7 @@ public:
// Private functions. // Private functions.
private: private:
void onCollide(Physical& other, uint16 category); void onCollide(Body& other, uint16 category);
void onThink(float elapsedTime); void onThink(float elapsedTime);
// Private variables. // Private variables.

View file

@ -15,7 +15,7 @@
*/ */
void void
Collection::insert(std::shared_ptr<Sprite> drawable) { Collection::insert(std::shared_ptr<Sprite> drawable) {
Physical::Category cat = drawable->getCategory(); Body::Category cat = drawable->getCategory();
auto item = std::find(mDrawables[cat].begin(), mDrawables[cat].end(), drawable); auto item = std::find(mDrawables[cat].begin(), mDrawables[cat].end(), drawable);
if (item == mDrawables[cat].end()) { if (item == mDrawables[cat].end()) {
mDrawables[cat].push_back(drawable); mDrawables[cat].push_back(drawable);

View file

@ -36,7 +36,7 @@ private:
// Private variables. // Private variables.
private: private:
std::map<Physical::Category, std::vector<std::shared_ptr<Sprite> > > mDrawables; std::map<Body::Category, std::vector<std::shared_ptr<Sprite> > > mDrawables;
}; };
#endif /* DG_COLLECTION_H_ */ #endif /* DG_COLLECTION_H_ */

View file

@ -305,7 +305,7 @@ Pathfinder::Pathfinder(b2World& world) :
* Sometimes moves into wall and instantly out again. * Sometimes moves into wall and instantly out again.
*/ */
std::vector<Vector2f> std::vector<Vector2f>
Pathfinder::getPath(Physical& physical, const Vector2f& destination) { Pathfinder::getPath(Body& physical, const Vector2f& destination) {
Vertex start(vertex(physical.getPosition())); Vertex start(vertex(physical.getPosition()));
Vertex goal(vertex(destination)); Vertex goal(vertex(destination));
@ -389,7 +389,7 @@ FilterVertex::operator()(Vertex const& vertex) const {
bool bool
FilterVertex::Callback::ReportFixture(b2Fixture* fixture) { FilterVertex::Callback::ReportFixture(b2Fixture* fixture) {
Physical& physical = *static_cast<Physical*>(fixture->GetBody()->GetUserData()); Body& physical = *static_cast<Body*>(fixture->GetBody()->GetUserData());
if (!physical.isMovable() && physical.isSolid()) { if (!physical.isMovable() && physical.isSolid()) {
empty = false; empty = false;
return true; return true;

View file

@ -10,7 +10,7 @@
#include <Box2D/Box2D.h> #include <Box2D/Box2D.h>
#include "../abstract/Physical.h" #include "../abstract/Body.h"
#include "../types/Vector.h" #include "../types/Vector.h"
/** /**
@ -21,7 +21,7 @@ class Pathfinder {
public: public:
Pathfinder(b2World& world); Pathfinder(b2World& world);
std::vector<Vector2f> getPath(Physical& physical, const Vector2f& destination); std::vector<Vector2f> getPath(Body& physical, const Vector2f& destination);
// Private variables. // Private variables.
private: private: