diff --git a/source/Game.cpp b/source/Game.cpp index 446972e..33783e6 100644 --- a/source/Game.cpp +++ b/source/Game.cpp @@ -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(new Enemy(mWorld, mCollection, mPathfinder, + mWorld.insert(std::shared_ptr(new Enemy(mWorld, mPathfinder, Vector2f(400.0f, 200.0f), Yaml("enemy.yaml")))); - mPlayer = std::unique_ptr(new Player(mWorld, mCollection, mPathfinder, + mPlayer = std::shared_ptr(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()); diff --git a/source/Game.h b/source/Game.h index 5f3d50a..c8fd530 100644 --- a/source/Game.h +++ b/source/Game.h @@ -13,16 +13,14 @@ #include -#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 mPlayer; + std::shared_ptr mPlayer; bool mQuit; bool mPaused; diff --git a/source/World.cpp b/source/World.cpp old mode 100644 new mode 100755 index bafd7e5..a6d0c24 --- a/source/World.cpp +++ b/source/World.cpp @@ -1,23 +1,70 @@ -/* - * World.cpp - * - * Created on: 22.12.2012 - * Author: Felix - */ - -#include "World.h" - -World::World() { - // TODO Auto-generated constructor stub - -} - -World::~World() { - // TODO Auto-generated destructor stub -} - - -void -World::step() { - -} +/* + * World.cpp + * + * Created on: 29.08.2012 + * Author: Felix + */ + +#include "World.h" + +#include + +/** + * 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 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 +World::remove(std::shared_ptr 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(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(*item), states); + } + } +} + diff --git a/source/World.h b/source/World.h old mode 100644 new mode 100755 index 166aeed..311449f --- a/source/World.h +++ b/source/World.h @@ -1,19 +1,45 @@ -/* - * World.h - * - * Created on: 22.12.2012 - * Author: Felix - */ - -#ifndef DG_WORLD_H_ -#define DG_WORLD_H_ - -class World { -public: - World(); - ~World(); - - void step(); -}; - -#endif /* DG_WORLD_H_ */ +/* + * World.h + * + * Created on: 29.08.2012 + * Author: Felix + */ + +#ifndef DG_WORLD_H_ +#define DG_WORLD_H_ + +#include +#include + +#include + +#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 drawable); + void remove(std::shared_ptr drawable); + void step(int elapsed); + void checkDelete(); + +// Private functions. +private: + void draw(sf::RenderTarget& target, sf::RenderStates states) const; + +// Private variables. +private: + std::map > > mDrawables; +}; + +#endif /* DG_WORLD_H_ */ diff --git a/source/abstract/Body.cpp b/source/abstract/Body.cpp index 77a25e2..8be5c53 100755 --- a/source/abstract/Body.cpp +++ b/source/abstract/Body.cpp @@ -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), diff --git a/source/abstract/Body.h b/source/abstract/Body.h index 6160217..00b69c6 100755 --- a/source/abstract/Body.h +++ b/source/abstract/Body.h @@ -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; diff --git a/source/abstract/Character.cpp b/source/abstract/Character.cpp index f8f3aa9..ca1f3bf 100644 --- a/source/abstract/Character.cpp +++ b/source/abstract/Character.cpp @@ -25,16 +25,15 @@ std::vector Character::mCharacterInstances = std::vector /** * 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(new Corpse(mWorld, - getPosition(), Yaml("body.yaml")))); + mWorld.insert(std::shared_ptr(new Corpse(getPosition(), Yaml("body.yaml")))); } /** diff --git a/source/abstract/Character.h b/source/abstract/Character.h index 12ece5f..0c08b69 100644 --- a/source/abstract/Character.h +++ b/source/abstract/Character.h @@ -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 mCharacterInstances; - Collection& mCollection; - Pathfinder& mPathfinder; World& mWorld; + Pathfinder& mPathfinder; const int mMaxHealth; int mCurrentHealth; //< Current health. Between 0 and mMaxHealth. diff --git a/source/effects/Bullet.cpp b/source/effects/Bullet.cpp index 39c6903..8d8ba8f 100755 --- a/source/effects/Bullet.cpp +++ b/source/effects/Bullet.cpp @@ -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)) { diff --git a/source/effects/Bullet.h b/source/effects/Bullet.h index cd2481b..5bf1c2f 100755 --- a/source/effects/Bullet.h +++ b/source/effects/Bullet.h @@ -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); diff --git a/source/items/Weapon.cpp b/source/items/Weapon.cpp index 79e563d..88affdd 100755 --- a/source/items/Weapon.cpp +++ b/source/items/Weapon.cpp @@ -11,7 +11,7 @@ #include -#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(new Bullet(mHolder.getPosition() + offset, mWorld, + return std::shared_ptr(new Bullet(mHolder.getPosition() + offset, mHolder, mHolder.getAngle(), Yaml(mBullet))); } diff --git a/source/items/Weapon.h b/source/items/Weapon.h index 12a84cf..4021ae6 100755 --- a/source/items/Weapon.h +++ b/source/items/Weapon.h @@ -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; diff --git a/source/particle/Emitter.cpp b/source/particle/Emitter.cpp index 5901f60..6118a8a 100755 --- a/source/particle/Emitter.cpp +++ b/source/particle/Emitter.cpp @@ -7,7 +7,7 @@ #include "Emitter.h" -Emitter::Emitter(Collection& collection) : +Emitter::Emitter(World& collection) : mCollection(collection) { } diff --git a/source/particle/Emitter.h b/source/particle/Emitter.h index 1e5f214..6107ca7 100755 --- a/source/particle/Emitter.h +++ b/source/particle/Emitter.h @@ -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_ */ diff --git a/source/sprites/Corpse.cpp b/source/sprites/Corpse.cpp index 4b3eb2e..05ea2f8 100644 --- a/source/sprites/Corpse.cpp +++ b/source/sprites/Corpse.cpp @@ -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)) { } diff --git a/source/sprites/Corpse.h b/source/sprites/Corpse.h index 6e7bc80..b453301 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(World& world, const Vector2f& position, const Yaml& config); + Corpse(const Vector2f& position, const Yaml& config); }; #endif /* DG_CORPSE_H_ */ diff --git a/source/sprites/Enemy.cpp b/source/sprites/Enemy.cpp index 2271f60..282771f 100644 --- a/source/sprites/Enemy.cpp +++ b/source/sprites/Enemy.cpp @@ -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 diff --git a/source/sprites/Enemy.h b/source/sprites/Enemy.h index 1c568d5..acf5ac2 100644 --- a/source/sprites/Enemy.h +++ b/source/sprites/Enemy.h @@ -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_ */ diff --git a/source/sprites/Player.cpp b/source/sprites/Player.cpp index f70a2fb..8703020 100644 --- a/source/sprites/Player.cpp +++ b/source/sprites/Player.cpp @@ -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) { } diff --git a/source/sprites/Player.h b/source/sprites/Player.h index e1dde55..58d3742 100644 --- a/source/sprites/Player.h +++ b/source/sprites/Player.h @@ -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); diff --git a/source/sprites/TileManager.cpp b/source/sprites/TileManager.cpp index 2084039..e1f8a7c 100644 --- a/source/sprites/TileManager.cpp +++ b/source/sprites/TileManager.cpp @@ -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(new Tile(type, position, mWorld))); + std::shared_ptr tile = std::shared_ptr(new Tile(type, position)); + mTiles.push_back(tile); + mWorld.insert(tile); } /** diff --git a/source/sprites/TileManager.h b/source/sprites/TileManager.h index 659e4dc..7d22850 100644 --- a/source/sprites/TileManager.h +++ b/source/sprites/TileManager.h @@ -54,7 +54,7 @@ private: // Private variables. private: World& mWorld; - std::vector > mTiles; + std::vector > 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; diff --git a/source/types/Instances.h b/source/types/Instances.h deleted file mode 100644 index 8a4bbef..0000000 --- a/source/types/Instances.h +++ /dev/null @@ -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_ */ diff --git a/source/util/Collection.cpp b/source/util/Collection.cpp deleted file mode 100755 index c7238fd..0000000 --- a/source/util/Collection.cpp +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Collection.cpp - * - * Created on: 29.08.2012 - * Author: Felix - */ - -#include "Collection.h" - -#include - -/** - * 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 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 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(*item), states); - } - } -} - diff --git a/source/util/Collection.h b/source/util/Collection.h deleted file mode 100755 index aebdc81..0000000 --- a/source/util/Collection.h +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Collection.h - * - * Created on: 29.08.2012 - * Author: Felix - */ - -#ifndef DG_COLLECTION_H_ -#define DG_COLLECTION_H_ - -#include -#include - -#include - -#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 drawable); - void remove(std::shared_ptr drawable); - void checkDelete(); - -// Private functions. -private: - void draw(sf::RenderTarget& target, sf::RenderStates states) const; - -// Private variables. -private: - std::map > > mDrawables; -}; - -#endif /* DG_COLLECTION_H_ */