void EntityDefinitionManager::load(const String& path) { Preferences::PreferenceManager& prefs = Preferences::PreferenceManager::preferences(); const Color& defaultColor = prefs.getColor(Preferences::EntityBoundsColor); EntityDefinitionMap newDefinitions; IO::FileManager fileManager; IO::MappedFile::Ptr file = fileManager.mapFile(path); if (file.get() != NULL) { try { const String extension = fileManager.pathExtension(path); if (Utility::equalsString(extension, "def", false)) { IO::DefParser parser(file->begin(), file->end(), defaultColor); EntityDefinition* definition = NULL; while ((definition = parser.nextDefinition()) != NULL) Utility::insertOrReplace(newDefinitions, definition->name(), definition); } else if (Utility::equalsString(extension, "fgd", false)) { IO::FgdParser parser(file->begin(), file->end(), defaultColor); EntityDefinition* definition = NULL; while ((definition = parser.nextDefinition()) != NULL) Utility::insertOrReplace(newDefinitions, definition->name(), definition); } clear(); m_entityDefinitions = newDefinitions; m_path = path; } catch (IO::ParserException e) { Utility::deleteAll(newDefinitions); m_console.error(e.what()); } } else { m_console.error("Unable to open entity definition file %s", path.c_str()); } }
//--------------------------------------- Entity* MageGame::SpawnEntity( const std::string& name, const Vec2f& position ) { EntityDefinition* entityDef = EntityDefinition::GetDefinitionByName( name ); if ( entityDef ) { Entity& entity = entityDef->Create( position ); entity.SetGameMap( &mGameMap ); return &entity; } ConsolePrintf( CONSOLE_ERROR, "Failed to spawn Entity: No Entity found '%s'\n", name.c_str() ); return NULL; }
void EntityDefinitionManager::updateGroups() { clearGroups(); using GroupMap = std::map<String, EntityDefinitionList>; GroupMap groupMap; for (size_t i = 0; i < m_definitions.size(); ++i) { EntityDefinition* definition = m_definitions[i]; const String groupName = definition->groupName(); groupMap[groupName].push_back(definition); } for (const auto& entry : groupMap) { const String& groupName = entry.first; const EntityDefinitionList& definitions = entry.second; m_groups.push_back(EntityDefinitionGroup(groupName, definitions)); } }
EntityDefinitionManager::EntityDefinitionGroups EntityDefinitionManager::groups(EntityDefinition::Type type, SortOrder order) { EntityDefinitionGroups groups; EntityDefinitionList list = definitions(type, order); EntityDefinitionList ungrouped; EntityDefinitionList::const_iterator it, end; for (it = list.begin(), end = list.end(); it != end; ++it) { EntityDefinition* definition = *it; const String groupName = definition->groupName(); if (groupName.empty()) ungrouped.push_back(definition); else groups[groupName].push_back(definition); } for (it = ungrouped.begin(), end = ungrouped.end(); it != end; ++it) { EntityDefinition* definition = *it; const String shortName = Utility::capitalize(definition->shortName()); EntityDefinitionGroups::iterator groupIt = groups.find(shortName); if (groupIt == groups.end()) groups["Misc"].push_back(definition); else groupIt->second.push_back(definition); } EntityDefinitionGroups::iterator groupIt, groupEnd; for (groupIt = groups.begin(), groupEnd = groups.end(); groupIt != groupEnd; ++groupIt) { EntityDefinitionList& definitions = groupIt->second; if (order == Usage) std::sort(definitions.begin(), definitions.end(), CompareEntityDefinitionsByUsage()); else std::sort(definitions.begin(), definitions.end(), CompareEntityDefinitionsByName(true)); } return groups; }