10#include <unordered_map>
88 CommandBuffer _commandBuffer;
89 std::unordered_map<ComponentTypeId, std::unique_ptr<SparseSetBase>> _pools;
90 std::vector<std::unique_ptr<Bond>> _bonds;
91 std::unordered_map<ComponentTypeId, Bond*> _bondMap;
103 template <
typename T>
112 template <
typename T>
121 void destroy_immediate(
Entity e);
129 template <
typename T>
130 void bind_immediate(
Entity e, T component);
137 template <
typename T>
138 void unbind_immediate(
Entity e);
151 template <
typename T>
157 friend class CommandBuffer;
210 template <
typename T>
222 template <
typename T>
234 template <
typename T>
247 template <
typename T>
257 template <
typename T>
266 template <
typename T>
277 template <
typename... Components>
289 template <
typename... Components>
307 template <
typename T>
314 auto location = _pools.find(
id);
317 if (location == _pools.end()) {
318 location = _pools.emplace(
id, std::make_unique<
SparseSet<T>>()).first;
322 return static_cast<SparseSet<T>*
>(location->second.get());
325 template <
typename T>
326 SparseSet<T>* Nexus::find_pool()
329 uint32_t
id = get_component_type_id<T>();
332 auto location = _pools.find(
id);
335 if (location == _pools.end()) {
340 return static_cast<SparseSet<T>*
>(location->second.get());
343 template <
typename T>
344 void Nexus::bind_immediate(
Entity e, T component)
347 SparseSet<T>* pool = get_pool<T>();
350 pool->insert(e, std::move(component));
353 Bond* currentBond = find_bond<T>();
354 if (currentBond !=
nullptr) {
355 currentBond->on_component_bound(e);
359 template <
typename T>
360 void Nexus::unbind_immediate(
Entity e)
363 SparseSet<T>* pool = find_pool<T>();
364 if (pool ==
nullptr)
return;
367 Bond* currentBond = find_bond<T>();
368 if (currentBond !=
nullptr) {
369 currentBond->on_component_unbound(e);
376 template <
typename T>
377 Bond* Nexus::find_bond()
383 auto location = _bondMap.find(
id);
386 if (location == _bondMap.end()) {
391 return location->second;
395 template <
typename T>
398 _commandBuffer.defer_bind<T>(e, component);
401 template <
typename T>
404 _commandBuffer.defer_unbind<T>(e);
408 template <
typename T>
415 pool->
replace(e, std::move(component));
418 template <
typename T>
423 assert(pool !=
nullptr &&
"[Nexus] Get called for a component type with no registered pool.");
429 template <
typename T>
434 if (pool ==
nullptr)
return nullptr;
440 template <
typename T>
445 if (pool ==
nullptr)
return false;
451 template <
typename... Components>
456 std::array<
SparseSetBase*,
sizeof...(Components)> pools = { get_pool<Components>()... };
462 template <
typename... Components>
469 Bond* found =
nullptr;
471 bool sameBond =
true;
477 auto location = _bondMap.find(
id);
478 if (location == _bondMap.end())
continue;
484 if (found ==
nullptr) {
486 found = location->second;
489 }
else if (found != location->second) {
498 std::vector<SparseSetBase*> pools = { get_pool<Components>()... };
505 std::unique_ptr<Bond> owned(
new Bond(std::move(pools)));
508 Bond* bondPtr = owned.get();
510 _bondMap[id] = bondPtr;
514 _bonds.push_back(std::move(owned));
521 if (mapped ==
sizeof...(Components) && sameBond && found->
get_pool_count() ==
sizeof...(Components)) {
527 assert(
false &&
"[Nexus] bond<>() was requested for components that are already bonded!");
535 template <
typename T>
539 _bindQueue.push_back([
this, e, component = std::move(component)]()
mutable {
540 _nexus.bind_immediate<T>(e, std::move(component));
544 template <
typename T>
548 _unbindQueue.push_back([
this, e]() {
549 _nexus.unbind_immediate<T>(e);
Defines the BondHandle, this is the view that users will access to iterate over the Entities of a Bon...
Defines the Bond class which will be a permanent group of component pools that are kept sorted so tha...
Defines the Command Buffer to solve undefined behavior resulting from modifying containers while iter...
Owns the lifetime of every Entity in the engine, it creates, destroys, and valildates every entity....
Defines the Entity alias and the free functions used to pack and unpack it.
uint32_t Entity
A packed identifier: 20 bits of index, 12 bits of generation.
Definition Entity.hpp:20
ComponentTypeId get_component_type_id()
returns the ComponenTypeId for component of type T.
Definition Nexus.hpp:69
uint32_t ComponentTypeId
alias for component ids for pool storage
Definition Nexus.hpp:31
Declares the SparseSetBase interface.
Defines the concrete SparseSet templated class.
Defines the Survey, how the users will iterate over Entities with specified components in Systems.
Lightweight view over a Bond. This is returned by value from Nexus::Bond<Components....
Definition BondHandle.hpp:31
A Bond permanently reorders every pool within it so that every Entity that has all of the components ...
Definition Bond.hpp:29
size_t get_pool_count() const
Returns the number of component pools in the bond.
Definition Bond.cpp:85
void defer_bind(Entity e, T component)
Queues a bind_immediate command lambda.
Definition Nexus.hpp:536
void defer_unbind(Entity e)
Queues an unbind_immediate command lambda.
Definition Nexus.hpp:545
Creates, destroys, and validates Entity handles.
Definition EntityManager.hpp:36
void bind(Entity e, T component)
Binds a component of type T to Entity e.
Definition Nexus.hpp:396
void flush()
calls _commandBuffer.flush() to call the immediate methods that actually apply the deferred changes t...
Definition Nexus.cpp:47
Nexus()
Construct a new Nexus object and instantiates the command buffer.
Definition Nexus.cpp:6
void unbind(Entity e)
Unbinds a component of type T from Entity e if possible.
Definition Nexus.hpp:402
SurveyHandle< Components... > survey()
Returns a survey of the specified components.
Definition Nexus.hpp:452
Entity create()
Creates a new Entity.
Definition Nexus.cpp:9
BondHandle< Components... > bond()
Does one of three things depending on the current state of Bonds already in the Nexus:
Definition Nexus.hpp:463
bool is_valid(Entity e) const
Checks whether an Entity is still valid.
Definition Nexus.cpp:42
void destroy(Entity e)
Destroys an Entity and removes it from every component pool it is in.
Definition Nexus.cpp:15
void patch(Entity e, T component)
Overwrites an Entity's existing component of type T with new component data.
Definition Nexus.hpp:409
T & get(Entity e)
Returns a reference to the Entity's component of type T.
Definition Nexus.hpp:419
bool has(Entity e)
Check whether an Entity currently has a component of type T.
Definition Nexus.hpp:441
T * try_to_get(Entity e)
Returns a reference to the Entity's component of type T if it has it and nullptr otherwise.
Definition Nexus.hpp:430
Type-erased interface for SparseSet<T>.
Definition SparseSetBase.hpp:21
SparseSet concrete class templated to a component type, acts as Imago ECS's component pool.
Definition SparseSet.hpp:33
bool has(Entity e) const override
checks whether an Entity currently has a component stored in this pool
Definition SparseSet.hpp:233
T & replace(Entity e, T component)
replaces a component with new data
Definition SparseSet.hpp:131
T * try_to_get(Entity e)
tries to get a component and returns nullptr if one does not exist
Definition SparseSet.hpp:176
T & get(Entity e)
returns a reference to the Entity's component
Definition SparseSet.hpp:150
The Survey takes Components as templated arguments and acts as an iterator over all entities with all...
Definition SurveyHandle.hpp:32