-
Notifications
You must be signed in to change notification settings - Fork 0
/
player_sfml.cpp
417 lines (388 loc) · 11.3 KB
/
player_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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
#include "player_sfml.h"
#include <cassert>
#include "sprites_sfml.h"
void draw_player( //!OCLINT cannot make this any shorter
const player& p,
sf::RenderWindow& w,
Sprites_sfml& sprites
)
{
switch(p.get_amino_acid()) {
case amino_acid::alanine: return draw_alanine (p, w, sprites);
case amino_acid::arginine: return draw_arginine (p, w, sprites);
case amino_acid::asparagine: return draw_asparagine (p, w, sprites);
case amino_acid::aspartic_acid: return draw_aspartic_acid(p, w, sprites);
case amino_acid::cysteine: return draw_cysteine (p, w, sprites);
case amino_acid::glutamic_acid: return draw_glutamic_acid(p, w, sprites);
case amino_acid::glutamine: return draw_glutamine (p, w, sprites);
case amino_acid::glycine: return draw_glycine (p, w, sprites);
case amino_acid::histidine: return draw_histidine (p, w, sprites);
case amino_acid::isoleucine: return draw_isoleucine (p, w, sprites);
case amino_acid::leucine: return draw_leucine (p, w, sprites);
case amino_acid::lysine: return draw_lysine (p, w, sprites);
case amino_acid::methionine: return draw_methionine (p, w, sprites);
case amino_acid::phenylalanine: return draw_phenylalanine(p, w, sprites);
case amino_acid::proline: return draw_proline (p, w, sprites);
case amino_acid::serine: return draw_serine (p, w, sprites);
case amino_acid::threonine: return draw_threonine (p, w, sprites);
case amino_acid::tryptophan: return draw_tryptophan (p, w, sprites);
case amino_acid::tyrosine: return draw_tyrosine (p, w, sprites);
case amino_acid::valine: return draw_valine (p, w, sprites);
}
assert(!"should not get here"); //!OCLINT accepted idiom
}
void draw_players(
std::vector<player> ps,
sf::RenderWindow &w,
Sprites_sfml& sprites)
{
for(int i{0}; i < static_cast<int>(ps.size()); ++i)
{
if(ps[i].get_hp() <= 0) continue;
const int window_size = w.getSize().x;
//Must we draw the 'shadow' player left or right?
const bool must_right{ps[i].get_x() < window_size / 2};
const int dx = must_right ? window_size : -window_size;
const bool must_down{ps[i].get_y() < window_size / 2};
const int dy = must_down ? window_size : -window_size;
//Real position
draw_player(ps[i], w, sprites);
//Horizontal of player
ps[i].set_position(ps[i].get_x() + dx, ps[i].get_y());
draw_player(ps[i], w, sprites);
//Down-Right of player
ps[i].set_position(ps[i].get_x(), ps[i].get_y() + dy);
draw_player(ps[i], w, sprites);
//Bacl Below player
ps[i].set_position(ps[i].get_x() - dx, ps[i].get_y());
draw_player(ps[i], w, sprites);
}
}
/// all aminoacids have been scaled to the size of arginine
void draw_alanine(
const player& p,
sf::RenderWindow& w,
Sprites_sfml& sprites
)
{
sf::Sprite& sprite = sprites.get(amino_acid::alanine);
sprite.setPosition(p.get_x(), p.get_y());
sprite.setRotation(p.get_rotation() + 30);
w.draw(sprite);
}
void draw_arginine(
const player& p,
sf::RenderWindow& w,
Sprites_sfml& sprites
)
{
sf::Sprite& sprite = sprites.get(amino_acid::arginine);
sprite.setPosition(p.get_x(), p.get_y());
sprite.setRotation(p.get_rotation() + 30);
w.draw(sprite);
}
void draw_asparagine(
const player& p,
sf::RenderWindow& w,
Sprites_sfml& sprites
)
{
sf::Sprite& sprite = sprites.get(amino_acid::asparagine);
sprite.setPosition(p.get_x(), p.get_y());
sprite.setRotation(p.get_rotation() + 30);
w.draw(sprite);
}
void draw_aspartic_acid(
const player& p,
sf::RenderWindow& w,
Sprites_sfml& sprites
)
{
sf::Sprite& sprite = sprites.get(amino_acid::aspartic_acid);
sprite.setPosition(p.get_x(), p.get_y());
sprite.setRotation(p.get_rotation() + 30);
w.draw(sprite);
}
void draw_cysteine(
const player& p,
sf::RenderWindow& w,
Sprites_sfml& sprites
)
{
sf::Sprite& sprite = sprites.get(amino_acid::cysteine);
sprite.setPosition(p.get_x(), p.get_y());
sprite.setRotation(p.get_rotation() + 30);
w.draw(sprite);
}
void draw_glutamic_acid(
const player& p,
sf::RenderWindow& w,
Sprites_sfml& sprites
)
{
sf::Sprite& sprite = sprites.get(amino_acid::glutamic_acid);
sprite.setPosition(p.get_x(), p.get_y());
sprite.setRotation(p.get_rotation() + 30);
w.draw(sprite);
}
void draw_glutamine(
const player& p,
sf::RenderWindow& w,
Sprites_sfml& sprites
)
{
sf::Sprite& sprite = sprites.get(amino_acid::glutamine);
sprite.setPosition(p.get_x(), p.get_y());
sprite.setRotation(p.get_rotation() + 30);
w.draw(sprite);
}
void draw_glycine(
const player& p,
sf::RenderWindow& w,
Sprites_sfml& sprites
)
{
sf::Sprite& sprite = sprites.get(amino_acid::glycine);
sprite.setPosition(p.get_x(), p.get_y());
sprite.setRotation(p.get_rotation() + 30);
w.draw(sprite);
}
void draw_histidine(
const player& p,
sf::RenderWindow& w,
Sprites_sfml& sprites
)
{
sf::Sprite& sprite = sprites.get(amino_acid::histidine);
sprite.setPosition(p.get_x(), p.get_y());
sprite.setRotation(p.get_rotation() + 30);
w.draw(sprite);
}
void draw_isoleucine(
const player& p,
sf::RenderWindow& w,
Sprites_sfml& sprites
)
{
sf::Sprite& sprite = sprites.get(amino_acid::isoleucine);
sprite.setPosition(p.get_x(), p.get_y());
sprite.setRotation(p.get_rotation() + 30);
w.draw(sprite);
}
void draw_leucine(
const player& p,
sf::RenderWindow& w,
Sprites_sfml& sprites
)
{
sf::Sprite& sprite = sprites.get(amino_acid::leucine);
sprite.setPosition(p.get_x(), p.get_y());
sprite.setRotation(p.get_rotation() + 30);
w.draw(sprite);
}
void draw_lysine(
const player& p,
sf::RenderWindow& w,
Sprites_sfml& sprites
)
{
sf::Sprite& sprite = sprites.get(amino_acid::lysine);
sprite.setPosition(p.get_x(), p.get_y());
sprite.setRotation(p.get_rotation() + 30);
w.draw(sprite);
}
void draw_methionine(
const player& p,
sf::RenderWindow& w,
Sprites_sfml& sprites
)
{
sf::Sprite& sprite = sprites.get(amino_acid::methionine);
sprite.setPosition(p.get_x(), p.get_y());
sprite.setRotation(p.get_rotation() + 30);
w.draw(sprite);
}
void draw_phenylalanine(
const player& p,
sf::RenderWindow& w,
Sprites_sfml& sprites
)
{
sf::Sprite& sprite = sprites.get(amino_acid::phenylalanine);
sprite.setPosition(p.get_x(), p.get_y());
sprite.setRotation(p.get_rotation() + 30);
w.draw(sprite);
}
void draw_proline(
const player& p,
sf::RenderWindow& w,
Sprites_sfml& sprites
)
{
sf::Sprite& sprite = sprites.get(amino_acid::proline);
sprite.setPosition(p.get_x(), p.get_y());
sprite.setRotation(p.get_rotation() + 30);
w.draw(sprite);
}
void draw_serine(
const player& p,
sf::RenderWindow& w,
Sprites_sfml& sprites
)
{
sf::Sprite& sprite = sprites.get(amino_acid::serine);
sprite.setPosition(p.get_x(), p.get_y());
sprite.setRotation(p.get_rotation() + 30);
w.draw(sprite);
}
void draw_threonine(
const player& p,
sf::RenderWindow& w,
Sprites_sfml& sprites
)
{
sf::Sprite& sprite = sprites.get(amino_acid::threonine);
sprite.setPosition(p.get_x(), p.get_y());
sprite.setRotation(p.get_rotation() + 30);
w.draw(sprite);
}
void draw_tryptophan(
const player& p,
sf::RenderWindow& w,
Sprites_sfml& sprites
)
{
sf::Sprite& sprite = sprites.get(amino_acid::tryptophan);
sprite.setPosition(p.get_x(), p.get_y());
sprite.setRotation(p.get_rotation() + 30);
w.draw(sprite);
}
void draw_tyrosine(
const player& p,
sf::RenderWindow& w,
Sprites_sfml& sprites
)
{
sf::Sprite& sprite = sprites.get(amino_acid::tyrosine);
sprite.setPosition(p.get_x(), p.get_y());
sprite.setRotation(p.get_rotation() + 30);
w.draw(sprite);
}
void draw_valine(
const player& p,
sf::RenderWindow& w,
Sprites_sfml& sprites
)
{
sf::Sprite& sprite = sprites.get(amino_acid::valine);
sprite.setPosition(p.get_x(), p.get_y());
sprite.setRotation(p.get_rotation() + 30);
w.draw(sprite);
}
void draw_hit_ranges(
sf::CircleShape hit_range,
sf::RenderWindow &w
)
{
const int window_size = w.getSize().x;
//Must we draw the 'shadow' hitranges left or right?
const bool must_right{hit_range.getPosition().x < window_size / 2};
const int dx = must_right ? window_size : -window_size;
const bool must_down{hit_range.getPosition().y < window_size / 2};
const int dy = must_down ? window_size : -window_size;
//Real position
w.draw(hit_range);
//Horizontal of player
hit_range.setPosition(hit_range.getPosition() + sf::Vector2f(dx, 0));
w.draw(hit_range);
//Down-Right of player
hit_range.setPosition(hit_range.getPosition() + sf::Vector2f(0, dy));
w.draw(hit_range);
//Bacl Below player
hit_range.setPosition(hit_range.getPosition() + sf::Vector2f(-dx, 0));
w.draw(hit_range);
}
void draw_life_bar(
sf::RectangleShape life_bar,
sf::RenderWindow &w)
{
w.draw(life_bar);
}
void respond_to_joystick( //!OCLINT cannot simplify this even more
std::vector<player> &players,
std::vector<bullet> &,
game& g
)
{
// player 3 controls
if(players[2].get_hp() > 0)
{
if(sf::Joystick::isButtonPressed(0, 0)) { players[2].decelerate(); } // A button
if(sf::Joystick::isButtonPressed(0, 1)) { players[2].turn_right(); } // B button
if(sf::Joystick::isButtonPressed(0, 2)) { players[2].turn_left(); } // X button
if(sf::Joystick::isButtonPressed(0, 3)) { players[2].accelerate(); } // Y button
if(sf::Joystick::isButtonPressed(0, 4))
{
g.do_action(2, action::shoot);
}
if(sf::Joystick::isButtonPressed(0, 5))
{
g.do_action(2, action::use_power);
}
}
//player 4 controls
if(players[3].get_hp() > 0)
{
if(sf::Joystick::isButtonPressed(1, 0)) { players[3].decelerate(); } // A button
if(sf::Joystick::isButtonPressed(1, 1)) { players[3].turn_right(); } // B button
if(sf::Joystick::isButtonPressed(1, 2)) { players[3].turn_left(); } // X button
if(sf::Joystick::isButtonPressed(1, 3)) { players[3].accelerate() ; } // Y button
if(sf::Joystick::isButtonPressed(1, 4))
{
g.do_action(3, action::shoot);
}
if(sf::Joystick::isButtonPressed(1, 5))
{
g.do_action(3, action::use_power);
}
}
}
void respond_to_key(
std::vector<player> &players,
std::vector<bullet> &,
game& g
)
{
// player1 controls
if(players[0].get_hp() > 0)
{
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left )) { players[0].turn_left (); }
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) { players[0].turn_right(); }
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up )) { players[0].accelerate(); }
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down )) { players[0].decelerate(); }
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Space))
{
g.do_action(0, action::shoot);
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::RShift))
{
g.do_action(0, action::use_power);
}
}
// player2 controls
if(players[1].get_hp() > 0)
{
if(sf::Keyboard::isKeyPressed(sf::Keyboard::W)) { players[1].accelerate(); }
if(sf::Keyboard::isKeyPressed(sf::Keyboard::D)) { players[1].turn_right(); }
if(sf::Keyboard::isKeyPressed(sf::Keyboard::S)) { players[1].decelerate(); }
if(sf::Keyboard::isKeyPressed(sf::Keyboard::A)) { players[1].turn_left (); }
if(sf::Keyboard::isKeyPressed(sf::Keyboard::X))
{
g.do_action(1, action::shoot);
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Q))
{
g.do_action(1, action::use_power);
}
}
}