Skip to content

Commit

Permalink
TileLayer: add getGid(fge::Vector2f position)
Browse files Browse the repository at this point in the history
  • Loading branch information
JonathSpirit committed Dec 27, 2024
1 parent eb062fb commit 36687f2
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 19 deletions.
17 changes: 8 additions & 9 deletions includes/FastEngine/C_tilelayer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,27 +173,26 @@ class FGE_API TileLayer : public fge::Transformable, public fge::Drawable
/**
* \brief Shortcut to set a global tile id and a new tileset
*
* \param x The x position of the tile
* \param y The y position of the tile
* \param position The position of the tile
* \param tileSets The list of tilesets
* \param gid The global tile id
*/
void setGid(std::size_t x, std::size_t y, std::span<std::shared_ptr<TileSet>> tileSets, GlobalTileId gid);
void setGid(fge::Vector2size position, std::span<std::shared_ptr<TileSet>> tileSets, GlobalTileId gid);
[[nodiscard]] GlobalTileId getGid(fge::Vector2size position);
[[nodiscard]] GlobalTileId getGid(fge::Vector2f position);
/**
* \brief Shortcut to set a global tile id
*
* \param x The x position of the tile
* \param y The y position of the tile
* \param position The position of the tile
* \param gid The global tile id
*/
void setGid(std::size_t x, std::size_t y, GlobalTileId gid);
void setGid(fge::Vector2size position, GlobalTileId gid);
/**
* \brief Set the tiles matrix size
*
* \param x The x size of the matrix
* \param y The y size of the matrix
* \param size The size of the matrix
*/
void setGridSize(std::size_t x, std::size_t y);
void setGridSize(fge::Vector2size size);

/**
* \brief Refresh all tiles with a list of tilesets
Expand Down
46 changes: 36 additions & 10 deletions sources/C_tilelayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,34 +156,60 @@ fge::Matrix<TileLayer::Tile> const& TileLayer::getTiles() const
{
return this->g_tiles;
}
void TileLayer::setGid(std::size_t x, std::size_t y, std::span<std::shared_ptr<TileSet>> tileSets, GlobalTileId gid)
void TileLayer::setGid(fge::Vector2size position, std::span<std::shared_ptr<TileSet>> tileSets, GlobalTileId gid)
{
auto* tile = this->g_tiles.getPtr(x, y);
auto* tile = this->g_tiles.getPtr(position.x, position.y);
if (tile != nullptr)
{
tile->g_gid = gid;
tile->g_tileSet = TileLayer::retrieveAssociatedTileSet(tileSets, gid);
if (tile->g_tileSet)
{
tile->g_position = {static_cast<float>(tile->g_tileSet->getTileSize().x * x),
static_cast<float>(tile->g_tileSet->getTileSize().y * y)};
tile->g_position = {static_cast<float>(tile->g_tileSet->getTileSize().x * position.x),
static_cast<float>(tile->g_tileSet->getTileSize().y * position.y)};
}
tile->updatePositions();
tile->updateTexCoords();
}
}
void TileLayer::setGid(std::size_t x, std::size_t y, GlobalTileId gid)
GlobalTileId TileLayer::getGid(fge::Vector2size position)
{
auto* data = this->g_tiles.getPtr(x, y);
auto* tile = this->g_tiles.getPtr(position.x, position.y);
if (tile != nullptr)
{
return tile->g_gid;
}
return 0;
}
GlobalTileId TileLayer::getGid(fge::Vector2f position)
{
auto const bounds = this->getLocalBounds();
auto const tileLocalPosition = this->getInverseTransform() * position;

auto const size = this->g_tiles.getSize();

fge::Vector2size const positionIndex{
static_cast<std::size_t>(static_cast<float>(size.x) * tileLocalPosition.x / bounds._width),
static_cast<std::size_t>(static_cast<float>(size.y) * tileLocalPosition.y / bounds._height)};

if (positionIndex.x < this->g_tiles.getSizeX() && positionIndex.y < this->g_tiles.getSizeY())
{
return this->getGid(positionIndex);
}
return 0;
}
void TileLayer::setGid(fge::Vector2size position, GlobalTileId gid)
{
auto* data = this->g_tiles.getPtr(position.x, position.y);
if (data != nullptr)
{
data->g_gid = gid;
}
}
void TileLayer::setGridSize(std::size_t x, std::size_t y)
void TileLayer::setGridSize(fge::Vector2size size)
{
this->g_tiles.clear();
this->g_tiles.setSize(x, y);
this->g_tiles.setSize(size.x, size.y);
}

void TileLayer::refreshTextures(std::span<std::shared_ptr<TileSet>> tileSets)
Expand Down Expand Up @@ -278,7 +304,7 @@ void from_json(nlohmann::json const& j, fge::TileLayer& p)
j.at("height").get_to(h);

p.clear();
p.setGridSize(w, h);
p.setGridSize({w, h});

auto const& dataArray = j.at("data");
if (dataArray.is_array() && dataArray.size() == (w * h))
Expand All @@ -289,7 +315,7 @@ void from_json(nlohmann::json const& j, fge::TileLayer& p)
for (std::size_t iw = 0; iw < w; ++iw)
{
int gid = it->get<int>();
p.setGid(iw, ih, gid);
p.setGid({iw, ih}, gid);
++it;
}
}
Expand Down

0 comments on commit 36687f2

Please sign in to comment.