From 637ea180c380d8216e7b207527490d50ceccd5cf Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Sun, 3 Mar 2013 21:26:29 +0100 Subject: [PATCH] Replaced setTile with insertTile and removeTile. --- source/Game.cpp | 23 +++++++++++++++-------- source/sprites/TileManager.cpp | 20 ++++++++++++++++++-- source/sprites/TileManager.h | 3 ++- 3 files changed, 35 insertions(+), 11 deletions(-) diff --git a/source/Game.cpp b/source/Game.cpp index 798dc8b..8ba2da1 100644 --- a/source/Game.cpp +++ b/source/Game.cpp @@ -39,16 +39,23 @@ Game::Game(sf::RenderWindow& window) : */ void Game::generate() { - for (int x = 0; x < 10; x++) - for (int y = 0; y < 10; y++) - mTileManager.setTile(TileManager::TilePosition(x, y), TileManager::Type::WALL); + for (int x = 0; x < 11; x++) + mTileManager.insertTile(TileManager::TilePosition(x, 0), TileManager::Type::WALL); + for (int x = 0; x < 11; x++) + mTileManager.insertTile(TileManager::TilePosition(x, 10), TileManager::Type::WALL); + for (int y = 1; y < 9; y++) + mTileManager.insertTile(TileManager::TilePosition(0, y), TileManager::Type::WALL); + for (int y = 1; y < 9; y++) + mTileManager.insertTile(TileManager::TilePosition(10, y), TileManager::Type::WALL); - for (int x = 1; x < 9; x++) - for (int y = 1; y < 9; y++) - mTileManager.setTile(TileManager::TilePosition(x, y), TileManager::Type::FLOOR); + for (int x = 1; x < 10; x++) + for (int y = 1; y < 10; y++) + mTileManager.insertTile(TileManager::TilePosition(x, y), TileManager::Type::FLOOR); - for (int x = 1; x < 5; x++) - mTileManager.setTile(TileManager::TilePosition(x, 4), TileManager::Type::WALL); + for (int x = 1; x < 5; x++) { + mTileManager.removeTile(TileManager::TilePosition(x, 4)); + mTileManager.insertTile(TileManager::TilePosition(x, 4), TileManager::Type::WALL); + } mWorld.insert(std::shared_ptr(new Enemy(mWorld, mPathfinder, sf::Vector2f(400.0f, 200.0f), Yaml("enemy.yaml")))); diff --git a/source/sprites/TileManager.cpp b/source/sprites/TileManager.cpp index 040b3da..dd47b17 100644 --- a/source/sprites/TileManager.cpp +++ b/source/sprites/TileManager.cpp @@ -7,6 +7,7 @@ #include "TileManager.h" +#include #include #include @@ -85,17 +86,32 @@ TileManager::Tile::getTilePosition() const { * @param type Type of tile to be inserted. */ void -TileManager::setTile(const TilePosition& position, Type type) { +TileManager::insertTile(const TilePosition& position, Type type) { + #ifndef NDEBUG for (auto it = mTiles.begin(); it != mTiles.end(); it++) { if ((*it)->getTilePosition() == position) { - mTiles.erase(it); + // Inserted multiple tiles at the same position. + assert(false); } } + #endif std::shared_ptr tile = std::shared_ptr(new Tile(type, position)); mTiles.push_back(tile); mWorld.insert(tile); } + +void +TileManager::removeTile(const TilePosition& position) { + for (auto it = mTiles.begin(); it != mTiles.end(); it++) { + if ((*it)->getTilePosition() == position) { + mWorld.remove(*it); + mTiles.erase(it); + } + } + +} + /** * \copydoc sf::Drawable::draw */ diff --git a/source/sprites/TileManager.h b/source/sprites/TileManager.h index 947839f..78e68c8 100644 --- a/source/sprites/TileManager.h +++ b/source/sprites/TileManager.h @@ -40,7 +40,8 @@ public: public: TileManager(World& world); - void setTile(const TilePosition& position, Type type); + void insertTile(const TilePosition& position, Type type); + void removeTile(const TilePosition& position); // Private types. private: