Added World DeleteEntity, HasComponent and GetEntityChildren
This commit is contained in:
@@ -14,11 +14,15 @@ public:
|
|||||||
|
|
||||||
// Create empty entity
|
// Create empty entity
|
||||||
EntityID CreateEntity(EntityID parent = 0);
|
EntityID CreateEntity(EntityID parent = 0);
|
||||||
|
// Delete entity and all components within
|
||||||
|
void DeleteEntity(EntityID entity);
|
||||||
|
|
||||||
// Register a component type and allocate space for it
|
// Register a component type and allocate space for it
|
||||||
void RegisterComponent(ComponentInfo& ci);
|
void RegisterComponent(ComponentInfo& ci);
|
||||||
// Attach a component to an entity and fill it with default values
|
// Attach a component to an entity and fill it with default values
|
||||||
ComponentWrapper AttachComponent(EntityID entity, std::string componentType);
|
ComponentWrapper AttachComponent(EntityID entity, std::string componentType);
|
||||||
|
// Check if an entity has a component
|
||||||
|
bool HasComponent(EntityID entity, std::string componentType);
|
||||||
// Get a component of an entity
|
// Get a component of an entity
|
||||||
ComponentWrapper GetComponent(EntityID entity, std::string componentType);
|
ComponentWrapper GetComponent(EntityID entity, std::string componentType);
|
||||||
// Delete a component off an entity
|
// Delete a component off an entity
|
||||||
@@ -29,6 +33,8 @@ public:
|
|||||||
EntityID GetParent(EntityID entity);
|
EntityID GetParent(EntityID entity);
|
||||||
// Get all component pools
|
// Get all component pools
|
||||||
const std::unordered_map<std::string, ComponentPool*>& GetComponentPools() const { return m_ComponentPools; }
|
const std::unordered_map<std::string, ComponentPool*>& GetComponentPools() const { return m_ComponentPools; }
|
||||||
|
// Get the entity children map
|
||||||
|
const std::unordered_multimap<EntityID, EntityID>& GetEntityChildren() const { return m_EntityChildren; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
EntityID m_CurrentEntityID = 1;
|
EntityID m_CurrentEntityID = 1;
|
||||||
|
|||||||
@@ -11,12 +11,43 @@ EntityID World::CreateEntity(EntityID parent /*= 0*/)
|
|||||||
{
|
{
|
||||||
EntityID newEntity = generateEntityID();
|
EntityID newEntity = generateEntityID();
|
||||||
m_EntityParents[newEntity] = parent;
|
m_EntityParents[newEntity] = parent;
|
||||||
if (parent != 0) {
|
m_EntityChildren.insert(std::make_pair(parent, newEntity));
|
||||||
m_EntityChildren.insert(std::make_pair(parent, newEntity));
|
|
||||||
}
|
|
||||||
return newEntity;
|
return newEntity;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void World::DeleteEntity(EntityID entity)
|
||||||
|
{
|
||||||
|
// Delete components
|
||||||
|
for (auto& pair : m_ComponentPools) {
|
||||||
|
auto& pool = pair.second;
|
||||||
|
if (pool->KnowsEntity(entity)) {
|
||||||
|
auto& c = pool->GetByEntity(entity);
|
||||||
|
pool->Delete(c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Loop through children
|
||||||
|
std::vector<EntityID> childrenToDelete;
|
||||||
|
auto children = m_EntityChildren.equal_range(entity);
|
||||||
|
for (auto it = children.first; it != children.second; ++it) {
|
||||||
|
childrenToDelete.push_back(it->second);
|
||||||
|
}
|
||||||
|
for (auto& child : childrenToDelete) {
|
||||||
|
DeleteEntity(child);
|
||||||
|
}
|
||||||
|
|
||||||
|
EntityID parent = m_EntityParents.at(entity);
|
||||||
|
m_EntityParents.erase(entity);
|
||||||
|
auto parentChildren = m_EntityChildren.equal_range(parent);
|
||||||
|
for (auto it = parentChildren.first; it != parentChildren.second; ++it) {
|
||||||
|
if (it->second == entity) {
|
||||||
|
m_EntityChildren.erase(it);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void World::RegisterComponent(ComponentInfo& ci)
|
void World::RegisterComponent(ComponentInfo& ci)
|
||||||
{
|
{
|
||||||
m_ComponentPools[ci.Name] = new ComponentPool(ci);
|
m_ComponentPools[ci.Name] = new ComponentPool(ci);
|
||||||
@@ -35,6 +66,13 @@ ComponentWrapper World::AttachComponent(EntityID entity, std::string componentTy
|
|||||||
return c;
|
return c;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
bool World::HasComponent(EntityID entity, std::string componentType)
|
||||||
|
{
|
||||||
|
ComponentPool* pool = m_ComponentPools.at(componentType);
|
||||||
|
return pool->KnowsEntity(entity);
|
||||||
|
}
|
||||||
|
|
||||||
ComponentWrapper World::GetComponent(EntityID entity, std::string componentType)
|
ComponentWrapper World::GetComponent(EntityID entity, std::string componentType)
|
||||||
{
|
{
|
||||||
ComponentPool* pool = m_ComponentPools.at(componentType);
|
ComponentPool* pool = m_ComponentPools.at(componentType);
|
||||||
|
|||||||
Reference in New Issue
Block a user