Removed Collection::Level, using Physical::Category order instead.

This commit is contained in:
Felix Ableitner 2012-09-16 20:33:00 +02:00
parent dbfa7c10c2
commit 3cc8c93845
7 changed files with 21 additions and 29 deletions

View file

@ -41,10 +41,10 @@ Game::Game(const Vector2i& resolution) :
mTileManager.generate(); mTileManager.generate();
for (int i = 0; i < 500; i += 50) { for (int i = 0; i < 500; i += 50) {
mCollection.insert(std::shared_ptr<Sprite>(new Cover(Vector2f(i, i), Vector2i(20, 20), mCollection.insert(std::shared_ptr<Sprite>(new Cover(Vector2f(i, i), Vector2i(20, 20),
mWorld)), Collection::LEVEL_STATIC); mWorld)));
} }
mCollection.insert(std::shared_ptr<Sprite>(new Enemy(mWorld, Vector2f(400.0f, 200.0f), mCollection)), mCollection.insert(std::shared_ptr<Sprite>(new Enemy(mWorld, Vector2f(400.0f, 200.0f),
Collection::LEVEL_ACTOR); mCollection)));
} }
/** /**

View file

@ -117,9 +117,9 @@ Physical::getDelete() const {
/** /**
* Returns the Physical::Category of this object. * Returns the Physical::Category of this object.
*/ */
uint16 Physical::Category
Physical::getCategory() const { Physical::getCategory() const {
return mBody->GetFixtureList()->GetFilterData().categoryBits; return (Category) mBody->GetFixtureList()->GetFilterData().categoryBits;
} }
/** /**

View file

@ -49,14 +49,15 @@ public:
/** /**
* Categories of physical objects, for Box2D collision filtering. * 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. * @warning An object may only have one category.
*/ */
enum Category { enum Category {
CATEGORY_NONSOLID = 0,
CATEGORY_WORLD = 1 << 1, CATEGORY_WORLD = 1 << 1,
CATEGORY_ACTOR = 1 << 2, CATEGORY_NONSOLID = 1 << 2,
CATEGORY_PARTICLE = 1 << 3 CATEGORY_PARTICLE = 1 << 3,
CATEGORY_ACTOR = 1 << 4
}; };
/** /**
@ -76,7 +77,7 @@ public:
Vector2f getSpeed() const; Vector2f getSpeed() const;
float getAngle() const; float getAngle() const;
bool getDelete() const; bool getDelete() const;
uint16 getCategory() const; Category getCategory() const;
virtual bool doesCollide(Physical& other); virtual bool doesCollide(Physical& other);
virtual void onCollide(Physical& other, uint16 category); virtual void onCollide(Physical& other, uint16 category);

View file

@ -19,5 +19,5 @@ Emitter::~Emitter() {
*/ */
void void
Emitter::emit() { Emitter::emit() {
mCollection.insert(createParticle(), Collection::LEVEL_PARTICLE); mCollection.insert(createParticle());
} }

View file

@ -20,8 +20,7 @@ Enemy::Enemy(b2World& world, const Vector2f& position, Collection& collection) :
Enemy::~Enemy() { Enemy::~Enemy() {
// Insert here to avoid altering b2d data during timestep. // Insert here to avoid altering b2d data during timestep.
mCollection.insert(std::shared_ptr<Sprite>(new Body(mWorld, getPosition())), mCollection.insert(std::shared_ptr<Sprite>(new Body(mWorld, getPosition())));
Collection::LEVEL_STATIC);
} }
void void

View file

@ -14,10 +14,11 @@
* An object can't be inserted more than once at the same level. * An object can't be inserted more than once at the same level.
*/ */
void void
Collection::insert(std::shared_ptr<Sprite> drawable, Level level) { Collection::insert(std::shared_ptr<Sprite> drawable) {
auto item = std::find(mDrawables[level].begin(), mDrawables[level].end(), drawable); Physical::Category cat = drawable->getCategory();
if (item == mDrawables[level].end()) { auto item = std::find(mDrawables[cat].begin(), mDrawables[cat].end(), drawable);
mDrawables[level].push_back(drawable); if (item == mDrawables[cat].end()) {
mDrawables[cat].push_back(drawable);
} }
} }

View file

@ -19,22 +19,13 @@ class Sprite;
/** /**
* A collection of sprites, which can be put into different layers. * 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 { 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 functions.
public: public:
void insert(std::shared_ptr<Sprite> drawable, Level level); void insert(std::shared_ptr<Sprite> drawable);
void remove(std::shared_ptr<Sprite> drawable); void remove(std::shared_ptr<Sprite> drawable);
void checkDelete(); void checkDelete();
@ -44,7 +35,7 @@ private:
// Private variables. // Private variables.
private: private:
std::map<Level, std::vector<std::shared_ptr<Sprite> > > mDrawables; std::map<Physical::Category, std::vector<std::shared_ptr<Sprite> > > mDrawables;
}; };
#endif /* DG_COLLECTION_H_ */ #endif /* DG_COLLECTION_H_ */