diff --git a/source/Game.cpp b/source/Game.cpp index 7c5fd37..7752c9f 100644 --- a/source/Game.cpp +++ b/source/Game.cpp @@ -26,18 +26,15 @@ const float Game::TICKS_GOAL = 1000 / Game::FPS_GOAL; * Initializes game, including window and objects (sprites). */ Game::Game(sf::RenderWindow& window) : - mWorld(b2Vec2(0, 0)), mWindow(window), mView(Vector2f(0, 0), mWindow.getView().getSize()), //mFps("test"), - mTileManager(mWorld), - mPathfinder(mTileManager), + mTileManager(mWorld), mElapsed(0), mQuit(false), mPaused(false) { mWindow.setFramerateLimit(FPS_GOAL); - mWindow.setKeyRepeatEnabled(true); - mWorld.SetContactListener(this); + mWindow.setKeyRepeatEnabled(true); generate(); } @@ -83,7 +80,7 @@ Game::loop() { for (; !mPaused && (left >= TICKS_GOAL); left -= TICKS_GOAL) { Character::think(TICKS_GOAL); - mWorld.Step(1.0f / FPS_GOAL, 8, 3); + mWorld.step(); mCollection.checkDelete(); } @@ -245,19 +242,3 @@ String Game::getFps() { return str((mElapsed != 0) ? 1000.0f / mElapsed : 0.0f, 2); } - -/** - * Begin of collision, call callback function on both objects. - */ -void -Game::BeginContact(b2Contact* contact) { - Body& first = *static_cast(contact->GetFixtureA()->GetBody()->GetUserData()); - Body& second = *static_cast(contact->GetFixtureB()->GetBody()->GetUserData()); - - if (!first.doesCollide(second) || !second.doesCollide(first)) { - contact->SetEnabled(false); - return; - } - first.onCollide(second, second.getCategory()); - second.onCollide(first, first.getCategory()); -} diff --git a/source/Game.h b/source/Game.h index 4def72a..0f6cab3 100644 --- a/source/Game.h +++ b/source/Game.h @@ -13,28 +13,27 @@ #include -#include - +#include "World.h" #include "sprites/TileManager.h" #include "sprites/Player.h" #include "types/String.h" #include "util/Collection.h" #include "util/Pathfinder.h" +class World; class Player; class Collection; /* * Use vertex for tiles. */ -class Game : private sf::NonCopyable, public b2ContactListener { +class Game : private sf::NonCopyable { // Public functions. public: Game(sf::RenderWindow& window); ~Game(); void loop(); - void BeginContact(b2Contact* contact); // Private functions. private: @@ -55,7 +54,7 @@ private: static const int FPS_GOAL; static const float TICKS_GOAL; - b2World mWorld; + World mWorld; sf::RenderWindow& mWindow; sf::Clock mClock; diff --git a/source/abstract/Body.cpp b/source/abstract/Body.cpp index a23292c..f9bdb69 100755 --- a/source/abstract/Body.cpp +++ b/source/abstract/Body.cpp @@ -21,63 +21,19 @@ const Vector2i Body::DEFAULT_SIZE = Vector2i(50, 50); */ Body::Body(const PhysicalData& data, const Yaml& config, const Vector2i& pSize) : mDelete(false) { - Vector2i size = (pSize == Vector2i()) - ? config.get(KEY_SIZE, DEFAULT_SIZE) - : pSize; - assert(size != Vector2i()); - - b2BodyDef bodyDef; - bodyDef.type = (data.moving) - ? b2_dynamicBody - : b2_staticBody; - bodyDef.position = vector(data.position); - bodyDef.allowSleep = true; - bodyDef.fixedRotation = true; - bodyDef.bullet = data.bullet; - bodyDef.userData = this; - - mBody = data.world.CreateBody(&bodyDef); - - b2Shape* shape; - if (data.circle) { - assert(size.x == size.y); - shape = new b2CircleShape; - shape->m_radius = pixelToMeter(size.x) / 2; - } - else { - b2PolygonShape* box = new b2PolygonShape; - box->SetAsBox(pixelToMeter(size.x) / 2, - pixelToMeter(size.y) / 2); - shape = dynamic_cast(box); - } - - b2FixtureDef fixtureDef; - fixtureDef.shape = shape; - fixtureDef.density = 1.0f; - fixtureDef.filter.categoryBits = data.category; - fixtureDef.filter.maskBits = ~data.maskExclude; - fixtureDef.restitution = 0; - fixtureDef.density = (data.bullet) ? 0 : 10000; - - mBody->CreateFixture(&fixtureDef); - - delete shape; } /** - * Removes body from world. + * Used to make this class pure virtual without any pure virtual function. */ Body::~Body() { - mBody->GetWorld()->DestroyBody(mBody); } /** * Initializes container. - * - * @link Physical::PhysicalData */ -Body::PhysicalData::PhysicalData( const Vector2f& position, b2World& world, uint16 category, - uint16 maskExclude, bool moving, bool bullet, bool circle) : +Body::PhysicalData::PhysicalData( const Vector2f& position, World& world, Category category, + unsigned short maskExclude, bool moving, bool bullet, bool circle) : position(position), world(world), category(category), @@ -92,7 +48,7 @@ Body::PhysicalData::PhysicalData( const Vector2f& position, b2World& world, uint */ Vector2f Body::getPosition() const { - return vector(mBody->GetPosition()); + return Vector2f(); } /** @@ -100,7 +56,7 @@ Body::getPosition() const { */ Vector2f Body::getSpeed() const { - return vector(mBody->GetLinearVelocity()); + return Vector2f(); } /** @@ -108,7 +64,7 @@ Body::getSpeed() const { */ float Body::getAngle() const { - return thor::toDegree(mBody->GetAngle()); + return 0; } /** @@ -124,7 +80,7 @@ Body::getDelete() const { */ Body::Category Body::getCategory() const { - return (Category) mBody->GetFixtureList()->GetFilterData().categoryBits; + return CATEGORY_WORLD; } /** @@ -132,8 +88,7 @@ Body::getCategory() const { */ Vector2f Body::getSize() const { - b2AABB aabb(mBody->GetFixtureList()->GetAABB(0)); - return vector(aabb.upperBound - aabb.lowerBound); + return Vector2f(); } /** @@ -141,7 +96,7 @@ Body::getSize() const { */ bool Body::isSolid() const { - return mBody->GetFixtureList()->GetFilterData().maskBits != 0; + return false; } /** @@ -149,7 +104,7 @@ Body::isSolid() const { */ bool Body::isMovable() const { - return mBody->GetType() != b2_staticBody; + return false; } /** @@ -172,7 +127,7 @@ Body::doesCollide(Body& other) { * @param category The Category of the other object (as passed in constructor). */ void -Body::onCollide(Body& other, uint16 type) { +Body::onCollide(Body& other, Category type) { } /** @@ -191,11 +146,6 @@ Body::setDelete(bool value) { */ void Body::setSpeed(Vector2f direction, float speed) { - if (direction != Vector2f()) { - direction = thor::unitVector(direction); - } - direction *= speed; - mBody->SetLinearVelocity(vector(direction)); } /** @@ -203,5 +153,4 @@ Body::setSpeed(Vector2f direction, float speed) { */ void Body::setAngle(float angle) { - mBody->SetTransform(mBody->GetPosition(), thor::toRadian(angle)); } diff --git a/source/abstract/Body.h b/source/abstract/Body.h index 3994a25..73c1577 100755 --- a/source/abstract/Body.h +++ b/source/abstract/Body.h @@ -8,12 +8,12 @@ #ifndef DG_BODY_H_ #define DG_BODY_H_ -#include - +#include "../World.h" #include "../types/String.h" #include "../types/Vector.h" #include "../util/Yaml.h" +class World; class Yaml; /** @@ -24,31 +24,6 @@ class Yaml; class Body { // Public types. public: - /** - * POD container that carries all data required to construct this class. - */ - class PhysicalData { - public: - PhysicalData() = default; - 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; - /// Box2D world object. - b2World& world; - /// The category for collision filtering. Only one may be set. @link Physical::Category - uint16 category; - /// All categories set here will have collisions disabled with this object. - uint16 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; - }; - /** * Categories of physical objects, for Box2D collision filtering. * The order of categories is also used for render order in Collection::draw() @@ -63,6 +38,31 @@ public: CATEGORY_ACTOR = 1 << 4 }; + /** + * POD container that carries all data required to construct this class. + */ + class PhysicalData { + 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. + World& world; + /// The category for collision filtering. Only one may be set. @link Physical::Category + 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; + }; + /** * Common collision masking values. */ @@ -87,7 +87,7 @@ public: bool isMovable() const; virtual bool doesCollide(Body& other); - virtual void onCollide(Body& other, uint16 category); + virtual void onCollide(Body& other, Category category); // Public variables. public: @@ -103,7 +103,6 @@ protected: // Private variables. private: - b2Body* mBody; bool mDelete; }; diff --git a/source/abstract/Sprite.cpp b/source/abstract/Sprite.cpp index 6913d4c..d21de7d 100644 --- a/source/abstract/Sprite.cpp +++ b/source/abstract/Sprite.cpp @@ -31,7 +31,7 @@ Sprite::Sprite(const Yaml& config, const PhysicalData& data, const Vector2i& siz } /** - * Does nothing. + * Used to make this class pure virtual without any pure virtual function. */ Sprite::~Sprite() { } diff --git a/source/effects/Bullet.cpp b/source/effects/Bullet.cpp index 71a21dc..6c1a87f 100755 --- a/source/effects/Bullet.cpp +++ b/source/effects/Bullet.cpp @@ -6,6 +6,8 @@ */ #include "Bullet.h" + +#include #include "../abstract/Character.h" #include "../util/Loader.h" @@ -23,14 +25,16 @@ const float Bullet::DEFAULT_SPEED = 500; * @param world Box2d world. * @param texture Texture to display for bullet. */ -Bullet::Bullet(const Vector2f& position, b2World& world, Body& shooter, float direction, +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)), mShooter(shooter), mDamage(config.get(KEY_DAMAGE, DEFAULT_DAMAGE)), - mSpeed(config.get(KEY_SPEED, DEFAULT_SPEED)) { - setSpeed(angle(direction), mSpeed); + mSpeed(config.get(KEY_SPEED, DEFAULT_SPEED)) { + Vector2f dir(1.0f, 0); + thor::setPolarAngle(dir, direction); + setSpeed(dir, mSpeed); setAngle(direction); } @@ -38,7 +42,7 @@ Bullet::Bullet(const Vector2f& position, b2World& world, Body& shooter, float di * @copydoc Physical::onCollide */ void -Bullet::onCollide(Body& other, uint16 type) { +Bullet::onCollide(Body& other, Category type) { // Make sure we do not damage twice. if (!getDelete()) { // Call onShot on other, with damage as param. diff --git a/source/effects/Bullet.h b/source/effects/Bullet.h index 73a3222..cd2481b 100755 --- a/source/effects/Bullet.h +++ b/source/effects/Bullet.h @@ -21,10 +21,10 @@ class Yaml; class Bullet : public Particle { // Public functions. public: - Bullet(const Vector2f& position, b2World& world, Body& shooter, float direction, + Bullet(const Vector2f& position, World& world, Body& shooter, float direction, const Yaml& config); - void onCollide(Body& other, uint16 category); + void onCollide(Body& other, Category category); bool doesCollide(Body& other); // Private variables. diff --git a/source/items/Weapon.cpp b/source/items/Weapon.cpp index 338c400..66a3d1b 100755 --- a/source/items/Weapon.cpp +++ b/source/items/Weapon.cpp @@ -25,10 +25,11 @@ Weapon::Weapon(const Instances& instances, Body& holder, const Yaml& config) : mWorld(instances.world), mBullet(config.get(KEY_BULLET, DEFAULT_BULLET)), mTimer(sf::milliseconds(config.get(KEY_INTERVAL, DEFAULT_INTERVAL))) { + Vector2f holderSize = mHolder.getSize(); Yaml bullet(mBullet); - Vector2i bulletSize = bullet.get(Body::KEY_SIZE, Body::DEFAULT_SIZE); - mOffset = Vector2f(0, std::max(mHolder.getSize().x, mHolder.getSize().y) / 2 + - b2_linearSlop + + Vector2i bulletSize = bullet.get(Body::KEY_SIZE, Vector2i()); + mOffset = Vector2f(0, + std::max(holderSize.x, holderSize.y) / 2 + std::max(bulletSize.x, bulletSize.y) / 2); } diff --git a/source/items/Weapon.h b/source/items/Weapon.h index 82832e9..47de0c5 100755 --- a/source/items/Weapon.h +++ b/source/items/Weapon.h @@ -46,7 +46,7 @@ private: static const int DEFAULT_INTERVAL; Body& mHolder; - b2World& mWorld; + World& mWorld; Vector2f mOffset; //< Offset to the point where bullets are inserted (from holder center). const String mBullet; diff --git a/source/sprites/Corpse.cpp b/source/sprites/Corpse.cpp index 17d8cd1..6ca8bed 100644 --- a/source/sprites/Corpse.cpp +++ b/source/sprites/Corpse.cpp @@ -7,7 +7,7 @@ #include "Corpse.h" -Corpse::Corpse(b2World& world, const Vector2f& position, const Yaml& config) : +Corpse::Corpse(World& world, const Vector2f& position, const Yaml& config) : Sprite(config, PhysicalData(position, world, CATEGORY_NONSOLID, MASK_NONE, false)) { } diff --git a/source/sprites/Corpse.h b/source/sprites/Corpse.h index 0520c80..6e7bc80 100644 --- a/source/sprites/Corpse.h +++ b/source/sprites/Corpse.h @@ -17,7 +17,7 @@ class Yaml; class Corpse : public Sprite { // Public functions. public: - Corpse(b2World& world, const Vector2f& position, const Yaml& config); + Corpse(World& world, const Vector2f& position, const Yaml& config); }; #endif /* DG_CORPSE_H_ */ diff --git a/source/sprites/Enemy.h b/source/sprites/Enemy.h index 7515acc..3836f77 100644 --- a/source/sprites/Enemy.h +++ b/source/sprites/Enemy.h @@ -30,7 +30,7 @@ private: // Private variablese. private: - b2World& mWorld; + World& mWorld; Collection& mCollection; }; diff --git a/source/sprites/Player.cpp b/source/sprites/Player.cpp index 34f4c32..d6b0dae 100644 --- a/source/sprites/Player.cpp +++ b/source/sprites/Player.cpp @@ -61,22 +61,22 @@ Player::move(const Vector2f& destination) { void Player::setDirection(Direction direction, bool unset) { if (unset) { - mDirection = mDirection & ~(uint8) direction; + mDirection = mDirection & ~direction; } else { - mDirection = mDirection | (uint8) direction; + mDirection = mDirection | direction; } // Convert directions into a vector. Vector2f dirVec(0, 0); - if (mDirection & (uint8) Direction::RIGHT) { + if (mDirection & Direction::RIGHT) { dirVec.x += 1.0f; } - if (mDirection & (uint8) Direction::LEFT) { + if (mDirection & Direction::LEFT) { dirVec.x += - 1.0f; } - if (mDirection & (uint8) Direction::DOWN) { + if (mDirection & Direction::DOWN) { dirVec.y += 1.0f; } - if (mDirection & (uint8) Direction::UP) { + if (mDirection & Direction::UP) { dirVec.y += - 1.0f; } setSpeed(dirVec, getMovementSpeed()); @@ -92,14 +92,14 @@ Player::onThink(float elapsedTime) { Character::move(); } // Look towards crosshair. - setAngle(angle(mCrosshairPosition)); + setAngle(thor::polarAngle(mCrosshairPosition)); } /** * Stop movement if we collide with anything except bullets. */ void -Player::onCollide(Body& other, uint16 category) { +Player::onCollide(Body& other, Category category) { if (category != CATEGORY_PARTICLE) { setDestination(getPosition()); } diff --git a/source/sprites/Player.h b/source/sprites/Player.h index 99cc935..fa3a9fe 100644 --- a/source/sprites/Player.h +++ b/source/sprites/Player.h @@ -33,7 +33,7 @@ public: /** * Movement directions that can be set via Player::setDirection(). */ - enum class Direction : uint8 { + enum Direction : unsigned char { RIGHT = 1 << 0, LEFT = 1 << 1, UP = 1 << 2, @@ -51,13 +51,13 @@ public: // Private functions. private: - void onCollide(Body& other, uint16 category); + void onCollide(Body& other, Category category); void onThink(float elapsedTime); // Private variables. private: Vector2f mCrosshairPosition; //< Relative position of the point to fire at (mouse cursor). - uint8 mDirection; //< Current movement direction for direct control. + unsigned char mDirection; //< Current movement direction for direct control. }; #endif /* DG_PLAYER_H_ */ diff --git a/source/sprites/TileManager.cpp b/source/sprites/TileManager.cpp index cc8244f..9854fd3 100644 --- a/source/sprites/TileManager.cpp +++ b/source/sprites/TileManager.cpp @@ -21,7 +21,7 @@ const Vector2i TileManager::TILE_SIZE = Vector2i(100, 100); * * @param world Box2D world to create (physical) tiles in. */ -TileManager::TileManager(b2World& world) : +TileManager::TileManager(World& world) : mWorld(world) { } @@ -32,7 +32,7 @@ TileManager::TileManager(b2World& world) : * @param pPosition Position of the tile in tile coordinates. * @param world Box2D world object. */ -TileManager::Tile::Tile(Type type, const TilePosition& position, b2World& world) : +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)), mType(type) { diff --git a/source/sprites/TileManager.h b/source/sprites/TileManager.h index 433d2b2..659e4dc 100644 --- a/source/sprites/TileManager.h +++ b/source/sprites/TileManager.h @@ -9,15 +9,14 @@ #define DG_TILEMANAGER_H_ #include -#include #include -#include - +#include "../World.h" #include "../abstract/Sprite.h" #include "../types/Vector.h" #include "../types/String.h" +class World; class Sprite; class TileManager : public sf::Drawable { @@ -40,7 +39,7 @@ public: // Public functions. public: - TileManager(b2World& world); + TileManager(World& world); void setTile(const TilePosition& position, Type type); @@ -54,7 +53,7 @@ private: // Private variables. private: - b2World& mWorld; + World& mWorld; std::vector > mTiles; }; @@ -64,7 +63,7 @@ private: class TileManager::Tile : public Sprite { // Public functions. public: - Tile(Type type, const TilePosition& position, b2World& world); + Tile(Type type, const TilePosition& position, World& world); Type getType() const; TilePosition getTilePosition() const; diff --git a/source/types/Instances.h b/source/types/Instances.h index 2819bca..8a4bbef 100644 --- a/source/types/Instances.h +++ b/source/types/Instances.h @@ -8,12 +8,12 @@ #ifndef DG_INSTANCES_H_ #define DG_INSTANCES_H_ -#include - +#include "../World.h" #include "../sprites/TileManager.h" #include "../util/Collection.h" #include "../util/Pathfinder.h" +class World; class Pathfinder; class TileManager; class Collection; @@ -23,13 +23,13 @@ class Collection; */ struct Instances { Instances() = default; - Instances(Pathfinder& p, TileManager& t, Collection& c, b2World& w) : + Instances(Pathfinder& p, TileManager& t, Collection& c, World& w) : pathfinder(p), tilemanager(t), collection(c), world(w) {}; Pathfinder& pathfinder; TileManager& tilemanager; Collection& collection; - b2World& world; + World& world; }; #endif /* DG_INSTANCES_H_ */ diff --git a/source/types/Vector.h b/source/types/Vector.h index ba0f026..a054abe 100644 --- a/source/types/Vector.h +++ b/source/types/Vector.h @@ -12,8 +12,6 @@ #include -#include - #include /** @@ -26,58 +24,4 @@ typedef sf::Vector2f Vector2f; */ typedef sf::Vector2i Vector2i; -/** - * Constant for conversion between Box2D vectors and SFML vectors. - */ -static const int PIXELS_PER_METER = 25; - -/** - * Converts a distance from pixels to meters. - */ -inline float -pixelToMeter(float in) { - return in / PIXELS_PER_METER; -} - -/** - * Converts a distance from meters to pixels. - */ -inline float -meterToPixel(float in) { - return in * PIXELS_PER_METER; -} - -/** - * Converts Box2D metric vector to SFML pixel vector. - */ -inline Vector2f -vector(const b2Vec2& in) { - return Vector2f(meterToPixel(in.x), meterToPixel(in.y)); -} - -/** - * Converts SFML pixel vector to Box2D metric vector. - */ -inline b2Vec2 -vector(const Vector2f& in) { - return b2Vec2(pixelToMeter(in.x), pixelToMeter(in.y)); -} - -/** - * Converts a vector to an SFML angle with the same direction. - */ -inline float -angle(Vector2f in) { - return 180 - thor::toDegree(atan2(in.x, in.y)); -} - -/** - * Converts an SFML angle to a unit vector with the same direction. - */ -inline Vector2f -angle(float in) { - in = thor::toRadian(180 - in); - return Vector2f(sin(in), cos(in)); -} - #endif /* DG_VECTOR_H_ */ diff --git a/source/util/Pathfinder.h b/source/util/Pathfinder.h index 4545f21..01b043d 100644 --- a/source/util/Pathfinder.h +++ b/source/util/Pathfinder.h @@ -8,8 +8,6 @@ #ifndef PATHFINDER_H_ #define PATHFINDER_H_ -#include - #include "../abstract/Body.h" #include "../types/Vector.h" diff --git a/source/util/Timer.cpp b/source/util/Timer.cpp index 096235e..ae8cc7b 100644 --- a/source/util/Timer.cpp +++ b/source/util/Timer.cpp @@ -10,7 +10,7 @@ #include /** - * INitializes time limit to zero. + * Initializes time limit to zero. */ Timer::Timer() { }