-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.cpp
157 lines (121 loc) · 3.46 KB
/
game.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
#include "game.h"
#include "play_state.h"
#include "menu_state.h"
#include "scores_state.h"
#include "end_state.h"
const unsigned WINDOW_WIDTH = 800;
const unsigned WINDOW_HEIGHT = 600;
const sf::String TITLE = "Arkanoid";
const sf::Time TIME_PER_FRAME = sf::seconds(1.f / 60.f);
const int HIGHSCORES_CNT = 8;
Game::Game() :
window(sf::VideoMode(WINDOW_WIDTH, WINDOW_HEIGHT), TITLE),
time_per_frame(TIME_PER_FRAME),
isPaused(false),
isWinner(false)
{
addState(States::Code::Menu);
player_score.scores = 0;
player_score.name = "You";
}
Game::~Game() {
while(!screens.empty()) popState();
}
GameState* Game::getState() const {
if(screens.empty()) return nullptr;
return screens.top();
}
void Game::addState(States::Code state) {
switch (state) {
case States::Code::Game:
screens.push(new PlayState(this));
break;
case States::Code::Menu:
screens.push(new MenuState(this));
break;
case States::Code::Pause:
break;
case States::Code::Scores:
screens.push(new ScoresState(this));
break;
case States::Code::EndGame:
screens.push(new EndState(this));
default:
break;
}
}
void Game::changeState(States::Code state) {
if(!screens.empty())
popState();
addState(state);
}
void Game::popState() {
delete screens.top();
screens.pop();
}
sf::RenderWindow *Game::getWindow() {
return &window;
}
ResourseManager& Game::getResourseManager() {
return resourse_manager;
}
void Game::changePauseState() {
isPaused = !isPaused;
}
bool Game::getPauseState() const {
return isPaused;
}
void Game::run() {
sf::Clock clock;
sf::Time timeSinceLastUpdate = sf::Time::Zero;
while (window.isOpen()) {
timeSinceLastUpdate += clock.restart();
if(getState() == nullptr)
return;
while(timeSinceLastUpdate > time_per_frame) {
if(!isPaused)
getState()->update();
timeSinceLastUpdate -= time_per_frame;
}
getState()->render();
getState()->processEvents();
}
}
void Game::loadHighScores(std::vector<ScoreRecord> &scores) const {
std::ifstream datafile (ResourceLocations::HighscoresData);
if (!datafile.is_open())
std::cout << "Error to open: " + ResourceLocations::HighscoresData;
ScoreRecord record;
while(datafile >> record.name >> record.scores)
scores.push_back(record);
datafile.close();
}
void Game::saveScore() {
std::vector<ScoreRecord> records;
loadHighScores(records);
if(player_score.scores <= records.back().scores)
return;
records.push_back(player_score);
std::sort(records.begin(), records.end(),
[](const ScoreRecord& left, const ScoreRecord& right) {
return left.scores > right.scores;});
std::ofstream scoresfile(ResourceLocations::HighscoresData);
for(int i = 0; i < HIGHSCORES_CNT; i++)
scoresfile << (records[i].name + " " + std::to_string(records[i].scores) + " ");
scoresfile.close();
}
void Game::setGameScore(unsigned score) {
player_score.scores = score;
}
unsigned Game::getPlayerScore() const {
return player_score.scores;
}
std::string Game::getPlayerName() const {
return player_score.name;
}
void Game::setWinnerStatus(bool is_win) {
isWinner = is_win;
}
bool Game::isWin() const {
return isWinner;
}