diff --git a/source/TileManager.cpp b/source/TileManager.cpp index 1a64e64..75d34c4 100755 --- a/source/TileManager.cpp +++ b/source/TileManager.cpp @@ -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) { } diff --git a/source/abstract/Physical.cpp b/source/abstract/Physical.cpp index 522f941..4039ed0 100755 --- a/source/abstract/Physical.cpp +++ b/source/abstract/Physical.cpp @@ -11,14 +11,17 @@ #include +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(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(box); } @@ -70,16 +73,15 @@ 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) : - position(position), - size(size), - world(world), - category(category), - maskExclude(maskExclude), - moving(moving), - bullet(bullet), - circle(circle) { +Physical::PhysicalData::PhysicalData( const Vector2f& position, b2World& world, uint16 category, + uint16 maskExclude, bool moving, bool bullet, bool circle) : + position(position), + world(world), + category(category), + maskExclude(maskExclude), + moving(moving), + bullet(bullet), + circle(circle) { } /** diff --git a/source/abstract/Physical.h b/source/abstract/Physical.h index 66490c3..9415be9 100755 --- a/source/abstract/Physical.h +++ b/source/abstract/Physical.h @@ -10,7 +10,11 @@ #include +#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; }; diff --git a/source/abstract/Sprite.cpp b/source/abstract/Sprite.cpp index f264161..a6d46e0 100644 --- a/source/abstract/Sprite.cpp +++ b/source/abstract/Sprite.cpp @@ -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(config.get(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& texture, const PhysicalData& data) : - Physical(data), + Physical(data, Yaml("tile.yaml")), mTexture(texture), - mSize(data.size) { + mSize(Vector2i(getSize())) { } /** diff --git a/source/effects/Bullet.cpp b/source/effects/Bullet.cpp index e960bb9..40a9ff4 100755 --- a/source/effects/Bullet.cpp +++ b/source/effects/Bullet.cpp @@ -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("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(KEY_DAMAGE)), diff --git a/source/sprite/Body.cpp b/source/sprite/Body.cpp index 13ce3c9..989b443 100755 --- a/source/sprite/Body.cpp +++ b/source/sprite/Body.cpp @@ -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)) { } diff --git a/source/sprite/Cover.cpp b/source/sprite/Cover.cpp index 2488f95..bd08f1b 100755 --- a/source/sprite/Cover.cpp +++ b/source/sprite/Cover.cpp @@ -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)) { } diff --git a/source/sprite/Enemy.cpp b/source/sprite/Enemy.cpp index f1daa09..b5f3444 100644 --- a/source/sprite/Enemy.cpp +++ b/source/sprite/Enemy.cpp @@ -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) { diff --git a/source/sprite/Player.cpp b/source/sprite/Player.cpp index c30ae29..12a2cfd 100644 --- a/source/sprite/Player.cpp +++ b/source/sprite/Player.cpp @@ -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), diff --git a/source/util/Yaml.h b/source/util/Yaml.h index 24dc914..0b44fc8 100644 --- a/source/util/Yaml.h +++ b/source/util/Yaml.h @@ -13,6 +13,9 @@ #include #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_ */