From f42bdf5a24ea52e97f34f3407082b4a93e76d634 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Sat, 22 Dec 2012 13:44:17 +0100 Subject: [PATCH] Re-implemented Body funcionality (without library). --- source/abstract/Body.cpp | 52 ++++++++++++++-------------------- source/abstract/Body.h | 44 ++++++++++++---------------- source/abstract/Character.cpp | 2 +- source/abstract/Character.h | 2 +- source/abstract/Sprite.cpp | 2 +- source/abstract/Sprite.h | 4 +-- source/effects/Bullet.cpp | 3 +- source/items/Weapon.cpp | 2 +- source/particle/Particle.cpp | 2 +- source/particle/Particle.h | 2 +- source/sprites/Corpse.cpp | 2 +- source/sprites/Enemy.cpp | 3 +- source/sprites/Player.cpp | 7 +++-- source/sprites/TileManager.cpp | 5 ++-- 14 files changed, 58 insertions(+), 74 deletions(-) diff --git a/source/abstract/Body.cpp b/source/abstract/Body.cpp index f9bdb69..77a25e2 100755 --- a/source/abstract/Body.cpp +++ b/source/abstract/Body.cpp @@ -19,7 +19,12 @@ const Vector2i Body::DEFAULT_SIZE = Vector2i(50, 50); * * @param data Data needed for construction. */ -Body::Body(const PhysicalData& data, const Yaml& config, const Vector2i& pSize) : +Body::Body(const Data& data, const Yaml& config, const Vector2i& pSize) : + mPosition(data.position), + mSize(config.get(KEY_SIZE, DEFAULT_SIZE)), + mAngle(0), + mCategory(data.category), + mMask(data.mask), mDelete(false) { } @@ -32,15 +37,13 @@ Body::~Body() { /** * Initializes container. */ -Body::PhysicalData::PhysicalData( const Vector2f& position, World& world, Category category, - unsigned short maskExclude, bool moving, bool bullet, bool circle) : - position(position), +Body::Data::Data(World& world, const Vector2f& position, float angle, + Category category, unsigned short maskExclude) : world(world), + position(position), + angle(angle), category(category), - maskExclude(maskExclude), - moving(moving), - bullet(bullet), - circle(circle) { + mask(maskExclude) { } /** @@ -48,7 +51,7 @@ Body::PhysicalData::PhysicalData( const Vector2f& position, World& world, Catego */ Vector2f Body::getPosition() const { - return Vector2f(); + return mPosition; } /** @@ -56,7 +59,7 @@ Body::getPosition() const { */ Vector2f Body::getSpeed() const { - return Vector2f(); + return mSpeed; } /** @@ -64,7 +67,7 @@ Body::getSpeed() const { */ float Body::getAngle() const { - return 0; + return mAngle; } /** @@ -80,31 +83,15 @@ Body::getDelete() const { */ Body::Category Body::getCategory() const { - return CATEGORY_WORLD; + return mCategory; } /** * Returns the size of the body as a vector. */ -Vector2f +Vector2i Body::getSize() const { - return Vector2f(); -} - -/** - * Returns true if collisions are enabled for the body. - */ -bool -Body::isSolid() const { - return false; -} - -/** - * Returns true if the body is able to move. - */ -bool -Body::isMovable() const { - return false; + return mSize; } /** @@ -146,6 +133,10 @@ Body::setDelete(bool value) { */ void Body::setSpeed(Vector2f direction, float speed) { + if (direction != Vector2f()) { + thor::setLength(direction, speed); + } + mSpeed = direction; } /** @@ -153,4 +144,5 @@ Body::setSpeed(Vector2f direction, float speed) { */ void Body::setAngle(float angle) { + mAngle = angle; } diff --git a/source/abstract/Body.h b/source/abstract/Body.h index 73c1577..6160217 100755 --- a/source/abstract/Body.h +++ b/source/abstract/Body.h @@ -39,41 +39,31 @@ public: }; /** - * POD container that carries all data required to construct this class. + * POD container that carries all data required to construct an object of this class. */ - class PhysicalData { + class Data { public: - PhysicalData() = default; - PhysicalData(const Vector2f& position, World& world, Category category, - unsigned short maskExclude, bool moving, bool bullet = false, bool circle = false); - /// World position of the body in pixel coordinates. - const Vector2f& position; - /// Box2D world object. + Data() = default; + Data(World& world, const Vector2f& position, float angle, + Category category, unsigned short maskExclude); World& world; - /// The category for collision filtering. Only one may be set. @link Physical::Category + const Vector2f& position; + float angle; Category category; - /// All categories set here will have collisions disabled with this object. - unsigned short maskExclude; - /// True if the body may move on its own (player, monster). - bool moving; - /// True if the object is a bullet. - bool bullet; - /// True if the body collides as a circle. Radius is side length / 2, - /// both sides must be equal. - bool circle; + unsigned short mask; }; /** * Common collision masking values. */ - enum Mask { + enum Mask : unsigned short { MASK_NONE = 0xffff, //< Disables any collisions. MASK_ALL = 0 //< Enables all collisions. }; // Public functions. public: - Body(const PhysicalData& data, const Yaml& config, const Vector2i& pSize = Vector2i()); + Body(const Data& data, const Yaml& config, const Vector2i& pSize = Vector2i()); virtual ~Body() = 0; Vector2f getPosition() const; @@ -81,10 +71,7 @@ public: float getAngle() const; bool getDelete() const; Category getCategory() const; - Vector2f getSize() const; - - bool isSolid() const; - bool isMovable() const; + Vector2i getSize() const; virtual bool doesCollide(Body& other); virtual void onCollide(Body& other, Category category); @@ -98,11 +85,16 @@ public: protected: void setDelete(bool value); void setSpeed(Vector2f direction, float speed); - void setAngle(float angle); - + void setAngle(float angle); // Private variables. private: + Vector2f mPosition; + Vector2i mSize; + Vector2f mSpeed; + float mAngle; + Category mCategory; + unsigned short mMask; bool mDelete; }; diff --git a/source/abstract/Character.cpp b/source/abstract/Character.cpp index 3603e71..f8f3aa9 100644 --- a/source/abstract/Character.cpp +++ b/source/abstract/Character.cpp @@ -26,7 +26,7 @@ std::vector Character::mCharacterInstances = std::vector * Saves pointer to this instance in static var for think(). */ Character::Character(World& world, Collection& collection, Pathfinder& pathfinder, - const PhysicalData& data, const Yaml& config) : + const Data& data, const Yaml& config) : Sprite(config, data), mCollection(collection), mPathfinder(pathfinder), diff --git a/source/abstract/Character.h b/source/abstract/Character.h index 3e7327c..12ece5f 100644 --- a/source/abstract/Character.h +++ b/source/abstract/Character.h @@ -31,7 +31,7 @@ class Character : public Sprite { // Public functions. public: Character(World& world, Collection& collection, Pathfinder& pathfinder, - const PhysicalData& data, const Yaml& config); + const Data& data, const Yaml& config); virtual ~Character() = 0; static void think(float elapsedTime); diff --git a/source/abstract/Sprite.cpp b/source/abstract/Sprite.cpp index d21de7d..ac7a2bc 100644 --- a/source/abstract/Sprite.cpp +++ b/source/abstract/Sprite.cpp @@ -19,7 +19,7 @@ const String Sprite::DEFAULT_TEXTURE = ""; * * @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 Data& data, const Vector2i& size) : Body(data, config, size), mSize(Vector2i(getSize())) { String texture = config.get(KEY_TEXTURE, DEFAULT_TEXTURE); diff --git a/source/abstract/Sprite.h b/source/abstract/Sprite.h index 1976acf..2a2736c 100644 --- a/source/abstract/Sprite.h +++ b/source/abstract/Sprite.h @@ -28,8 +28,8 @@ class Yaml; class Sprite : public sf::Drawable, public Body { // Public functions. public: - Sprite(const Yaml& config, const PhysicalData& data, const Vector2i& size = Vector2i()); - Sprite(const std::shared_ptr& texture, const PhysicalData& data); + Sprite(const Yaml& config, const Data& data, const Vector2i& size = Vector2i()); + Sprite(const std::shared_ptr& texture, const Data& data); virtual ~Sprite() = 0; // Protected functions. diff --git a/source/effects/Bullet.cpp b/source/effects/Bullet.cpp index 6c1a87f..39c6903 100755 --- a/source/effects/Bullet.cpp +++ b/source/effects/Bullet.cpp @@ -27,8 +27,7 @@ const float Bullet::DEFAULT_SPEED = 500; */ Bullet::Bullet(const Vector2f& position, World& world, Body& shooter, float direction, const Yaml& config) : - Particle(config, PhysicalData(position, world, CATEGORY_PARTICLE, CATEGORY_PARTICLE, - true, true, true)), + Particle(config, Data(world, position, 0, CATEGORY_PARTICLE, CATEGORY_PARTICLE)), mShooter(shooter), mDamage(config.get(KEY_DAMAGE, DEFAULT_DAMAGE)), mSpeed(config.get(KEY_SPEED, DEFAULT_SPEED)) { diff --git a/source/items/Weapon.cpp b/source/items/Weapon.cpp index f6e3bce..79e563d 100755 --- a/source/items/Weapon.cpp +++ b/source/items/Weapon.cpp @@ -25,7 +25,7 @@ Weapon::Weapon(World& world, Collection& collection, Body& holder, const Yaml& c mWorld(world), mBullet(config.get(KEY_BULLET, DEFAULT_BULLET)), mTimer(sf::milliseconds(config.get(KEY_INTERVAL, DEFAULT_INTERVAL))) { - Vector2f holderSize = mHolder.getSize(); + Vector2i holderSize = mHolder.getSize(); Yaml bullet(mBullet); Vector2i bulletSize = bullet.get(Body::KEY_SIZE, Vector2i()); mOffset = Vector2f(0, diff --git a/source/particle/Particle.cpp b/source/particle/Particle.cpp index 742a061..458d64f 100755 --- a/source/particle/Particle.cpp +++ b/source/particle/Particle.cpp @@ -7,7 +7,7 @@ #include "Particle.h" -Particle::Particle(const Yaml& config, const PhysicalData& data) : +Particle::Particle(const Yaml& config, const Data& data) : Sprite(config, data) { } diff --git a/source/particle/Particle.h b/source/particle/Particle.h index 65aa45f..3612b0c 100755 --- a/source/particle/Particle.h +++ b/source/particle/Particle.h @@ -20,7 +20,7 @@ class Yaml; class Particle : public Sprite { // Public functions. public: - Particle(const Yaml& config, const PhysicalData& data); + Particle(const Yaml& config, const Data& data); virtual ~Particle(); }; diff --git a/source/sprites/Corpse.cpp b/source/sprites/Corpse.cpp index 6ca8bed..4b3eb2e 100644 --- a/source/sprites/Corpse.cpp +++ b/source/sprites/Corpse.cpp @@ -8,6 +8,6 @@ #include "Corpse.h" Corpse::Corpse(World& world, const Vector2f& position, const Yaml& config) : - Sprite(config, PhysicalData(position, world, CATEGORY_NONSOLID, MASK_NONE, false)) { + Sprite(config, Data(world, position, 0, CATEGORY_NONSOLID, MASK_NONE)) { } diff --git a/source/sprites/Enemy.cpp b/source/sprites/Enemy.cpp index 1ebbc1a..2271f60 100644 --- a/source/sprites/Enemy.cpp +++ b/source/sprites/Enemy.cpp @@ -12,8 +12,7 @@ Enemy::Enemy(World& world, Collection& collection, Pathfinder& pathfinder, const Vector2f& position, const Yaml& config) : Character(world, collection, pathfinder, - PhysicalData(position, world, CATEGORY_ACTOR, MASK_ALL, - true, false, true), + Data(world, position, 0, CATEGORY_ACTOR, MASK_ALL), config), mWorld(world), mCollection(collection) { diff --git a/source/sprites/Player.cpp b/source/sprites/Player.cpp index af11483..f70a2fb 100644 --- a/source/sprites/Player.cpp +++ b/source/sprites/Player.cpp @@ -19,8 +19,7 @@ Player::Player(World& world, Collection& collection, Pathfinder& pathfinder, const Vector2f& position, const Yaml& config) : Character(world, collection, pathfinder, - PhysicalData(position, world, CATEGORY_ACTOR, MASK_ALL, true, - false, true), + Data(world, position, 0, CATEGORY_ACTOR, MASK_ALL), config), mDirection(0) { } @@ -95,7 +94,9 @@ Player::onThink(float elapsedTime) { Character::move(); } // Look towards crosshair. - setAngle(thor::polarAngle(mCrosshairPosition)); + if (mCrosshairPosition != Vector2f()) { + setAngle(thor::polarAngle(mCrosshairPosition) + 90); + } } /** diff --git a/source/sprites/TileManager.cpp b/source/sprites/TileManager.cpp index 9854fd3..2084039 100644 --- a/source/sprites/TileManager.cpp +++ b/source/sprites/TileManager.cpp @@ -33,8 +33,9 @@ TileManager::TileManager(World& world) : * @param world Box2D world object. */ TileManager::Tile::Tile(Type type, const TilePosition& position, World& world) : - Sprite(Yaml(getConfig(type)), PhysicalData(Vector2f(position.x * TILE_SIZE.x, position.y * TILE_SIZE.y), - world, CATEGORY_WORLD, (type == Type::FLOOR) ? MASK_NONE : MASK_ALL, false)), + Sprite(Yaml(getConfig(type)), Data(world, + Vector2f(position.x * TILE_SIZE.x, position.y * TILE_SIZE.y), 0, + CATEGORY_WORLD, (type == Type::FLOOR) ? MASK_NONE : MASK_ALL)), mType(type) { }