summary refs log tree commit diff stats
path: root/src/world.cpp
diff options
context:
space:
mode:
authorKelly Rauchenberger <fefferburbia@gmail.com>2018-04-28 09:22:44 -0400
committerKelly Rauchenberger <fefferburbia@gmail.com>2018-04-28 09:22:44 -0400
commit8016a7146fec3f6f43ca05723441750e5aae3d4d (patch)
tree0d527c1af80cf9ac34a027f9ee6f1acbb95db9f4 /src/world.cpp
parentf782b81ba10c9b7a1e221b16de0aaa7b6c521729 (diff)
downloadtherapy-8016a7146fec3f6f43ca05723441750e5aae3d4d.tar.gz
therapy-8016a7146fec3f6f43ca05723441750e5aae3d4d.tar.bz2
therapy-8016a7146fec3f6f43ca05723441750e5aae3d4d.zip
Restructured the way the world is loaded
The World class was removed and replaced by the RealizingSystem and RealizableComponent. The realizable entity is intended to be a singleton and to represent the world. The Map class was also removed and integrated into the MappableComponent.

These changes are to facilitate implementation of map objects without needing special intermediary objects (including the Map class). Now, map entities are created as soon as the world is created, and map object entities will be as well. They will simply be deactivated while the map is not active. Multiple players are now slightly better supported, which will be important in the future.

This will likely become inefficient as the world becomes bigger, and some sort of sector-loading process will have to be designed. This also reduces the usefulness of EntityManager's entity-searching capabilities (which are not the most efficiently implemented currently anyway), and will likely in the future require some added functionality to better search subsets of entities.

A lot of the components were also rewritten to use bare member variables instead of accessor methods, as they never had special functionality and just took up space. These components were also documented.
Diffstat (limited to 'src/world.cpp')
-rw-r--r--src/world.cpp155
1 files changed, 0 insertions, 155 deletions
diff --git a/src/world.cpp b/src/world.cpp deleted file mode 100644 index 3b6bd41..0000000 --- a/src/world.cpp +++ /dev/null
@@ -1,155 +0,0 @@
1#include "world.h"
2#include <libxml/parser.h>
3#include <stdexcept>
4#include <cstring>
5#include "consts.h"
6
7inline xmlChar* getProp(xmlNodePtr node, const char* attr)
8{
9 xmlChar* key = xmlGetProp(node, reinterpret_cast<const xmlChar*>(attr));
10 if (key == nullptr)
11 {
12 throw std::invalid_argument("Error parsing world file");
13 }
14
15 return key;
16}
17
18World::World(std::string filename)
19{
20 xmlDocPtr doc = xmlParseFile(filename.c_str());
21 if (doc == nullptr)
22 {
23 throw std::invalid_argument("Cannot find world file");
24 }
25
26 xmlNodePtr top = xmlDocGetRootElement(doc);
27 if (top == nullptr)
28 {
29 throw std::invalid_argument("Error parsing world file");
30 }
31
32 if (xmlStrcmp(top->name, reinterpret_cast<const xmlChar*>("world")))
33 {
34 throw std::invalid_argument("Error parsing world file");
35 }
36
37 xmlChar* key = nullptr;
38
39 key = getProp(top, "startx");
40 startX_ = atoi(reinterpret_cast<char*>(key));
41 xmlFree(key);
42
43 key = getProp(top, "starty");
44 startY_ = atoi(reinterpret_cast<char*>(key));
45 xmlFree(key);
46
47 key = getProp(top, "startmap");
48 startMap_ = atoi(reinterpret_cast<char*>(key));
49 xmlFree(key);
50
51 for (xmlNodePtr node = top->xmlChildrenNode;
52 node != nullptr;
53 node = node->next)
54 {
55 if (!xmlStrcmp(node->name, reinterpret_cast<const xmlChar*>("map")))
56 {
57 key = getProp(node, "id");
58 size_t mapId = atoi(reinterpret_cast<char*>(key));
59 xmlFree(key);
60
61 key = getProp(node, "title");
62 std::string mapTitle(reinterpret_cast<char*>(key));
63 xmlFree(key);
64
65 std::vector<int> mapTiles;
66 Map::Adjacent leftAdj;
67 Map::Adjacent rightAdj;
68 Map::Adjacent upAdj;
69 Map::Adjacent downAdj;
70
71 for (xmlNodePtr mapNode = node->xmlChildrenNode;
72 mapNode != nullptr;
73 mapNode = mapNode->next)
74 {
75 if (!xmlStrcmp(
76 mapNode->name,
77 reinterpret_cast<const xmlChar*>("environment")))
78 {
79 key = xmlNodeGetContent(mapNode);
80
81 mapTiles.clear();
82 mapTiles.push_back(atoi(strtok(reinterpret_cast<char*>(key), ",\n")));
83 for (size_t i = 1; i < (MAP_WIDTH * MAP_HEIGHT); i++)
84 {
85 mapTiles.push_back(atoi(strtok(nullptr, ",\n")));
86 }
87
88 xmlFree(key);
89 } else if (!xmlStrcmp(
90 mapNode->name,
91 reinterpret_cast<const xmlChar*>("adjacent")))
92 {
93 key = getProp(mapNode, "type");
94 std::string adjTypeStr(reinterpret_cast<char*>(key));
95 xmlFree(key);
96
97 Map::Adjacent::Type adjType;
98 if (adjTypeStr == "wall")
99 {
100 adjType = Map::Adjacent::Type::wall;
101 } else if (adjTypeStr == "wrap")
102 {
103 adjType = Map::Adjacent::Type::wrap;
104 } else if (adjTypeStr == "warp")
105 {
106 adjType = Map::Adjacent::Type::warp;
107 } else if (adjTypeStr == "reverseWarp")
108 {
109 adjType = Map::Adjacent::Type::reverse;
110 } else {
111 throw std::logic_error("Invalid adjacency type");
112 }
113
114 key = getProp(mapNode, "map");
115 int adjMapId = atoi(reinterpret_cast<char*>(key));
116 xmlFree(key);
117
118 key = getProp(mapNode, "dir");
119 std::string adjDir(reinterpret_cast<char*>(key));
120 xmlFree(key);
121
122 if (adjDir == "left")
123 {
124 leftAdj = {adjType, adjMapId};
125 } else if (adjDir == "right")
126 {
127 rightAdj = {adjType, adjMapId};
128 } else if (adjDir == "up")
129 {
130 upAdj = {adjType, adjMapId};
131 } else if (adjDir == "down")
132 {
133 downAdj = {adjType, adjMapId};
134 } else {
135 throw std::logic_error("Invalid adjacency direction");
136 }
137 }
138 }
139
140 maps_.emplace(
141 std::piecewise_construct,
142 std::forward_as_tuple(mapId),
143 std::forward_as_tuple(
144 mapId,
145 std::move(mapTiles),
146 std::move(mapTitle),
147 leftAdj,
148 rightAdj,
149 upAdj,
150 downAdj));
151 }
152 }
153
154 xmlFreeDoc(doc);
155}