-
Notifications
You must be signed in to change notification settings - Fork 0
/
scores_state.cpp
140 lines (109 loc) · 4.35 KB
/
scores_state.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
#include "scores_state.h"
const size_t TITLE_SIZE = 80;
const float TITLE_POSITION_X = 300.f;
const float TITLE_POSITION_Y = 7.f;
const size_t SUBTITLE_SIZE = 50;
const size_t BODY_TEXT_SIZE = 45;
const float PLAYER_SUBTITLE_POS_X = 100.f;
const float PLAYER_SUBTITLE_POS_Y = 100.f;
const float MARGIN_TOP = 50.f;
const float MARGIN_LEFT = 550.f;
namespace ScoreStateNames {
const std::string Title = "HIGHSCORES";
const std::string Player = "PLAYER";
const std::string Score = "POINTS";
const std::string BackToMain = "Press space for back to main menu";
}
ScoresState::ScoresState(Game *state_holder) : GameState(state_holder) {
sf::Texture *texture = game->getResourseManager().getTexture(Textures::Code::ScoreBackground);
sf::Font *font = game->getResourseManager().getFont(Fonts::Code::Score);
backgroundSprite.setTexture(*texture);
title.setFont(*font);
title.setString(ScoreStateNames::Title);
title.setColor(sf::Color::Magenta);
title.setCharacterSize(TITLE_SIZE);
title.setPosition(sf::Vector2f(TITLE_POSITION_X,
TITLE_POSITION_Y));
player_title.setFont(*font);
player_title.setString(ScoreStateNames::Player);
player_title.setColor(sf::Color::Cyan);
player_title.setCharacterSize(SUBTITLE_SIZE);
player_title.setPosition(sf::Vector2f(PLAYER_SUBTITLE_POS_X,
PLAYER_SUBTITLE_POS_Y));
scores_title.setFont(*font);
scores_title.setString(ScoreStateNames::Score);
scores_title.setColor(sf::Color::Cyan);
scores_title.setCharacterSize(SUBTITLE_SIZE);
scores_title.setPosition(sf::Vector2f(PLAYER_SUBTITLE_POS_X + MARGIN_LEFT,
PLAYER_SUBTITLE_POS_Y));
std::vector<ScoreRecord> vec;
game->loadHighScores(vec);
sf::Text player;
sf::Text score;
for(int i = 0; i < vec.size(); i++) {
player.setString(vec[i].name);
player.setCharacterSize(BODY_TEXT_SIZE);
player.setFont(*font);
player.setPosition(sf::Vector2f(PLAYER_SUBTITLE_POS_X,
PLAYER_SUBTITLE_POS_Y + MARGIN_TOP * (i + 1)));
score.setFont(*font);
score.setString(std::to_string(vec[i].scores));
score.setCharacterSize(BODY_TEXT_SIZE);
score.setPosition(sf::Vector2f(PLAYER_SUBTITLE_POS_X + MARGIN_LEFT + MARGIN_TOP / 2.f,
PLAYER_SUBTITLE_POS_Y + MARGIN_TOP * (i + 1)));
if(vec[i].name == game->getPlayerName()) {
player.setColor(sf::Color::Red);
score.setColor(sf::Color::Red);
}
else {
player.setColor(sf::Color::Blue);
score.setColor(sf::Color::Blue);
}
highscores.push_back(std::make_pair(player, score));
}
back_to_main.setFont(*font);
back_to_main.setString(ScoreStateNames::BackToMain);
back_to_main.setColor(sf::Color::Cyan);
back_to_main.setCharacterSize(BODY_TEXT_SIZE);
back_to_main.setPosition(sf::Vector2f(TITLE_POSITION_X - 120.f,
highscores.back().first.getPosition().y + MARGIN_TOP));
}
/** Nothing to do in update, cause we must update text only in case
* when user provide input.
* Not on every loop iteration **/
void ScoresState::update() { }
void ScoresState::render() {
game->getWindow()->clear();
game->getWindow()->draw(backgroundSprite);
game->getWindow()->draw(title);
game->getWindow()->draw(player_title);
game->getWindow()->draw(scores_title);
game->getWindow()->draw(back_to_main);
for (const std::pair<sf::Text, sf::Text> &record : highscores) {
game->getWindow()->draw(record.first);
game->getWindow()->draw(record.second);
}
game->getWindow()->display();
}
void ScoresState::processEvents() {
sf::Event event;
while (game->getWindow()->pollEvent(event)) {
switch (event.type)
{
case sf::Event::Closed:
game->popState();
return;
case sf::Event::KeyPressed:
switch (event.key.code) {
case sf::Keyboard::Space:
game->popState();
return;
default:
break;
}
break;
default:
break;
}
}
}