-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecordsScreen.cpp
More file actions
182 lines (164 loc) · 6.69 KB
/
RecordsScreen.cpp
File metadata and controls
182 lines (164 loc) · 6.69 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
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
//
// Created by vb-jakub on 18.06.24.
//
#include "RecordsScreen.h"
#include <iostream>
#include <fstream>
//Font is taken from the website with free assets - https://www.fontsquirrel.com/fonts/lato?q%5Bterm%5D=Lato&q%5Bsearch_check%5D=Y
//Textures is taken from the website with free assets - https://gamecodeschool.com/gamemaker-studio/buiding-a-timberman-game-in-gamemaker-studio-2/
RecordsScreen::RecordsScreen(sf::RenderWindow & windowRef, GameView &viewRef, GameMode currentGameMode, int playerScore) :
win(windowRef), view(viewRef){
offsetX=50;
offsetY=150;
recordsOffsetX=430;
recordsOffsetY=80;
letterSize=50;
if(!font.loadFromFile("assets/Lato_Font.ttf"))
std::cerr << "ERROR: Font did't open." << std::endl;
resetButton.setFont(font);
resetButton.setCharacterSize(letterSize+10);
resetButton.setString("RESET");
resetButton.setFillColor(sf::Color::Red);
resetButton.setOrigin(resetButton.getLocalBounds().width/2.0f,resetButton.getLocalBounds().height/2.0f);
resetButton.setPosition(static_cast<float>(win.getSize().x)/2.0f,static_cast<float>(win.getSize().y)-120);
this->playerScore=playerScore;
playerNick="";
switch(currentGameMode) {
case EASY: file="/EasyMode.txt"; break;
case NORMAL: file="/NormalMode.txt"; break;
case HARD: file="/HardMode.txt"; break;
case DEBUG: file="/DebugMode.txt"; break;
}
readFromFile();
setPlayerPosition();
addNewRecord();
if(!soundBuffer.loadFromFile("sounds/click.wav"))
std::cerr << "ERROR: Click sound didn't open." << std::endl;
click.setBuffer(soundBuffer);
}
void RecordsScreen::readFromFile() {
std::ifstream readStream(recordsDir+file);
if(readStream) {
PlayerInfo tmp;
int points;
char space;
std::string name;
while(readStream>>points) {
tmp.points=points;
readStream.get(space);
getline(readStream,name);
tmp.playerName=name;
recordsBoard.push_back(tmp);
}
readStream.close();
}else std::cerr << "ERROR: Record file did't open." << std::endl;
}
void RecordsScreen::setPlayerPosition() {
isNewRecord=false;
playerPosition=-1;
for(int place=0; place<static_cast<int>(recordsBoard.size()) and !isNewRecord; ++place)
if(recordsBoard[place].points<playerScore) {
isNewRecord=true;
playerPosition=place;
}
if(!isNewRecord and recordsBoard.size()<10) {
isNewRecord=true;
playerPosition=static_cast<int>(recordsBoard.size());
}
}
void RecordsScreen::addNewRecord() {
if(isNewRecord) {
PlayerInfo newScore{playerScore, playerNick};
recordsBoard.push_back(newScore);
std::sort(recordsBoard.begin(),recordsBoard.end(),
[](const PlayerInfo &next, const PlayerInfo &actual){return next.points>actual.points;});
while(recordsBoard.size()>10){
recordsBoard.pop_back();
}
}
}
void RecordsScreen::drawRecordsScreen() const {
view.draw();
sf::RectangleShape background{sf::Vector2f(static_cast<float>(win.getSize().x),static_cast<float>(win.getSize().y))};
background.setFillColor(sf::Color(0,0,0,128));
win.draw(background);
sf::Text text;
text.setFont(font);
text.setCharacterSize(letterSize+15);
text.setFillColor(sf::Color::White);
text.setString("TOP PLAYERS");
text.setOrigin(text.getLocalBounds().width/2.0f,text.getLocalBounds().height/2.0f);
text.setPosition(static_cast<float>(win.getSize().x)/2.0f,50);
win.draw(text);
win.draw(resetButton);
drawRecordsBoard();
}
void RecordsScreen::drawRecordsBoard() const {
sf::Text text;
text.setFont(font);
text.setCharacterSize(letterSize);
for(int place=0; place<static_cast<int>(recordsBoard.size()); ++place) {
switch(place) {
case 0: text.setFillColor(sf::Color(212,175,55)); break;
case 1: text.setFillColor(sf::Color(165,169,180)); break;
case 2: text.setFillColor(sf::Color(110,77,37)); break;
default: text.setFillColor(sf::Color::White); break;
}
if(place<5) text.setPosition(offsetX,offsetY+static_cast<float>(place)*recordsOffsetY);
else text.setPosition(offsetX+recordsOffsetX,offsetY+static_cast<float>(place-5)*recordsOffsetY);
text.setString(std::to_string(recordsBoard[place].points));
win.draw(text);
text.move(90,0);
text.setString(recordsBoard[place].playerName);
win.draw(text);
}
}
void RecordsScreen::controllRecordScreen(const sf::Event &event, bool &reset) {
controllResetButton(event,reset);
if(isNewRecord) controllNickEnter(event);
}
void RecordsScreen::controllResetButton(const sf::Event &event, bool &reset) {
sf::Vector2i mousePosition=sf::Mouse::getPosition(win);
if(resetButton.getGlobalBounds().contains(static_cast<sf::Vector2f>(mousePosition)))
resetButton.setCharacterSize(letterSize+45);
else resetButton.setCharacterSize(letterSize+10);
resetButton.setOrigin(resetButton.getLocalBounds().width/2.0f,resetButton.getLocalBounds().height/2.0f);
resetButton.setPosition(static_cast<float>(win.getSize().x)/2.0f,static_cast<float>(win.getSize().y)-120);
if(event.type==sf::Event::MouseButtonPressed and event.mouseButton.button==sf::Mouse::Left) {
click.play();
if(resetButton.getGlobalBounds().contains(static_cast<sf::Vector2f>(mousePosition))) reset=true;
}
}
void RecordsScreen::controllNickEnter(const sf::Event &event) {
if(event.type==sf::Event::TextEntered) {
auto sign=event.text.unicode;
//if press enter
if(sign==13){
saveToFile();
isNewRecord=false;
click.play();
}
if(playerNick.length()<8) {
//if press number,large or small letter
if((sign>47 and sign<58) or (sign>64 and sign<91) or (sign>96 and sign<123)) {
playerNick.push_back(static_cast<char>(event.text.unicode));
recordsBoard[playerPosition].playerName=playerNick;
click.play();
}
}
//if press backspace
if(sign==8) {
if(!playerNick.empty()) playerNick.pop_back();
recordsBoard[playerPosition].playerName=playerNick;
click.play();
}
}
}
void RecordsScreen::saveToFile() const {
std::ofstream saveStream(recordsDir+file);
if(saveStream) {
for(int place=0; place<static_cast<int>(recordsBoard.size()); ++place)
saveStream << recordsBoard[place].points << " " << recordsBoard[place].playerName << std::endl;
saveStream.close();
}else std::cerr << "ERROR: Record file did't open." << std::endl;
}