Changed Physical size to be passed via Yaml.

This commit is contained in:
Felix Ableitner 2012-10-13 12:22:18 +02:00
parent daaf8c8d9a
commit 9b61304d1e
10 changed files with 56 additions and 33 deletions

View file

@ -34,7 +34,7 @@ TileManager::TileManager(b2World& world) :
*/
TileManager::Tile::Tile(Type type, const TilePosition& position, b2World& world) :
Sprite(getTexture(type), PhysicalData(Vector2f(position.x * TILE_SIZE.x, position.y * TILE_SIZE.y),
TILE_SIZE, world, CATEGORY_WORLD, (type == Type::FLOOR) ? MASK_NONE : MASK_ALL, false)),
world, CATEGORY_WORLD, (type == Type::FLOOR) ? MASK_NONE : MASK_ALL, false)),
mType(type) {
}

View file

@ -11,14 +11,17 @@
#include <Thor/Vectors.hpp>
const String Physical::KEY_SIZE = "size";
/**
* Initializes Box2D body.
*
* @param data Data needed for construction.
*/
Physical::Physical(const PhysicalData& data) :
Physical::Physical(const PhysicalData& data, const Yaml& config) :
mDelete(false) {
assert(data.size != Vector2i());
Vector2i size = config.get<Vector2i>(KEY_SIZE);
assert(size != Vector2i());
b2BodyDef bodyDef;
bodyDef.type = (data.moving)
@ -34,14 +37,14 @@ Physical::Physical(const PhysicalData& data) :
b2Shape* shape;
if (data.circle) {
assert(data.size.x == data.size.y);
assert(size.x == size.y);
shape = new b2CircleShape;
shape->m_radius = pixelToMeter(data.size.x) / 2;
shape->m_radius = pixelToMeter(size.x) / 2;
}
else {
b2PolygonShape* box = new b2PolygonShape;
box->SetAsBox(pixelToMeter(data.size.x) / 2,
pixelToMeter(data.size.y) / 2);
box->SetAsBox(pixelToMeter(size.x) / 2,
pixelToMeter(size.y) / 2);
shape = dynamic_cast<b2Shape*>(box);
}
@ -70,10 +73,9 @@ Physical::~Physical() {
*
* @link Physical::PhysicalData
*/
Physical::PhysicalData::PhysicalData( const Vector2f& position, const Vector2i& size,
b2World& world, uint16 category, uint16 maskExclude, bool moving, bool bullet, bool circle) :
Physical::PhysicalData::PhysicalData( const Vector2f& position, b2World& world, uint16 category,
uint16 maskExclude, bool moving, bool bullet, bool circle) :
position(position),
size(size),
world(world),
category(category),
maskExclude(maskExclude),

View file

@ -10,7 +10,11 @@
#include <Box2D/Box2D.h>
#include "../util/String.h"
#include "../util/Vector.h"
#include "../util/Yaml.h"
class Yaml;
/**
* An object with physical properties.
@ -26,13 +30,10 @@ public:
class PhysicalData {
public:
PhysicalData() = default;
PhysicalData(const Vector2f& position, const Vector2i& size, b2World& world,
uint16 category, uint16 maskExclude, bool moving, bool bullet = false,
bool circle = false);
PhysicalData(const Vector2f& position, b2World& world, uint16 category,
uint16 maskExclude, bool moving, bool bullet = false, bool circle = false);
/// World position of the body in pixel coordinates.
const Vector2f& position;
/// Pixel size of the body if it is a box.
Vector2i size;
/// Box2D world object.
b2World& world;
/// The category for collision filtering. Only one may be set. @link Physical::Category
@ -72,7 +73,7 @@ public:
// Public functions.
public:
Physical(const PhysicalData& data);
Physical(const PhysicalData& data, const Yaml& config);
virtual ~Physical() = 0;
Vector2f getPosition() const;
@ -97,6 +98,8 @@ protected:
// Private variables.
private:
static const String KEY_SIZE;
b2Body* mBody;
bool mDelete;
};

View file

@ -18,10 +18,10 @@ const String Sprite::KEY_TEXTURE = "texture";
* @param texturePath Relative path to the texture file in the resource folder.
*/
Sprite::Sprite(const Yaml& config, const PhysicalData& data) :
Physical(data),
Physical(data, config),
mTexture(ResourceManager::i().acquire(Loader::i()
.fromFile<sf::Texture>(config.get<String>(KEY_TEXTURE)))),
mSize(data.size) {
mSize(Vector2i(getSize())) {
}
/**
@ -30,9 +30,9 @@ Sprite::Sprite(const Yaml& config, const PhysicalData& data) :
* @param texture Pointer to the texture to be used (must already be loaded).
*/
Sprite::Sprite(const std::shared_ptr<sf::Texture>& texture, const PhysicalData& data) :
Physical(data),
Physical(data, Yaml("tile.yaml")),
mTexture(texture),
mSize(data.size) {
mSize(Vector2i(getSize())) {
}
/**

View file

@ -25,7 +25,7 @@ const String Bullet::KEY_SPEED = "speed";
Bullet::Bullet(const Vector2f& position, b2World& world, Physical& shooter, float direction,
const Yaml& config) :
Particle(ResourceManager::i().acquire(Loader::i().fromFile<sf::Texture>("bullet.png")),
PhysicalData(position, SIZE, world, CATEGORY_PARTICLE, CATEGORY_PARTICLE,
PhysicalData(position, world, CATEGORY_PARTICLE, CATEGORY_PARTICLE,
true, true, true), config),
mShooter(shooter),
mDamage(config.get<int>(KEY_DAMAGE)),

View file

@ -8,7 +8,6 @@
#include "Body.h"
Body::Body(b2World& world, const Vector2f& position, const Yaml& config) :
Sprite(config, PhysicalData(position, Vector2i(50, 50), world,
CATEGORY_NONSOLID, MASK_NONE, false)) {
Sprite(config, PhysicalData(position, world, CATEGORY_NONSOLID, MASK_NONE, false)) {
}

View file

@ -8,6 +8,6 @@
#include "Cover.h"
Cover::Cover(const Vector2f& position, const Vector2i& size, b2World& world, const Yaml& config) :
Sprite(config, PhysicalData(position, size, world, CATEGORY_WORLD, MASK_ALL, false)) {
Sprite(config, PhysicalData(position, world, CATEGORY_WORLD, MASK_ALL, false)) {
}

View file

@ -10,7 +10,7 @@
#include "Body.h"
Enemy::Enemy(const Instances& instances, const Vector2f& position, const Yaml& config) :
Character(instances, "enemy.png", PhysicalData(position, Vector2i(50, 50), instances.world,
Character(instances, "enemy.png", PhysicalData(position, instances.world,
CATEGORY_ACTOR, MASK_ALL, true, false, true), config),
mWorld(instances.world),
mCollection(instances.collection) {

View file

@ -21,7 +21,7 @@ const float Player::POINT_REACHED_DISTANCE = 1.0f;
* Initializes Sprite.
*/
Player::Player(const Instances& instances, const Vector2f& position, const Yaml& config) :
Character(instances, "player.png", PhysicalData(position, SIZE, instances.world,
Character(instances, "player.png", PhysicalData(position, instances.world,
CATEGORY_ACTOR, MASK_ALL, true, false, true), config),
mWeapon(instances, *this, Yaml("weapon.yaml")),
mDirection(0),

View file

@ -13,6 +13,9 @@
#include <yaml-cpp/yaml.h>
#include "String.h"
#include "Vector.h"
namespace details {};
/**
* Interface to a YAML file.
@ -23,6 +26,7 @@ public:
Yaml(const String& filename);
static void setFolder(const String& folder);
/**
* Gets a value of a specified type by key. Throws exception if key not found.
*
@ -43,4 +47,19 @@ private:
YAML::Node mNode;
};
/**
* Stream output operators to specialize Yaml::get for other types.
*/
namespace {
void operator>>(const YAML::Node& node, Vector2i& vector) {
node[0] >> vector.x;
node[1] >> vector.y;
}
void operator>>(const YAML::Node& node, Vector2f& vector) {
node[0] >> vector.x;
node[1] >> vector.y;
}
};
#endif /* DG_YAML_H_ */