-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameSettings.cpp
186 lines (168 loc) · 4.06 KB
/
GameSettings.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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include "GameSettings.h"
#include <fstream>
#include <iostream>
#include <sstream>
GameSettings* GameSettings::instance = NULL;
GameSettings::GameSettings()
{
}
GameSettings::~GameSettings()
{
}
GameSettings* GameSettings::getInstance()
{
if (instance == nullptr)
{
instance = new GameSettings();
}
return instance;
}
GameSettings::Controls_Settings* GameSettings::getSettings()
{
if (controlsSettings == nullptr)
{
controlsSettings = new Controls_Settings();
initKeys();
initConfig();
}
return controlsSettings;
}
GameSettings::Highscores* GameSettings::getHighscores()
{
if (highscores == nullptr)
{
highscores = new Highscores();
initHighscores();
}
return highscores;
}
void GameSettings::loadFiles()
{
getSettings();
getHighscores();
}
void GameSettings::saveKeys()
{
using namespace std;
ofstream outFile;
outFile.open("Config/Keybinds.ini", ios::out);
for (std::map<Controls_Key, sf::Keyboard::Key>::iterator it = controlsSettings->keybinds.begin(); it != controlsSettings->keybinds.end(); ++it)
{
outFile << controlsKeyStringMap.left.find(it->first)->second << ' ' << it->second << endl;
}
outFile.close();
}
void GameSettings::saveConfig()
{
using namespace std;
ofstream outFile;
outFile.open("Config/Config.ini", ios::out);
outFile << "SFX " << controlsSettings->sfx << endl;
outFile << "MUSIC " << controlsSettings->music << endl;
outFile.close();
}
void GameSettings::saveHighscores()
{
using namespace std;
std::cout << "save hs: " << highscores->limit << " " << highscores->endless << endl;
ofstream outFile;
outFile.open("Config/scores.sav", ios::out);
outFile << highscores->limit << " " << highscores->endless << " " << highscores->sprintTime << endl;
for (auto const& [name, score] : highscores->dropToBeatHS)
{
outFile << name << std::endl; // song name
outFile << score << std::endl; // song highscore
}
outFile.close();
}
int GameSettings::getBeatMapThreshold(std::string songName)
{
using namespace std;
ifstream beatStream("BeatMaps/" + songName +"/"+ songName+ ".txt");
int beatCount = 0;
if (beatStream.is_open()) {
char beat[10];
while (beatStream.getline(beat, 10, '\r')) // read bpm
{
beatCount++;
}
// remove the first init line
beatCount--;
}
cout << songName << " : " << 150 * beatCount << std::endl;
return 150 * beatCount;
}
void GameSettings::initKeys()
{
using namespace std;
ifstream keybindsStream("Config/Keybinds.ini");
controlsSettings->keybinds.clear();
if (keybindsStream.is_open())
{
string key = "";
int keyVal = 0;
while (keybindsStream >> key >> keyVal)
{
controlsSettings->keybinds[controlsKeyStringMap.right.find(key)->second] = sf::Keyboard::Key(keyVal);
std::cout << key << " " << keyVal << std::endl;
}
}
keybindsStream.close();
}
void GameSettings::initConfig()
{
using namespace std;
ifstream configStream("Config/Config.ini");
if (configStream.is_open())
{
string key = "";
int keyVal = 0;
while (configStream >> key >> keyVal)
{
if (key == "SFX")
{
controlsSettings->sfx = keyVal;
}
else if (key == "MUSIC")
{
controlsSettings->music = keyVal;
}
}
}
configStream.close();
}
void GameSettings::initHighscores()
{
using namespace std;
ifstream scoresStream("Config/scores.sav");
if (scoresStream.is_open()) {
std::string line("");
int limitHS, endlessHS, sprintHS;
// parse the first line
if (std::getline(scoresStream, line));
{
std::istringstream iss(line);
if (iss >> limitHS >> endlessHS >> sprintHS)
{
highscores->limit = limitHS;
highscores->endless = endlessHS;
highscores->sprintTime = sprintHS;
}
}
std::string songName;
int songHS;
while (std::getline(scoresStream, line)) // read song name here
{
songName = std::string(line);
highscores->dropToBeatThreshold.insert(std::pair<std::string, int> (songName, getBeatMapThreshold(songName)));
if (std::getline(scoresStream, line))
{
std::istringstream iss(line);
if (iss >> songHS) // read next line = song hs
{
highscores->dropToBeatHS.insert(std::pair<std::string, int>(songName, songHS));
}
}
}
}
}