From 046ee24a341468e9b3ea2983a731dbce18b52ac6 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Sun, 13 May 2018 11:00:02 -0400 Subject: Integrated RealizableComponent into RealizingSystem --- src/systems/realizing.cpp | 105 +++++++++++++++++++--------------------------- 1 file changed, 42 insertions(+), 63 deletions(-) (limited to 'src/systems/realizing.cpp') 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 @@ #include "game.h" #include "consts.h" #include "animation.h" -#include "components/realizable.h" #include "components/mappable.h" #include "components/animatable.h" #include "components/playable.h" @@ -30,25 +29,21 @@ inline xmlChar* getProp(xmlNodePtr node, const char* attr) } // TODO: neither the XML doc nor any of the emplaced entities are properly -// destroyed if this method throws an exception. -EntityManager::id_type RealizingSystem::initSingleton( +// destroyed if this constructor throws an exception. +RealizingSystem::RealizingSystem( + Game& game, std::string worldFile, - std::string prototypeFile) + std::string prototypeFile) : + System(game), + worldFile_(std::move(worldFile)), + prototypeFile_(std::move(prototypeFile)) { - id_type world = game_.getEntityManager().emplaceEntity(); - - auto& realizable = game_.getEntityManager(). - emplaceComponent(world); - - realizable.worldFile = worldFile; - realizable.prototypeFile = prototypeFile; - auto& mapping = game_.getSystemManager().getSystem(); xmlChar* key = nullptr; // Create a mapping between prototype names and the XML trees defining them. - xmlDocPtr protoXml = xmlParseFile(prototypeFile.c_str()); + xmlDocPtr protoXml = xmlParseFile(prototypeFile_.c_str()); if (protoXml == nullptr) { throw std::invalid_argument("Cannot find prototypes file"); @@ -82,7 +77,7 @@ EntityManager::id_type RealizingSystem::initSingleton( } // Create entities from the world definition. - xmlDocPtr doc = xmlParseFile(worldFile.c_str()); + xmlDocPtr doc = xmlParseFile(worldFile_.c_str()); if (doc == nullptr) { throw std::invalid_argument("Cannot find world file"); @@ -100,15 +95,15 @@ EntityManager::id_type RealizingSystem::initSingleton( } key = getProp(top, "startx"); - realizable.startingPos.x() = atoi(reinterpret_cast(key)); + startingPos_.x() = atoi(reinterpret_cast(key)); xmlFree(key); key = getProp(top, "starty"); - realizable.startingPos.y() = atoi(reinterpret_cast(key)); + startingPos_.y() = atoi(reinterpret_cast(key)); xmlFree(key); key = getProp(top, "startmap"); - realizable.startingMapId = atoi(reinterpret_cast(key)); + startingMapId_ = atoi(reinterpret_cast(key)); xmlFree(key); for (xmlNodePtr node = top->xmlChildrenNode; @@ -291,78 +286,62 @@ EntityManager::id_type RealizingSystem::initSingleton( mapping.generateBoundaries(map); - realizable.maps.insert(map); - realizable.entityByMapId[mappable.mapId] = map; + entityByMapId_[mappable.mapId] = map; } } xmlFreeDoc(doc); xmlFreeDoc(protoXml); - loadMap(realizable.entityByMapId[realizable.startingMapId]); - - return world; + activateMap(entityByMapId_[startingMapId_]); } -EntityManager::id_type RealizingSystem::getSingleton() const +void RealizingSystem::loadMap(id_type mapEntity) { - std::set result = - game_.getEntityManager().getEntitiesWithComponents< - RealizableComponent>(); - - if (result.empty()) - { - throw std::logic_error("No realizable entity found"); - } else if (result.size() > 1) - { - throw std::logic_error("Multiple realizable entities found"); - } - - return *std::begin(result); + deactivateMap(); + activateMap(mapEntity); } -void RealizingSystem::loadMap(id_type mapEntity) +void RealizingSystem::deactivateMap() { - id_type world = getSingleton(); + id_type oldMap = activeMap_; - auto& realizable = game_.getEntityManager(). - getComponent(world); + auto& oldMappable = game_.getEntityManager(). + getComponent(oldMap); - auto& animating = game_.getSystemManager().getSystem(); - auto& pondering = game_.getSystemManager().getSystem(); + // Deactivate any map objects from the old map. + for (id_type prototype : oldMappable.objects) + { + leaveActiveMap(prototype); + } + // Deactivate players that were on the old map. std::set players = game_.getEntityManager().getEntitiesWithComponents< PlayableComponent>(); - if (realizable.hasActiveMap) + for (id_type player : players) { - id_type oldMap = realizable.activeMap; - - auto& oldMappable = game_.getEntityManager(). - getComponent(oldMap); + auto& playable = game_.getEntityManager(). + getComponent(player); - // Deactivate any map objects from the old map. - for (id_type prototype : oldMappable.objects) + if (playable.mapId == oldMap) { - leaveActiveMap(prototype); + leaveActiveMap(player); } + } +} - // Deactivate players that were on the old map. - for (id_type player : players) - { - auto& playable = game_.getEntityManager(). - getComponent(player); +void RealizingSystem::activateMap(id_type mapEntity) +{ + auto& animating = game_.getSystemManager().getSystem(); + auto& pondering = game_.getSystemManager().getSystem(); - if (playable.mapId == oldMap) - { - leaveActiveMap(player); - } - } - } + std::set players = + game_.getEntityManager().getEntitiesWithComponents< + PlayableComponent>(); - realizable.hasActiveMap = true; - realizable.activeMap = mapEntity; + activeMap_ = mapEntity; auto& mappable = game_.getEntityManager(). getComponent(mapEntity); -- cgit 1.4.1