Skip to content

Commit

Permalink
Fix Layer::getCoordIdAt calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
Madour committed May 19, 2024
1 parent 6d270c9 commit 7cb678a
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 10 deletions.
2 changes: 1 addition & 1 deletion include/LDtkLoader/Layer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ namespace ldtk {
auto getEntitiesByTag(const std::string& tag) const -> const std::vector<ref_wrapper<Entity>>&;
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);

Expand Down
5 changes: 4 additions & 1 deletion include/LDtkLoader/Tile.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 3 additions & 2 deletions src/Layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>(), tile["px"][1].get<int>()},
key == "gridTiles" ? tile["d"][0].get<int>() : tile["d"][1].get<int>(),
tile["t"].get<int>(),
tile["f"].get<int>(),
tile["a"].get<float>()
Expand Down Expand Up @@ -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;
}
11 changes: 5 additions & 6 deletions src/Tile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 7cb678a

Please sign in to comment.