Skip to content

Commit

Permalink
Add new layer view functions
Browse files Browse the repository at this point in the history
  • Loading branch information
albin-johansson committed Oct 6, 2024
1 parent b576aec commit f4b4d68
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 2 deletions.
23 changes: 23 additions & 0 deletions source/base/lib/inc/tactile/base/document/layer_view.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,29 @@ class ILayerView
[[nodiscard]]
virtual auto get_tile(const Index2D& index) const -> std::optional<TileID> = 0;

/**
* Returns the position of a tile in its parent tileset.
*
* \param tile_id The target tile identifier.
*
* \return
* The position of the tile in the tileset if successful; an empty optional otherwise.
*/
[[nodiscard]]
virtual auto get_tile_position_in_tileset(TileID tile_id) const
-> std::optional<Index2D> = 0;

/**
* Indicates whether the tile at a given world position is animated.
*
* \param world_pos The world position of the tile to check.
*
* \return
* True if the tile is animated; false otherwise.
*/
[[nodiscard]]
virtual auto is_tile_animated(const Index2D& world_pos) const -> bool = 0;

/**
* Returns the tile encoding format used by the layer.
*
Expand Down
6 changes: 6 additions & 0 deletions source/core/inc/tactile/core/document/layer_view_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ class LayerViewImpl final : public ILayerView
[[nodiscard]]
auto get_tile(const Index2D& index) const -> std::optional<TileID> override;

[[nodiscard]]
auto get_tile_position_in_tileset(TileID tile_id) const -> std::optional<Index2D> override;

[[nodiscard]]
auto is_tile_animated(const Index2D& position) const -> bool override;

[[nodiscard]]
auto get_tile_encoding() const -> TileEncoding override;

Expand Down
44 changes: 44 additions & 0 deletions source/core/src/tactile/core/document/layer_view_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#include "tactile/core/layer/tile_layer.hpp"
#include "tactile/core/map/map.hpp"
#include "tactile/core/meta/meta.hpp"
#include "tactile/core/tile/animation.hpp"
#include "tactile/core/tile/tileset.hpp"

namespace tactile {

Expand Down Expand Up @@ -161,6 +163,48 @@ auto LayerViewImpl::get_tile(const Index2D& index) const -> std::optional<TileID
return get_layer_tile(registry, mLayerId, index);
}

auto LayerViewImpl::get_tile_position_in_tileset(const TileID tile_id) const
-> std::optional<Index2D>
{
const auto& registry = mDocument->get_registry();

const auto tile_index = get_tile_index(registry, tile_id);
if (!tile_index.has_value()) {
return std::nullopt;
}

const auto tileset_id = find_tileset(registry, tile_id);
if (tileset_id == kInvalidEntity) {
return std::nullopt;
}

const auto& tileset = registry.get<CTileset>(tileset_id);
return Index2D::from_1d(static_cast<Index2D::value_type>(*tile_index), tileset.extent.cols);
}

auto LayerViewImpl::is_tile_animated(const Index2D& position) const -> bool
{
const auto& registry = mDocument->get_registry();

const auto tile_id = get_layer_tile(registry, mLayerId, position).value();
if (tile_id == kEmptyTile) {
return false;
}

const auto tileset_id = find_tileset(registry, tile_id);
if (tileset_id == kInvalidEntity) {
return false;
}

const auto& tileset = registry.get<CTileset>(tileset_id);
const auto& tileset_instance = registry.get<CTilesetInstance>(tileset_id);

const auto tile_index = tile_id - tileset_instance.tile_range.first_id;
const auto tile_entity = tileset.tiles.at(static_cast<std::size_t>(tile_index));

return registry.has<CAnimation>(tile_entity);
}

auto LayerViewImpl::get_tile_encoding() const -> TileEncoding
{
return _get_tile_format().encoding;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ struct Gd3Map final
{
Gd3Scene scene;
ExtResourceId tileset_id;
Int2 tile_size;
Gd3Tileset tileset;
std::vector<Gd3Layer> layers;
std::unordered_map<TileID, ExtResourceId> tileset_texture_ids;
Expand Down
6 changes: 4 additions & 2 deletions source/godot_tscn_format/lib/src/gd3_document_converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,14 +150,15 @@ auto _convert_tile(const ILayerView& layer,

[[nodiscard]]
auto _convert_tile_layer(const ILayerView& layer,
const Int2 tile_size,
const Gd3Tileset& gd_tileset,
std::string parent_path) -> Gd3Layer
{
Gd3Layer gd_layer {};
_convert_common_layer_data(layer, gd_layer, std::move(parent_path));

auto& gd_tile_layer = gd_layer.value.emplace<Gd3TileLayer>();
gd_tile_layer.cell_size = layer.get_tile_size();
gd_tile_layer.cell_size = tile_size;

const auto extent = layer.get_extent().value();
for (Extent2D::value_type row = 0; row < extent.rows; ++row) {
Expand Down Expand Up @@ -275,6 +276,7 @@ auto Gd3DocumentConverter::visit(const IMapView& map) -> std::expected<void, std
m_map.tileset.scene.next_ext_resource_id = 1;
m_map.tileset.scene.next_sub_resource_id = 1;

m_map.tile_size = map.get_tile_size();
m_map.tileset_id = m_map.scene.next_ext_resource_id++;
m_map.scene.root_meta = _convert_meta(map.get_meta());

Expand Down Expand Up @@ -304,7 +306,7 @@ auto Gd3DocumentConverter::visit(const ILayerView& layer)
Gd3Layer gd_layer {};
switch (layer.get_type()) {
case LayerType::kTileLayer: {
gd_layer = _convert_tile_layer(layer, m_map.tileset, parent_path);
gd_layer = _convert_tile_layer(layer, m_map.tile_size, m_map.tileset, parent_path);
break;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,13 @@ class LayerViewMock : public ILayerView

MOCK_METHOD(std::optional<TileID>, get_tile, (const Index2D&), (const, override));

MOCK_METHOD(std::optional<Index2D>,
get_tile_position_in_tileset,
(TileID),
(const, override));

MOCK_METHOD(bool, is_tile_animated, (const Index2D&), (const, override));

MOCK_METHOD(TileEncoding, get_tile_encoding, (), (const, override));

MOCK_METHOD(std::optional<CompressionFormat>, get_tile_compression, (), (const, override));
Expand Down

0 comments on commit f4b4d68

Please sign in to comment.