30 std::lock_guard lk{ _mutex };
37 static inline std::mutex _mutex;
38 static inline std::vector<command_buffer*>
45 std::lock_guard lk{ _mutex };
46 _command_buffers.push_back(
this);
50 void push(
auto&&... args) {
51 _commands.emplace_back(T{ std::forward<decltype(args)>(args)... });
54 auto staging() noexcept -> registry& {
58 void play_commands(registry& registry) {
59 while (!_commands.empty()) {
60 auto command = std::move(_commands.front());
61 _commands.pop_front();
63 std::visit([&](
auto&& cmd) { cmd.execute(_staging, registry); }, command);
67 class command_create {
69 command_create(
entity ent, placeholder_entity reserved) : _staging_entity(ent), _reserved(reserved) {
72 void execute(registry& staging, registry& destination) {
73 staging.get_entity(_staging_entity).move(destination, _reserved);
78 placeholder_entity _reserved;
83 command_clone(
entity ent, placeholder_entity reserved) : _entity(ent), _reserved(reserved) {
86 void execute(registry& staging, registry& destination) {
87 destination.get_entity(_entity).clone(_reserved);
92 placeholder_entity _reserved;
97 using set_fn_t = std::function<void(registry&,
entity, registry&,
entity)>;
100 _staging_entity(ent), _destination_entity(dest), _set_fn(
std::move(fn)) {
103 void execute(registry& staging, registry& destination) {
104 _set_fn(staging, _staging_entity, destination, _destination_entity);
109 entity _destination_entity;
113 class command_remove {
115 using remove_fn_t = std::function<void(registry&,
entity)>;
117 command_remove(
entity ent, remove_fn_t fn) : _entity(ent), _remove_fn(
std::move(fn)) {
120 void execute(registry& staging, registry& destination) {
121 _remove_fn(destination, _entity);
126 remove_fn_t _remove_fn;
129 class command_destroy {
131 command_destroy(
entity ent) : _entity(ent) {
134 void execute(registry& staging, registry& registry) {
135 registry.get_entity(_entity).destroy();
142 using command = std::variant<
151 std::deque<command> _commands;
167 auto staging_entity = _commands.staging().template create<C>(C{ std::forward<Args>(args)... });
168 _commands.push<command_buffer::command_set>(staging_entity,
170 [](
auto& staging_registry,
auto staging_entity,
auto& dest_registry,
auto dest_entity) {
171 dest_registry.get_entity(dest_entity)
172 .template set<C>(std::move(staging_registry.get_entity(staging_entity).template get<C>()));
180 template<component C>
182 _commands.push<command_buffer::command_remove>(
189 _commands.push<command_buffer::command_destroy>(_entity);
196 _commands.push<command_buffer::command_clone>(_entity,
entity);
202 [[nodiscard]]
operator entity() const noexcept {
256 auto staging_entity = _cmds.staging().template
create<Args...>(std::forward<Args>(args)...);
257 _cmds.push<command_buffer::command_create>(staging_entity,
entity);
264 _cmds.push<command_buffer::command_destroy>(ent);
constexpr void sync()
Synchronizes concurently reserved entities.
Definition base_registry.hpp:104
constexpr auto reserve() -> placeholder_entity
Reserves an entity in a thread-safe manner.
Definition base_registry.hpp:98
This class manages command buffers and facilitates command execution.
Definition command.hpp:16
static void flush(registry ®istry)
Flushes all commands in the command buffers to the given registry.
Definition command.hpp:27
static auto get() -> command_buffer &
Retrieves the thread-local command buffer instance.
Definition command.hpp:20
This class provides a reference to a command entity, allowing operations such as setting,...
Definition command.hpp:158
auto set(Args &&... args) -> command_entity_ref &
Sets a component of type C for the entity.
Definition command.hpp:166
void destroy()
Destroys the entity.
Definition command.hpp:188
auto remove() -> command_entity_ref &
Removes a component of type C from the entity.
Definition command.hpp:181
auto clone() const -> command_entity_ref
Clones the entity.
Definition command.hpp:194
This class is responsible for writing commands to a command_buffer.
Definition command.hpp:235
auto get_entity(entity ent) -> command_entity_ref
Retrieves a reference to a command entity.
Definition command.hpp:245
void destroy(entity ent)
Destroys an existing entity.
Definition command.hpp:263
auto create(Args &&... args) -> command_entity_ref
Creates a new entity with the given components.
Definition command.hpp:254
command_writer(registry ®)
Constructs a command_writer with a given registry.
Definition command.hpp:239
Registry is a container for all our entities and components. Components are stored in continuously in...
Definition registry.hpp:13
constexpr auto get_entity(entity ent) noexcept -> entity_ref
Retrieves a mutable reference to an entity.
Definition registry.hpp:51
Component concept. The component must be a struct/class that can be move constructed and move assigna...
Definition component.hpp:86
Definition archetype.hpp:11
detail::handle< struct entity_tag_t > entity
Represents an entity, consisting of an ID and generation.
Definition entity.hpp:13