Replaced setTile with insertTile and removeTile.

This commit is contained in:
Felix Ableitner 2013-03-03 21:26:29 +01:00
parent f0a9c3320d
commit 637ea180c3
3 changed files with 35 additions and 11 deletions

View file

@ -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<Sprite>(new Enemy(mWorld, mPathfinder,
sf::Vector2f(400.0f, 200.0f), Yaml("enemy.yaml"))));

View file

@ -7,6 +7,7 @@
#include "TileManager.h"
#include <assert.h>
#include <string>
#include <Thor/Resources.hpp>
@ -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> tile = std::shared_ptr<Tile>(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
*/

View file

@ -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: