diff --git a/include/LDtkLoader/Layer.hpp b/include/LDtkLoader/Layer.hpp index 1601e8d..1950c92 100644 --- a/include/LDtkLoader/Layer.hpp +++ b/include/LDtkLoader/Layer.hpp @@ -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&; + auto getIntGridValPositions(const std::string& intgridval_name) const -> const std::vector&; auto hasEntity(const std::string& entity_name) const -> bool; auto allEntities() const -> const std::vector&; @@ -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); @@ -73,6 +76,8 @@ namespace ldtk { std::map> m_tiles_map; std::map> m_intgrid; + mutable std::map> m_intgridpos_by_value; + mutable std::map> m_intgridpos_by_name; std::vector m_entities; mutable std::unordered_map>> m_entities_by_name; diff --git a/src/Layer.cpp b/src/Layer.cpp index fd5e4ee..12aa8df 100644 --- a/src/Layer.cpp +++ b/src/Layer.cpp @@ -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() != 0) { - m_intgrid.emplace(coord_id, m_definition->m_intgrid_values.at(val.get())); + auto& intgridval = m_definition->m_intgrid_values.at(val.get()); + 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++; } @@ -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& +{ + 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& +{ + 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& { return m_entities; @@ -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}; +} diff --git a/src/Tile.cpp b/src/Tile.cpp index 21fbb56..4078ea9 100644 --- a/src/Tile.cpp +++ b/src/Tile.cpp @@ -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