-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathComponentSerializer.cpp
93 lines (66 loc) · 2.37 KB
/
ComponentSerializer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
Copyright (c) 2017 InversePalindrome
Nihil - ComponentSerializer.cpp
InversePalindrome.com
*/
#include "ComponentSerializer.hpp"
#include "FilePaths.hpp"
#include <brigand/algorithms/for_each.hpp>
#include <boost/algorithm/string/trim.hpp>
#include <map>
#include <fstream>
#include <sstream>
ComponentSerializer::ComponentSerializer(Entities& entities) :
entities(entities)
{
}
EntityProperties& ComponentSerializer::getProperties()
{
return this->properties;
}
void ComponentSerializer::serialize(const std::string& fileName)
{
std::multimap<std::int32_t, std::string> entities;
brigand::for_each<ComponentList>([this, &entities](auto componentType)
{
using Type = decltype(componentType)::type;
this->entities.for_each<Type>([&entities](auto entity, auto & component)
{
std::stringstream stream;
std::int32_t entityID;
std::string componentData;
stream << component;
stream >> entityID;
if (entityID > 0)
{
std::getline(stream, componentData);
boost::trim_left(componentData);
entities.emplace(entityID, componentData);
}
});
});
std::ofstream outFile(Path::games / fileName);
for (auto entityItr = std::begin(entities); entityItr != std::end(entities);)
{
auto entityID = entityItr->first;
outFile << "Entity " << entityID << '\n';
do
{
outFile << entityItr->second << '\n';
++entityItr;
} while (entityItr != std::end(entities) && entityID == entityItr->first);
outFile << '\n';
}
}
void ComponentSerializer::saveBlueprint(const std::string & fileName, const std::vector<std::tuple<std::int32_t, std::string, sf::Vector2f>> & entitiesData)
{
std::ofstream outFile(Path::blueprints / fileName);
for (const auto& [entityID, fileName, position] : entitiesData)
{
outFile << entityID << ' ' << fileName << ' ' << position.x << ' ' << position.y << '\n';
}
}
void ComponentSerializer::setProperties(const EntityProperties & properties)
{
this->properties = properties;
}