2012-09-09 20:50:15 +00:00
|
|
|
/*
|
|
|
|
* TileManager.cpp
|
|
|
|
*
|
|
|
|
* Created on: 08.08.2012
|
|
|
|
* Author: Felix
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "TileManager.h"
|
|
|
|
|
|
|
|
#include <Thor/Resources.hpp>
|
|
|
|
|
2012-10-11 21:24:22 +00:00
|
|
|
#include "abstract/Sprite.h"
|
2012-09-09 20:50:15 +00:00
|
|
|
#include "util/Loader.h"
|
|
|
|
#include "util/ResourceManager.h"
|
2012-10-11 21:24:22 +00:00
|
|
|
#include "util/String.h"
|
2012-09-09 20:50:15 +00:00
|
|
|
|
|
|
|
const Vector2i TileManager::TILE_SIZE = Vector2i(100, 100);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Loads tile resources.
|
|
|
|
*
|
|
|
|
* @param world Box2D world to create (physical) tiles in.
|
|
|
|
*/
|
|
|
|
TileManager::TileManager(b2World& world) :
|
|
|
|
mWorld(world) {
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructs a tile.
|
|
|
|
*
|
|
|
|
* @param pType Type of the tile to create.
|
|
|
|
* @param pPosition Position of the tile in tile coordinates.
|
|
|
|
* @param world Box2D world object.
|
|
|
|
*/
|
|
|
|
TileManager::Tile::Tile(Type type, const TilePosition& position, b2World& world) :
|
2012-10-13 10:48:16 +00:00
|
|
|
Sprite(Yaml(getConfig(type)), PhysicalData(Vector2f(position.x * TILE_SIZE.x, position.y * TILE_SIZE.y),
|
2012-10-13 10:22:18 +00:00
|
|
|
world, CATEGORY_WORLD, (type == Type::FLOOR) ? MASK_NONE : MASK_ALL, false)),
|
2012-09-09 20:50:15 +00:00
|
|
|
mType(type) {
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a texture key for a certain tile type.
|
|
|
|
*
|
|
|
|
* @param type The type of tile to load a resource key for.
|
|
|
|
* @return Resource key to the correct texture.
|
|
|
|
*/
|
2012-10-13 10:48:16 +00:00
|
|
|
String
|
|
|
|
TileManager::Tile::getConfig(Type type) {
|
2012-10-11 21:24:22 +00:00
|
|
|
String filename;
|
2012-09-09 20:50:15 +00:00
|
|
|
switch (type) {
|
2012-10-01 07:41:38 +00:00
|
|
|
case Type::FLOOR:
|
2012-10-13 10:48:16 +00:00
|
|
|
filename = "tile_floor.yaml";
|
2012-09-09 20:50:15 +00:00
|
|
|
break;
|
2012-10-01 07:41:38 +00:00
|
|
|
case Type::WALL:
|
2012-10-13 10:48:16 +00:00
|
|
|
filename = "tile_wall.yaml";
|
2012-09-09 20:50:15 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new aurora::Exception("Invalid tile type.");
|
|
|
|
}
|
2012-10-13 10:48:16 +00:00
|
|
|
return filename;
|
2012-09-09 20:50:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the Type of this tile.
|
|
|
|
*/
|
|
|
|
TileManager::Type
|
|
|
|
TileManager::Tile::getType() const {
|
|
|
|
return mType;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the position of the tile with tile width/height as a unit.
|
|
|
|
*/
|
|
|
|
TileManager::TilePosition
|
|
|
|
TileManager::Tile::getTilePosition() const {
|
|
|
|
return TilePosition(getPosition().x / TILE_SIZE.x, getPosition().y / TILE_SIZE.y);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Insert a tile at the position. Deletes an existing tile first if one is at the position.
|
|
|
|
*
|
|
|
|
* @param position Grid coordinate of the tile (not pixel coordinate).
|
|
|
|
* @param type Type of tile to be inserted.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
TileManager::setTile(const TilePosition& position, Type type) {
|
|
|
|
for (auto it = mTiles.begin(); it != mTiles.end(); it++) {
|
|
|
|
if ((*it)->getTilePosition() == position) {
|
|
|
|
mTiles.erase(it);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mTiles.push_back(std::unique_ptr<Tile>(new Tile(type, position, mWorld)));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \copydoc sf::Drawable::draw
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
TileManager::draw(sf::RenderTarget& target, sf::RenderStates states) const {
|
|
|
|
for (auto it = mTiles.begin(); it != mTiles.end(); it++) {
|
|
|
|
target.draw((**it), states);
|
|
|
|
}
|
|
|
|
}
|