-
Notifications
You must be signed in to change notification settings - Fork 0
/
play_state.cpp
193 lines (147 loc) · 5.07 KB
/
play_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
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
187
188
189
190
191
192
193
#include "play_state.h"
const float PADDLE_VELOCITY = 6.f;
const float PADDLE_WIDTH = 60.f;
const float PADDLE_HEIGHT = 15.f;
const float PADDLE_START_COORD_X = WINDOW_WIDTH / 2.f;
const float PADDLE_START_COORD_Y = WINDOW_HEIGHT - 30.f;
const float BALL_VELOCITY = 5.f;
const float BALL_RADIUS = 9.f;
const float BALL_START_COORD_X = PADDLE_START_COORD_X;
const float BALL_START_COORD_Y = PADDLE_START_COORD_Y + BALL_RADIUS;
const float BLOCK_WIDTH = 50.f;
const float BLOCK_HEIGHT = 20.f;
const int BLOCK_ROWS_CNT = 4;
const int BLOCK_COLUMNS_CNT = 11;
const sf::Keyboard::Key BUTTON_LEFT = sf::Keyboard::Key::Left;
const sf::Keyboard::Key BUTTON_RIGHT = sf::Keyboard::Key::Right;
PlayState::PlayState(Game *state_holder) :
GameState(state_holder),
ball(BALL_START_COORD_X, BALL_START_COORD_Y, BALL_RADIUS, BALL_VELOCITY),
paddle(PADDLE_START_COORD_X, PADDLE_START_COORD_Y,
PADDLE_WIDTH, PADDLE_HEIGHT, PADDLE_VELOCITY),
isGameOver(false)
{
Block::resetScore();
default_command = new DefaultCommand();
button_left = new MoveLeftCommand();
button_right = new MoveRightCommand();
current_command = default_command;
sf::Texture *texture = game->getResourseManager().getTexture(Textures::Code::GameBackground);
backgroundSprite.setTexture(*texture);
BlockSpawner spawner(BLOCK_WIDTH, BLOCK_HEIGHT);
Block *curr = nullptr;
ball.attach(&paddle);
for(int i = 0; i < BLOCK_COLUMNS_CNT; i++)
for(int j = 0; j < BLOCK_ROWS_CNT; j++) {
curr = spawner.getBlock(j % 3 + 1);
curr->setPosition(sf::Vector2f((i + 1) * (BLOCK_WIDTH + 2) + 75,
(j + 1) * (BLOCK_HEIGHT + 2) + 50));
blocks.push_back(curr);
ball.attach(curr);
}
sf::Font *font = game->getResourseManager().getFont(Fonts::Score);
score_text.setColor(sf::Color::Cyan);
score_text.setCharacterSize(40);
score_text.setPosition(WINDOW_WIDTH - 150.f, 0.f);
score_text.setFont(*font);
pause_text.setColor(sf::Color(255, 255, 255));
pause_text.setCharacterSize(65);
pause_text.setPosition(WINDOW_WIDTH / 2.f - 50.f, WINDOW_HEIGHT / 2.f - 50.f);
pause_text.setFont(*font);
pause_text.setString("PAUSED");
}
PlayState::~PlayState() {
if(button_left != nullptr)
delete button_left;
if(button_right != nullptr)
delete button_right;
if(default_command != nullptr)
delete default_command;
for (Block *block : blocks) {
delete block;
}
blocks.clear();
}
void PlayState::processEvents() {
sf::Event event;
if(isGameOver) {
game->setGameScore(Block::getScore());
game->saveScore();
game->changeState(States::EndGame);
return;
}
while (game->getWindow()->pollEvent(event)) {
switch (event.type)
{
case sf::Event::Closed:
game->setGameScore(Block::getScore());
game->popState();
return;
case sf::Event::KeyPressed:
switch (event.key.code) {
case sf::Keyboard::P:
game->changePauseState();
return;
case sf::Keyboard::Escape:
game->changeState(States::Menu);
return;
default:
break;
}
break;
default:
break;
}
}
if(sf::Keyboard::isKeyPressed(BUTTON_LEFT) && paddle.getLeft() > 0)
current_command = button_left;
else if(sf::Keyboard::isKeyPressed(BUTTON_RIGHT) &&
paddle.getRight() < WINDOW_WIDTH)
current_command = button_right;
else
current_command = default_command;
}
void PlayState::update() {
if(ball.getLeft() < 0)
ball.setVelocityX(BALL_VELOCITY);
else if(ball.getRight() > WINDOW_WIDTH)
ball.setVelocityX(-BALL_VELOCITY);
if(ball.getTop() < 0)
ball.setVelocityY(BALL_VELOCITY);
else if(ball.getBottom() > WINDOW_HEIGHT) {
isGameOver = true;
return;
}
if(current_command != nullptr)
current_command->Execute(paddle);
if(blocks.size() == 0) {
game->setWinnerStatus(true);
isGameOver = true;
return;
}
ball.update();
paddle.update();
for(int i = 0; i < blocks.size(); i++) {
if(!blocks[i]->isAlive()) {
ball.detach(blocks[i]);
delete blocks[i];
blocks.erase(blocks.begin() + i);
i--;
}
}
sf::String scores_string = sf::String("SCORE: ") +
sf::String(std::to_string(Block::getScore()));
score_text.setString(scores_string);
}
void PlayState::render() {
game->getWindow()->clear();
game->getWindow()->draw(backgroundSprite);
game->getWindow()->draw(ball);
game->getWindow()->draw(paddle);
game->getWindow()->draw(score_text);
for(Block *block : blocks)
game->getWindow()->draw(*block);
if(game->getPauseState())
game->getWindow()->draw(pause_text);
game->getWindow()->display();
}