functional interface casting for component containers

This commit is contained in:
benkyd
2022-06-08 18:33:37 +00:00
parent 53a838ca20
commit 8717e3326b
6 changed files with 85 additions and 52 deletions

View File

@@ -3,45 +3,61 @@
#include <Aeon/Includes.hpp>
struct Entity;
namespace EC
{
struct IComponentArr
// I wish i diddn't have to do it like this
// someone fix this ahaha
struct IComponentContainer
{
virtual ~IComponentContainer() = default;
virtual void Create(const Entity&) = 0;
virtual void Destroy(const Entity&) = 0;
};
template <typename TComponent>
struct ComponentArr : public IComponentArr
struct ComponentContainer : public IComponentContainer
{
ComponentContainer()
{
}
void Destroy(const Entity&) override
{
}
private:
void Destroy();
};
class ComponentController
{
public:
ComponentController()
inline ComponentController()
{
}
~ComponentController()
inline ~ComponentController()
{
}
template <typename TComponent>
void Register()
inline void Register()
{
const char* name = typeid(TComponent).name();
std::cout << name << std::endl;
std::string componentTypeName = static_cast<std::string>(typeid(TComponent).name());
std::cout << componentTypeName << std::endl;
}
private:
std::map<std::string, IComponentArr>
std::map<std::string, IComponentContainer> mComponentContainers;
};

View File

@@ -6,20 +6,7 @@
namespace EC
{
struct Entity
{
uint32_t id;
inline bool operator==( const Entity& rhs )
{
return rhs.id == id;
}
inline bool operator!=( const Entity& rhs )
{
return rhs.id != id;
}
};
using Entity = uint32_t;
}

View File

@@ -0,0 +1,39 @@
#include "EntityController.hpp"
using namespace EC;
EntityRegistry::EntityRegistry()
{
}
EntityRegistry::~EntityRegistry()
{
}
Entity EntityRegistry::Create()
{
mEntityCeiling++;
uint32_t entityId = mEntityCeiling;
Entity entity( { entityId } );
mEntities.push( entity );
return entity;
}
Entity EntityRegistry::Copy( const Entity entity )
{
// look up everything, create a new entity and populate
// with the components in the og entity
return Entity( { 0 } );
}
Entity EntityRegistry::Destroy( Entity entity )
{
auto id = entity.id;
mEntities = std::move(n.back()); // move last element to removal location
mEntities.pop_back();
}

View File

@@ -13,38 +13,32 @@ namespace EC
class EntityRegistry
{
public:
EntityRegistry()
{
EntityRegistry();
~EntityRegistry();
}
~EntityRegistry()
{
}
Entity& Create();
Entity& Copy(const Entity& entity);
Entity& Destroy(Entity& entity);
bool Valid(const Entity& entity);
Entity Create()
Entity Copy( const Entity entity );
void Destroy( Entity entity );
bool Valid(const Entity entity);
// add, replace components
template <typename TComponent>
TComponent& Add(const Entity& entity);
TComponent& Add(const Entity entity);
template <typename TComponent>
TComponent& Replace(const Entity& entity);
TComponent& Replace(const Entity entity);
// replace in-place
template <typename TComponent>
TComponent& Patch(const Entity& entity);
TComponent& Patch(const Entity entity);
// Get component from entity based on T
template <typename TComponent>
TComponent& Get(const Entity& entity);
TComponent& Get(const Entity entity);
// Get std::optional from entity based on T
template <typename TComponent>
std::optional<TComponent&> Opt(const Entity& entity);
std::optional<TComponent&> Opt(const Entity entity);
// TODO: Sort by component properties, for example list of
// entities with the renderable components, sorted by Y pos
@@ -53,7 +47,11 @@ public:
// std::vector<T&> Sort(std::function<;
private:
// std::map<Entity<uint32_t>, std::vector<g
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
};

View File

@@ -21,5 +21,7 @@ extern "C" {
#include <map>
#include <optional>
#include <functional>
#include <queue>
#include <array>
#endif

View File

@@ -106,17 +106,8 @@ public:
};
struct GenericComponent
{
int x,y;
};
int main( int argc, char** argv )
{
EC::ComponentController cController;
cController.Register<GenericComponent>();
ExampleGame game;
return 0;