2012-10-14 16:14:06 +00:00
|
|
|
/*
|
|
|
|
* TileManager.cpp
|
|
|
|
*
|
|
|
|
* Created on: 08.08.2012
|
|
|
|
* Author: Felix
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "TileManager.h"
|
|
|
|
|
2013-04-04 20:44:15 +00:00
|
|
|
#include <Thor/Vectors.hpp>
|
|
|
|
|
|
|
|
#include "../util/Interval.h"
|
2012-10-14 16:14:06 +00:00
|
|
|
#include "../util/Loader.h"
|
2013-03-29 16:59:35 +00:00
|
|
|
#include "../util/Yaml.h"
|
2013-03-29 17:34:51 +00:00
|
|
|
#include "../World.h"
|
2012-10-14 16:14:06 +00:00
|
|
|
|
2013-04-27 17:42:44 +00:00
|
|
|
const sf::Vector2i TileManager::TILE_SIZE = sf::Vector2i(75, 75);
|
2012-10-14 16:14:06 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Loads tile resources.
|
|
|
|
*
|
|
|
|
* @param world Box2D world to create (physical) tiles in.
|
|
|
|
*/
|
2012-12-22 00:14:30 +00:00
|
|
|
TileManager::TileManager(World& world) :
|
2012-09-09 20:50:15 +00:00
|
|
|
mWorld(world) {
|
2012-10-14 16:14:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2012-12-22 13:56:17 +00:00
|
|
|
TileManager::Tile::Tile(Type type, const TilePosition& position) :
|
2012-12-23 14:50:49 +00:00
|
|
|
Sprite(Data(sf::Vector2f(position.x * TILE_SIZE.x, position.y * TILE_SIZE.y),
|
2013-03-30 01:36:06 +00:00
|
|
|
CATEGORY_WORLD, (type == Type::FLOOR) ? MASK_NONE : MASK_ALL),
|
2012-12-23 14:50:49 +00:00
|
|
|
Yaml(getConfig(type))),
|
2012-10-14 16:14:06 +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-12-22 14:10:26 +00:00
|
|
|
std::string
|
2012-10-14 16:14:06 +00:00
|
|
|
TileManager::Tile::getConfig(Type type) {
|
2012-12-22 14:10:26 +00:00
|
|
|
std::string filename;
|
2012-10-14 16:14:06 +00:00
|
|
|
switch (type) {
|
|
|
|
case Type::FLOOR:
|
|
|
|
filename = "tile_floor.yaml";
|
|
|
|
break;
|
|
|
|
case Type::WALL:
|
|
|
|
filename = "tile_wall.yaml";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new aurora::Exception("Invalid tile type.");
|
|
|
|
}
|
|
|
|
return filename;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the Type of this tile.
|
2012-09-09 20:50:15 +00:00
|
|
|
*/
|
2012-10-14 16:14:06 +00:00
|
|
|
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 {
|
2012-09-09 20:50:15 +00:00
|
|
|
return TilePosition(getPosition().x / TILE_SIZE.x, getPosition().y / TILE_SIZE.y);
|
2012-10-14 16:14:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2012-09-09 20:50:15 +00:00
|
|
|
void
|
2013-03-03 20:26:29 +00:00
|
|
|
TileManager::insertTile(const TilePosition& position, Type type) {
|
|
|
|
#ifndef NDEBUG
|
2012-10-14 16:14:06 +00:00
|
|
|
for (auto it = mTiles.begin(); it != mTiles.end(); it++) {
|
2013-04-04 21:13:08 +00:00
|
|
|
if ((*it)->getTilePosition() == position)
|
2013-03-03 20:26:29 +00:00
|
|
|
// Inserted multiple tiles at the same position.
|
|
|
|
assert(false);
|
2012-10-14 16:14:06 +00:00
|
|
|
}
|
2013-03-03 20:26:29 +00:00
|
|
|
#endif
|
2012-12-22 13:56:17 +00:00
|
|
|
std::shared_ptr<Tile> tile = std::shared_ptr<Tile>(new Tile(type, position));
|
|
|
|
mTiles.push_back(tile);
|
|
|
|
mWorld.insert(tile);
|
2012-10-14 16:14:06 +00:00
|
|
|
}
|
|
|
|
|
2013-04-04 20:44:15 +00:00
|
|
|
/*
|
|
|
|
* Performs a raycast between two points to check if the path between them is
|
|
|
|
* clear of walls.
|
|
|
|
*
|
|
|
|
* @param lineStart First point of the line to test.
|
|
|
|
* @param lineEnd Second point of the line to test.
|
|
|
|
* @return True if the ray was not blocked.
|
|
|
|
*/
|
|
|
|
bool
|
|
|
|
TileManager::raycast(const sf::Vector2f& lineStart,
|
|
|
|
const sf::Vector2f& lineEnd) const {
|
|
|
|
assert(lineStart != lineEnd);
|
|
|
|
sf::Vector2f lineCenter = lineStart + 0.5f * (lineEnd - lineStart);
|
|
|
|
for (auto it : mTiles) {
|
2013-04-04 21:13:08 +00:00
|
|
|
if (it->getType() == Type::FLOOR)
|
2013-04-04 20:44:15 +00:00
|
|
|
continue;
|
|
|
|
sf::Vector2f axis = it->getPosition() - lineCenter;
|
2013-04-04 21:13:08 +00:00
|
|
|
if (axis == sf::Vector2f())
|
2013-04-04 20:44:15 +00:00
|
|
|
return false;
|
2013-03-03 20:26:29 +00:00
|
|
|
|
2013-04-04 20:44:15 +00:00
|
|
|
axis = thor::unitVector(axis);
|
|
|
|
sf::Vector2f halfsize = it->getSize() / 2.0f;
|
|
|
|
float rectPosProjected = thor::dotProduct(axis, it->getPosition());
|
|
|
|
float lineStartProjected = thor::dotProduct(axis, lineStart);
|
|
|
|
float lineEndProjected = thor::dotProduct(axis, lineEnd);
|
|
|
|
// For corner projections, those on the same line with the rect
|
|
|
|
// center are equal by value, so we only need one on each axis
|
|
|
|
// and take the maximum.
|
|
|
|
float rectHalfWidthProjected = std::max(
|
|
|
|
abs(thor::dotProduct(axis, halfsize)),
|
|
|
|
abs(thor::dotProduct(axis,
|
|
|
|
sf::Vector2f(halfsize.x, -halfsize.y))));
|
|
|
|
Interval line = Interval::IntervalFromPoints(lineStartProjected, lineEndProjected);
|
|
|
|
Interval rect = Interval::IntervalFromRadius(rectPosProjected, rectHalfWidthProjected);
|
|
|
|
// Allow movement if sprites are moving apart.
|
2013-04-04 21:13:08 +00:00
|
|
|
if (line.getOverlap(rect).getLength() > 0.0f)
|
2013-04-04 20:44:15 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2013-03-03 20:26:29 +00:00
|
|
|
}
|
|
|
|
|
2012-10-14 16:14:06 +00:00
|
|
|
/**
|
|
|
|
* \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);
|
|
|
|
}
|
|
|
|
}
|