Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ add_library(ecs_engine STATIC
src/engine/EngineSystem.h
src/engine/FrameClock.h
src/engine/WorldQuery.h
src/engine/CommandBuffer.h
src/engine/EntityRegistry.cpp
src/engine/EngineRuntime.cpp
src/engine/FrameClock.cpp
Expand Down Expand Up @@ -110,6 +111,7 @@ if (BUILD_TESTING)
tests/DxfImportServiceTests.cpp
tests/FacilityLayoutBuilderTests.cpp
tests/WorldQueryTests.cpp
tests/CommandBufferTests.cpp
)

target_include_directories(safecrowd_tests
Expand Down
85 changes: 85 additions & 0 deletions src/engine/CommandBuffer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#pragma once

#include <functional>
#include <tuple>
#include <utility>
#include <vector>

#include "engine/EcsCore.h"

namespace safecrowd::engine {

class CommandBuffer {
public:
template <typename... Ts>
void spawnEntity(Ts... components) {
commands_.push_back(
[comps = std::make_tuple(std::move(components)...)](EcsCore& core) mutable {
Entity e = core.createEntity();
std::apply([&](auto&... c) { (core.addComponent(e, std::move(c)), ...); }, comps);
});
}

void destroyEntity(Entity entity) {
commands_.emplace_back([entity](EcsCore& core) {
core.destroyEntity(entity);
});
}

template <typename T>
void addComponent(Entity entity, T component) {
commands_.emplace_back([entity, comp = std::move(component)](EcsCore& core) mutable {
core.addComponent(entity, std::move(comp));
});
}

template <typename T>
void removeComponent(Entity entity) {
commands_.emplace_back([entity](EcsCore& core) {
core.removeComponent<T>(entity);
});
}

void flush(EcsCore& core) {
for (auto& cmd : commands_) {
cmd(core);
}
commands_.clear();
}

[[nodiscard]] bool empty() const noexcept {
return commands_.empty();
}

private:
std::vector<std::function<void(EcsCore&)>> commands_;
};

class WorldCommands {
public:
explicit WorldCommands(CommandBuffer& buffer) : buffer_(buffer) {}

template <typename... Ts>
void spawnEntity(Ts... components) {
buffer_.spawnEntity(std::move(components)...);
}

void destroyEntity(Entity entity) {
buffer_.destroyEntity(entity);
}

template <typename T>
void addComponent(Entity entity, T component) {
buffer_.addComponent(entity, std::move(component));
}

template <typename T>
void removeComponent(Entity entity) {
buffer_.removeComponent<T>(entity);
}

private:
CommandBuffer& buffer_;
};

} // namespace safecrowd::engine
129 changes: 129 additions & 0 deletions tests/CommandBufferTests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
#include "TestSupport.h"

#include "engine/CommandBuffer.h"

namespace {

struct Position {
float x{0.0f};
float y{0.0f};
};

struct Velocity {
float vx{0.0f};
float vy{0.0f};
};

} // namespace

SC_TEST(CommandBuffer_DestroyEntity_IsDeferred) {
safecrowd::engine::EcsCore core;
safecrowd::engine::CommandBuffer buffer;

const auto e = core.createEntity();
buffer.destroyEntity(e);

SC_EXPECT_TRUE(core.isAlive(e));

buffer.flush(core);

SC_EXPECT_TRUE(!core.isAlive(e));
}

SC_TEST(CommandBuffer_AddComponent_IsDeferred) {
safecrowd::engine::EcsCore core;
safecrowd::engine::CommandBuffer buffer;

const auto e = core.createEntity();
buffer.addComponent(e, Position{1.0f, 2.0f});

SC_EXPECT_TRUE(!core.hasComponent<Position>(e));

buffer.flush(core);

SC_EXPECT_TRUE(core.hasComponent<Position>(e));
SC_EXPECT_NEAR(core.getComponent<Position>(e).x, 1.0f, 1e-6);
}

SC_TEST(CommandBuffer_RemoveComponent_IsDeferred) {
safecrowd::engine::EcsCore core;
safecrowd::engine::CommandBuffer buffer;

const auto e = core.createEntity();
core.addComponent(e, Position{});
buffer.removeComponent<Position>(e);

SC_EXPECT_TRUE(core.hasComponent<Position>(e));

buffer.flush(core);

SC_EXPECT_TRUE(!core.hasComponent<Position>(e));
}

SC_TEST(CommandBuffer_Flush_AppliesCommandsInOrder) {
safecrowd::engine::EcsCore core;
safecrowd::engine::CommandBuffer buffer;

const auto e = core.createEntity();
buffer.addComponent(e, Position{});
buffer.removeComponent<Position>(e);

buffer.flush(core);

SC_EXPECT_TRUE(!core.hasComponent<Position>(e));
}

SC_TEST(CommandBuffer_Flush_ClearsBuffer) {
safecrowd::engine::EcsCore core;
safecrowd::engine::CommandBuffer buffer;

const auto e = core.createEntity();
buffer.destroyEntity(e);

SC_EXPECT_TRUE(!buffer.empty());

buffer.flush(core);

SC_EXPECT_TRUE(buffer.empty());
}

SC_TEST(CommandBuffer_SpawnEntity_CreatesEntityWithComponents) {
safecrowd::engine::EcsCore core;
safecrowd::engine::CommandBuffer buffer;

buffer.spawnEntity(Position{3.0f, 4.0f}, Velocity{1.0f, 0.0f});

SC_EXPECT_TRUE(buffer.empty() == false);

buffer.flush(core);

const auto result = [&] {
std::vector<safecrowd::engine::Entity> alive;
core.entityRegistry().eachAlive([&](safecrowd::engine::Entity entity, const safecrowd::engine::Signature&) {
if (core.hasComponent<Position>(entity) && core.hasComponent<Velocity>(entity)) {
alive.push_back(entity);
}
});
return alive;
}();

SC_EXPECT_EQ(result.size(), std::size_t{1});
SC_EXPECT_NEAR(core.getComponent<Position>(result[0]).x, 3.0f, 1e-6);
SC_EXPECT_NEAR(core.getComponent<Velocity>(result[0]).vx, 1.0f, 1e-6);
}

SC_TEST(WorldCommands_ForwardsToBuffer) {
safecrowd::engine::EcsCore core;
safecrowd::engine::CommandBuffer buffer;
safecrowd::engine::WorldCommands cmds{buffer};

const auto e = core.createEntity();
cmds.addComponent(e, Position{5.0f, 6.0f});

SC_EXPECT_TRUE(!core.hasComponent<Position>(e));

buffer.flush(core);

SC_EXPECT_TRUE(core.hasComponent<Position>(e));
SC_EXPECT_NEAR(core.getComponent<Position>(e).y, 6.0f, 1e-6);
}
Loading