From 8142a9c87a13cecc7a3698e877f24d89f128c074 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Sat, 21 Apr 2018 14:50:52 -0400 Subject: Started working on prototype objects --- src/world.cpp | 67 +++++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 54 insertions(+), 13 deletions(-) (limited to 'src/world.cpp') diff --git a/src/world.cpp b/src/world.cpp index 3b6bd41..d239067 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -1,19 +1,8 @@ #include "world.h" -#include #include #include #include "consts.h" - -inline xmlChar* getProp(xmlNodePtr node, const char* attr) -{ - xmlChar* key = xmlGetProp(node, reinterpret_cast(attr)); - if (key == nullptr) - { - throw std::invalid_argument("Error parsing world file"); - } - - return key; -} +#include "xml.h" World::World(std::string filename) { @@ -67,6 +56,7 @@ World::World(std::string filename) Map::Adjacent rightAdj; Map::Adjacent upAdj; Map::Adjacent downAdj; + std::list objects; for (xmlNodePtr mapNode = node->xmlChildrenNode; mapNode != nullptr; @@ -77,6 +67,10 @@ World::World(std::string filename) reinterpret_cast("environment"))) { key = xmlNodeGetContent(mapNode); + if (key == nullptr) + { + throw std::invalid_argument("Error parsing XML file"); + } mapTiles.clear(); mapTiles.push_back(atoi(strtok(reinterpret_cast(key), ",\n"))); @@ -86,6 +80,52 @@ World::World(std::string filename) } xmlFree(key); + } else if (!xmlStrcmp( + mapNode->name, + reinterpret_cast("entity"))) + { + key = getProp(mapNode, "type"); + std::string entType(reinterpret_cast(key)); + xmlFree(key); + + key = getProp(mapNode, "x"); + int entX = atoi(reinterpret_cast(key)); + xmlFree(key); + + key = getProp(mapNode, "y"); + int entY = atoi(reinterpret_cast(key)); + xmlFree(key); + + key = getProp(mapNode, "index"); + int entIndex = atoi(reinterpret_cast(key)); + xmlFree(key); + + std::map items; + + for (xmlNodePtr entityNode = mapNode->xmlChildrenNode; + entityNode != nullptr; + entityNode = entityNode->next) + { + key = getProp(entityNode, "id"); + std::string itemId(reinterpret_cast(key)); + xmlFree(key); + + key = xmlNodeGetContent(entityNode); + if (key == nullptr) + { + throw std::invalid_argument("Error parsing XML file"); + } + + items[itemId] = atoi(reinterpret_cast(key)); + xmlFree(key); + } + + objects.emplace_back( + std::move(entType), + entX, + entY, + entIndex, + std::move(items)); } else if (!xmlStrcmp( mapNode->name, reinterpret_cast("adjacent"))) @@ -147,7 +187,8 @@ World::World(std::string filename) leftAdj, rightAdj, upAdj, - downAdj)); + downAdj, + std::move(objects))); } } -- cgit 1.4.1