Skip to content

Commit

Permalink
Add copy_tile function
Browse files Browse the repository at this point in the history
  • Loading branch information
albin-johansson committed Apr 30, 2024
1 parent d3db759 commit 61218b0
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 37 deletions.
16 changes: 15 additions & 1 deletion source/core/inc/tactile/core/tile/tile.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace tactile {

class Registry;

/// \addtogroup tile
/// \addtogroup Tile
/// \{

/**
Expand Down Expand Up @@ -66,6 +66,20 @@ auto make_tile(Registry& registry, TileIndex index) -> EntityID;
*/
void destroy_tile(Registry& registry, EntityID tile_entity);

/**
* Creates a deep copy of a tile.
*
* \pre The specified entity must be a tile.
*
* \param registry The associated registry.
* \param tile_entity The tile that will be copied.
*
* \return
* A tile entity.
*/
[[nodiscard]]
auto copy_tile(Registry& registry, EntityID tile_entity) -> EntityID;

/// \}

} // namespace tactile
21 changes: 21 additions & 0 deletions source/core/src/tactile/core/tile/tile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,25 @@ void destroy_tile(Registry& registry, const EntityID tile_entity)
registry.destroy(tile_entity);
}

auto copy_tile(Registry& registry, const EntityID tile_entity) -> EntityID
{
TACTILE_ASSERT(is_tile(registry, tile_entity));
const auto& meta = registry.get<CMeta>(tile_entity);
const auto& tile = registry.get<CTile>(tile_entity);

const auto copy_entity = registry.make_entity();
registry.add<CMeta>(copy_entity, meta);

auto& tile_copy = registry.add<CTile>(copy_entity);
tile_copy.index = tile.index;

tile_copy.objects.reserve(tile_copy.objects.size());
for (const auto object_entity : tile.objects) {
tile_copy.objects.push_back(copy_object(registry, object_entity));
}

TACTILE_ASSERT(is_tile(registry, copy_entity));
return copy_entity;
}

} // namespace tactile
103 changes: 67 additions & 36 deletions source/core/test/src/tile/tile_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,24 @@

namespace tactile {

/**
* \trace tactile::make_tile
* \trace tactile::is_tile
*/
TEST(Tile, MakeTile)
class TileTest : public testing::Test
{
Registry registry {};
const auto tile_entity = make_tile(registry, TileIndex {42});
protected:
Registry mRegistry {};
};

EXPECT_TRUE(is_tile(registry, tile_entity));
ASSERT_TRUE(registry.has<CMeta>(tile_entity));
ASSERT_TRUE(registry.has<CTile>(tile_entity));
/// \trace tactile::make_tile
/// \trace tactile::is_tile
TEST_F(TileTest, MakeTile)
{
const auto tile_entity = make_tile(mRegistry, TileIndex {42});

EXPECT_TRUE(is_tile(mRegistry, tile_entity));
ASSERT_TRUE(mRegistry.has<CMeta>(tile_entity));
ASSERT_TRUE(mRegistry.has<CTile>(tile_entity));

const auto& meta = registry.get<CMeta>(tile_entity);
const auto& tile = registry.get<CTile>(tile_entity);
const auto& meta = mRegistry.get<CMeta>(tile_entity);
const auto& tile = mRegistry.get<CTile>(tile_entity);

EXPECT_EQ(meta.name, "");
EXPECT_EQ(meta.properties.size(), 0);
Expand All @@ -34,40 +37,68 @@ TEST(Tile, MakeTile)
EXPECT_EQ(tile.objects.size(), 0);
}

/**
* \trace tactile::destroy_tile
*/
TEST(Tile, DestroyTile)
/// \trace tactile::destroy_tile
TEST_F(TileTest, DestroyTile)
{
Registry registry {};

const auto tile_entity = make_tile(registry, TileIndex {10});
ASSERT_TRUE(registry.is_valid(tile_entity));
const auto tile_entity = make_tile(mRegistry, TileIndex {10});
ASSERT_TRUE(mRegistry.is_valid(tile_entity));

{
auto& tile = registry.get<CTile>(tile_entity);
auto& tile = mRegistry.get<CTile>(tile_entity);
tile.objects.push_back(
make_object(registry, ObjectID {1}, ObjectType::kRect));
make_object(mRegistry, ObjectID {1}, ObjectType::kRect));
tile.objects.push_back(
make_object(registry, ObjectID {2}, ObjectType::kEllipse));
make_object(mRegistry, ObjectID {2}, ObjectType::kEllipse));
tile.objects.push_back(
make_object(registry, ObjectID {3}, ObjectType::kPoint));
make_object(mRegistry, ObjectID {3}, ObjectType::kPoint));
}

// Tiles: 1 * (1 CMeta + 1 CTile)
// Objects: 3 * (1 CMeta + 1 CObject)
EXPECT_EQ(registry.count(), 8);
EXPECT_EQ(registry.count<CTile>(), 1);
EXPECT_EQ(registry.count<CMeta>(), 4);
EXPECT_EQ(registry.count<CObject>(), 3);

destroy_tile(registry, tile_entity);

EXPECT_FALSE(registry.is_valid(tile_entity));
EXPECT_FALSE(is_tile(registry, tile_entity));
EXPECT_EQ(registry.count(), 0);
EXPECT_EQ(registry.count<CTile>(), 0);
EXPECT_EQ(registry.count<CObject>(), 0);
EXPECT_EQ(mRegistry.count(), 8);
EXPECT_EQ(mRegistry.count<CTile>(), 1);
EXPECT_EQ(mRegistry.count<CMeta>(), 4);
EXPECT_EQ(mRegistry.count<CObject>(), 3);

destroy_tile(mRegistry, tile_entity);

EXPECT_FALSE(mRegistry.is_valid(tile_entity));
EXPECT_FALSE(is_tile(mRegistry, tile_entity));
EXPECT_EQ(mRegistry.count(), 0);
EXPECT_EQ(mRegistry.count<CTile>(), 0);
EXPECT_EQ(mRegistry.count<CObject>(), 0);
}

/// \trace tactile::copy_tile
TEST_F(TileTest, CopyTile)
{
const auto e1 = make_tile(mRegistry, TileIndex {35});

auto& meta1 = mRegistry.get<CMeta>(e1);
meta1.name = "abcdef";
meta1.properties["x"] = "y";
meta1.components[UUID::generate()];

auto& tile1 = mRegistry.get<CTile>(e1);
tile1.objects.push_back(
make_object(mRegistry, ObjectID {1}, ObjectType::kPoint));
tile1.objects.push_back(
make_object(mRegistry, ObjectID {2}, ObjectType::kRect));
tile1.objects.push_back(
make_object(mRegistry, ObjectID {3}, ObjectType::kEllipse));

const auto e2 = copy_tile(mRegistry, e1);
EXPECT_TRUE(is_tile(mRegistry, e2));

const auto& meta2 = mRegistry.get<CMeta>(e2);
EXPECT_EQ(meta2.name, meta1.name);
EXPECT_EQ(meta2.properties, meta1.properties);
EXPECT_EQ(meta2.components, meta1.components);

const auto& tile2 = mRegistry.get<CTile>(e2);
EXPECT_EQ(tile2.index, tile1.index);
EXPECT_EQ(tile2.objects.size(), tile1.objects.size());
EXPECT_NE(tile2.objects, tile1.objects);
}

} // namespace tactile

0 comments on commit 61218b0

Please sign in to comment.