diff options
Diffstat (limited to 'src/game.cpp')
-rw-r--r-- | src/game.cpp | 164 |
1 files changed, 5 insertions, 159 deletions
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 @@ | |||
1 | #include "game.h" | 1 | #include "game.h" |
2 | #include <cstdlib> | 2 | #include <cstdlib> |
3 | #include <libxml/parser.h> | ||
4 | #include "renderer.h" | 3 | #include "renderer.h" |
5 | #include "muxer.h" | 4 | #include "muxer.h" |
6 | #include "map.h" | 5 | #include "map.h" |
@@ -11,164 +10,11 @@ | |||
11 | #include "components/map_collision.h" | 10 | #include "components/map_collision.h" |
12 | #include "consts.h" | 11 | #include "consts.h" |
13 | 12 | ||
14 | Game::Game(const char* mapfile) | 13 | Game::Game(const char* mapfile) : world(mapfile) |
15 | { | 14 | { |
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 | 15 | // Set up entities |
170 | player = std::make_shared<Entity>(); | 16 | player = std::make_shared<Entity>(); |
171 | player->position = startPos; | 17 | player->position = world.getStartingPosition(); |
172 | player->size = std::make_pair(10.0,12.0); | 18 | player->size = std::make_pair(10.0,12.0); |
173 | 19 | ||
174 | auto player_input = std::make_shared<UserMovementComponent>(); | 20 | auto player_input = std::make_shared<UserMovementComponent>(); |
@@ -180,7 +26,7 @@ Game::Game(const char* mapfile) | |||
180 | auto player_anim = std::make_shared<PlayerSpriteComponent>(); | 26 | auto player_anim = std::make_shared<PlayerSpriteComponent>(); |
181 | player->addComponent(player_anim); | 27 | player->addComponent(player_anim); |
182 | 28 | ||
183 | Map& startingMap = maps[startMap]; | 29 | const Map& startingMap = world.getStartingMap(); |
184 | save = {&startingMap, player->position}; | 30 | save = {&startingMap, player->position}; |
185 | 31 | ||
186 | loadMap(startingMap, player->position); | 32 | loadMap(startingMap, player->position); |
@@ -326,7 +172,7 @@ void Game::playerDie() | |||
326 | }); | 172 | }); |
327 | } | 173 | } |
328 | 174 | ||
329 | const Map& Game::getMap(int id) const | 175 | const World& Game::getWorld() const |
330 | { | 176 | { |
331 | return maps.at(id); | 177 | return world; |
332 | } | 178 | } |