Vulkan Schnee 0.0.1 (22840dc8)
High-performance rendering engine
Loading...
Searching...
No Matches
EntityContainer.h
1#pragma once
2
3#include "Engine/Entity/Entity.h"
4
5#include <algorithm>
6#include <cstddef>
7#include <cstdint>
8#include <memory>
9#include <type_traits>
10#include <utility>
11#include <vector>
12
13#include <plog/Log.h>
14
15namespace Engine::Entities
16{
21 {
22 public:
27
34 template <class T, typename... Args>
35 requires std::is_base_of_v<Entity, T> && std::is_constructible_v<T, Args...>
36 T * spawnEntity( Args &&... args )
37 {
38 auto entity = std::make_unique<T>( std::forward<Args>( args )... );
39 return adoptEntity( std::move( entity ) );
40 }
41
48 template <class T>
49 requires std::is_base_of_v<Entity, T>
50 T * adoptEntity( std::unique_ptr<T> entity )
51 {
52 if ( entity == nullptr )
53 {
54 PLOGW << "Attempted to adopt a null entity.";
55 return nullptr;
56 }
57
58 if ( entity->owningContainer_ != nullptr )
59 {
60 PLOGW << "Attempted to adopt an entity that already has an owning container.";
61 return nullptr;
62 }
63
64 T * rawEntity = entity.get();
65 rawEntity->owningContainer_ = this;
66 entities_.push_back( std::move( entity ) );
67 ++revision_;
68
69 onEntitySpawned( *rawEntity );
70 rawEntity->engineBeginPlay();
71
72 return rawEntity;
73 }
74
79 void destroyEntity( Entity * entity );
80
86
91
96
101 void reserve( std::size_t capacity );
102
106 [[nodiscard]] std::vector<Entity *> getEntities() const;
107
111 [[nodiscard]] std::uint64_t getRevision() const { return revision_; }
112
118 template<typename Func>
119 void forEachEntity(Func&& func) const
120 {
121 for (const auto& entity : entities_)
122 {
123 if (entity != nullptr)
124 {
125 func(entity.get());
126 }
127 }
128 }
129
130 protected:
135 virtual void onEntitySpawned( Entity & entity );
136
141 virtual void onEntityDestroyed( Entity & entity );
142
143 private:
144 std::vector<std::unique_ptr<Entity>> entities_;
145 std::vector<Entity *> pendingDestroyEntities_;
146 std::uint64_t revision_ = 0;
147 };
148} // namespace Engine::Entities
Owns entity lifetime and deferred destruction for a scene or subsystem.
void destroyEntity(Entity *entity)
Queues an entity for deferred destruction.
void destroyEntityImmediate(Entity *entity)
Destroys an entity immediately.
virtual void onEntitySpawned(Entity &entity)
Called after an entity is adopted and before begin play.
virtual void onEntityDestroyed(Entity &entity)
Called before an entity is removed from the container.
std::vector< std::unique_ptr< Entity > > entities_
void flushPendingEntityDestruction()
Destroys all entities queued for deferred destruction.
void reserve(std::size_t capacity)
Reserves entity storage capacity.
void forEachEntity(Func &&func) const
Invokes a callback for each live entity.
void cleanupEntities()
Destroys all container-owned entities and clears pending destruction.
std::vector< Entity * > pendingDestroyEntities_
T * adoptEntity(std::unique_ptr< T > entity)
Adopts an existing entity and starts its engine lifecycle.
std::uint64_t getRevision() const
Returns the container revision incremented by ownership changes.
virtual ~EntityContainer()
Destroys all remaining container-owned entities.
std::vector< Entity * > getEntities() const
Returns raw pointers to all live entities.
T * spawnEntity(Args &&... args)
Constructs and adopts an entity owned by this container.
The entity class is the base class of everything which is attached to a scene. If it is part of a sce...
Definition Entity.h:18