DYTSrouce/src/entities/Entity.cpp

183 lines
3.8 KiB
C++
Raw Normal View History

2025-01-04 04:12:51 +00:00
#include "entities/Entity.h"
#include <QUuid>
#include "common/SpdLogger.h"
#include "entities/SceneComponent.h"
#include "entities/EntitiesManager.h"
#include "entities/ComponentFactory.h"
#include "workspace/WorkSpace.h"
Entity::Entity(QObject* parent)
: QObject(parent) {
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() {
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);
}