![]() |
vkmEngine 1.0.0
A C++ game engine · vkmengine.com
|
Central registry managing entities and an open set of component types. More...
#include <scene.h>
Public Member Functions | |
| EntityId | createEntity () |
| Create a new entity and assign a unique EntityId. | |
| EntityId | createEntityAt (uint32_t index) |
| Allocate an entity at the requested slot index. | |
| void | destroyEntity (EntityId id) |
| Destroy an entity by removing all of its components and recycling its slot. | |
| EntityId | entityAt (uint32_t index) const |
The full id of the entity in slot index, generation included. | |
| template<typename T> | |
| auto & | add (EntityId entity, T &&component) |
| Give an entity a component it does not have yet. | |
| template<typename T> | |
| void | remove (EntityId entity) |
| Take a component of type T off an entity. | |
| template<typename T> | |
| bool | has (EntityId entity) const |
| Check if an entity has a component of type T. | |
| template<typename T> | |
| T & | get (EntityId entity) |
| Get a mutable reference to an entity's component of type T. | |
| template<typename T> | |
| const T & | get (EntityId entity) const |
| Get a const reference to an entity's component of type T. | |
| template<typename T> | |
| T * | tryGet (EntityId entity) |
| The entity's T, or null when it has none. | |
| template<typename T> | |
| const T * | tryGet (EntityId entity) const |
| The entity's T, or null when it has none. | |
| template<typename T> | |
| size_t | count () const |
| Number of live components of type T. | |
| template<typename First, typename... Rest, typename Fn> | |
| void | forEach (Fn &&fn) |
| Iterate all live components densely (no holes). | |
| template<typename Fn> | |
| void | forEachEntity (Fn &&fn) const |
| Invoke fn(EntityId) for every live entity in this scene. | |
| void | clear () |
| Drop every component set and reset the entity allocator, the environment and the physics settings in one pass. | |
| void | compact () |
| Compact every component SparseSet to reclaim wasted memory. | |
| void | swap (Scene &other) noexcept |
| Swap internal state with another Scene. | |
| void | addObserver (ISceneObserver *observer) |
| Register an observer, notified at the start of every destroyEntity. | |
| void | removeObserver (ISceneObserver *observer) |
| Unregister an observer added with addObserver. | |
| template<typename T> | |
| SparseSet< T > * | storage () |
| Direct access to the typed SparseSet for component type T. | |
| uint64_t | epoch () const |
| Identity of the world these entities belong to, bumped by clear() and swap(). | |
| Environment & | environment () |
| The scene's lighting environment, saved with it. | |
| PhysicsSettings & | physics () |
| The scene's physics world parameters, saved with it. | |
Central registry managing entities and an open set of component types.
Entities come from a SlotAllocator; components live in SparseSet<T>s created on first use, so any type can be a component.
|
inline |
|
inline |
Allocate an entity at the requested slot index.
For recreating an entity at a recorded slot (a scene file, an undo step, a peer) with no id remap. Empties the slot as createEntity() does.
| index | Slot to claim; must not hold a live entity. |
|
inline |
Destroy an entity by removing all of its components and recycling its slot.
The teardown is keyed on the bare slot, so a stale id asserts and is refused, or it would destroy the slot's new owner.
| id | Entity to destroy. |
|
inline |
The full id of the entity in slot index, generation included.
Total: a dead slot yields an id that fails isAlive().
| index | Slot index; any value is accepted. |
|
inline |
Give an entity a component it does not have yet.
A second T asserts; without asserts the existing one is kept. To overwrite, remove<T>() then add, or assign through get<T>().
| T | Component type; storage is created on first use. |
| entity | Entity to add to; alive, without a T. |
| component | Moved or copied in. |
|
inline |
Take a component of type T off an entity.
A missing T is fine. A stale entity asserts and is refused, or the removal would hit the slot's new owner.
| T | Component type. |
| entity | Entity to take it from; must be alive. |
|
inline |
Check if an entity has a component of type T.
| T | Component type. |
| entity | Entity to look on; any value is accepted. |
|
inline |
Get a mutable reference to an entity's component of type T.
| T | Component type. |
| entity | Entity to read; must be alive and carry a T. |
|
inline |
Get a const reference to an entity's component of type T.
| T | Component type. |
| entity | Entity to read; must be alive and carry a T. |
|
inline |
The entity's T, or null when it has none.
| T | Component type. |
| entity | Entity to look on; any value is accepted. |
|
inline |
The entity's T, or null when it has none.
| T | Component type. |
| entity | Entity to look on; any value is accepted. |
|
inline |
Number of live components of type T.
| T | Component type. |
|
inline |
Iterate all live components densely (no holes).
Walks First, yielding entities that also carry every Rest; put the rarest first. Do not add or remove a First, or create or destroy entities, during the walk (see SparseSet::forEach); collect and act after.
| First | Component type iterated. |
| Rest | Further required component types. |
| fn | Callable as void(EntityId, First&, Rest&...). |
|
inline |
Invoke fn(EntityId) for every live entity in this scene.
| Fn | Callable taking an EntityId. |
| fn | Called once per live entity, ascending by slot. |
|
inline |
Drop every component set and reset the entity allocator, the environment and the physics settings in one pass.
The sets are destroyed, not emptied: a set's vtable may live in the gameplay module, which a project switch unloads next.
|
inlinenoexcept |
Swap internal state with another Scene.
For committing a staging scene atomically. Re-fetch any storage<T>() pointer held across it; it now names the other scene's set.
| other | Scene to exchange state with. |
|
inline |
|
inline |
Unregister an observer added with addObserver.
| observer | Observer to stop notifying; absent is a no-op. |
|
inline |
Direct access to the typed SparseSet for component type T.
For index-based or parallel iteration.
| T | Component type. |
|
inline |
|
inline |
The scene's lighting environment, saved with it.
|
inline |
The scene's physics world parameters, saved with it.