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/components/map_collision.cpp | 16 ++-- src/game.cpp | 164 ++------------------------------------- src/game.h | 7 +- src/map.cpp | 107 +++++-------------------- src/map.h | 35 ++++----- src/world.cpp | 134 ++++++++++++++++++++++++++++++++ src/world.h | 21 +++++ 7 files changed, 208 insertions(+), 276 deletions(-) create mode 100644 src/world.cpp create mode 100644 src/world.h (limited to 'src') diff --git a/src/components/map_collision.cpp b/src/components/map_collision.cpp index 9adad26..83ad33d 100644 --- a/src/components/map_collision.cpp +++ b/src/components/map_collision.cpp @@ -5,10 +5,10 @@ MapCollisionComponent::MapCollisionComponent(const Map& map) : map(map) { - addCollision(-6, 0, GAME_HEIGHT, Direction::left, collisionFromMoveType(map.getLeftMoveType())); - addCollision(GAME_WIDTH+6, 0, GAME_HEIGHT, Direction::right, collisionFromMoveType(map.getRightMoveType())); - addCollision(-7, 0, GAME_WIDTH, Direction::up, collisionFromMoveType(map.getUpMoveType())); - addCollision(GAME_HEIGHT+6, 0, GAME_WIDTH, Direction::down, collisionFromMoveType(map.getDownMoveType())); + addCollision(-6, 0, GAME_HEIGHT, Direction::left, collisionFromMoveType(map.getAdjacent(Map::MoveDir::Left).type)); + addCollision(GAME_WIDTH+6, 0, GAME_HEIGHT, Direction::right, collisionFromMoveType(map.getAdjacent(Map::MoveDir::Right).type)); + addCollision(-7, 0, GAME_WIDTH, Direction::up, collisionFromMoveType(map.getAdjacent(Map::MoveDir::Up).type)); + addCollision(GAME_HEIGHT+6, 0, GAME_WIDTH, Direction::down, collisionFromMoveType(map.getAdjacent(Map::MoveDir::Down).type)); for (int i=0; i -#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; } diff --git a/src/game.h b/src/game.h index 3f0fcc8..dd4b2f7 100644 --- a/src/game.h +++ b/src/game.h @@ -6,6 +6,7 @@ #include #include #include "map.h" +#include "world.h" class Entity; struct GLFWwindow; @@ -24,14 +25,11 @@ class Game { void saveGame(); void schedule(double time, std::function callback); void playerDie(); - const Map& getMap(int id) const; + const World& getWorld() const; private: friend void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods); - std::map maps; - int startMap; - std::pair startPos; std::list> entities; std::list> nextEntities; std::pair nextPosition; @@ -41,6 +39,7 @@ class Game { Savefile save; std::list>> scheduled; bool shouldQuit = false; + World world; }; #endif diff --git a/src/map.cpp b/src/map.cpp index fa940ef..c4f31d1 100644 --- a/src/map.cpp +++ b/src/map.cpp @@ -20,14 +20,7 @@ Map::Map(const Map& map) id = map.id; title = map.title; - leftMap = map.leftMap; - rightMap = map.rightMap; - downMap = map.downMap; - upMap = map.upMap; - leftType = map.leftType; - rightType = map.rightType; - upType = map.upType; - downType = map.downType; + adjacents = map.adjacents; entities = map.entities; } @@ -52,14 +45,7 @@ void swap(Map& first, Map& second) { std::swap(first.mapdata, second.mapdata); std::swap(first.title, second.title); - std::swap(first.leftMap, second.leftMap); - std::swap(first.rightMap, second.rightMap); - std::swap(first.downMap, second.downMap); - std::swap(first.upMap, second.upMap); - std::swap(first.leftType, second.leftType); - std::swap(first.rightType, second.rightType); - std::swap(first.upType, second.upType); - std::swap(first.downType, second.downType); + std::swap(first.adjacents, second.adjacents); std::swap(first.id, second.id); std::swap(first.entities, second.entities); } @@ -109,44 +95,24 @@ Map::MoveType Map::moveTypeForShort(std::string str) return MoveType::Wall; } -Map::MoveType Map::getLeftMoveType() const +Map::MoveDir Map::moveDirForShort(std::string str) { - return leftType; -} - -Map::MoveType Map::getRightMoveType() const -{ - return rightType; -} - -Map::MoveType Map::getUpMoveType() const -{ - return upType; -} - -Map::MoveType Map::getDownMoveType() const -{ - return downType; -} - -int Map::getLeftMapID() const -{ - return leftMap; -} - -int Map::getRightMapID() const -{ - return rightMap; -} - -int Map::getUpMapID() const -{ - return upMap; + if (str == "right") return MoveDir::Right; + if (str == "up") return MoveDir::Up; + if (str == "down") return MoveDir::Down; + + return MoveDir::Left; } -int Map::getDownMapID() const +static const Map::Adjacent defaultAdjacent {}; +const Map::Adjacent& Map::getAdjacent(MoveDir dir) const { - return downMap; + if (adjacents.count(dir) > 0) + { + return adjacents.at(dir); + } else { + return defaultAdjacent; + } } bool Map::moveTypeTakesMap(MoveType type) @@ -171,44 +137,11 @@ void Map::setTitle(std::string title) this->title = title; } -void Map::setLeftMoveType(MoveType type) -{ - leftType = type; -} - -void Map::setRightMoveType(MoveType type) -{ - rightType = type; -} - -void Map::setUpMoveType(MoveType type) -{ - upType = type; -} - -void Map::setDownMoveType(MoveType type) -{ - downType = type; -} - -void Map::setLeftMapID(int id) -{ - leftMap = id; -} - -void Map::setRightMapID(int id) -{ - rightMap = id; -} - -void Map::setUpMapID(int id) -{ - upMap = id; -} - -void Map::setDownMapID(int id) +void Map::setAdjacent(MoveDir dir, MoveType type, int map) { - downMap = id; + Adjacent& cur = adjacents[dir]; + cur.type = type; + if (map != -1) cur.map = map; } void Map::addEntity(EntityData& data) diff --git a/src/map.h b/src/map.h index 1234dbb..4e661ab 100644 --- a/src/map.h +++ b/src/map.h @@ -3,6 +3,7 @@ #include #include +#include class Entity; @@ -23,25 +24,31 @@ class Map { ReverseWarp }; + enum class MoveDir { + Left, + Right, + Up, + Down + }; + struct EntityData { std::string name; std::pair position; }; + struct Adjacent { + MoveType type = MoveType::Wall; + int map = -1; + }; + static MoveType moveTypeForShort(std::string str); + static MoveDir moveDirForShort(std::string str); static bool moveTypeTakesMap(MoveType type); int getID() const; const int* getMapdata() const; std::string getTitle() const; - MoveType getLeftMoveType() const; - MoveType getRightMoveType() const; - MoveType getUpMoveType() const; - MoveType getDownMoveType() const; - int getLeftMapID() const; - int getRightMapID() const; - int getUpMapID() const; - int getDownMapID() const; + const Adjacent& getAdjacent(MoveDir dir) const; void createEntities(std::list>& entities) const; bool operator==(const Map& other) const; @@ -49,22 +56,14 @@ class Map { void setMapdata(int* mapdata); void setTitle(std::string title); - void setLeftMoveType(MoveType type); - void setRightMoveType(MoveType type); - void setUpMoveType(MoveType type); - void setDownMoveType(MoveType type); - void setLeftMapID(int id); - void setRightMapID(int id); - void setUpMapID(int id); - void setDownMapID(int id); + void setAdjacent(MoveDir dir, MoveType type, int map); void addEntity(EntityData& data); private: int* mapdata; std::string title; int id; std::list entities; - MoveType leftType, rightType, upType, downType; - int leftMap, rightMap, upMap, downMap; + std::map adjacents; }; #endif diff --git a/src/world.cpp b/src/world.cpp new file mode 100644 index 0000000..17288fa --- /dev/null +++ b/src/world.cpp @@ -0,0 +1,134 @@ +#include "world.h" +#include +#include "consts.h" + +World::World(const char* filename) +{ + xmlDocPtr doc = xmlParseFile(filename); + if (doc == nullptr) + { + exit(2); + } + + xmlNodePtr top = xmlDocGetRootElement(doc); + if (top == nullptr) + { + exit(2); + } + + if (xmlStrcmp(top->name, (const xmlChar*) "world")) + { + exit(2); + } + + xmlChar* startxKey = xmlGetProp(top, (xmlChar*) "startx"); + if (startxKey == 0) exit(2); + startX = atoi((char*) startxKey); + xmlFree(startxKey); + + xmlChar* startyKey = xmlGetProp(top, (xmlChar*) "starty"); + if (startyKey == 0) exit(2); + startY = atoi((char*) startyKey); + xmlFree(startyKey); + + xmlChar* startmapKey = xmlGetProp(top, (xmlChar*) "startmap"); + if (startxKey == 0) exit(2); + startMap = atoi((char*) startmapKey); + xmlFree(startmapKey); + + for (xmlNodePtr node = top->xmlChildrenNode; node != NULL; node = node->next) + { + 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); + + maps.emplace(theId, theId); + Map& map = maps[theId]; + + xmlChar* titleKey = xmlGetProp(node, (xmlChar*) "title"); + if (titleKey == 0) exit(2); + map.setTitle((char*) titleKey); + xmlFree(titleKey); + + for (xmlNodePtr mapNode = node->xmlChildrenNode; mapNode != NULL; mapNode = mapNode->next) + { + 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*) "entity")) + { + Map::EntityData data; + + xmlChar* typeKey = xmlGetProp(mapNode, (const xmlChar*) "type"); + if (typeKey == 0) exit(2); + data.name = (char*) typeKey; + xmlFree(typeKey); + + xmlChar* xKey = xmlGetProp(mapNode, (const xmlChar*) "x"); + if (xKey == 0) exit(2); + data.position.first = atoi((char*) xKey); + xmlFree(xKey); + + xmlChar* yKey = xmlGetProp(mapNode, (const xmlChar*) "y"); + if (yKey == 0) exit(2); + data.position.second = atoi((char*) yKey); + xmlFree(yKey); + + map.addEntity(data); + } else if (!xmlStrcmp(mapNode->name, (const xmlChar*) "adjacent")) + { + Map::MoveDir direction; + Map::MoveType moveType; + int mapId = 0; + + xmlChar* dirKey = xmlGetProp(mapNode, (const xmlChar*) "dir"); + if (dirKey == 0) exit(2); + direction = Map::moveDirForShort((char*) dirKey); + xmlFree(dirKey); + + xmlChar* typeKey = xmlGetProp(mapNode, (const xmlChar*) "type"); + if (typeKey == 0) exit(2); + moveType = Map::moveTypeForShort((char*) typeKey); + xmlFree(typeKey); + + xmlChar* mapIdKey = xmlGetProp(mapNode, (const xmlChar*) "map"); + if (mapIdKey != 0) + { + mapId = atoi((char*) mapIdKey); + } + xmlFree(mapIdKey); + + map.setAdjacent(direction, moveType, mapId); + } + } + } + } + + xmlFreeDoc(doc); +} + +const Map& World::getMap(int id) const +{ + return maps.at(id); +} + +const Map& World::getStartingMap() const +{ + return maps.at(startMap); +} + +std::pair World::getStartingPosition() const +{ + return std::make_pair(startX, startY); +} diff --git a/src/world.h b/src/world.h new file mode 100644 index 0000000..f566487 --- /dev/null +++ b/src/world.h @@ -0,0 +1,21 @@ +#ifndef WORLD_H +#define WORLD_H + +#include +#include "map.h" + +class World { + public: + World(const char* filename); + const Map& getMap(int id) const; + const Map& getStartingMap() const; + std::pair getStartingPosition() const; + + private: + std::map maps; + int startMap; + int startX; + int startY; +}; + +#endif /* end of include guard: WORLD_H */ -- cgit 1.4.1