diff --git a/source/Game.cpp b/source/Game.cpp index 2dbd85b..f0b5d95 100644 --- a/source/Game.cpp +++ b/source/Game.cpp @@ -41,10 +41,10 @@ Game::Game(const Vector2i& resolution) : mTileManager.generate(); for (int i = 0; i < 500; i += 50) { mCollection.insert(std::shared_ptr(new Cover(Vector2f(i, i), Vector2i(20, 20), - mWorld)), Collection::LEVEL_STATIC); + mWorld))); } - mCollection.insert(std::shared_ptr(new Enemy(mWorld, Vector2f(400.0f, 200.0f), mCollection)), - Collection::LEVEL_ACTOR); + mCollection.insert(std::shared_ptr(new Enemy(mWorld, Vector2f(400.0f, 200.0f), + mCollection))); } /** diff --git a/source/abstract/Physical.cpp b/source/abstract/Physical.cpp index 9dc65e1..87b1207 100755 --- a/source/abstract/Physical.cpp +++ b/source/abstract/Physical.cpp @@ -117,9 +117,9 @@ Physical::getDelete() const { /** * Returns the Physical::Category of this object. */ -uint16 +Physical::Category Physical::getCategory() const { - return mBody->GetFixtureList()->GetFilterData().categoryBits; + return (Category) mBody->GetFixtureList()->GetFilterData().categoryBits; } /** diff --git a/source/abstract/Physical.h b/source/abstract/Physical.h index dc35d1c..5acd851 100755 --- a/source/abstract/Physical.h +++ b/source/abstract/Physical.h @@ -49,14 +49,15 @@ public: /** * Categories of physical objects, for Box2D collision filtering. + * The order of categories is also used for render order. * * @warning An object may only have one category. */ enum Category { - CATEGORY_NONSOLID = 0, CATEGORY_WORLD = 1 << 1, - CATEGORY_ACTOR = 1 << 2, - CATEGORY_PARTICLE = 1 << 3 + CATEGORY_NONSOLID = 1 << 2, + CATEGORY_PARTICLE = 1 << 3, + CATEGORY_ACTOR = 1 << 4 }; /** @@ -76,7 +77,7 @@ public: Vector2f getSpeed() const; float getAngle() const; bool getDelete() const; - uint16 getCategory() const; + Category getCategory() const; virtual bool doesCollide(Physical& other); virtual void onCollide(Physical& other, uint16 category); diff --git a/source/particle/Emitter.cpp b/source/particle/Emitter.cpp index 03dc5be..5901f60 100755 --- a/source/particle/Emitter.cpp +++ b/source/particle/Emitter.cpp @@ -19,5 +19,5 @@ Emitter::~Emitter() { */ void Emitter::emit() { - mCollection.insert(createParticle(), Collection::LEVEL_PARTICLE); + mCollection.insert(createParticle()); } diff --git a/source/sprite/Enemy.cpp b/source/sprite/Enemy.cpp index dd81dc7..49daf48 100644 --- a/source/sprite/Enemy.cpp +++ b/source/sprite/Enemy.cpp @@ -20,8 +20,7 @@ Enemy::Enemy(b2World& world, const Vector2f& position, Collection& collection) : Enemy::~Enemy() { // Insert here to avoid altering b2d data during timestep. - mCollection.insert(std::shared_ptr(new Body(mWorld, getPosition())), - Collection::LEVEL_STATIC); + mCollection.insert(std::shared_ptr(new Body(mWorld, getPosition()))); } void diff --git a/source/util/Collection.cpp b/source/util/Collection.cpp index debb13d..762822d 100755 --- a/source/util/Collection.cpp +++ b/source/util/Collection.cpp @@ -14,10 +14,11 @@ * An object can't be inserted more than once at the same level. */ void -Collection::insert(std::shared_ptr drawable, Level level) { - auto item = std::find(mDrawables[level].begin(), mDrawables[level].end(), drawable); - if (item == mDrawables[level].end()) { - mDrawables[level].push_back(drawable); +Collection::insert(std::shared_ptr drawable) { + Physical::Category cat = drawable->getCategory(); + auto item = std::find(mDrawables[cat].begin(), mDrawables[cat].end(), drawable); + if (item == mDrawables[cat].end()) { + mDrawables[cat].push_back(drawable); } } diff --git a/source/util/Collection.h b/source/util/Collection.h index be7d778..c0dc6b8 100755 --- a/source/util/Collection.h +++ b/source/util/Collection.h @@ -19,22 +19,13 @@ class Sprite; /** * A collection of sprites, which can be put into different layers. + * + * Uses Sprite instead of sf::Drawable to also manage deleting objects. */ class Collection : public sf::Drawable { -// Public types. -public: - /** - * Determines in what order sprites are rendered, dynamics and actors should be on top. - */ - enum Level { - LEVEL_STATIC, - LEVEL_PARTICLE, - LEVEL_ACTOR - }; - // Public functions. public: - void insert(std::shared_ptr drawable, Level level); + void insert(std::shared_ptr drawable); void remove(std::shared_ptr drawable); void checkDelete(); @@ -44,7 +35,7 @@ private: // Private variables. private: - std::map > > mDrawables; + std::map > > mDrawables; }; #endif /* DG_COLLECTION_H_ */