issues with entity allocation

This commit is contained in:
benkyd
2022-06-08 20:00:52 +00:00
parent b90898fca6
commit 01c31f46f4
6 changed files with 73 additions and 19 deletions

View File

@@ -12,7 +12,6 @@ namespace EC
// someone fix this ahaha
struct IComponentContainer
{
virtual ~IComponentContainer() = default;
virtual void Create(const Entity) = 0;
virtual void Destroy(const Entity) = 0;
};
@@ -25,6 +24,10 @@ struct ComponentContainer : public IComponentContainer
}
void Create(const Entity) override
{
}
void Destroy(const Entity) override
{
@@ -57,7 +60,7 @@ public:
private:
std::map<std::string, IComponentContainer> mComponentContainers;
// std::map<std::string, IComponentContainer> mComponentContainers;
};

View File

@@ -8,6 +8,11 @@ namespace EC
using Entity = uint32_t;
struct EntityDebugMetadata
{
std::string DebugName;
};
}
#endif

View File

@@ -15,25 +15,37 @@ EntityRegistry::~EntityRegistry()
Entity EntityRegistry::Create()
{
mEntityCeiling++;
uint32_t entityId = mEntityCeiling;
Entity entity( { entityId } );
mEntities.push( entity );
return entity;
uint32_t entityId;
if ( mFreedEntities.empty() )
{
mEntityCeiling++;
entityId = mEntityCeiling;
}
else
{
mFreedEntities.pop();
}
mEntities[entityId] = std::vector<std::string>();
return entityId;
}
Entity EntityRegistry::Copy( const Entity entity )
Entity Copy( const Entity& entity )
{
// look up everything, create a new entity and populate
// with the components in the og entity
return Entity( { 0 } );
return static_cast<uint32_t>( 0 );
}
Entity EntityRegistry::Destroy( Entity entity )
void EntityRegistry::Destroy( Entity entity )
{
auto id = entity.id;
mEntities = std::move(n.back()); // move last element to removal location
mEntities.pop_back();
if ( !this->Valid( entity ) ) return;
mFreedEntities.push(entity);
mEntities.erase(entity);
}
bool EntityRegistry::Valid( const Entity entity )
{
return mEntities.find(entity) != mEntities.end();
}

View File

@@ -16,10 +16,10 @@ public:
EntityRegistry();
~EntityRegistry();
Entity Create()
Entity Create();
Entity Copy( const Entity entity );
void Destroy( Entity entity );
bool Valid(const Entity entity);
bool Valid( const Entity entity );
// add, replace components
template <typename TComponent>
@@ -47,12 +47,16 @@ public:
// std::vector<T&> Sort(std::function<;
private:
// Keep track of IDs that were freed for re-allocation
std::queue<uint32_t> mFreedEntities;
uint32_t mEntityCeiling = 0;
// On destroy, the last entity is moved to the position
// of the old entity for cache coherency
std::map<Entity, std::vector<std::string>> mEntities
// Currently unused
std::unordered_map<Entity, EntityDebugMetadata> mEntityDebug;
// That's not very cache-friendly of you
// optimisations coming soon TM
std::unordered_map<Entity, std::vector<std::string>> mEntities;
};
}

View File

@@ -3,7 +3,12 @@
// shut up
#ifdef __WIN32
#include <SDL.h>
#else
#include <SDL2/SDL.h>
#endif
extern "C" {
#include <Aeon/ThirdParty/glad.h>
}

View File

@@ -91,6 +91,31 @@ public:
ExampleGame()
: App( { "Example" }, { "Game with AEON!" } )
{
EC::EntityRegistry registry;
EC::Entity entity1 = registry.Create();
std::cout << entity1 << std::endl;
std::vector<EC::Entity> entities;
for (int i = 0; i < 100000; i++)
{
entities.push_back(registry.Create());
}
std::cout << entities[entities.size()] << std::endl;
for (int i = 0; i < 100000; i++)
{
registry.Destroy(entities[i]);
}
entity1 = registry.Create();
std::cout << entity1 << std::endl;
Level* level = new Level;
PushLayer( (Core::GameLayer*)level );
DebugLayer* debug = new DebugLayer;