void EntityManager::destroyEntity(int ID) { if (!entityExists(ID)) return; if (!mDestructionLocked) { // Tell the observers that this entity is about to be obliterated for (auto observer : mObservers) observer->onEntityDestroyed(createEntityRef(ID)); for (auto& componentRow : mComponents) // Component arrays, by type { if (componentRow[ID]) // If the entity has this component type, delete it { componentRow[ID].reset(); } } if (mEntityTags[ID] != -1) mTaggedEntities[mEntityTags[ID]].erase(std::find_if(mTaggedEntities[mEntityTags[ID]].begin(), mTaggedEntities[mEntityTags[ID]].end(), EntityRef::find(ID))); mEntityCount--; mUniqueIDs[ID] = EntityRef::NullUniqueID; mEntityBits[ID].reset(); mEntityTags[ID] = -1; mFreeIDs.push_back(ID); // Free up the entity's ID } else mEntitiesToRemove.push_back(ID); }
void EntityManager::removeComponentFromEntity(int ID, ComponentType componentType) { if (!entityExists(ID)) return; if (componentType >= mComponents.size()) // No entities have this component return; if (!mComponents[componentType][ID]) // This entity doesn't have this component return; if (!mDestructionLocked) { mEntityBits[ID] &= ComponentTypeManager::getBit(componentType).flip(); // Remove the component's bit from the entity's bits. // Tell the world that this component's been removed from this entity. for (auto observer : mObservers) observer->onEntityRemovedComponent(createEntityRef(ID), *mComponents[componentType][ID]); mComponents[componentType][ID].reset(); // Delete the component } else { if (mComponentsToRemove.find(ID) == mComponentsToRemove.end()) mComponentsToRemove[ID] = std::vector<ComponentType>(); mComponentsToRemove[ID].push_back(componentType); } }
EntityRef EntityManager::createEntityRef(int ID) { if (!entityExists(ID)) // Entity doesn't exist, return a null EntityRef { return EntityRef(this, EntityRef::NullID); } return EntityRef(this, ID); }
Entity* EntityManager::getOrCreateEntity( const std::string& name ) { if ( entityExists( name ) ) { return getEntity( name ); } return createEntity( name ); }
bool EntityManager::entityHasComponent(int ID, ComponentType componentType) { if (!entityExists(ID)) return false; if (static_cast<std::size_t>(componentType) < mComponents.size() && mComponents[componentType][ID].get() != nullptr) return true; return false; }
Entity EntityManager::cloneEntity(EntityID source) { if (!entityExists(source)) { return {0, *this, componentManager}; } Entity target = addEntity(); for (auto& container : componentManager.containers) { if (container) { container->cloneComponent(source, target); } } return target; }
void EntityManager::setEntityTag(int ID, int tag) { if (!entityExists(ID)) return; // Remove the entity from its old tag list if (mEntityTags[ID] != -1) mTaggedEntities[mEntityTags[ID]].erase(std::find_if(mTaggedEntities[mEntityTags[ID]].begin(), mTaggedEntities[mEntityTags[ID]].end(), EntityRef::find(ID))); mEntityTags[ID] = tag; if (tag != -1) { if (static_cast<int>(mTaggedEntities.size()) <= tag) mTaggedEntities.resize(tag+1); mTaggedEntities[tag].push_back(createEntityRef(ID)); } }
void EntityManager::destroyEntity(UniqueID uid) { if (!entityExists(uid)) { throw std::runtime_error("The specified entity could not be found."); } for (auto & pair: entityToComponentMaps) { if (pair.second.count(uid) != 0) { componentManager.removeComponent(pair.second[uid]); } } Index thisIndex = entityLookupTable[uid]; Index lastIndex = entityArray.getCount() - 1; Entity & thisEntity = entityArray[thisIndex]; Entity & lastEntity = entityArray[lastIndex]; thisEntity = lastEntity; entityArray.deleteLast(); entityLookupTable[lastEntity.uid] = thisIndex; entityLookupTable.erase(uid); }
const Entity & EntityManager::getEntity(UniqueID uid) const { if (!entityExists(uid)) { throw std::runtime_error("The specified entity could not be found."); } return entityArray[entityLookupTable.at(uid)]; }
void deleteEntity(EntityID inID) { if(entityExists(inID)) entitiesToDelete.push_back(inID); }