From 2ec163612042bfa5e4e1bf220b489506f7039677 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Wed, 18 Mar 2015 09:48:43 -0400 Subject: Game can now read map file from map editor (also added new map) --- src/game.cpp | 168 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 165 insertions(+), 3 deletions(-) (limited to 'src/game.cpp') diff --git a/src/game.cpp b/src/game.cpp index 1c9bb58..4a08744 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -1,4 +1,6 @@ #include "game.h" +#include +#include #include "renderer.h" #include "muxer.h" #include "map.h" @@ -7,11 +9,166 @@ #include "components/player_sprite.h" #include "components/map_render.h" #include "components/map_collision.h" +#include "consts.h" -Game::Game() +Game::Game(const char* 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 = std::make_pair(100.0,100.0); + player->position = startPos; player->size = std::make_pair(10.0,12.0); auto player_input = std::make_shared(); @@ -23,7 +180,7 @@ Game::Game() auto player_anim = std::make_shared(); player->addComponent(player_anim); - Map& startingMap = Map::getNamedMap("embarass"); + Map& startingMap = maps[startMap]; save = {&startingMap, player->position}; loadMap(startingMap, player->position); @@ -168,3 +325,8 @@ void Game::playerDie() player->send(*this, Message::Type::stopDying); }); } + +const Map& Game::getMap(int id) const +{ + return maps.at(id); +} -- cgit 1.4.1