From 11d547cde878a3e6cecf30ce054a3397beebc0b3 Mon Sep 17 00:00:00 2001 From: sippeangelo Date: Fri, 8 Jan 2016 12:42:32 +0100 Subject: [PATCH] Entity 0 is now a real entity in the world, created by the first entity read while importing. This makes the difference between map files and entity files automatic, since consecutive entity file loads should automatically get the world as parent, as long as the world entity has been created earlier. --- include/Engine/Core/EntityFile.h | 2 +- include/Engine/Core/EntityFileWriter.h | 1 + include/Engine/Core/World.h | 2 +- src/Engine/Core/EntityFileParser.cpp | 2 +- src/Engine/Core/EntityFileWriter.cpp | 11 +++++++++-- src/Engine/Core/World.cpp | 9 ++++++--- 6 files changed, 19 insertions(+), 8 deletions(-) diff --git a/include/Engine/Core/EntityFile.h b/include/Engine/Core/EntityFile.h index b6f0c434..962e5c4e 100644 --- a/include/Engine/Core/EntityFile.h +++ b/include/Engine/Core/EntityFile.h @@ -162,7 +162,7 @@ private: xercesc::SAX2XMLReader* m_Reader; //State m_CurrentScope = State::Unknown; std::stack m_StateStack; - unsigned int m_NextEntityID = 1; + unsigned int m_NextEntityID = 0; std::stack m_EntityStack; std::string m_CurrentComponent; std::string m_CurrentField; diff --git a/include/Engine/Core/EntityFileWriter.h b/include/Engine/Core/EntityFileWriter.h index ba4035e4..e9c7259c 100644 --- a/include/Engine/Core/EntityFileWriter.h +++ b/include/Engine/Core/EntityFileWriter.h @@ -25,6 +25,7 @@ public: } void WriteWorld(World* world); + void WriteEntity(World* world, EntityID entity); private: boost::filesystem::path m_FilePath; diff --git a/include/Engine/Core/World.h b/include/Engine/Core/World.h index b3209ea7..22e684b0 100644 --- a/include/Engine/Core/World.h +++ b/include/Engine/Core/World.h @@ -39,7 +39,7 @@ public: const std::unordered_multimap& GetEntityChildren() const { return m_EntityChildren; } private: - EntityID m_CurrentEntityID = 1; + EntityID m_CurrentEntityID = 0; std::unordered_map m_EntityParents; // TODO: This should be a more effective structure diff --git a/src/Engine/Core/EntityFileParser.cpp b/src/Engine/Core/EntityFileParser.cpp index 5e685abe..b3310cd4 100644 --- a/src/Engine/Core/EntityFileParser.cpp +++ b/src/Engine/Core/EntityFileParser.cpp @@ -27,7 +27,7 @@ void EntityFileParser::onStartEntity(EntityID entity, EntityID parent) void EntityFileParser::onStartComponent(EntityID entity, std::string component) { EntityID realEntity = m_EntityIDMapper.at(entity); - m_World->AttachComponent(entity, component); + m_World->AttachComponent(realEntity, component); LOG_DEBUG("Attached component of type \"%s\" to entity #%i (%i)", component.c_str(), entity, realEntity); } diff --git a/src/Engine/Core/EntityFileWriter.cpp b/src/Engine/Core/EntityFileWriter.cpp index f0c3684f..b192c525 100644 --- a/src/Engine/Core/EntityFileWriter.cpp +++ b/src/Engine/Core/EntityFileWriter.cpp @@ -3,6 +3,11 @@ #define X(str) XS::ToXMLCh(str) void EntityFileWriter::WriteWorld(World* world) +{ + WriteEntity(world, 0); +} + +void EntityFileWriter::WriteEntity(World* world, EntityID entity) { using namespace xercesc; DOMDocument* doc = m_DOMImplementation->createDocument(nullptr, X("Entity"), nullptr); @@ -13,16 +18,18 @@ void EntityFileWriter::WriteWorld(World* world) DOMElement* componentsElement = doc->createElement(X("Components")); root->appendChild(componentsElement); - appentEntityComponents(componentsElement, world, 0); + appentEntityComponents(componentsElement, world, entity); DOMElement* childrenElement = doc->createElement(X("Children")); root->appendChild(childrenElement); - appendEntityChildren(childrenElement, world, 0); + appendEntityChildren(childrenElement, world, entity); XMLFormatTarget* target = new StdOutFormatTarget(); DOMLSOutput* output = static_cast(m_DOMImplementation)->createLSOutput(); output->setByteStream(target); m_DOMLSSerializer->write(doc, output); + + doc->release(); } void EntityFileWriter::appendEntityChildren(xercesc::DOMElement* parentElement, const World* world, EntityID entity) diff --git a/src/Engine/Core/World.cpp b/src/Engine/Core/World.cpp index 4c45d0d5..74efda2e 100644 --- a/src/Engine/Core/World.cpp +++ b/src/Engine/Core/World.cpp @@ -10,12 +10,15 @@ World::~World() EntityID World::CreateEntity(EntityID parent /*= 0*/) { EntityID newEntity = generateEntityID(); - m_EntityParents[newEntity] = parent; - m_EntityChildren.insert(std::make_pair(parent, newEntity)); + if (newEntity != parent) { + m_EntityParents[newEntity] = parent; + m_EntityChildren.insert(std::make_pair(parent, newEntity)); + } else { + LOG_WARNING("Attempted to create an entity with itself as parent! Entity#%i with parent #%i", newEntity, parent); + } return newEntity; } - void World::DeleteEntity(EntityID entity) { // Delete components