DYTSrouce/src/entities/EntityFactory.cpp

129 lines
4.5 KiB
C++
Raw Normal View History

2025-11-01 13:30:06 +00:00
#include "entities/EntityFactory.h"
#include "entities/ComponentFactory.h"
#include "common/SpdLogger.h"
#include "workspace/WorkSpace.h"
#include <QStringList>
template<> EntityFactory* Singleton<EntityFactory>::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;
}
2025-11-01 16:02:20 +00:00
Entity* EntityFactory::CreateEntityWithComponents(const QString& type, const QString& mesh, bool useLabel, WorkSpace* workspace) {
2025-11-01 13:30:06 +00:00
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();
2025-11-01 16:02:20 +00:00
if (!mesh.isEmpty()) {
defaultMesh = mesh;
}
2025-11-01 13:30:06 +00:00
if (!defaultMesh.isEmpty()) {
SceneComponent* meshComponent = ComponentFactory::Create("MeshComponent", nullptr);
if (meshComponent) {
2025-11-01 16:02:20 +00:00
meshComponent->SetAttribute("mesh", defaultMesh.toLocal8Bit());
2025-11-01 13:30:06 +00:00
meshComponent->AttachEntity(entity);
LOG_INFO("EntityFactory::CreateEntityWithComponents - Added MeshComponent with mesh: {}", defaultMesh.toStdString());
}
}
2025-11-01 16:02:20 +00:00
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());
}
}
}
2025-11-01 13:30:06 +00:00
// 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();
}