Merged Collection into World.
This commit is contained in:
parent
f42bdf5a24
commit
a157f0857d
25 changed files with 169 additions and 259 deletions
|
@ -49,11 +49,12 @@ Game::generate() {
|
|||
for (int x = 1; x < 5; x++)
|
||||
mTileManager.setTile(TileManager::TilePosition(x, 4), TileManager::Type::WALL);
|
||||
|
||||
mCollection.insert(std::shared_ptr<Sprite>(new Enemy(mWorld, mCollection, mPathfinder,
|
||||
mWorld.insert(std::shared_ptr<Sprite>(new Enemy(mWorld, mPathfinder,
|
||||
Vector2f(400.0f, 200.0f), Yaml("enemy.yaml"))));
|
||||
|
||||
mPlayer = std::unique_ptr<Player>(new Player(mWorld, mCollection, mPathfinder,
|
||||
mPlayer = std::shared_ptr<Player>(new Player(mWorld, mPathfinder,
|
||||
Vector2f(200.0f, 100.0f), Yaml("player.yaml")));
|
||||
mWorld.insert(mPlayer);
|
||||
}
|
||||
/**
|
||||
* Closes window.
|
||||
|
@ -77,9 +78,9 @@ Game::loop() {
|
|||
}
|
||||
|
||||
Character::think(elapsed);
|
||||
mCollection.checkDelete();
|
||||
mWorld.checkDelete();
|
||||
|
||||
mWorld.step();
|
||||
mWorld.step(elapsed);
|
||||
|
||||
render();
|
||||
}
|
||||
|
@ -203,9 +204,7 @@ Game::render() {
|
|||
// Render world and dynamic stuff.
|
||||
mWindow.setView(mView);
|
||||
|
||||
mWindow.draw(mTileManager);
|
||||
mWindow.draw(mCollection);
|
||||
mWindow.draw(*mPlayer);
|
||||
mWindow.draw(mWorld);
|
||||
|
||||
// Render GUI and static stuff.
|
||||
mWindow.setView(mWindow.getDefaultView());
|
||||
|
|
|
@ -13,16 +13,14 @@
|
|||
|
||||
#include <Thor/Resources.hpp>
|
||||
|
||||
#include "World.h"
|
||||
#include "sprites/TileManager.h"
|
||||
#include "sprites/Player.h"
|
||||
#include "types/String.h"
|
||||
#include "util/Collection.h"
|
||||
#include "World.h"
|
||||
#include "util/Pathfinder.h"
|
||||
|
||||
class World;
|
||||
class Player;
|
||||
class Collection;
|
||||
class World;
|
||||
|
||||
/*
|
||||
* Use vertex for tiles.
|
||||
|
@ -51,16 +49,14 @@ private:
|
|||
private:
|
||||
static const int FPS_GOAL;
|
||||
|
||||
World mWorld;
|
||||
|
||||
sf::RenderWindow& mWindow;
|
||||
sf::Clock mClock;
|
||||
sf::View mView;
|
||||
|
||||
Collection mCollection;
|
||||
World mWorld;
|
||||
TileManager mTileManager;
|
||||
Pathfinder mPathfinder;
|
||||
std::unique_ptr<Player> mPlayer;
|
||||
std::shared_ptr<Player> mPlayer;
|
||||
|
||||
bool mQuit;
|
||||
bool mPaused;
|
||||
|
|
63
source/World.cpp
Normal file → Executable file
63
source/World.cpp
Normal file → Executable file
|
@ -1,23 +1,70 @@
|
|||
/*
|
||||
* World.cpp
|
||||
*
|
||||
* Created on: 22.12.2012
|
||||
* Created on: 29.08.2012
|
||||
* Author: Felix
|
||||
*/
|
||||
|
||||
#include "World.h"
|
||||
|
||||
World::World() {
|
||||
// TODO Auto-generated constructor stub
|
||||
#include <algorithm>
|
||||
|
||||
/**
|
||||
* Insert a drawable into the group. Drawables should only be handled with shared_ptr.
|
||||
* An object can't be inserted more than once at the same level.
|
||||
*/
|
||||
void
|
||||
World::insert(std::shared_ptr<Sprite> drawable) {
|
||||
Body::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);
|
||||
}
|
||||
}
|
||||
|
||||
World::~World() {
|
||||
// TODO Auto-generated destructor stub
|
||||
/**
|
||||
* Removes a drawable from the group.
|
||||
*/
|
||||
void
|
||||
World::remove(std::shared_ptr<Sprite> drawable) {
|
||||
for (auto v = mDrawables.begin(); v != mDrawables.end(); v++) {
|
||||
auto item = std::find(v->second.begin(), v->second.end(), drawable);
|
||||
if (item != v->second.end()) {
|
||||
v->second.erase(item);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
World::step() {
|
||||
|
||||
World::step(int elapsed) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes any sprites which return true for getDelete().
|
||||
*/
|
||||
void
|
||||
World::checkDelete() {
|
||||
for (auto v = mDrawables.begin(); v != mDrawables.end(); v++) {
|
||||
for (auto item = v->second.begin(); item != v->second.end(); ) {
|
||||
if ((*item)->getDelete()) {
|
||||
item = v->second.erase(item);
|
||||
}
|
||||
else {
|
||||
item++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Draws all elements in the group.
|
||||
*/
|
||||
void
|
||||
World::draw(sf::RenderTarget& target, sf::RenderStates states) const {
|
||||
for (auto v = mDrawables.begin(); v != mDrawables.end(); v++) {
|
||||
for (auto item : v->second) {
|
||||
target.draw(static_cast<sf::Drawable&>(*item), states);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
38
source/World.h
Normal file → Executable file
38
source/World.h
Normal file → Executable file
|
@ -1,19 +1,45 @@
|
|||
/*
|
||||
* World.h
|
||||
*
|
||||
* Created on: 22.12.2012
|
||||
* Created on: 29.08.2012
|
||||
* Author: Felix
|
||||
*/
|
||||
|
||||
#ifndef DG_WORLD_H_
|
||||
#define DG_WORLD_H_
|
||||
|
||||
class World {
|
||||
public:
|
||||
World();
|
||||
~World();
|
||||
#include <map>
|
||||
#include <vector>
|
||||
|
||||
void step();
|
||||
#include <SFML/Graphics.hpp>
|
||||
|
||||
#include "abstract/Body.h"
|
||||
#include "abstract/Sprite.h"
|
||||
|
||||
class Body;
|
||||
class Sprite;
|
||||
|
||||
/**
|
||||
* A collection of sprites, which can be put into different layers.
|
||||
*
|
||||
* Uses Sprite instead of sf::Drawable to also manage deleting objects.
|
||||
* Render order is determined by Physical::Category (higher number on top).
|
||||
*/
|
||||
class World : public sf::Drawable {
|
||||
// Public functions.
|
||||
public:
|
||||
void insert(std::shared_ptr<Sprite> drawable);
|
||||
void remove(std::shared_ptr<Sprite> drawable);
|
||||
void step(int elapsed);
|
||||
void checkDelete();
|
||||
|
||||
// Private functions.
|
||||
private:
|
||||
void draw(sf::RenderTarget& target, sf::RenderStates states) const;
|
||||
|
||||
// Private variables.
|
||||
private:
|
||||
std::map<Body::Category, std::vector<std::shared_ptr<Sprite> > > mDrawables;
|
||||
};
|
||||
|
||||
#endif /* DG_WORLD_H_ */
|
||||
|
|
|
@ -37,9 +37,8 @@ Body::~Body() {
|
|||
/**
|
||||
* Initializes container.
|
||||
*/
|
||||
Body::Data::Data(World& world, const Vector2f& position, float angle,
|
||||
Body::Data::Data(const Vector2f& position, float angle,
|
||||
Category category, unsigned short maskExclude) :
|
||||
world(world),
|
||||
position(position),
|
||||
angle(angle),
|
||||
category(category),
|
||||
|
|
|
@ -8,12 +8,10 @@
|
|||
#ifndef DG_BODY_H_
|
||||
#define DG_BODY_H_
|
||||
|
||||
#include "../World.h"
|
||||
#include "../types/String.h"
|
||||
#include "../types/Vector.h"
|
||||
#include "../util/Yaml.h"
|
||||
|
||||
class World;
|
||||
class Yaml;
|
||||
|
||||
/**
|
||||
|
@ -44,9 +42,7 @@ public:
|
|||
class Data {
|
||||
public:
|
||||
Data() = default;
|
||||
Data(World& world, const Vector2f& position, float angle,
|
||||
Category category, unsigned short maskExclude);
|
||||
World& world;
|
||||
Data(const Vector2f& position, float angle, Category category, unsigned short maskExclude);
|
||||
const Vector2f& position;
|
||||
float angle;
|
||||
Category category;
|
||||
|
|
|
@ -25,16 +25,15 @@ std::vector<Character*> Character::mCharacterInstances = std::vector<Character*>
|
|||
/**
|
||||
* Saves pointer to this instance in static var for think().
|
||||
*/
|
||||
Character::Character(World& world, Collection& collection, Pathfinder& pathfinder,
|
||||
Character::Character(World& world, Pathfinder& pathfinder,
|
||||
const Data& data, const Yaml& config) :
|
||||
Sprite(config, data),
|
||||
mCollection(collection),
|
||||
mPathfinder(pathfinder),
|
||||
mWorld(world),
|
||||
mPathfinder(pathfinder),
|
||||
mMaxHealth(config.get(KEY_HEALTH, DEFAULT_HEALTH)),
|
||||
mCurrentHealth(mMaxHealth),
|
||||
mMovementSpeed(config.get(KEY_SPEED, DEFAULT_SPEED)),
|
||||
mWeapon(world, collection, *this, Yaml("weapon.yaml")),
|
||||
mWeapon(world, *this, Yaml("weapon.yaml")),
|
||||
mStartPathfinding(false) {
|
||||
mCharacterInstances.push_back(this);
|
||||
}
|
||||
|
@ -47,8 +46,7 @@ Character::~Character() {
|
|||
assert(it != mCharacterInstances.end());
|
||||
mCharacterInstances.erase(it);
|
||||
|
||||
mCollection.insert(std::shared_ptr<Sprite>(new Corpse(mWorld,
|
||||
getPosition(), Yaml("body.yaml"))));
|
||||
mWorld.insert(std::shared_ptr<Sprite>(new Corpse(getPosition(), Yaml("body.yaml"))));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -30,7 +30,7 @@ class Yaml;
|
|||
class Character : public Sprite {
|
||||
// Public functions.
|
||||
public:
|
||||
Character(World& world, Collection& collection, Pathfinder& pathfinder,
|
||||
Character(World& world, Pathfinder& pathfinder,
|
||||
const Data& data, const Yaml& config);
|
||||
virtual ~Character() = 0;
|
||||
|
||||
|
@ -58,9 +58,8 @@ private:
|
|||
|
||||
static std::vector<Character*> mCharacterInstances;
|
||||
|
||||
Collection& mCollection;
|
||||
Pathfinder& mPathfinder;
|
||||
World& mWorld;
|
||||
Pathfinder& mPathfinder;
|
||||
|
||||
const int mMaxHealth;
|
||||
int mCurrentHealth; //< Current health. Between 0 and mMaxHealth.
|
||||
|
|
|
@ -25,9 +25,9 @@ const float Bullet::DEFAULT_SPEED = 500;
|
|||
* @param world Box2d world.
|
||||
* @param texture Texture to display for bullet.
|
||||
*/
|
||||
Bullet::Bullet(const Vector2f& position, World& world, Body& shooter, float direction,
|
||||
Bullet::Bullet(const Vector2f& position, Body& shooter, float direction,
|
||||
const Yaml& config) :
|
||||
Particle(config, Data(world, position, 0, CATEGORY_PARTICLE, CATEGORY_PARTICLE)),
|
||||
Particle(config, Data(position, 0, CATEGORY_PARTICLE, CATEGORY_PARTICLE)),
|
||||
mShooter(shooter),
|
||||
mDamage(config.get(KEY_DAMAGE, DEFAULT_DAMAGE)),
|
||||
mSpeed(config.get(KEY_SPEED, DEFAULT_SPEED)) {
|
||||
|
|
|
@ -21,7 +21,7 @@ class Yaml;
|
|||
class Bullet : public Particle {
|
||||
// Public functions.
|
||||
public:
|
||||
Bullet(const Vector2f& position, World& world, Body& shooter, float direction,
|
||||
Bullet(const Vector2f& position, Body& shooter, float direction,
|
||||
const Yaml& config);
|
||||
|
||||
void onCollide(Body& other, Category category);
|
||||
|
|
|
@ -11,7 +11,7 @@
|
|||
|
||||
#include <thor/Vectors.hpp>
|
||||
|
||||
#include "../util/Collection.h"
|
||||
#include "../World.h"
|
||||
#include "../effects/Bullet.h"
|
||||
|
||||
const String Weapon::KEY_BULLET = "bullet";
|
||||
|
@ -19,10 +19,10 @@ const String Weapon::DEFAULT_BULLET = "bullet.yaml";
|
|||
const String Weapon::KEY_INTERVAL = "interval";
|
||||
const int Weapon::DEFAULT_INTERVAL = 250;
|
||||
|
||||
Weapon::Weapon(World& world, Collection& collection, Body& holder, const Yaml& config) :
|
||||
Emitter(collection),
|
||||
mHolder(holder),
|
||||
Weapon::Weapon(World& world, Body& holder, const Yaml& config) :
|
||||
Emitter(world),
|
||||
mWorld(world),
|
||||
mHolder(holder),
|
||||
mBullet(config.get(KEY_BULLET, DEFAULT_BULLET)),
|
||||
mTimer(sf::milliseconds(config.get(KEY_INTERVAL, DEFAULT_INTERVAL))) {
|
||||
Vector2i holderSize = mHolder.getSize();
|
||||
|
@ -50,6 +50,6 @@ Weapon::createParticle() {
|
|||
// Minus to account for positive y-axis going downwards in SFML.
|
||||
Vector2f offset(- mOffset);
|
||||
thor::rotate(offset, mHolder.getAngle());
|
||||
return std::shared_ptr<Particle>(new Bullet(mHolder.getPosition() + offset, mWorld,
|
||||
return std::shared_ptr<Particle>(new Bullet(mHolder.getPosition() + offset,
|
||||
mHolder, mHolder.getAngle(), Yaml(mBullet)));
|
||||
}
|
||||
|
|
|
@ -29,7 +29,7 @@ class Yaml;
|
|||
class Weapon : public Emitter {
|
||||
// Public functions.
|
||||
public:
|
||||
Weapon(World& world, Collection& collection, Body& holder, const Yaml& config);
|
||||
Weapon(World& world, Body& holder, const Yaml& config);
|
||||
|
||||
void fire();
|
||||
|
||||
|
@ -44,8 +44,8 @@ private:
|
|||
static const String KEY_INTERVAL;
|
||||
static const int DEFAULT_INTERVAL;
|
||||
|
||||
Body& mHolder;
|
||||
World& mWorld;
|
||||
Body& mHolder;
|
||||
|
||||
Vector2f mOffset; //< Offset to the point where bullets are inserted (from holder center).
|
||||
const String mBullet;
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
|
||||
#include "Emitter.h"
|
||||
|
||||
Emitter::Emitter(Collection& collection) :
|
||||
Emitter::Emitter(World& collection) :
|
||||
mCollection(collection) {
|
||||
}
|
||||
|
||||
|
|
|
@ -9,17 +9,17 @@
|
|||
#define DG_EMITTER_H_
|
||||
|
||||
#include "../abstract/Body.h"
|
||||
#include "../util/Collection.h"
|
||||
#include "../World.h"
|
||||
#include "Particle.h"
|
||||
|
||||
class Body;
|
||||
class Collection;
|
||||
class World;
|
||||
class Particle;
|
||||
|
||||
class Emitter {
|
||||
// Public functions.
|
||||
public:
|
||||
Emitter(Collection& collection);
|
||||
Emitter(World& collection);
|
||||
virtual ~Emitter();
|
||||
|
||||
// Protected functions.
|
||||
|
@ -30,7 +30,7 @@ protected:
|
|||
|
||||
// Private variables.
|
||||
private:
|
||||
Collection& mCollection;
|
||||
World& mCollection;
|
||||
};
|
||||
|
||||
#endif /* DG_EMITTER_H_ */
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
|
||||
#include "Corpse.h"
|
||||
|
||||
Corpse::Corpse(World& world, const Vector2f& position, const Yaml& config) :
|
||||
Sprite(config, Data(world, position, 0, CATEGORY_NONSOLID, MASK_NONE)) {
|
||||
Corpse::Corpse(const Vector2f& position, const Yaml& config) :
|
||||
Sprite(config, Data(position, 0, CATEGORY_NONSOLID, MASK_NONE)) {
|
||||
}
|
||||
|
||||
|
|
|
@ -17,7 +17,7 @@ class Yaml;
|
|||
class Corpse : public Sprite {
|
||||
// Public functions.
|
||||
public:
|
||||
Corpse(World& world, const Vector2f& position, const Yaml& config);
|
||||
Corpse(const Vector2f& position, const Yaml& config);
|
||||
};
|
||||
|
||||
#endif /* DG_CORPSE_H_ */
|
||||
|
|
|
@ -9,13 +9,11 @@
|
|||
|
||||
#include "Corpse.h"
|
||||
|
||||
Enemy::Enemy(World& world, Collection& collection, Pathfinder& pathfinder,
|
||||
Enemy::Enemy(World& collection, Pathfinder& pathfinder,
|
||||
const Vector2f& position, const Yaml& config) :
|
||||
Character(world, collection, pathfinder,
|
||||
Data(world, position, 0, CATEGORY_ACTOR, MASK_ALL),
|
||||
config),
|
||||
mWorld(world),
|
||||
mCollection(collection) {
|
||||
Character(collection, pathfinder,
|
||||
Data(position, 0, CATEGORY_ACTOR, MASK_ALL),
|
||||
config) {
|
||||
}
|
||||
|
||||
void
|
||||
|
|
|
@ -8,32 +8,25 @@
|
|||
#ifndef DG_ENEMY_H_
|
||||
#define DG_ENEMY_H
|
||||
|
||||
#include "../World.h"
|
||||
#include "../abstract/Character.h"
|
||||
#include "../util/Collection.h"
|
||||
#include "../World.h"
|
||||
#include "../types/Vector.h"
|
||||
#include "../util/Yaml.h"
|
||||
|
||||
class World;
|
||||
class Character;
|
||||
class Collection;
|
||||
class World;
|
||||
class Instances;
|
||||
class Yaml;
|
||||
|
||||
class Enemy : public Character {
|
||||
// Public functions.
|
||||
public:
|
||||
Enemy(World& world, Collection& collection, Pathfinder& pathfinder,
|
||||
Enemy(World& collection, Pathfinder& pathfinder,
|
||||
const Vector2f& position, const Yaml& config);
|
||||
|
||||
// Private functions.
|
||||
private:
|
||||
void onThink(float elapsedTime);
|
||||
|
||||
// Private variablese.
|
||||
private:
|
||||
World& mWorld;
|
||||
Collection& mCollection;
|
||||
};
|
||||
|
||||
#endif /* DG_ENEMY_H_ */
|
||||
|
|
|
@ -16,10 +16,10 @@
|
|||
/**
|
||||
* Initializes Sprite.
|
||||
*/
|
||||
Player::Player(World& world, Collection& collection, Pathfinder& pathfinder,
|
||||
Player::Player(World& world, Pathfinder& pathfinder,
|
||||
const Vector2f& position, const Yaml& config) :
|
||||
Character(world, collection, pathfinder,
|
||||
Data(world, position, 0, CATEGORY_ACTOR, MASK_ALL),
|
||||
Character(world, pathfinder,
|
||||
Data(position, 0, CATEGORY_ACTOR, MASK_ALL),
|
||||
config),
|
||||
mDirection(0) {
|
||||
}
|
||||
|
|
|
@ -41,7 +41,7 @@ public:
|
|||
|
||||
// Public functions.
|
||||
public:
|
||||
Player(World& world, Collection& collection, Pathfinder& pathfinder,
|
||||
Player(World& world, Pathfinder& pathfinder,
|
||||
const Vector2f& position, const Yaml& config);
|
||||
|
||||
void setCrosshairPosition(const Vector2f& position);
|
||||
|
|
|
@ -32,8 +32,8 @@ TileManager::TileManager(World& world) :
|
|||
* @param pPosition Position of the tile in tile coordinates.
|
||||
* @param world Box2D world object.
|
||||
*/
|
||||
TileManager::Tile::Tile(Type type, const TilePosition& position, World& world) :
|
||||
Sprite(Yaml(getConfig(type)), Data(world,
|
||||
TileManager::Tile::Tile(Type type, const TilePosition& position) :
|
||||
Sprite(Yaml(getConfig(type)), Data(
|
||||
Vector2f(position.x * TILE_SIZE.x, position.y * TILE_SIZE.y), 0,
|
||||
CATEGORY_WORLD, (type == Type::FLOOR) ? MASK_NONE : MASK_ALL)),
|
||||
mType(type) {
|
||||
|
@ -90,7 +90,9 @@ TileManager::setTile(const TilePosition& position, Type type) {
|
|||
mTiles.erase(it);
|
||||
}
|
||||
}
|
||||
mTiles.push_back(std::unique_ptr<Tile>(new Tile(type, position, mWorld)));
|
||||
std::shared_ptr<Tile> tile = std::shared_ptr<Tile>(new Tile(type, position));
|
||||
mTiles.push_back(tile);
|
||||
mWorld.insert(tile);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -54,7 +54,7 @@ private:
|
|||
// Private variables.
|
||||
private:
|
||||
World& mWorld;
|
||||
std::vector<std::unique_ptr<Tile> > mTiles;
|
||||
std::vector<std::shared_ptr<Tile> > mTiles;
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -63,7 +63,7 @@ private:
|
|||
class TileManager::Tile : public Sprite {
|
||||
// Public functions.
|
||||
public:
|
||||
Tile(Type type, const TilePosition& position, World& world);
|
||||
Tile(Type type, const TilePosition& position);
|
||||
|
||||
Type getType() const;
|
||||
TilePosition getTilePosition() const;
|
||||
|
|
|
@ -1,35 +0,0 @@
|
|||
/*
|
||||
* Instances.h
|
||||
*
|
||||
* Created on: 04.10.2012
|
||||
* Author: Felix
|
||||
*/
|
||||
|
||||
#ifndef DG_INSTANCES_H_
|
||||
#define DG_INSTANCES_H_
|
||||
|
||||
#include "../World.h"
|
||||
#include "../sprites/TileManager.h"
|
||||
#include "../util/Collection.h"
|
||||
#include "../util/Pathfinder.h"
|
||||
|
||||
class World;
|
||||
class Pathfinder;
|
||||
class TileManager;
|
||||
class Collection;
|
||||
|
||||
/**
|
||||
* POD class that holds instances of major classes used by other objects.
|
||||
*/
|
||||
struct Instances {
|
||||
Instances() = default;
|
||||
Instances(Pathfinder& p, TileManager& t, Collection& c, World& w) :
|
||||
pathfinder(p), tilemanager(t), collection(c), world(w) {};
|
||||
|
||||
Pathfinder& pathfinder;
|
||||
TileManager& tilemanager;
|
||||
Collection& collection;
|
||||
World& world;
|
||||
};
|
||||
|
||||
#endif /* DG_INSTANCES_H_ */
|
|
@ -1,66 +0,0 @@
|
|||
/*
|
||||
* Collection.cpp
|
||||
*
|
||||
* Created on: 29.08.2012
|
||||
* Author: Felix
|
||||
*/
|
||||
|
||||
#include "Collection.h"
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
/**
|
||||
* Insert a drawable into the group. Drawables should only be handled with shared_ptr.
|
||||
* An object can't be inserted more than once at the same level.
|
||||
*/
|
||||
void
|
||||
Collection::insert(std::shared_ptr<Sprite> drawable) {
|
||||
Body::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);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes a drawable from the group.
|
||||
*/
|
||||
void
|
||||
Collection::remove(std::shared_ptr<Sprite> drawable) {
|
||||
for (auto v = mDrawables.begin(); v != mDrawables.end(); v++) {
|
||||
auto item = std::find(v->second.begin(), v->second.end(), drawable);
|
||||
if (item != v->second.end()) {
|
||||
v->second.erase(item);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes any sprites which return true for getDelete().
|
||||
*/
|
||||
void
|
||||
Collection::checkDelete() {
|
||||
for (auto v = mDrawables.begin(); v != mDrawables.end(); v++) {
|
||||
for (auto item = v->second.begin(); item != v->second.end(); ) {
|
||||
if ((*item)->getDelete()) {
|
||||
item = v->second.erase(item);
|
||||
}
|
||||
else {
|
||||
item++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Draws all elements in the group.
|
||||
*/
|
||||
void
|
||||
Collection::draw(sf::RenderTarget& target, sf::RenderStates states) const {
|
||||
for (auto v = mDrawables.begin(); v != mDrawables.end(); v++) {
|
||||
for (auto item : v->second) {
|
||||
target.draw(static_cast<sf::Drawable&>(*item), states);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,42 +0,0 @@
|
|||
/*
|
||||
* Collection.h
|
||||
*
|
||||
* Created on: 29.08.2012
|
||||
* Author: Felix
|
||||
*/
|
||||
|
||||
#ifndef DG_COLLECTION_H_
|
||||
#define DG_COLLECTION_H_
|
||||
|
||||
#include <map>
|
||||
#include <vector>
|
||||
|
||||
#include <SFML/Graphics.hpp>
|
||||
|
||||
#include "../abstract/Sprite.h"
|
||||
|
||||
class Sprite;
|
||||
|
||||
/**
|
||||
* A collection of sprites, which can be put into different layers.
|
||||
*
|
||||
* Uses Sprite instead of sf::Drawable to also manage deleting objects.
|
||||
* Render order is determined by Physical::Category (higher number on top).
|
||||
*/
|
||||
class Collection : public sf::Drawable {
|
||||
// Public functions.
|
||||
public:
|
||||
void insert(std::shared_ptr<Sprite> drawable);
|
||||
void remove(std::shared_ptr<Sprite> drawable);
|
||||
void checkDelete();
|
||||
|
||||
// Private functions.
|
||||
private:
|
||||
void draw(sf::RenderTarget& target, sf::RenderStates states) const;
|
||||
|
||||
// Private variables.
|
||||
private:
|
||||
std::map<Body::Category, std::vector<std::shared_ptr<Sprite> > > mDrawables;
|
||||
};
|
||||
|
||||
#endif /* DG_COLLECTION_H_ */
|
Reference in a new issue