#include "CEntityComponentsManager.h" #include "CEngine.h" void CEntityComponentsManager::Add(std::unique_ptr&& comp) { std::string type = comp->GetType(); Items[type] = std::move(comp); Items[type]->OverridePersistent(m_Entity); //TODO add component net message } bool CEntityComponentsManager::IsComponentPresent(const std::string& type) const { return Items.find(type) != Items.end(); } std::unique_ptr& CEntityComponentsManager::GetComponent(const std::string& type) { auto it = Items.find(type); if (it == Items.end()){ throw std::runtime_error("No such component"); } return it->second; } void CEntityComponentsManager::CreateComponent(const std::string& type, bool instantInit) { if(IsComponentPresent(type)) { Log::ErrInstance() << "Entity component " << type << " already exist!\n"; return; } std::unique_ptr comp = CEngine::GetInstance()->EntityComponentsFactory.create>(type); if(!comp) { return; } Add(std::move(comp)); if(instantInit) { GetComponent(type)->Init(); } } CEntity* CEntityComponentsManager::GetEntity() { return m_Entity; } void CEntityComponentsManager::Trace(const std::function& func) { for (auto& component : Items) { func(component.second.get()); } }