From 7cb678a9dc4e8fdc0538139179193ab38d73cbda Mon Sep 17 00:00:00 2001 From: Modar Nasser Date: Sun, 19 May 2024 16:38:21 +0200 Subject: [PATCH] Fix Layer::getCoordIdAt calculation --- include/LDtkLoader/Layer.hpp | 2 +- include/LDtkLoader/Tile.hpp | 5 ++++- src/Layer.cpp | 5 +++-- src/Tile.cpp | 11 +++++------ 4 files changed, 13 insertions(+), 10 deletions(-) diff --git a/include/LDtkLoader/Layer.hpp b/include/LDtkLoader/Layer.hpp index 8090874..1601e8d 100644 --- a/include/LDtkLoader/Layer.hpp +++ b/include/LDtkLoader/Layer.hpp @@ -56,7 +56,7 @@ namespace ldtk { auto getEntitiesByTag(const std::string& tag) const -> const std::vector>&; auto getEntity(const IID& entity_iid) const -> const Entity&; - auto getCoordIdAt(int x, int y) const -> int; + auto getCoordIdAt(int grid_x, int grid_y) const -> int; Layer(const nlohmann::json& j, const World* w, const Level* l); diff --git a/include/LDtkLoader/Tile.hpp b/include/LDtkLoader/Tile.hpp index 79c71a3..3704497 100644 --- a/include/LDtkLoader/Tile.hpp +++ b/include/LDtkLoader/Tile.hpp @@ -35,7 +35,10 @@ namespace ldtk { static const Tile None; - Tile(const Layer* l, IntPoint pos, int tile_id, int flips, float a); + Tile(const Layer* l, IntPoint pos, int coord_id, int tile_id, int flips, float a); + + private: + IntPoint m_position; }; auto operator==(const Tile& l, const Tile& r) -> bool; diff --git a/src/Layer.cpp b/src/Layer.cpp index d3b8fba..fd5e4ee 100644 --- a/src/Layer.cpp +++ b/src/Layer.cpp @@ -31,6 +31,7 @@ Layer::Layer(const nlohmann::json& j, const World* w, const Level* l) m_tiles.emplace_back( this, IntPoint{tile["px"][0].get(), tile["px"][1].get()}, + key == "gridTiles" ? tile["d"][0].get() : tile["d"][1].get(), tile["t"].get(), tile["f"].get(), tile["a"].get() @@ -163,7 +164,7 @@ auto Layer::getEntity(const IID& entity_iid) const -> const Entity& ldtk_error("Entity with IID \"" + entity_iid.str() + "\" not found in Layer \"" + getName() + "\"."); } -auto Layer::getCoordIdAt(int x, int y) const -> int +auto Layer::getCoordIdAt(int grid_x, int grid_y) const -> int { - return (x + y * m_grid_size.x) / getCellSize(); + return grid_x + grid_y * m_grid_size.x; } diff --git a/src/Tile.cpp b/src/Tile.cpp index 1816bed..21fbb56 100644 --- a/src/Tile.cpp +++ b/src/Tile.cpp @@ -7,23 +7,22 @@ using namespace ldtk; -const Tile Tile::None{nullptr, {-1, -1}, -1, 0, 0}; +const Tile Tile::None{nullptr, {-1, -1}, -1, -1, 0, 0}; -Tile::Tile(const Layer* l, IntPoint pos, int tile_id, int flips, float a) +Tile::Tile(const Layer* l, IntPoint pos, int coord_id, int tile_id, int flips, float a) : layer(l) -, coordId(l == nullptr ? -1 : l->getCoordIdAt(pos.x, pos.y)) +, coordId(coord_id) , tileId(tile_id) , flipX((flips & 1) != 0) , flipY(((flips >> 1) & 1) != 0) , alpha(a) +, m_position(pos) {} auto Tile::getPosition() const -> IntPoint { - auto cell_size = layer->getCellSize(); - auto grid_pos = getGridPosition(); auto offset = layer->getOffset(); - return {grid_pos.x * cell_size + offset.x, grid_pos.y * cell_size + offset.y}; + return {m_position.x + offset.x, m_position.y + offset.y}; } auto Tile::getGridPosition() const -> IntPoint