-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataManager.cpp
More file actions
131 lines (117 loc) · 4.37 KB
/
Copy pathDataManager.cpp
File metadata and controls
131 lines (117 loc) · 4.37 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include "DataManager.hpp"
std::map <std::string, bool> DataManager::bMap;
std::map <std::string, float> DataManager::fMap;
std::map <std::string, std::string> DataManager::sMap;
std::map <std::string, sf::Vector2f> DataManager::f2Map;
void DataManager::load() {
std::ifstream file(DATAPATH+std::string("savefile"));
if (!file.is_open()) return;
std::vector<std::pair<int, std::string> > v;
int lineNum = 0;
std::string line;
while (getline(file,line)) {
++lineNum;
for (int i = 0; i < int(line.size()-1); ++i) {
if (line[i] == '/' && line[i+1] == '/') {
line = line.substr(0,i);
break;
}
}
std::istringstream aux(line);
std::string s;
while(aux >> s)
v.push_back(std::make_pair(lineNum,s));
}
int dataType = -1; // 0-> bool, 1 -> float, 2 -> string, 3->Vector2f
int numberOfelements = 0;
for (int i = 0; i < int(v.size()); ++i) {
if (numberOfelements == 0) {
numberOfelements = myStoi(v[i].second);
++dataType;
continue;
}
--numberOfelements;
// std::cout << v[i].second << std::endl;
switch(dataType){
case 0:
bMap.insert(std::make_pair(v[i].second,myStob(v[i+1].second)));
i += 1;
break;
case 1:
fMap.insert(std::make_pair(v[i].second,std::stof(v[i+1].second)));
i += 1;
break;
case 2:
sMap.insert(std::make_pair(v[i].second,v[i+1].second));
i += 1;
break;
case 3:
f2Map.insert(std::make_pair(v[i].second,sf::Vector2f(std::stof(v[i+1].second),std::stof(v[i+2].second))));
i += 2;
break;
default:
exit(EXIT_FAILURE);
}
}
}
void DataManager::save() {
std::ofstream file;
file.open(DATAPATH+std::string("savefile"));
int boolSize = bMap.size();
int floatSize = fMap.size();
int stringSize = sMap.size();
int vec2fSize = f2Map.size();
file << boolSize << std::endl;
for (auto it = bMap.begin(); it != bMap.end(); ++it) file << it->first << " " << it->second << std::endl;
file << floatSize << std::endl;
for (auto it = fMap.begin(); it != fMap.end(); ++it) file << it->first << " " << it->second << std::endl;
file << stringSize << std::endl;
for (auto it = sMap.begin(); it != sMap.end(); ++it) file << it->first << " " << it->second << std::endl;
file << vec2fSize << std::endl;
for (auto it = f2Map.begin(); it != f2Map.end(); ++it) file << it->first << " " << it->second.x << " " << it->second.y << std::endl;
file.close();
}
void DataManager::reset() {
// TODO: First we must to save all the data that is not of the play (options, achivements, etc..)
bMap.clear();
fMap.clear();
sMap.clear();
f2Map.clear();
save();
}
bool DataManager::getBool(std::string key, bool defaultValue) {
auto it = bMap.find(key);
if (it == bMap.end()) return defaultValue;
else return it->second;
}
float DataManager::getFloat(std::string key, float defaultValue) {
auto it = fMap.find(key);
if (it == fMap.end()) return defaultValue;
else return it->second;
}
std::string DataManager::getString(std::string key, std::string defaultValue) {
auto it = sMap.find(key);
if (it == sMap.end()) return defaultValue;
else return it->second;
}
sf::Vector2f DataManager::getVector2f(std::string key, sf::Vector2f defaultValue) {
auto it = f2Map.find(key);
if (it == f2Map.end()) return defaultValue;
else return it->second;
}
void DataManager::setBool(std::string key, bool value) {
if (bMap.find(key) == bMap.end()) bMap.insert(std::make_pair(key,value));
else bMap.at(key) = value;
}
void DataManager::setFloat(std::string key, float value) {
if (fMap.find(key) == fMap.end()) fMap.insert(std::make_pair(key,value));
else fMap.at(key) = value;
}
void DataManager::setString(std::string key, std::string value) {
if (sMap.find(key) == sMap.end()) sMap.insert(std::make_pair(key,value));
else sMap.at(key) = value;
}
void DataManager::setVector2f(std::string key, sf::Vector2f value) {
if (f2Map.find(key) == f2Map.end()) f2Map.insert(std::make_pair(key,value));
else f2Map.at(key) = value;
}