diff options
| author | Kelly Rauchenberger <fefferburbia@gmail.com> | 2015-03-18 09:48:43 -0400 |
|---|---|---|
| committer | Kelly Rauchenberger <fefferburbia@gmail.com> | 2015-03-18 09:48:43 -0400 |
| commit | 2ec163612042bfa5e4e1bf220b489506f7039677 (patch) | |
| tree | ead735eb871c3faf813635d08a89ec48195dab35 /src/game.cpp | |
| parent | dc0f0d6fa178403080658887af3ca1a6c0e41188 (diff) | |
| download | therapy-2ec163612042bfa5e4e1bf220b489506f7039677.tar.gz therapy-2ec163612042bfa5e4e1bf220b489506f7039677.tar.bz2 therapy-2ec163612042bfa5e4e1bf220b489506f7039677.zip | |
Game can now read map file from map editor (also added new map)
Diffstat (limited to 'src/game.cpp')
| -rw-r--r-- | src/game.cpp | 168 |
1 files changed, 165 insertions, 3 deletions
| 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 @@ | |||
| 1 | #include "game.h" | 1 | #include "game.h" |
| 2 | #include <cstdlib> | ||
| 3 | #include <libxml/parser.h> | ||
| 2 | #include "renderer.h" | 4 | #include "renderer.h" |
| 3 | #include "muxer.h" | 5 | #include "muxer.h" |
| 4 | #include "map.h" | 6 | #include "map.h" |
| @@ -7,11 +9,166 @@ | |||
| 7 | #include "components/player_sprite.h" | 9 | #include "components/player_sprite.h" |
| 8 | #include "components/map_render.h" | 10 | #include "components/map_render.h" |
| 9 | #include "components/map_collision.h" | 11 | #include "components/map_collision.h" |
| 12 | #include "consts.h" | ||
| 10 | 13 | ||
| 11 | Game::Game() | 14 | Game::Game(const char* mapfile) |
| 12 | { | 15 | { |
| 16 | // Load maps | ||
| 17 | xmlDocPtr doc = xmlParseFile(mapfile); | ||
| 18 | if (doc == nullptr) | ||
| 19 | { | ||
| 20 | exit(2); | ||
| 21 | } | ||
| 22 | |||
| 23 | xmlNodePtr top = xmlDocGetRootElement(doc); | ||
| 24 | if (top == nullptr) | ||
| 25 | { | ||
| 26 | exit(2); | ||
| 27 | } | ||
| 28 | |||
| 29 | if (xmlStrcmp(top->name, (const xmlChar*) "world")) | ||
| 30 | { | ||
| 31 | exit(2); | ||
| 32 | } | ||
| 33 | |||
| 34 | for (xmlNodePtr node = top->xmlChildrenNode; node != NULL; node = node->next) | ||
| 35 | { | ||
| 36 | if (!xmlStrcmp(node->name, (const xmlChar*) "startpos")) | ||
| 37 | { | ||
| 38 | xmlChar* idKey = xmlGetProp(node, (xmlChar*) "id"); | ||
| 39 | if (idKey == 0) exit(2); | ||
| 40 | startMap = atoi((char*) idKey); | ||
| 41 | xmlFree(idKey); | ||
| 42 | |||
| 43 | xmlChar* posKey = xmlGetProp(node, (xmlChar*) "pos"); | ||
| 44 | if (posKey == 0) exit(2); | ||
| 45 | sscanf((char*) posKey, "%d,%d", &startPos.first, &startPos.second); | ||
| 46 | xmlFree(posKey); | ||
| 47 | } else if (!xmlStrcmp(node->name, (const xmlChar*) "map")) | ||
| 48 | { | ||
| 49 | xmlChar* idKey = xmlGetProp(node, (xmlChar*) "id"); | ||
| 50 | if (idKey == 0) exit(2); | ||
| 51 | int theId = atoi((char*) idKey); | ||
| 52 | xmlFree(idKey); | ||
| 53 | |||
| 54 | Map map {theId}; | ||
| 55 | |||
| 56 | for (xmlNodePtr mapNode = node->xmlChildrenNode; mapNode != NULL; mapNode = mapNode->next) | ||
| 57 | { | ||
| 58 | if (!xmlStrcmp(mapNode->name, (const xmlChar*) "name")) | ||
| 59 | { | ||
| 60 | xmlChar* key = xmlNodeListGetString(doc, mapNode->xmlChildrenNode, 1); | ||
| 61 | if (key != 0) | ||
| 62 | { | ||
| 63 | map.setTitle((char*) key); | ||
| 64 | } | ||
| 65 | |||
| 66 | xmlFree(key); | ||
| 67 | } else if (!xmlStrcmp(mapNode->name, (const xmlChar*) "environment")) | ||
| 68 | { | ||
| 69 | xmlChar* key = xmlNodeListGetString(doc, mapNode->xmlChildrenNode, 1); | ||
| 70 | int* mapdata = (int*) malloc(MAP_WIDTH*MAP_HEIGHT*sizeof(int)); | ||
| 71 | mapdata[0] = atoi(strtok((char*) key, ",\n")); | ||
| 72 | for (int i=1; i<(MAP_WIDTH*MAP_HEIGHT); i++) | ||
| 73 | { | ||
| 74 | mapdata[i] = atoi(strtok(NULL, ",\n")); | ||
| 75 | } | ||
| 76 | map.setMapdata(mapdata); | ||
| 77 | xmlFree(key); | ||
| 78 | } else if (!xmlStrcmp(mapNode->name, (const xmlChar*) "leftmap")) | ||
| 79 | { | ||
| 80 | xmlChar* typeKey = xmlGetProp(mapNode, (xmlChar*) "type"); | ||
| 81 | if (typeKey == 0) exit(2); | ||
| 82 | map.setLeftMoveType(Map::moveTypeForShort((char*) typeKey)); | ||
| 83 | xmlFree(typeKey); | ||
| 84 | |||
| 85 | if (Map::moveTypeTakesMap(map.getLeftMoveType())) | ||
| 86 | { | ||
| 87 | xmlChar* idKey = xmlGetProp(mapNode, (xmlChar*) "map"); | ||
| 88 | if (idKey == 0) exit(2); | ||
| 89 | map.setLeftMapID(atoi((char*) idKey)); | ||
| 90 | xmlFree(idKey); | ||
| 91 | } | ||
| 92 | } else if (!xmlStrcmp(mapNode->name, (const xmlChar*) "rightmap")) | ||
| 93 | { | ||
| 94 | xmlChar* typeKey = xmlGetProp(mapNode, (xmlChar*) "type"); | ||
| 95 | if (typeKey == 0) exit(2); | ||
| 96 | map.setRightMoveType(Map::moveTypeForShort((char*) typeKey)); | ||
| 97 | xmlFree(typeKey); | ||
| 98 | |||
| 99 | if (Map::moveTypeTakesMap(map.getRightMoveType())) | ||
| 100 | { | ||
| 101 | xmlChar* idKey = xmlGetProp(mapNode, (xmlChar*) "map"); | ||
| 102 | if (idKey == 0) exit(2); | ||
| 103 | map.setRightMapID(atoi((char*) idKey)); | ||
| 104 | xmlFree(idKey); | ||
| 105 | } | ||
| 106 | } else if (!xmlStrcmp(mapNode->name, (const xmlChar*) "upmap")) | ||
| 107 | { | ||
| 108 | xmlChar* typeKey = xmlGetProp(mapNode, (xmlChar*) "type"); | ||
| 109 | if (typeKey == 0) exit(2); | ||
| 110 | map.setUpMoveType(Map::moveTypeForShort((char*) typeKey)); | ||
| 111 | xmlFree(typeKey); | ||
| 112 | |||
| 113 | if (Map::moveTypeTakesMap(map.getUpMoveType())) | ||
| 114 | { | ||
| 115 | xmlChar* idKey = xmlGetProp(mapNode, (xmlChar*) "map"); | ||
| 116 | if (idKey == 0) exit(2); | ||
| 117 | map.setUpMapID(atoi((char*) idKey)); | ||
| 118 | xmlFree(idKey); | ||
| 119 | } | ||
| 120 | } else if (!xmlStrcmp(mapNode->name, (const xmlChar*) "downmap")) | ||
| 121 | { | ||
| 122 | xmlChar* typeKey = xmlGetProp(mapNode, (xmlChar*) "type"); | ||
| 123 | if (typeKey == 0) exit(2); | ||
| 124 | map.setDownMoveType(Map::moveTypeForShort((char*) typeKey)); | ||
| 125 | xmlFree(typeKey); | ||
| 126 | |||
| 127 | if (Map::moveTypeTakesMap(map.getDownMoveType())) | ||
| 128 | { | ||
| 129 | xmlChar* idKey = xmlGetProp(mapNode, (xmlChar*) "map"); | ||
| 130 | if (idKey == 0) exit(2); | ||
| 131 | map.setDownMapID(atoi((char*) idKey)); | ||
| 132 | xmlFree(idKey); | ||
| 133 | } | ||
| 134 | } else if (!xmlStrcmp(mapNode->name, (const xmlChar*) "entities")) | ||
| 135 | { | ||
| 136 | for (xmlNodePtr entityNode = mapNode->xmlChildrenNode; entityNode != NULL; entityNode = entityNode->next) | ||
| 137 | { | ||
| 138 | if (!xmlStrcmp(entityNode->name, (const xmlChar*) "entity")) | ||
| 139 | { | ||
| 140 | Map::EntityData data; | ||
| 141 | |||
| 142 | for (xmlNodePtr entityDataNode = entityNode->xmlChildrenNode; entityDataNode != NULL; entityDataNode = entityDataNode->next) | ||
| 143 | { | ||
| 144 | if (!xmlStrcmp(entityDataNode->name, (const xmlChar*) "entity-type")) | ||
| 145 | { | ||
| 146 | xmlChar* key = xmlNodeListGetString(doc, entityDataNode->xmlChildrenNode, 1); | ||
| 147 | data.name = (char*) key; | ||
| 148 | xmlFree(key); | ||
| 149 | } else if (!xmlStrcmp(entityDataNode->name, (const xmlChar*) "entity-position")) | ||
| 150 | { | ||
| 151 | xmlChar* key = xmlNodeListGetString(doc, entityDataNode->xmlChildrenNode, 1); | ||
| 152 | sscanf((char*) key, "%d,%d", &data.position.first, &data.position.second); | ||
| 153 | xmlFree(key); | ||
| 154 | } | ||
| 155 | } | ||
| 156 | |||
| 157 | map.addEntity(data); | ||
| 158 | } | ||
| 159 | } | ||
| 160 | } | ||
| 161 | } | ||
| 162 | |||
| 163 | maps[theId] = map; | ||
| 164 | } | ||
| 165 | } | ||
| 166 | |||
| 167 | xmlFreeDoc(doc); | ||
| 168 | |||
| 169 | // Set up entities | ||
| 13 | player = std::make_shared<Entity>(); | 170 | player = std::make_shared<Entity>(); |
| 14 | player->position = std::make_pair(100.0,100.0); | 171 | player->position = startPos; |
| 15 | player->size = std::make_pair(10.0,12.0); | 172 | player->size = std::make_pair(10.0,12.0); |
| 16 | 173 | ||
| 17 | auto player_input = std::make_shared<UserMovementComponent>(); | 174 | auto player_input = std::make_shared<UserMovementComponent>(); |
| @@ -23,7 +180,7 @@ Game::Game() | |||
| 23 | auto player_anim = std::make_shared<PlayerSpriteComponent>(); | 180 | auto player_anim = std::make_shared<PlayerSpriteComponent>(); |
| 24 | player->addComponent(player_anim); | 181 | player->addComponent(player_anim); |
| 25 | 182 | ||
| 26 | Map& startingMap = Map::getNamedMap("embarass"); | 183 | Map& startingMap = maps[startMap]; |
| 27 | save = {&startingMap, player->position}; | 184 | save = {&startingMap, player->position}; |
| 28 | 185 | ||
| 29 | loadMap(startingMap, player->position); | 186 | loadMap(startingMap, player->position); |
| @@ -168,3 +325,8 @@ void Game::playerDie() | |||
| 168 | player->send(*this, Message::Type::stopDying); | 325 | player->send(*this, Message::Type::stopDying); |
| 169 | }); | 326 | }); |
| 170 | } | 327 | } |
| 328 | |||
| 329 | const Map& Game::getMap(int id) const | ||
| 330 | { | ||
| 331 | return maps.at(id); | ||
| 332 | } | ||
