From 103587c2d5f9deb20e549a86cdf5023b429cc6a1 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Wed, 18 Mar 2015 18:23:54 -0400 Subject: Wrote an XML Schema describing maps file and also changed the spec a bit --- src/game.cpp | 164 ++--------------------------------------------------------- 1 file changed, 5 insertions(+), 159 deletions(-) (limited to 'src/game.cpp') diff --git a/src/game.cpp b/src/game.cpp index 4a08744..673c804 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -1,6 +1,5 @@ #include "game.h" #include -#include #include "renderer.h" #include "muxer.h" #include "map.h" @@ -11,164 +10,11 @@ #include "components/map_collision.h" #include "consts.h" -Game::Game(const char* mapfile) +Game::Game(const char* mapfile) : world(mapfile) { - // Load maps - xmlDocPtr doc = xmlParseFile(mapfile); - if (doc == nullptr) - { - exit(2); - } - - xmlNodePtr top = xmlDocGetRootElement(doc); - if (top == nullptr) - { - exit(2); - } - - if (xmlStrcmp(top->name, (const xmlChar*) "world")) - { - exit(2); - } - - for (xmlNodePtr node = top->xmlChildrenNode; node != NULL; node = node->next) - { - if (!xmlStrcmp(node->name, (const xmlChar*) "startpos")) - { - xmlChar* idKey = xmlGetProp(node, (xmlChar*) "id"); - if (idKey == 0) exit(2); - startMap = atoi((char*) idKey); - xmlFree(idKey); - - xmlChar* posKey = xmlGetProp(node, (xmlChar*) "pos"); - if (posKey == 0) exit(2); - sscanf((char*) posKey, "%d,%d", &startPos.first, &startPos.second); - xmlFree(posKey); - } else if (!xmlStrcmp(node->name, (const xmlChar*) "map")) - { - xmlChar* idKey = xmlGetProp(node, (xmlChar*) "id"); - if (idKey == 0) exit(2); - int theId = atoi((char*) idKey); - xmlFree(idKey); - - Map map {theId}; - - for (xmlNodePtr mapNode = node->xmlChildrenNode; mapNode != NULL; mapNode = mapNode->next) - { - if (!xmlStrcmp(mapNode->name, (const xmlChar*) "name")) - { - xmlChar* key = xmlNodeListGetString(doc, mapNode->xmlChildrenNode, 1); - if (key != 0) - { - map.setTitle((char*) key); - } - - xmlFree(key); - } else if (!xmlStrcmp(mapNode->name, (const xmlChar*) "environment")) - { - xmlChar* key = xmlNodeListGetString(doc, mapNode->xmlChildrenNode, 1); - int* mapdata = (int*) malloc(MAP_WIDTH*MAP_HEIGHT*sizeof(int)); - mapdata[0] = atoi(strtok((char*) key, ",\n")); - for (int i=1; i<(MAP_WIDTH*MAP_HEIGHT); i++) - { - mapdata[i] = atoi(strtok(NULL, ",\n")); - } - map.setMapdata(mapdata); - xmlFree(key); - } else if (!xmlStrcmp(mapNode->name, (const xmlChar*) "leftmap")) - { - xmlChar* typeKey = xmlGetProp(mapNode, (xmlChar*) "type"); - if (typeKey == 0) exit(2); - map.setLeftMoveType(Map::moveTypeForShort((char*) typeKey)); - xmlFree(typeKey); - - if (Map::moveTypeTakesMap(map.getLeftMoveType())) - { - xmlChar* idKey = xmlGetProp(mapNode, (xmlChar*) "map"); - if (idKey == 0) exit(2); - map.setLeftMapID(atoi((char*) idKey)); - xmlFree(idKey); - } - } else if (!xmlStrcmp(mapNode->name, (const xmlChar*) "rightmap")) - { - xmlChar* typeKey = xmlGetProp(mapNode, (xmlChar*) "type"); - if (typeKey == 0) exit(2); - map.setRightMoveType(Map::moveTypeForShort((char*) typeKey)); - xmlFree(typeKey); - - if (Map::moveTypeTakesMap(map.getRightMoveType())) - { - xmlChar* idKey = xmlGetProp(mapNode, (xmlChar*) "map"); - if (idKey == 0) exit(2); - map.setRightMapID(atoi((char*) idKey)); - xmlFree(idKey); - } - } else if (!xmlStrcmp(mapNode->name, (const xmlChar*) "upmap")) - { - xmlChar* typeKey = xmlGetProp(mapNode, (xmlChar*) "type"); - if (typeKey == 0) exit(2); - map.setUpMoveType(Map::moveTypeForShort((char*) typeKey)); - xmlFree(typeKey); - - if (Map::moveTypeTakesMap(map.getUpMoveType())) - { - xmlChar* idKey = xmlGetProp(mapNode, (xmlChar*) "map"); - if (idKey == 0) exit(2); - map.setUpMapID(atoi((char*) idKey)); - xmlFree(idKey); - } - } else if (!xmlStrcmp(mapNode->name, (const xmlChar*) "downmap")) - { - xmlChar* typeKey = xmlGetProp(mapNode, (xmlChar*) "type"); - if (typeKey == 0) exit(2); - map.setDownMoveType(Map::moveTypeForShort((char*) typeKey)); - xmlFree(typeKey); - - if (Map::moveTypeTakesMap(map.getDownMoveType())) - { - xmlChar* idKey = xmlGetProp(mapNode, (xmlChar*) "map"); - if (idKey == 0) exit(2); - map.setDownMapID(atoi((char*) idKey)); - xmlFree(idKey); - } - } else if (!xmlStrcmp(mapNode->name, (const xmlChar*) "entities")) - { - for (xmlNodePtr entityNode = mapNode->xmlChildrenNode; entityNode != NULL; entityNode = entityNode->next) - { - if (!xmlStrcmp(entityNode->name, (const xmlChar*) "entity")) - { - Map::EntityData data; - - for (xmlNodePtr entityDataNode = entityNode->xmlChildrenNode; entityDataNode != NULL; entityDataNode = entityDataNode->next) - { - if (!xmlStrcmp(entityDataNode->name, (const xmlChar*) "entity-type")) - { - xmlChar* key = xmlNodeListGetString(doc, entityDataNode->xmlChildrenNode, 1); - data.name = (char*) key; - xmlFree(key); - } else if (!xmlStrcmp(entityDataNode->name, (const xmlChar*) "entity-position")) - { - xmlChar* key = xmlNodeListGetString(doc, entityDataNode->xmlChildrenNode, 1); - sscanf((char*) key, "%d,%d", &data.position.first, &data.position.second); - xmlFree(key); - } - } - - map.addEntity(data); - } - } - } - } - - maps[theId] = map; - } - } - - xmlFreeDoc(doc); - // Set up entities player = std::make_shared(); - player->position = startPos; + player->position = world.getStartingPosition(); player->size = std::make_pair(10.0,12.0); auto player_input = std::make_shared(); @@ -180,7 +26,7 @@ Game::Game(const char* mapfile) auto player_anim = std::make_shared(); player->addComponent(player_anim); - Map& startingMap = maps[startMap]; + const Map& startingMap = world.getStartingMap(); save = {&startingMap, player->position}; loadMap(startingMap, player->position); @@ -326,7 +172,7 @@ void Game::playerDie() }); } -const Map& Game::getMap(int id) const +const World& Game::getWorld() const { - return maps.at(id); + return world; } -- cgit 1.4.1