#include "entities/Component.h" #include #include "common/SpdLogger.h" #include "entities/ComponentFactory.h" #include "entities/SceneComponent.h" #include "entities/Entity.h" Component::Component(Component* parent) : QObject(parent) { if (nullptr != parent) { owner_ = parent->owner_; } 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(this)); if (nullptr == conponent) { xmlElement = xmlElement->NextSiblingElement(); continue; } //conponent->AttachEntity(GetEntity()); conponent->Serialize(xmlElement); conponent->AttachParent(reinterpret_cast(this)); //conponent->AttachTo(reinterpret_cast(this)); xmlElement = xmlElement->NextSiblingElement(); } } void Component::UnSerialize(tinyxml2::XMLElement* element) { tinyxml2::XMLElement* componentElement = element->InsertNewChildElement(GetSelfTypeName().c_str()); SaveAttribute(componentElement); std::vector 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 Component::GetChildren() const { return std::vector(); } bool Component::OnDestroy() { return true; } std::string Component::GetTypeName() { return "Component"; } std::string Component::GetSelfTypeName() { 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; }