diff options
author | Kelly Rauchenberger <fefferburbia@gmail.com> | 2015-03-18 18:23:54 -0400 |
---|---|---|
committer | Kelly Rauchenberger <fefferburbia@gmail.com> | 2015-03-18 18:23:54 -0400 |
commit | 103587c2d5f9deb20e549a86cdf5023b429cc6a1 (patch) | |
tree | f1e2fc60b86395e0cc40646ad1e4396a835b682f /src | |
parent | 2ec163612042bfa5e4e1bf220b489506f7039677 (diff) | |
download | therapy-103587c2d5f9deb20e549a86cdf5023b429cc6a1.tar.gz therapy-103587c2d5f9deb20e549a86cdf5023b429cc6a1.tar.bz2 therapy-103587c2d5f9deb20e549a86cdf5023b429cc6a1.zip |
Wrote an XML Schema describing maps file and also changed the spec a bit
Diffstat (limited to 'src')
-rw-r--r-- | src/components/map_collision.cpp | 16 | ||||
-rw-r--r-- | src/game.cpp | 164 | ||||
-rw-r--r-- | src/game.h | 7 | ||||
-rw-r--r-- | src/map.cpp | 107 | ||||
-rw-r--r-- | src/map.h | 35 | ||||
-rw-r--r-- | src/world.cpp | 134 | ||||
-rw-r--r-- | src/world.h | 21 |
7 files changed, 208 insertions, 276 deletions
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 @@ | |||
5 | 5 | ||
6 | MapCollisionComponent::MapCollisionComponent(const Map& map) : map(map) | 6 | MapCollisionComponent::MapCollisionComponent(const Map& map) : map(map) |
7 | { | 7 | { |
8 | addCollision(-6, 0, GAME_HEIGHT, Direction::left, collisionFromMoveType(map.getLeftMoveType())); | 8 | addCollision(-6, 0, GAME_HEIGHT, Direction::left, collisionFromMoveType(map.getAdjacent(Map::MoveDir::Left).type)); |
9 | addCollision(GAME_WIDTH+6, 0, GAME_HEIGHT, Direction::right, collisionFromMoveType(map.getRightMoveType())); | 9 | addCollision(GAME_WIDTH+6, 0, GAME_HEIGHT, Direction::right, collisionFromMoveType(map.getAdjacent(Map::MoveDir::Right).type)); |
10 | addCollision(-7, 0, GAME_WIDTH, Direction::up, collisionFromMoveType(map.getUpMoveType())); | 10 | addCollision(-7, 0, GAME_WIDTH, Direction::up, collisionFromMoveType(map.getAdjacent(Map::MoveDir::Up).type)); |
11 | addCollision(GAME_HEIGHT+6, 0, GAME_WIDTH, Direction::down, collisionFromMoveType(map.getDownMoveType())); | 11 | addCollision(GAME_HEIGHT+6, 0, GAME_WIDTH, Direction::down, collisionFromMoveType(map.getAdjacent(Map::MoveDir::Down).type)); |
12 | 12 | ||
13 | for (int i=0; i<MAP_WIDTH*MAP_HEIGHT; i++) | 13 | for (int i=0; i<MAP_WIDTH*MAP_HEIGHT; i++) |
14 | { | 14 | { |
@@ -189,16 +189,16 @@ void MapCollisionComponent::processCollision(Game& game, Entity& collider, Colli | |||
189 | { | 189 | { |
190 | if (dir == Direction::left) | 190 | if (dir == Direction::left) |
191 | { | 191 | { |
192 | game.loadMap(game.getMap(map.getLeftMapID()), std::make_pair(GAME_WIDTH-collider.size.first/2, old_position.second)); | 192 | game.loadMap(game.getWorld().getMap(map.getAdjacent(Map::MoveDir::Left).map), std::make_pair(GAME_WIDTH-collider.size.first/2, old_position.second)); |
193 | } else if (dir == Direction::right) | 193 | } else if (dir == Direction::right) |
194 | { | 194 | { |
195 | game.loadMap(game.getMap(map.getRightMapID()), std::make_pair(-collider.size.first/2, old_position.second)); | 195 | game.loadMap(game.getWorld().getMap(map.getAdjacent(Map::MoveDir::Right).map), std::make_pair(-collider.size.first/2, old_position.second)); |
196 | } else if (dir == Direction::up) | 196 | } else if (dir == Direction::up) |
197 | { | 197 | { |
198 | game.loadMap(game.getMap(map.getUpMapID()), std::make_pair(old_position.first, GAME_HEIGHT-collider.size.second/2)); | 198 | game.loadMap(game.getWorld().getMap(map.getAdjacent(Map::MoveDir::Up).map), std::make_pair(old_position.first, GAME_HEIGHT-collider.size.second/2)); |
199 | } else if (dir == Direction::down) | 199 | } else if (dir == Direction::down) |
200 | { | 200 | { |
201 | game.loadMap(game.getMap(map.getDownMapID()), std::make_pair(old_position.first, -collider.size.second/2)); | 201 | game.loadMap(game.getWorld().getMap(map.getAdjacent(Map::MoveDir::Down).map), std::make_pair(old_position.first, -collider.size.second/2)); |
202 | } | 202 | } |
203 | } else if (collision.type == Collision::Type::reverse) | 203 | } else if (collision.type == Collision::Type::reverse) |
204 | { | 204 | { |
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 | } |
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 @@ | |||
6 | #include <list> | 6 | #include <list> |
7 | #include <map> | 7 | #include <map> |
8 | #include "map.h" | 8 | #include "map.h" |
9 | #include "world.h" | ||
9 | 10 | ||
10 | class Entity; | 11 | class Entity; |
11 | struct GLFWwindow; | 12 | struct GLFWwindow; |
@@ -24,14 +25,11 @@ class Game { | |||
24 | void saveGame(); | 25 | void saveGame(); |
25 | void schedule(double time, std::function<void ()> callback); | 26 | void schedule(double time, std::function<void ()> callback); |
26 | void playerDie(); | 27 | void playerDie(); |
27 | const Map& getMap(int id) const; | 28 | const World& getWorld() const; |
28 | 29 | ||
29 | private: | 30 | private: |
30 | friend void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods); | 31 | friend void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods); |
31 | 32 | ||
32 | std::map<int, Map> maps; | ||
33 | int startMap; | ||
34 | std::pair<int, int> startPos; | ||
35 | std::list<std::shared_ptr<Entity>> entities; | 33 | std::list<std::shared_ptr<Entity>> entities; |
36 | std::list<std::shared_ptr<Entity>> nextEntities; | 34 | std::list<std::shared_ptr<Entity>> nextEntities; |
37 | std::pair<double, double> nextPosition; | 35 | std::pair<double, double> nextPosition; |
@@ -41,6 +39,7 @@ class Game { | |||
41 | Savefile save; | 39 | Savefile save; |
42 | std::list<std::pair<double, std::function<void ()>>> scheduled; | 40 | std::list<std::pair<double, std::function<void ()>>> scheduled; |
43 | bool shouldQuit = false; | 41 | bool shouldQuit = false; |
42 | World world; | ||
44 | }; | 43 | }; |
45 | 44 | ||
46 | #endif | 45 | #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) | |||
20 | 20 | ||
21 | id = map.id; | 21 | id = map.id; |
22 | title = map.title; | 22 | title = map.title; |
23 | leftMap = map.leftMap; | 23 | adjacents = map.adjacents; |
24 | rightMap = map.rightMap; | ||
25 | downMap = map.downMap; | ||
26 | upMap = map.upMap; | ||
27 | leftType = map.leftType; | ||
28 | rightType = map.rightType; | ||
29 | upType = map.upType; | ||
30 | downType = map.downType; | ||
31 | entities = map.entities; | 24 | entities = map.entities; |
32 | } | 25 | } |
33 | 26 | ||
@@ -52,14 +45,7 @@ void swap(Map& first, Map& second) | |||
52 | { | 45 | { |
53 | std::swap(first.mapdata, second.mapdata); | 46 | std::swap(first.mapdata, second.mapdata); |
54 | std::swap(first.title, second.title); | 47 | std::swap(first.title, second.title); |
55 | std::swap(first.leftMap, second.leftMap); | 48 | std::swap(first.adjacents, second.adjacents); |
56 | std::swap(first.rightMap, second.rightMap); | ||
57 | std::swap(first.downMap, second.downMap); | ||
58 | std::swap(first.upMap, second.upMap); | ||
59 | std::swap(first.leftType, second.leftType); | ||
60 | std::swap(first.rightType, second.rightType); | ||
61 | std::swap(first.upType, second.upType); | ||
62 | std::swap(first.downType, second.downType); | ||
63 | std::swap(first.id, second.id); | 49 | std::swap(first.id, second.id); |
64 | std::swap(first.entities, second.entities); | 50 | std::swap(first.entities, second.entities); |
65 | } | 51 | } |
@@ -109,44 +95,24 @@ Map::MoveType Map::moveTypeForShort(std::string str) | |||
109 | return MoveType::Wall; | 95 | return MoveType::Wall; |
110 | } | 96 | } |
111 | 97 | ||
112 | Map::MoveType Map::getLeftMoveType() const | 98 | Map::MoveDir Map::moveDirForShort(std::string str) |
113 | { | 99 | { |
114 | return leftType; | 100 | if (str == "right") return MoveDir::Right; |
115 | } | 101 | if (str == "up") return MoveDir::Up; |
116 | 102 | if (str == "down") return MoveDir::Down; | |
117 | Map::MoveType Map::getRightMoveType() const | 103 | |
118 | { | 104 | return MoveDir::Left; |
119 | return rightType; | ||
120 | } | ||
121 | |||
122 | Map::MoveType Map::getUpMoveType() const | ||
123 | { | ||
124 | return upType; | ||
125 | } | ||
126 | |||
127 | Map::MoveType Map::getDownMoveType() const | ||
128 | { | ||
129 | return downType; | ||
130 | } | ||
131 | |||
132 | int Map::getLeftMapID() const | ||
133 | { | ||
134 | return leftMap; | ||
135 | } | ||
136 | |||
137 | int Map::getRightMapID() const | ||
138 | { | ||
139 | return rightMap; | ||
140 | } | ||
141 | |||
142 | int Map::getUpMapID() const | ||
143 | { | ||
144 | return upMap; | ||
145 | } | 105 | } |
146 | 106 | ||
147 | int Map::getDownMapID() const | 107 | static const Map::Adjacent defaultAdjacent {}; |
108 | const Map::Adjacent& Map::getAdjacent(MoveDir dir) const | ||
148 | { | 109 | { |
149 | return downMap; | 110 | if (adjacents.count(dir) > 0) |
111 | { | ||
112 | return adjacents.at(dir); | ||
113 | } else { | ||
114 | return defaultAdjacent; | ||
115 | } | ||
150 | } | 116 | } |
151 | 117 | ||
152 | bool Map::moveTypeTakesMap(MoveType type) | 118 | bool Map::moveTypeTakesMap(MoveType type) |
@@ -171,44 +137,11 @@ void Map::setTitle(std::string title) | |||
171 | this->title = title; | 137 | this->title = title; |
172 | } | 138 | } |
173 | 139 | ||
174 | void Map::setLeftMoveType(MoveType type) | 140 | void Map::setAdjacent(MoveDir dir, MoveType type, int map) |
175 | { | ||
176 | leftType = type; | ||
177 | } | ||
178 | |||
179 | void Map::setRightMoveType(MoveType type) | ||
180 | { | ||
181 | rightType = type; | ||
182 | } | ||
183 | |||
184 | void Map::setUpMoveType(MoveType type) | ||
185 | { | ||
186 | upType = type; | ||
187 | } | ||
188 | |||
189 | void Map::setDownMoveType(MoveType type) | ||
190 | { | ||
191 | downType = type; | ||
192 | } | ||
193 | |||
194 | void Map::setLeftMapID(int id) | ||
195 | { | ||
196 | leftMap = id; | ||
197 | } | ||
198 | |||
199 | void Map::setRightMapID(int id) | ||
200 | { | ||
201 | rightMap = id; | ||
202 | } | ||
203 | |||
204 | void Map::setUpMapID(int id) | ||
205 | { | ||
206 | upMap = id; | ||
207 | } | ||
208 | |||
209 | void Map::setDownMapID(int id) | ||
210 | { | 141 | { |
211 | downMap = id; | 142 | Adjacent& cur = adjacents[dir]; |
143 | cur.type = type; | ||
144 | if (map != -1) cur.map = map; | ||
212 | } | 145 | } |
213 | 146 | ||
214 | void Map::addEntity(EntityData& data) | 147 | 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 @@ | |||
3 | 3 | ||
4 | #include <string> | 4 | #include <string> |
5 | #include <list> | 5 | #include <list> |
6 | #include <map> | ||
6 | 7 | ||
7 | class Entity; | 8 | class Entity; |
8 | 9 | ||
@@ -23,25 +24,31 @@ class Map { | |||
23 | ReverseWarp | 24 | ReverseWarp |
24 | }; | 25 | }; |
25 | 26 | ||
27 | enum class MoveDir { | ||
28 | Left, | ||
29 | Right, | ||
30 | Up, | ||
31 | Down | ||
32 | }; | ||
33 | |||
26 | struct EntityData { | 34 | struct EntityData { |
27 | std::string name; | 35 | std::string name; |
28 | std::pair<int, int> position; | 36 | std::pair<int, int> position; |
29 | }; | 37 | }; |
30 | 38 | ||
39 | struct Adjacent { | ||
40 | MoveType type = MoveType::Wall; | ||
41 | int map = -1; | ||
42 | }; | ||
43 | |||
31 | static MoveType moveTypeForShort(std::string str); | 44 | static MoveType moveTypeForShort(std::string str); |
45 | static MoveDir moveDirForShort(std::string str); | ||
32 | static bool moveTypeTakesMap(MoveType type); | 46 | static bool moveTypeTakesMap(MoveType type); |
33 | 47 | ||
34 | int getID() const; | 48 | int getID() const; |
35 | const int* getMapdata() const; | 49 | const int* getMapdata() const; |
36 | std::string getTitle() const; | 50 | std::string getTitle() const; |
37 | MoveType getLeftMoveType() const; | 51 | const Adjacent& getAdjacent(MoveDir dir) const; |
38 | MoveType getRightMoveType() const; | ||
39 | MoveType getUpMoveType() const; | ||
40 | MoveType getDownMoveType() const; | ||
41 | int getLeftMapID() const; | ||
42 | int getRightMapID() const; | ||
43 | int getUpMapID() const; | ||
44 | int getDownMapID() const; | ||
45 | 52 | ||
46 | void createEntities(std::list<std::shared_ptr<Entity>>& entities) const; | 53 | void createEntities(std::list<std::shared_ptr<Entity>>& entities) const; |
47 | bool operator==(const Map& other) const; | 54 | bool operator==(const Map& other) const; |
@@ -49,22 +56,14 @@ class Map { | |||
49 | 56 | ||
50 | void setMapdata(int* mapdata); | 57 | void setMapdata(int* mapdata); |
51 | void setTitle(std::string title); | 58 | void setTitle(std::string title); |
52 | void setLeftMoveType(MoveType type); | 59 | void setAdjacent(MoveDir dir, MoveType type, int map); |
53 | void setRightMoveType(MoveType type); | ||
54 | void setUpMoveType(MoveType type); | ||
55 | void setDownMoveType(MoveType type); | ||
56 | void setLeftMapID(int id); | ||
57 | void setRightMapID(int id); | ||
58 | void setUpMapID(int id); | ||
59 | void setDownMapID(int id); | ||
60 | void addEntity(EntityData& data); | 60 | void addEntity(EntityData& data); |
61 | private: | 61 | private: |
62 | int* mapdata; | 62 | int* mapdata; |
63 | std::string title; | 63 | std::string title; |
64 | int id; | 64 | int id; |
65 | std::list<EntityData> entities; | 65 | std::list<EntityData> entities; |
66 | MoveType leftType, rightType, upType, downType; | 66 | std::map<MoveDir, Adjacent> adjacents; |
67 | int leftMap, rightMap, upMap, downMap; | ||
68 | }; | 67 | }; |
69 | 68 | ||
70 | #endif | 69 | #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 @@ | |||
1 | #include "world.h" | ||
2 | #include <libxml/parser.h> | ||
3 | #include "consts.h" | ||
4 | |||
5 | World::World(const char* filename) | ||
6 | { | ||
7 | xmlDocPtr doc = xmlParseFile(filename); | ||
8 | if (doc == nullptr) | ||
9 | { | ||
10 | exit(2); | ||
11 | } | ||
12 | |||
13 | xmlNodePtr top = xmlDocGetRootElement(doc); | ||
14 | if (top == nullptr) | ||
15 | { | ||
16 | exit(2); | ||
17 | } | ||
18 | |||
19 | if (xmlStrcmp(top->name, (const xmlChar*) "world")) | ||
20 | { | ||
21 | exit(2); | ||
22 | } | ||
23 | |||
24 | xmlChar* startxKey = xmlGetProp(top, (xmlChar*) "startx"); | ||
25 | if (startxKey == 0) exit(2); | ||
26 | startX = atoi((char*) startxKey); | ||
27 | xmlFree(startxKey); | ||
28 | |||
29 | xmlChar* startyKey = xmlGetProp(top, (xmlChar*) "starty"); | ||
30 | if (startyKey == 0) exit(2); | ||
31 | startY = atoi((char*) startyKey); | ||
32 | xmlFree(startyKey); | ||
33 | |||
34 | xmlChar* startmapKey = xmlGetProp(top, (xmlChar*) "startmap"); | ||
35 | if (startxKey == 0) exit(2); | ||
36 | startMap = atoi((char*) startmapKey); | ||
37 | xmlFree(startmapKey); | ||
38 | |||
39 | for (xmlNodePtr node = top->xmlChildrenNode; node != NULL; node = node->next) | ||
40 | { | ||
41 | if (!xmlStrcmp(node->name, (const xmlChar*) "map")) | ||
42 | { | ||
43 | xmlChar* idKey = xmlGetProp(node, (xmlChar*) "id"); | ||
44 | if (idKey == 0) exit(2); | ||
45 | int theId = atoi((char*) idKey); | ||
46 | xmlFree(idKey); | ||
47 | |||
48 | maps.emplace(theId, theId); | ||
49 | Map& map = maps[theId]; | ||
50 | |||
51 | xmlChar* titleKey = xmlGetProp(node, (xmlChar*) "title"); | ||
52 | if (titleKey == 0) exit(2); | ||
53 | map.setTitle((char*) titleKey); | ||
54 | xmlFree(titleKey); | ||
55 | |||
56 | for (xmlNodePtr mapNode = node->xmlChildrenNode; mapNode != NULL; mapNode = mapNode->next) | ||
57 | { | ||
58 | if (!xmlStrcmp(mapNode->name, (const xmlChar*) "environment")) | ||
59 | { | ||
60 | xmlChar* key = xmlNodeListGetString(doc, mapNode->xmlChildrenNode, 1); | ||
61 | int* mapdata = (int*) malloc(MAP_WIDTH*MAP_HEIGHT*sizeof(int)); | ||
62 | mapdata[0] = atoi(strtok((char*) key, ",\n")); | ||
63 | for (int i=1; i<(MAP_WIDTH*MAP_HEIGHT); i++) | ||
64 | { | ||
65 | mapdata[i] = atoi(strtok(NULL, ",\n")); | ||
66 | } | ||
67 | map.setMapdata(mapdata); | ||
68 | xmlFree(key); | ||
69 | } else if (!xmlStrcmp(mapNode->name, (const xmlChar*) "entity")) | ||
70 | { | ||
71 | Map::EntityData data; | ||
72 | |||
73 | xmlChar* typeKey = xmlGetProp(mapNode, (const xmlChar*) "type"); | ||
74 | if (typeKey == 0) exit(2); | ||
75 | data.name = (char*) typeKey; | ||
76 | xmlFree(typeKey); | ||
77 | |||
78 | xmlChar* xKey = xmlGetProp(mapNode, (const xmlChar*) "x"); | ||
79 | if (xKey == 0) exit(2); | ||
80 | data.position.first = atoi((char*) xKey); | ||
81 | xmlFree(xKey); | ||
82 | |||
83 | xmlChar* yKey = xmlGetProp(mapNode, (const xmlChar*) "y"); | ||
84 | if (yKey == 0) exit(2); | ||
85 | data.position.second = atoi((char*) yKey); | ||
86 | xmlFree(yKey); | ||
87 | |||
88 | map.addEntity(data); | ||
89 | } else if (!xmlStrcmp(mapNode->name, (const xmlChar*) "adjacent")) | ||
90 | { | ||
91 | Map::MoveDir direction; | ||
92 | Map::MoveType moveType; | ||
93 | int mapId = 0; | ||
94 | |||
95 | xmlChar* dirKey = xmlGetProp(mapNode, (const xmlChar*) "dir"); | ||
96 | if (dirKey == 0) exit(2); | ||
97 | direction = Map::moveDirForShort((char*) dirKey); | ||
98 | xmlFree(dirKey); | ||
99 | |||
100 | xmlChar* typeKey = xmlGetProp(mapNode, (const xmlChar*) "type"); | ||
101 | if (typeKey == 0) exit(2); | ||
102 | moveType = Map::moveTypeForShort((char*) typeKey); | ||
103 | xmlFree(typeKey); | ||
104 | |||
105 | xmlChar* mapIdKey = xmlGetProp(mapNode, (const xmlChar*) "map"); | ||
106 | if (mapIdKey != 0) | ||
107 | { | ||
108 | mapId = atoi((char*) mapIdKey); | ||
109 | } | ||
110 | xmlFree(mapIdKey); | ||
111 | |||
112 | map.setAdjacent(direction, moveType, mapId); | ||
113 | } | ||
114 | } | ||
115 | } | ||
116 | } | ||
117 | |||
118 | xmlFreeDoc(doc); | ||
119 | } | ||
120 | |||
121 | const Map& World::getMap(int id) const | ||
122 | { | ||
123 | return maps.at(id); | ||
124 | } | ||
125 | |||
126 | const Map& World::getStartingMap() const | ||
127 | { | ||
128 | return maps.at(startMap); | ||
129 | } | ||
130 | |||
131 | std::pair<int, int> World::getStartingPosition() const | ||
132 | { | ||
133 | return std::make_pair(startX, startY); | ||
134 | } | ||
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 @@ | |||
1 | #ifndef WORLD_H | ||
2 | #define WORLD_H | ||
3 | |||
4 | #include <map> | ||
5 | #include "map.h" | ||
6 | |||
7 | class World { | ||
8 | public: | ||
9 | World(const char* filename); | ||
10 | const Map& getMap(int id) const; | ||
11 | const Map& getStartingMap() const; | ||
12 | std::pair<int, int> getStartingPosition() const; | ||
13 | |||
14 | private: | ||
15 | std::map<int, Map> maps; | ||
16 | int startMap; | ||
17 | int startX; | ||
18 | int startY; | ||
19 | }; | ||
20 | |||
21 | #endif /* end of include guard: WORLD_H */ | ||