summary refs log tree commit diff stats
path: root/src/systems/realizing.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/systems/realizing.cpp')
-rw-r--r--src/systems/realizing.cpp105
1 files changed, 42 insertions, 63 deletions
diff --git a/src/systems/realizing.cpp b/src/systems/realizing.cpp index 2ee5897..7f5aefb 100644 --- a/src/systems/realizing.cpp +++ b/src/systems/realizing.cpp
@@ -6,7 +6,6 @@
6#include "game.h" 6#include "game.h"
7#include "consts.h" 7#include "consts.h"
8#include "animation.h" 8#include "animation.h"
9#include "components/realizable.h"
10#include "components/mappable.h" 9#include "components/mappable.h"
11#include "components/animatable.h" 10#include "components/animatable.h"
12#include "components/playable.h" 11#include "components/playable.h"
@@ -30,25 +29,21 @@ inline xmlChar* getProp(xmlNodePtr node, const char* attr)
30} 29}
31 30
32// TODO: neither the XML doc nor any of the emplaced entities are properly 31// TODO: neither the XML doc nor any of the emplaced entities are properly
33// destroyed if this method throws an exception. 32// destroyed if this constructor throws an exception.
34EntityManager::id_type RealizingSystem::initSingleton( 33RealizingSystem::RealizingSystem(
34 Game& game,
35 std::string worldFile, 35 std::string worldFile,
36 std::string prototypeFile) 36 std::string prototypeFile) :
37 System(game),
38 worldFile_(std::move(worldFile)),
39 prototypeFile_(std::move(prototypeFile))
37{ 40{
38 id_type world = game_.getEntityManager().emplaceEntity();
39
40 auto& realizable = game_.getEntityManager().
41 emplaceComponent<RealizableComponent>(world);
42
43 realizable.worldFile = worldFile;
44 realizable.prototypeFile = prototypeFile;
45
46 auto& mapping = game_.getSystemManager().getSystem<MappingSystem>(); 41 auto& mapping = game_.getSystemManager().getSystem<MappingSystem>();
47 42
48 xmlChar* key = nullptr; 43 xmlChar* key = nullptr;
49 44
50 // Create a mapping between prototype names and the XML trees defining them. 45 // Create a mapping between prototype names and the XML trees defining them.
51 xmlDocPtr protoXml = xmlParseFile(prototypeFile.c_str()); 46 xmlDocPtr protoXml = xmlParseFile(prototypeFile_.c_str());
52 if (protoXml == nullptr) 47 if (protoXml == nullptr)
53 { 48 {
54 throw std::invalid_argument("Cannot find prototypes file"); 49 throw std::invalid_argument("Cannot find prototypes file");
@@ -82,7 +77,7 @@ EntityManager::id_type RealizingSystem::initSingleton(
82 } 77 }
83 78
84 // Create entities from the world definition. 79 // Create entities from the world definition.
85 xmlDocPtr doc = xmlParseFile(worldFile.c_str()); 80 xmlDocPtr doc = xmlParseFile(worldFile_.c_str());
86 if (doc == nullptr) 81 if (doc == nullptr)
87 { 82 {
88 throw std::invalid_argument("Cannot find world file"); 83 throw std::invalid_argument("Cannot find world file");
@@ -100,15 +95,15 @@ EntityManager::id_type RealizingSystem::initSingleton(
100 } 95 }
101 96
102 key = getProp(top, "startx"); 97 key = getProp(top, "startx");
103 realizable.startingPos.x() = atoi(reinterpret_cast<char*>(key)); 98 startingPos_.x() = atoi(reinterpret_cast<char*>(key));
104 xmlFree(key); 99 xmlFree(key);
105 100
106 key = getProp(top, "starty"); 101 key = getProp(top, "starty");
107 realizable.startingPos.y() = atoi(reinterpret_cast<char*>(key)); 102 startingPos_.y() = atoi(reinterpret_cast<char*>(key));
108 xmlFree(key); 103 xmlFree(key);
109 104
110 key = getProp(top, "startmap"); 105 key = getProp(top, "startmap");
111 realizable.startingMapId = atoi(reinterpret_cast<char*>(key)); 106 startingMapId_ = atoi(reinterpret_cast<char*>(key));
112 xmlFree(key); 107 xmlFree(key);
113 108
114 for (xmlNodePtr node = top->xmlChildrenNode; 109 for (xmlNodePtr node = top->xmlChildrenNode;
@@ -291,78 +286,62 @@ EntityManager::id_type RealizingSystem::initSingleton(
291 286
292 mapping.generateBoundaries(map); 287 mapping.generateBoundaries(map);
293 288
294 realizable.maps.insert(map); 289 entityByMapId_[mappable.mapId] = map;
295 realizable.entityByMapId[mappable.mapId] = map;
296 } 290 }
297 } 291 }
298 292
299 xmlFreeDoc(doc); 293 xmlFreeDoc(doc);
300 xmlFreeDoc(protoXml); 294 xmlFreeDoc(protoXml);
301 295
302 loadMap(realizable.entityByMapId[realizable.startingMapId]); 296 activateMap(entityByMapId_[startingMapId_]);
303
304 return world;
305} 297}
306 298
307EntityManager::id_type RealizingSystem::getSingleton() const 299void RealizingSystem::loadMap(id_type mapEntity)
308{ 300{
309 std::set<id_type> result = 301 deactivateMap();
310 game_.getEntityManager().getEntitiesWithComponents< 302 activateMap(mapEntity);
311 RealizableComponent>();
312
313 if (result.empty())
314 {
315 throw std::logic_error("No realizable entity found");
316 } else if (result.size() > 1)
317 {
318 throw std::logic_error("Multiple realizable entities found");
319 }
320
321 return *std::begin(result);
322} 303}
323 304
324void RealizingSystem::loadMap(id_type mapEntity) 305void RealizingSystem::deactivateMap()
325{ 306{
326 id_type world = getSingleton(); 307 id_type oldMap = activeMap_;
327 308
328 auto& realizable = game_.getEntityManager(). 309 auto& oldMappable = game_.getEntityManager().
329 getComponent<RealizableComponent>(world); 310 getComponent<MappableComponent>(oldMap);
330 311
331 auto& animating = game_.getSystemManager().getSystem<AnimatingSystem>(); 312 // Deactivate any map objects from the old map.
332 auto& pondering = game_.getSystemManager().getSystem<PonderingSystem>(); 313 for (id_type prototype : oldMappable.objects)
314 {
315 leaveActiveMap(prototype);
316 }
333 317
318 // Deactivate players that were on the old map.
334 std::set<id_type> players = 319 std::set<id_type> players =
335 game_.getEntityManager().getEntitiesWithComponents< 320 game_.getEntityManager().getEntitiesWithComponents<
336 PlayableComponent>(); 321 PlayableComponent>();
337 322
338 if (realizable.hasActiveMap) 323 for (id_type player : players)
339 { 324 {
340 id_type oldMap = realizable.activeMap; 325 auto& playable = game_.getEntityManager().
341 326 getComponent<PlayableComponent>(player);
342 auto& oldMappable = game_.getEntityManager().
343 getComponent<MappableComponent>(oldMap);
344 327
345 // Deactivate any map objects from the old map. 328 if (playable.mapId == oldMap)
346 for (id_type prototype : oldMappable.objects)
347 { 329 {
348 leaveActiveMap(prototype); 330 leaveActiveMap(player);
349 } 331 }
332 }
333}
350 334
351 // Deactivate players that were on the old map. 335void RealizingSystem::activateMap(id_type mapEntity)
352 for (id_type player : players) 336{
353 { 337 auto& animating = game_.getSystemManager().getSystem<AnimatingSystem>();
354 auto& playable = game_.getEntityManager(). 338 auto& pondering = game_.getSystemManager().getSystem<PonderingSystem>();
355 getComponent<PlayableComponent>(player);
356 339
357 if (playable.mapId == oldMap) 340 std::set<id_type> players =
358 { 341 game_.getEntityManager().getEntitiesWithComponents<
359 leaveActiveMap(player); 342 PlayableComponent>();
360 }
361 }
362 }
363 343
364 realizable.hasActiveMap = true; 344 activeMap_ = mapEntity;
365 realizable.activeMap = mapEntity;
366 345
367 auto& mappable = game_.getEntityManager(). 346 auto& mappable = game_.getEntityManager().
368 getComponent<MappableComponent>(mapEntity); 347 getComponent<MappableComponent>(mapEntity);