#include "entities/Entity.h" #include #include "common/SpdLogger.h" #include "entities/SceneComponent.h" #include "entities/EntitiesManager.h" #include "entities/ComponentFactory.h" #include "workspace/WorkSpace.h" Entity::Entity(WorkSpace* workspace) : QObject(workspace) , workspace_(workspace) { Init(); } Entity::Entity(const QString& name, QObject* parent) : QObject(parent) , name_(name) { Init(); } Entity::~Entity() { } void Entity::Init() { QUuid uuid = QUuid::createUuid(); uuid_ = uuid.toString(); } void Entity::Serialize(const tinyxml2::XMLElement* element) { const tinyxml2::XMLAttribute* current = element->FirstAttribute(); while (nullptr != current) { const char* aName = current->Name(); const char* aValue = current->Value(); SetAttribute(aName, aValue); current = current->Next(); } const tinyxml2::XMLElement* xmlElement = element->FirstChildElement(); while (nullptr != xmlElement) { const char* name = xmlElement->Name(); SceneComponent* conponent = ComponentFactory::Create(name, rootComponet_); if (nullptr == conponent) { EntitiesManager::Get().Create(xmlElement, this, workspace_); xmlElement = xmlElement->NextSiblingElement(); continue; } conponent->AttachEntity(this); conponent->Serialize(xmlElement); if (!rootComponet_) { rootComponet_ = conponent; } xmlElement = xmlElement->NextSiblingElement(); } } void Entity::UnSerialize(tinyxml2::XMLElement* element) { tinyxml2::XMLElement* entityElement = element->InsertNewChildElement("Entity"); entityElement->SetAttribute("uuid", uuid_.toStdString().c_str()); entityElement->SetAttribute("name", name_.toStdString().c_str()); if (nullptr == rootComponet_) { return; } rootComponet_->UnSerialize(entityElement); } bool Entity::SetAttribute(const char* name, const char* value) { if (0 == strcmp(name, "name")) { SetName(value); return true; } else if (0 == strcmp(name, "uuid")) { SetUUid(value); return true; } return false; } void Entity::Begin() { isRunning_ = true; if (nullptr == rootComponet_) { return; } rootComponet_->Begin(); for (auto item : childer_) { item->Begin(); } } void Entity::Update(double dt) { if (nullptr == rootComponet_) { return; } rootComponet_->Update(dt); for (auto item : childer_) { item->Update(dt); } } void Entity::End() { for (auto item : childer_) { item->End(); } if (nullptr == rootComponet_) { return; } rootComponet_->End(); isRunning_ = false; } void Entity::Destory() { LOG_INFO("destory entity, {}", name_.toLocal8Bit().constData()); if (nullptr == workspace_) { LOG_WARN("workspace is nullptr"); return; } for (auto* children : childer_) { children->Destory(); } if (nullptr != rootComponet_) { rootComponet_->OnDestroy(); } workspace_->RemoveEntity(this); EntitiesManager::Get().DeleteEntity(this); } class Transform* Entity::GetTransform() const { if (nullptr == rootComponet_) { return nullptr; } return rootComponet_->GetTransform(); } void Entity::SetUUid(const QString& uuid) { uuid_ = uuid; } void Entity::UnLoad() {} void Entity::SetName(const QString& name) { name_ = name; emit NameChanged(name_); } void Entity::SetWorkspace(class WorkSpace* workspace) { workspace_ = workspace; if (nullptr != rootComponet_) { rootComponet_->AddToRender(); } } void Entity::SetParent(Entity* parent) { parent_ = parent; if (nullptr == parent) { LOG_WARN("parent is nullptr"); return; } WorkSpace* workSpace = parent->GetWorkspace(); SetWorkspace(workSpace); parent_->childer_.push_back(this); } bool Entity::HasComponent(const std::string& name) const { if (nullptr == rootComponet_) { return false; } return rootComponet_->HasComponent(name); } void Entity::SetVisible(bool v) { LOG_INFO("set visible: {}", v); if (nullptr == rootComponet_) { LOG_WARN("rootComponet_ is nullptr"); return; } rootComponet_->SetVisible(v); for (auto item : childer_) { item->SetVisible(v); } } bool Entity::IsVisible() const { if (nullptr == rootComponet_) { LOG_WARN("rootComponet_ is nullptr"); return false; } return rootComponet_->IsVisible(); }