#include "entities/EntityFactory.h" #include "entities/ComponentFactory.h" #include "common/SpdLogger.h" #include "workspace/WorkSpace.h" #include template<> EntityFactory* Singleton::instance_ = nullptr; Entity* EntityFactory::CreateEntity(const QString& type, WorkSpace* workspace) { auto it = creators_.find(type); if (it == creators_.end()) { LOG_WARN("EntityFactory::CreateEntity - Unknown entity type: {}", type.toStdString()); return nullptr; } Entity* entity = it->second->CreateEntity(workspace); if (!entity) { LOG_ERROR("EntityFactory::CreateEntity - Failed to create entity of type: {}", type.toStdString()); return nullptr; } LOG_INFO("EntityFactory::CreateEntity - Successfully created entity of type: {}", type.toStdString()); return entity; } Entity* EntityFactory::CreateEntityWithComponents(const QString& type, const QString& mesh, bool useLabel, WorkSpace* workspace) { Entity* entity = CreateEntity(type, workspace); if (!entity) { return nullptr; } auto it = creators_.find(type); if (it == creators_.end()) { return entity; } // Get default mesh QString defaultMesh = it->second->GetDefaultMesh(); if (!mesh.isEmpty()) { defaultMesh = mesh; } if (!defaultMesh.isEmpty()) { SceneComponent* meshComponent = ComponentFactory::Create("MeshComponent", nullptr); if (meshComponent) { meshComponent->SetAttribute("mesh", defaultMesh.toLocal8Bit()); meshComponent->AttachEntity(entity); LOG_INFO("EntityFactory::CreateEntityWithComponents - Added MeshComponent with mesh: {}", defaultMesh.toStdString()); } } if (useLabel) { SceneComponent* rootComponent = entity->GetRootComponent(); if (nullptr == rootComponent) { SceneComponent* meshComponent = ComponentFactory::Create("LabelComponent", nullptr); if (meshComponent) { meshComponent->AttachEntity(entity); LOG_INFO("EntityFactory::CreateEntityWithComponents - Added LabelComponent with mesh: {}", defaultMesh.toStdString()); } } else { SceneComponent* LabelComponent = ComponentFactory::Create("LabelComponent", rootComponent); if (LabelComponent) { LabelComponent->AttachTo(rootComponent); LOG_INFO("EntityFactory::CreateEntityWithComponents - Added LabelComponent with mesh: {}", defaultMesh.toStdString()); } } } // Add required components QStringList requiredComponents = it->second->GetRequiredComponents(); SceneComponent* rootComponent = entity->GetRootComponent(); for (int i = 0; i < requiredComponents.size(); ++i) { QString componentType = requiredComponents.at(i); SceneComponent* component = ComponentFactory::Create(componentType, rootComponent); if (component && rootComponent) { component->AttachTo(rootComponent); LOG_INFO("EntityFactory::CreateEntityWithComponents - Added component: {}", componentType.toStdString()); } else { LOG_WARN("EntityFactory::CreateEntityWithComponents - Failed to create component: {}", componentType.toStdString()); } } return entity; } QStringList EntityFactory::GetRegisteredTypes() const { QStringList types; for (auto it = creators_.begin(); it != creators_.end(); ++it) { types.append(it->first); } return types; } QString EntityFactory::GetDisplayName(const QString& type) const { auto it = creators_.find(type); if (it != creators_.end()) { return it->second->GetDisplayName(); } return type; // 返回类型名作为默认显示名 } QString EntityFactory::GetDescription(const QString& type) const { auto it = creators_.find(type); if (it != creators_.end()) { return it->second->GetDescription(); } return QString(); } QString EntityFactory::GetDefaultMesh(const QString& type) const { auto it = creators_.find(type); if (it != creators_.end()) { return it->second->GetDefaultMesh(); } return QString(); } QStringList EntityFactory::GetRequiredComponents(const QString& type) const { auto it = creators_.find(type); if (it != creators_.end()) { return it->second->GetRequiredComponents(); } return QStringList(); } bool EntityFactory::IsTypeSupported(const QString& type) const { return creators_.find(type) != creators_.end(); }