-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame_sfml.cpp
307 lines (273 loc) · 7.39 KB
/
game_sfml.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#include "game_sfml.h"
#include "bullet_sfml.h"
#include "choose_amino_acids_menu_sfml.h"
#include "choose_n_players_menu_sfml.h"
#include "game.h"
#include "menu_sfml.h"
#include "player_sfml.h"
game_sfml::game_sfml(
sf::RenderWindow& window,
game g,
Sprites_sfml& sprites
) : m_game{g},
m_hit_ranges{set_hit_ranges(
create_players(get_amino_acids(g), window.getSize().x), get_start_positions())},
m_life_bars{set_life_bars(get_amino_acids(g).size(), get_life_bar_positions())},
m_sprites(sprites),
m_window{window}
{
}
game_sfml::~game_sfml()
{
m_music.stop();
}
void game_sfml::resize_life_bars()
{
const auto players = get_players(*this);
//Show the players' HPs in the bars
for(auto i = 0u; i < players.size(); ++i)
{
m_life_bars[i].setSize(sf::Vector2f(players[i].get_hp(), m_life_bars[i].getSize().y));
}
}
std::vector<double> collect_hit_points(const game_sfml& g)
{
return collect_hit_points(g.get_game());
}
void game_sfml::display()
{
// 600 fps (current speed on Travis) for 60 seconds
const int kill_frame{get_is_profile_run(*this) ? 600 * 60: -1};
static int frame = 0;
//Kill in profiling
++frame;
if (kill_frame > 0 && frame > kill_frame)
{
m_window.close();
set_state(*this, program_state::quit);
return;
}
m_window.clear();
m_window.draw(m_sprites.get_background());
draw_game_components(m_window, m_life_bars, m_hit_ranges, get_bullets(*this));
draw_players(get_players(*this), m_window, m_sprites);
m_window.display();
}
void draw_game_components(
sf::RenderWindow &w,
std::vector<sf::RectangleShape> life_bars,
std::vector<sf::CircleShape> hit_ranges,
std::vector<bullet> bullets)
{
for(auto i{0u}; i != life_bars.size(); ++i) {
if(life_bars[i].getSize().x > 0.0) {
draw_life_bar(life_bars[i], w);
}
}
for(auto i{0u}; i != hit_ranges.size(); ++i) {
if(life_bars[i].getSize().x > 0.0) {
draw_hit_ranges(hit_ranges[i], w);
}
}
for(auto& bullet : bullets) {
const int window_size = w.getSize().x;
//Must we draw the 'shadow' player left or right?
const bool must_right{bullet.get_x() < window_size / 2};
const int dx = must_right ? window_size : -window_size;
const bool must_down{bullet.get_y() < window_size / 2};
const int dy = must_down ? window_size : -window_size;
//Real position
w.draw(to_sprite(bullet));
//Horizontal of player
bullet.set_position(bullet.get_x() + dx, bullet.get_y());
w.draw(to_sprite(bullet));
//Down-Right of player
bullet.set_position(bullet.get_x(), bullet.get_y() + dy);
w.draw(to_sprite(bullet));
//Below player
bullet.set_position(bullet.get_x() - dx, bullet.get_y());
w.draw(to_sprite(bullet));
}
}
void game_sfml::execute()
{
assert(get_state(*this) == program_state::battle);
while(m_window.isOpen())
{
tick();
//Quit
if (get_state(*this) == program_state::quit) return;
//Next screen
if (get_state(*this) == program_state::winner) return;
//Stay here
assert(get_state(*this) == program_state::battle);
}
}
const std::vector<bullet>& get_bullets(const game_sfml& g)
{
return get_bullets(g.get_game());
}
std::vector<bullet>& get_bullets(game_sfml& g)
{
return get_bullets(g.get_game());
}
const game& get_game(const game_sfml& g)
{
return g.get_game();
}
bool get_is_profile_run(const game_sfml& g)
{
return get_is_profile_run(g.get_game());
}
std::vector<sf::Vector2f> get_life_bar_positions()
{
const std::vector<sf::Vector2f> life_bar_positions {
sf::Vector2f(10 , 10 ),
sf::Vector2f(490, 10 ),
sf::Vector2f(10 , 580),
sf::Vector2f(490, 580)
};
return life_bar_positions;
}
const std::vector<player>& get_players(const game_sfml& g)
{
return get_players(g.get_game());
}
std::vector<player>& get_players(game_sfml& g)
{
return get_players(g.get_game());
}
std::vector<sf::Vector2f> get_start_positions()
{
const std::vector<sf::Vector2f> start_positions {
sf::Vector2f(175, 175),
sf::Vector2f(425, 175),
sf::Vector2f(175, 425),
sf::Vector2f(425, 425)
};
return start_positions;
}
const Sprites_sfml get_sprites(const game_sfml& g)
{
return g.get_sprites();
}
program_state get_state(const game_sfml& g)
{
return get_state(g.get_game());
}
const sf::RenderWindow& get_window(const game_sfml& g)
{
return g.get_window();
}
int get_winner(const game_sfml& g)
{
//Count the number of players that live
const std::vector<double> hps = collect_hit_points(g);
const int n_alive = std::count_if(
std::begin(hps), std::end(hps),
[](const double hp) { return hp > 0.0; }
);
//At least two players live
if (n_alive > 1) return 0;
//We have a winner or everyone died
const int n_players = hps.size();
for (int i=0; i!=n_players; ++i)
{
if (hps[i] > 0.0) { return i + 1; }
}
//Everyone, or the last two players, died at the same time
return -1;
}
void game_sfml::process_event(sf::Event event)
{
switch(event.type)
{
case sf::Event::Closed:
set_state(*this, program_state::quit);
m_window.close();
break;
case sf::Event::KeyPressed:
// keyboard support for player1 and player2
assert(get_players(*this).size() >= 1);
respond_to_key(
get_players(*this),
get_bullets(*this),
m_game
);
break;
case sf::Event::JoystickButtonPressed:
// joystick support for player3 and player4
respond_to_joystick(
get_players(*this),
get_bullets(*this),
m_game);
break;
default:
break;
}
}
std::vector<sf::CircleShape> set_hit_ranges(
std::vector<player> ps,
std::vector<sf::Vector2f> start_positions
)
{
std::vector<sf::CircleShape> hit_ranges;
for(auto i{0u}; i != ps.size(); ++i)
{
const float hit_range_size = get_hit_range_size();
sf::CircleShape hit_range;
hit_range.setPosition(start_positions[i]);
hit_range.setRadius(hit_range_size);
hit_range.setOrigin(sf::Vector2f(hit_range_size, hit_range_size));
hit_range.setOutlineColor(sf::Color::Blue);
hit_range.setOutlineThickness(2.0);
hit_range.setFillColor(sf::Color::Transparent);
hit_ranges.push_back(hit_range);
}
return hit_ranges;
}
std::vector<sf::RectangleShape> set_life_bars(
int player_amount,
std::vector<sf::Vector2f> life_bar_positions)
{
std::vector<sf::RectangleShape> life_bars;
for(auto i{0}; i != player_amount; ++i) {
sf::RectangleShape life_bar;
life_bar.setPosition(life_bar_positions[i]);
life_bar.setSize(sf::Vector2f(100, 10));
life_bar.setFillColor(sf::Color::Red);
life_bars.push_back(life_bar);
}
return life_bars;
}
void set_state(game_sfml& g, program_state p)
{
set_state(g.get_game(), p);
}
void game_sfml::tick()
{
m_game.tick();
assert(m_window.isOpen());
sf::Event event;
while(m_window.pollEvent(event))
{
process_event(event);
}
display();
assert(m_window.getSize().x == m_window.getSize().y);
//Move hit ranges
for(auto i = 0u; i != get_players(*this).size(); ++i)
{
assert(i < m_hit_ranges.size());
assert(i < get_players(*this).size());
m_hit_ranges[i].setPosition(get_players(*this)[i].get_x() + get_players(*this)[i].get_speed_x(),
get_players(*this)[i].get_y() + get_players(*this)[i].get_speed_y());
}
//Check if bullet hits player
resize_life_bars();
//Look for winner
if(get_winner(*this) != 0)
{
set_state(*this, program_state::winner);
}
}