#pragma once #include #include #include #include #include #include #include #include "U_Log.h" #include "CEntityComponent.h" class CEntity; class CEntityComponentsManager { public: CEntityComponentsManager() = delete; CEntityComponentsManager(CEntity* ent) : m_Entity(ent) {} void Add(std::unique_ptr&& comp); bool IsComponentPresent(const std::string& type) const; std::unique_ptr& GetComponent(const std::string& type); void CreateComponent(const std::string& type, bool instantInit = true); void Trace(const std::function& func); template void CreateComponent(bool instantInit = true) { if(IsComponentPresent()) { Log::ErrInstance() << "Entity component already exist!\n"; return; } Add(std::make_unique()); if(instantInit) { GetComponentTyped()->Init(); //GetComponentTyped()->PostInit(); TODO } } template bool IsComponentPresent() const { auto it = std::find_if(Items.begin(), Items.end(), [](const auto& kv) -> bool { return dynamic_cast(kv.second.get()); }); return it != Items.end(); } template ComponentType* GetComponentTyped() { auto it = std::find_if(Items.begin(), Items.end(), [](const auto& kv) -> bool { return dynamic_cast(kv.second.get()); }); if (it == Items.end()) { return nullptr; //throw std::runtime_error("Component with the specified type not found"); } return dynamic_cast((*it).second.get()); } template ComponentType* GetComponentTyped(const std::string& type) { const auto& it = Items.find(type); if (it == Items.end()) { throw std::runtime_error("No such component"); } if (ComponentType* casted = dynamic_cast(it->second.get())) { return casted; } else { return nullptr; //throw std::runtime_error("Component type mismatch"); } } //TODO delete component + net message CEntity* GetEntity(); std::unordered_map> Items; private: CEntity* m_Entity = nullptr; };