Skip to content

Commit

Permalink
Add getIntGridValPositions API
Browse files Browse the repository at this point in the history
Allows to get all the positions of a given intgridvalue.
  • Loading branch information
Madour committed May 25, 2024
1 parent 2d78c0d commit 8b6c344
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
5 changes: 5 additions & 0 deletions include/LDtkLoader/Layer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ namespace ldtk {
auto getTile(int grid_x, int grid_y) const -> const Tile&;

auto getIntGridVal(int grid_x, int grid_y) const -> const IntGridValue&;
auto getIntGridValPositions(int intgridval_value) const -> const std::vector<IntPoint>&;
auto getIntGridValPositions(const std::string& intgridval_name) const -> const std::vector<IntPoint>&;

auto hasEntity(const std::string& entity_name) const -> bool;
auto allEntities() const -> const std::vector<Entity>&;
Expand All @@ -57,6 +59,7 @@ namespace ldtk {
auto getEntity(const IID& entity_iid) const -> const Entity&;

auto getCoordIdAt(int grid_x, int grid_y) const -> int;
auto getGridPositionFromCoordId(int coord_id) const -> IntPoint;

Layer(const nlohmann::json& j, const World* w, const Level* l);

Expand All @@ -73,6 +76,8 @@ namespace ldtk {
std::map<int, ref_wrapper<const Tile>> m_tiles_map;

std::map<int, ref_wrapper<const IntGridValue>> m_intgrid;
mutable std::map<int, std::vector<IntPoint>> m_intgridpos_by_value;
mutable std::map<std::string, std::vector<IntPoint>> m_intgridpos_by_name;

std::vector<Entity> m_entities;
mutable std::unordered_map<std::string, std::vector<ref_wrapper<Entity>>> m_entities_by_name;
Expand Down
29 changes: 28 additions & 1 deletion src/Layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ Layer::Layer(const nlohmann::json& j, const World* w, const Level* l)
int coord_id = 0;
for (const auto& val : j["intGridCsv"]) {
if (val.get<int>() != 0) {
m_intgrid.emplace(coord_id, m_definition->m_intgrid_values.at(val.get<int>()));
auto& intgridval = m_definition->m_intgrid_values.at(val.get<int>());
m_intgrid.emplace(coord_id, intgridval);
m_intgridpos_by_value[intgridval.value].emplace_back(getGridPositionFromCoordId(coord_id));
m_intgridpos_by_name[intgridval.name].emplace_back(getGridPositionFromCoordId(coord_id));
}
coord_id++;
}
Expand Down Expand Up @@ -127,6 +130,22 @@ auto Layer::getIntGridVal(int grid_x, int grid_y) const -> const IntGridValue&
return IntGridValue::None;
}

auto Layer::getIntGridValPositions(int intgridval_value) const -> const std::vector<IntPoint>&
{
if (m_intgridpos_by_value.find(intgridval_value) != m_intgridpos_by_value.end()) {
return m_intgridpos_by_value.at(intgridval_value);
}
return m_intgridpos_by_value[intgridval_value];
}

auto Layer::getIntGridValPositions(const std::string& intgridval_name) const -> const std::vector<IntPoint>&
{
if (m_intgridpos_by_name.find(intgridval_name) != m_intgridpos_by_name.end()) {
return m_intgridpos_by_name.at(intgridval_name);
}
return m_intgridpos_by_name[intgridval_name];
}

auto Layer::allEntities() const -> const std::vector<Entity>&
{
return m_entities;
Expand Down Expand Up @@ -168,3 +187,11 @@ auto Layer::getCoordIdAt(int grid_x, int grid_y) const -> int
{
return grid_x + grid_y * m_grid_size.x;
}

auto Layer::getGridPositionFromCoordId(int coord_id) const -> IntPoint
{
const auto& grid_width = getGridSize().x;
auto y = coord_id / grid_width;
auto x = coord_id - y * grid_width;
return {x, y};
}
5 changes: 1 addition & 4 deletions src/Tile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@ auto Tile::getPosition() const -> IntPoint

auto Tile::getGridPosition() const -> IntPoint
{
const auto& grid_width = layer->getGridSize().x;
auto y = coordId / grid_width;
auto x = coordId - y * grid_width;
return {x, y};
return layer->getGridPositionFromCoordId(coordId);
}

auto Tile::getWorldPosition() const -> IntPoint
Expand Down

0 comments on commit 8b6c344

Please sign in to comment.