修改默认添加尾迹组件

This commit is contained in:
brige 2026-03-21 21:13:29 +08:00
parent 82938b4f52
commit 14dbd31886
2 changed files with 45 additions and 2 deletions

View File

@ -114,7 +114,8 @@ Entity* EntitiesManager::CreateMesh(const QString& mesh) {
SceneComponent* conponent = ComponentFactory::Create("MeshComponent", nullptr);
conponent->SetAttribute("mesh", mesh.toStdString().c_str());
conponent->AttachEntity(entity);
// Also add PathComponent so the entity has a path attribute by default
// Also add PathComponent so the entity has a path attribute by default.
SceneComponent* rootComponent = entity->GetRootComponent();
if (rootComponent) {
SceneComponent* pathComponent = ComponentFactory::Create("PathComponent", rootComponent);
@ -133,6 +134,26 @@ Entity* EntitiesManager::CreateMesh(const QString& mesh) {
LOG_WARN("EntitiesManager::CreateMesh - Failed to create PathComponent with no root");
}
}
// Add TrajectoryTraceComponent by default for all newly created mesh entities.
rootComponent = entity->GetRootComponent();
if (rootComponent) {
SceneComponent* traceComponent = ComponentFactory::Create("TrajectoryTraceComponent", rootComponent);
if (traceComponent) {
traceComponent->AttachTo(rootComponent);
LOG_INFO("EntitiesManager::CreateMesh - Added TrajectoryTraceComponent");
} else {
LOG_WARN("EntitiesManager::CreateMesh - Failed to create TrajectoryTraceComponent");
}
} else {
SceneComponent* traceComponent = ComponentFactory::Create("TrajectoryTraceComponent", nullptr);
if (traceComponent) {
traceComponent->AttachEntity(entity);
LOG_INFO("EntitiesManager::CreateMesh - Added TrajectoryTraceComponent with no root");
} else {
LOG_WARN("EntitiesManager::CreateMesh - Failed to create TrajectoryTraceComponent with no root");
}
}
return entity;
}

View File

@ -88,6 +88,28 @@ Entity* EntityFactory::CreateEntityWithComponents(const QString& type, const QSt
}
}
// Always add TrajectoryTraceComponent so newly created entities can edit/show traces immediately.
{
SceneComponent* rootComponent = entity->GetRootComponent();
if (nullptr == rootComponent) {
SceneComponent* traceComponent = ComponentFactory::Create("TrajectoryTraceComponent", nullptr);
if (traceComponent) {
traceComponent->AttachEntity(entity);
LOG_INFO("EntityFactory::CreateEntityWithComponents - Added TrajectoryTraceComponent");
} else {
LOG_WARN("EntityFactory::CreateEntityWithComponents - Failed to create TrajectoryTraceComponent");
}
} else {
SceneComponent* traceComponent = ComponentFactory::Create("TrajectoryTraceComponent", rootComponent);
if (traceComponent) {
traceComponent->AttachTo(rootComponent);
LOG_INFO("EntityFactory::CreateEntityWithComponents - Added TrajectoryTraceComponent");
} else {
LOG_WARN("EntityFactory::CreateEntityWithComponents - Failed to create TrajectoryTraceComponent");
}
}
}
// Add required components
QStringList requiredComponents = it->second->GetRequiredComponents();
SceneComponent* rootComponent = entity->GetRootComponent();
@ -148,4 +170,4 @@ QStringList EntityFactory::GetRequiredComponents(const QString& type) const {
bool EntityFactory::IsTypeSupported(const QString& type) const {
return creators_.find(type) != creators_.end();
}
}