summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
authorKelly Rauchenberger <fefferburbia@gmail.com>2018-04-28 18:52:55 -0400
committerKelly Rauchenberger <fefferburbia@gmail.com>2018-04-28 18:52:55 -0400
commit36cceabfc5ddd22d9ae0d6c4dee9d4041bf2e348 (patch)
tree070a870726b7659f47c26568181e166ea9face3b /src
parentd0f191b3b7419f846342ea5eba5a9bb69cf9bb14 (diff)
downloadtherapy-36cceabfc5ddd22d9ae0d6c4dee9d4041bf2e348.tar.gz
therapy-36cceabfc5ddd22d9ae0d6c4dee9d4041bf2e348.tar.bz2
therapy-36cceabfc5ddd22d9ae0d6c4dee9d4041bf2e348.zip
Implemented map object sprites
Map objects cannot be interacted with or collided with yet but the sprites are loaded.
Diffstat (limited to 'src')
-rw-r--r--src/components/realizable.h7
-rw-r--r--src/game.cpp5
-rw-r--r--src/systems/realizing.cpp104
-rw-r--r--src/systems/realizing.h7
4 files changed, 116 insertions, 7 deletions
diff --git a/src/components/realizable.h b/src/components/realizable.h index f6a7eb4..0858e7a 100644 --- a/src/components/realizable.h +++ b/src/components/realizable.h
@@ -19,6 +19,13 @@ public:
19 std::string worldFile; 19 std::string worldFile;
20 20
21 /** 21 /**
22 * Path to the XML file containing the map object prototype definitions.
23 *
24 * @managed_by RealizingSystem
25 */
26 std::string prototypeFile;
27
28 /**
22 * Starting map and player location for a new game. 29 * Starting map and player location for a new game.
23 * 30 *
24 * @managed_by RealizingSystem 31 * @managed_by RealizingSystem
diff --git a/src/game.cpp b/src/game.cpp index b7dd200..d10c52c 100644 --- a/src/game.cpp +++ b/src/game.cpp
@@ -40,7 +40,10 @@ Game::Game()
40 systemManager_.emplaceSystem<MappingSystem>(*this); 40 systemManager_.emplaceSystem<MappingSystem>(*this);
41 systemManager_.emplaceSystem<AnimatingSystem>(*this); 41 systemManager_.emplaceSystem<AnimatingSystem>(*this);
42 42
43 systemManager_.getSystem<RealizingSystem>().initSingleton("res/maps.xml"); 43 systemManager_.getSystem<RealizingSystem>().initSingleton(
44 "res/maps.xml",
45 "res/entities.xml");
46
44 systemManager_.getSystem<PlayingSystem>().initPlayer(); 47 systemManager_.getSystem<PlayingSystem>().initPlayer();
45 48
46 glfwSwapInterval(1); 49 glfwSwapInterval(1);
diff --git a/src/systems/realizing.cpp b/src/systems/realizing.cpp index 09c38f3..c86dd5e 100644 --- a/src/systems/realizing.cpp +++ b/src/systems/realizing.cpp
@@ -2,8 +2,10 @@
2#include <stdexcept> 2#include <stdexcept>
3#include <libxml/parser.h> 3#include <libxml/parser.h>
4#include <cstring> 4#include <cstring>
5#include <map>
5#include "game.h" 6#include "game.h"
6#include "consts.h" 7#include "consts.h"
8#include "animation.h"
7#include "components/realizable.h" 9#include "components/realizable.h"
8#include "components/mappable.h" 10#include "components/mappable.h"
9#include "components/animatable.h" 11#include "components/animatable.h"
@@ -27,16 +29,58 @@ inline xmlChar* getProp(xmlNodePtr node, const char* attr)
27 29
28// TODO: neither the XML doc nor any of the emplaced entities are properly 30// TODO: neither the XML doc nor any of the emplaced entities are properly
29// destroyed if this method throws an exception. 31// destroyed if this method throws an exception.
30EntityManager::id_type RealizingSystem::initSingleton(std::string filename) 32EntityManager::id_type RealizingSystem::initSingleton(
33 std::string worldFile,
34 std::string prototypeFile)
31{ 35{
32 id_type world = game_.getEntityManager().emplaceEntity(); 36 id_type world = game_.getEntityManager().emplaceEntity();
33 37
34 auto& realizable = game_.getEntityManager(). 38 auto& realizable = game_.getEntityManager().
35 emplaceComponent<RealizableComponent>(world); 39 emplaceComponent<RealizableComponent>(world);
36 40
41 realizable.worldFile = worldFile;
42 realizable.prototypeFile = prototypeFile;
43
37 auto& mapping = game_.getSystemManager().getSystem<MappingSystem>(); 44 auto& mapping = game_.getSystemManager().getSystem<MappingSystem>();
38 45
39 xmlDocPtr doc = xmlParseFile(filename.c_str()); 46 xmlChar* key = nullptr;
47
48 // Create a mapping between prototype names and the XML trees defining them.
49 xmlDocPtr protoXml = xmlParseFile(prototypeFile.c_str());
50 if (protoXml == nullptr)
51 {
52 throw std::invalid_argument("Cannot find prototypes file");
53 }
54
55 xmlNodePtr protoTop = xmlDocGetRootElement(protoXml);
56 if (protoTop == nullptr)
57 {
58 throw std::invalid_argument("Error parsing prototypes file");
59 }
60
61 if (xmlStrcmp(protoTop->name, reinterpret_cast<const xmlChar*>("entities")))
62 {
63 throw std::invalid_argument("Error parsing prototypes file");
64 }
65
66 std::map<std::string, xmlNodePtr> prototypes;
67
68 for (xmlNodePtr node = protoTop->xmlChildrenNode;
69 node != nullptr;
70 node = node->next)
71 {
72 if (!xmlStrcmp(node->name, reinterpret_cast<const xmlChar*>("entity")))
73 {
74 key = getProp(node, "id");
75 std::string prototypeId = reinterpret_cast<char*>(key);
76 xmlFree(key);
77
78 prototypes[prototypeId] = node;
79 }
80 }
81
82 // Create entities from the world definition.
83 xmlDocPtr doc = xmlParseFile(worldFile.c_str());
40 if (doc == nullptr) 84 if (doc == nullptr)
41 { 85 {
42 throw std::invalid_argument("Cannot find world file"); 86 throw std::invalid_argument("Cannot find world file");
@@ -53,8 +97,6 @@ EntityManager::id_type RealizingSystem::initSingleton(std::string filename)
53 throw std::invalid_argument("Error parsing world file"); 97 throw std::invalid_argument("Error parsing world file");
54 } 98 }
55 99
56 xmlChar* key = nullptr;
57
58 key = getProp(top, "startx"); 100 key = getProp(top, "startx");
59 realizable.startingX = atoi(reinterpret_cast<char*>(key)); 101 realizable.startingX = atoi(reinterpret_cast<char*>(key));
60 xmlFree(key); 102 xmlFree(key);
@@ -115,6 +157,59 @@ EntityManager::id_type RealizingSystem::initSingleton(std::string filename)
115 xmlFree(key); 157 xmlFree(key);
116 } else if (!xmlStrcmp( 158 } else if (!xmlStrcmp(
117 mapNode->name, 159 mapNode->name,
160 reinterpret_cast<const xmlChar*>("entity")))
161 {
162 id_type mapObject = game_.getEntityManager().emplaceEntity();
163
164 key = getProp(mapNode, "type");
165 std::string prototypeId = reinterpret_cast<char*>(key);
166 xmlFree(key);
167
168 xmlNodePtr prototypeNode = prototypes[prototypeId];
169
170 // Set the coordinates from the object definition.
171 auto& transformable = game_.getEntityManager().
172 emplaceComponent<TransformableComponent>(mapObject);
173
174 key = getProp(mapNode, "x");
175 transformable.origX = atoi(reinterpret_cast<char*>(key));
176 xmlFree(key);
177
178 key = getProp(mapNode, "y");
179 transformable.origY = atoi(reinterpret_cast<char*>(key));
180 xmlFree(key);
181
182 // Set the sprite and size using the prototype definition.
183 key = getProp(prototypeNode, "sprite");
184 std::string spritePath = reinterpret_cast<char*>(key);
185 xmlFree(key);
186
187 key = getProp(prototypeNode, "width");
188 transformable.origW = atoi(reinterpret_cast<char*>(key));
189 xmlFree(key);
190
191 key = getProp(prototypeNode, "height");
192 transformable.origH = atoi(reinterpret_cast<char*>(key));
193 xmlFree(key);
194
195 AnimationSet objectAnim(
196 spritePath.c_str(),
197 transformable.origW,
198 transformable.origH,
199 1);
200
201 objectAnim.emplaceAnimation("static", 0, 1, 1);
202
203 auto& animatable = game_.getEntityManager().
204 emplaceComponent<AnimatableComponent>(
205 mapObject,
206 std::move(objectAnim));
207
208 animatable.origAnimation = "static";
209
210 mappable.objects.push_back(mapObject);
211 } else if (!xmlStrcmp(
212 mapNode->name,
118 reinterpret_cast<const xmlChar*>("adjacent"))) 213 reinterpret_cast<const xmlChar*>("adjacent")))
119 { 214 {
120 key = getProp(mapNode, "type"); 215 key = getProp(mapNode, "type");
@@ -172,6 +267,7 @@ EntityManager::id_type RealizingSystem::initSingleton(std::string filename)
172 } 267 }
173 268
174 xmlFreeDoc(doc); 269 xmlFreeDoc(doc);
270 xmlFreeDoc(protoXml);
175 271
176 loadMap(realizable.entityByMapId[realizable.startingMapId]); 272 loadMap(realizable.entityByMapId[realizable.startingMapId]);
177 273
diff --git a/src/systems/realizing.h b/src/systems/realizing.h index c681892..595c58f 100644 --- a/src/systems/realizing.h +++ b/src/systems/realizing.h
@@ -1,6 +1,7 @@
1#ifndef REALIZING_H_6853748C 1#ifndef REALIZING_H_6853748C
2#define REALIZING_H_6853748C 2#define REALIZING_H_6853748C
3 3
4#include <string>
4#include "system.h" 5#include "system.h"
5 6
6class RealizingSystem : public System { 7class RealizingSystem : public System {
@@ -12,9 +13,11 @@ public:
12 13
13 /** 14 /**
14 * Creates the singleton realizable entity and initializes it with the 15 * Creates the singleton realizable entity and initializes it with the
15 * provided world definition. 16 * provided world definition and map object prototype definition.
16 */ 17 */
17 id_type initSingleton(std::string filename); 18 id_type initSingleton(
19 std::string worldFile,
20 std::string prototypeFile);
18 21
19 /** 22 /**
20 * Helper method that returns the entity ID of the (assumed) singleton entity 23 * Helper method that returns the entity ID of the (assumed) singleton entity