DYTSrouce/src/entities/Component.cpp

117 lines
2.8 KiB
C++
Raw Normal View History

2025-01-04 04:12:51 +00:00
#include "entities/Component.h"
#include <QUUID>
#include "common/SpdLogger.h"
#include "entities/ComponentFactory.h"
#include "entities/SceneComponent.h"
#include "entities/Entity.h"
2025-01-05 15:54:44 +00:00
Component::Component(Component* parent)
2025-01-04 04:12:51 +00:00
: QObject(parent) {
2025-01-05 15:54:44 +00:00
if (nullptr != parent) {
owner_ = parent->owner_;
}
2025-01-04 04:12:51 +00:00
uuid_ = QUuid::createUuid().toString();
}
Component::~Component() {
}
void Component::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, reinterpret_cast<SceneComponent*>(this));
if (nullptr == conponent) {
xmlElement = xmlElement->NextSiblingElement();
continue;
}
//conponent->AttachEntity(GetEntity());
conponent->Serialize(xmlElement);
conponent->AttachParent(reinterpret_cast<SceneComponent*>(this));
//conponent->AttachTo(reinterpret_cast<SceneComponent*>(this));
xmlElement = xmlElement->NextSiblingElement();
}
}
void Component::UnSerialize(tinyxml2::XMLElement* element) {
tinyxml2::XMLElement* componentElement = element->InsertNewChildElement(GetSelfTypeName().c_str());
SaveAttribute(componentElement);
std::vector<SceneComponent*> childer = GetChildren();
for (auto componenet : childer) {
componenet->UnSerialize(componentElement);
}
}
bool Component::SetAttribute(const char* name, const char* value) {
if (0 == strcmp("uuid", name)) {
SetUUId(value);
}
return true;
}
bool Component::SaveAttribute(tinyxml2::XMLElement* element) {
element->SetAttribute("uuid", uuid_.toStdString().c_str());
return true;
}
void Component::Begin() {
}
void Component::Update(double dt) {
}
void Component::End() {
}
std::vector<SceneComponent*> Component::GetChildren() const {
return std::vector<SceneComponent*>();
}
2025-07-20 07:19:43 +00:00
bool Component::HasComponent(const std::string& name) const {
return false;
}
2025-01-04 04:12:51 +00:00
bool Component::OnDestroy() {
return true;
}
std::string Component::GetTypeName() {
return "Component";
}
2025-07-20 07:19:43 +00:00
std::string Component::GetSelfTypeName() const {
2025-01-04 04:12:51 +00:00
return Component::GetTypeName();
}
void Component::AttachEntity(Entity* owner) {
if (nullptr == owner && owner_ != owner) {
return;
}
setParent(owner);
owner_ = owner;
}
bool Component::AttachTo(class SceneComponent* parent) {
return true;
}