/
Squaighter_V0-6.cpp
560 lines (436 loc) · 10.3 KB
/
Squaighter_V0-6.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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
#include <iostream>
#include <string>
#include <vector>
#include <cassert>
#include <SFML/Graphics.hpp>
enum class wing {left, right};
class ground
{
const sf::Vector2f m_windims{800.0f, 400.0f};
const float m_divis{10.0f};
const float m_side{m_windims.y/m_divis};
const sf::Vector2f m_posit{0.0f, m_windims.y - m_side};
const sf::Vector2f m_dims{m_windims.x, m_side};
sf::Color m_color{255, 255, 255};
sf::RectangleShape m_rect;
void set_rect_posit()
{
m_rect.setPosition(m_posit);
}
void set_rect()
{
m_rect.setSize(m_dims);
set_rect_posit();
m_rect.setFillColor(m_color);
}
public:
void displaying(sf::RenderWindow& window)
{
window.draw(m_rect);
}
float show_posit_y()
{
return m_posit.y;
}
ground(const sf::Vector2f& windims, const float divis, const sf::Color& color)
: m_windims(windims), m_divis(divis), m_side(m_windims.y/m_divis),
m_posit(0.0f, m_windims.y - m_side), m_dims(m_windims.x, m_side), m_color(color),
m_rect()
{
set_rect();
}
~ground()
{
}
};
class shot
{
const float m_squide{1.0f};
sf::Vector2f m_posit;
const sf::Vector2f m_speed;
const float m_side{0.25f*m_squide};
const sf::Vector2f m_sides {m_side, m_side};
const float m_radius{0.5f*m_side};
const sf::Color m_dark{63, 63, 63};
const sf::Color m_color;
sf::RectangleShape m_square;
void set_square_posit()
{
m_square.setPosition(m_posit.x - m_radius, m_posit.y - m_radius);
}
void set_square()
{
m_square.setSize(m_sides);
set_square_posit();
m_square.setFillColor(m_color);
}
public:
void move_shot()
{
m_posit = m_posit + m_speed;
set_square_posit();
}
void displaying(sf::RenderWindow& window)
{
window.draw(m_square);
}
shot(const float squide, const sf::Vector2f& posit, const sf::Vector2f& speed, const sf::Color& color)
: m_squide(squide), m_posit(posit), m_speed(speed), m_side(0.25f*m_squide), m_sides(m_side, m_side),
m_radius(0.5f*m_side), m_color(color + m_dark), m_square()
{
set_square();
}
~shot()
{
}
};
class square
{
const sf::Vector2f m_windims{800.0f, 400.0f};
sf::Vector2f m_posit{0.5f*m_windims};
const wing m_winger{wing::left};
const float m_divis{10.0f};
const float m_side{m_windims.y/m_divis};
const float m_radius{0.5f*m_side};
const sf::Vector2f m_sides{m_side, m_side};
const float m_speed_mult{0.0015f};
const sf::Vector2f m_speed_right{m_speed_mult*m_windims.x, 0.0f};
const sf::Vector2f m_speed_left{-m_speed_mult*m_windims.x, 0.0f};
sf::Vector2f m_speed_vert{0.0f, 0.0f};
const float m_jump_mult{0.007f};
const sf::Vector2f m_jump_up{0.0f, -m_jump_mult*m_windims.y};
const int m_max_jumps = 2;
int m_jumps = m_max_jumps;
bool pressing = false;
bool warping = false;
sf::Vector2f m_speed{0.0f, 0.0f};
const float m_accel_mult{0.00007f};
const sf::Vector2f m_accel{0.0f, m_accel_mult*m_windims.y};
sf::Color m_color{255, 255, 255};
sf::RectangleShape m_square;
const float m_shot_speed_mult{0.005f};
sf::Vector2f m_shot_speed{m_shot_speed_mult*m_windims.x, 0.0f};
std::vector <shot> m_shots;
bool dressing{false};
float x_pout()
{
return m_posit.x;
}
float y_pout()
{
return m_posit.y;
}
void x_pin(const float pin)
{
m_posit.x = pin;
}
void y_pin(const float pin)
{
m_posit.y = pin;
}
void reset_jumps()
{
m_jumps = m_max_jumps;
}
void set_wing()
{
if (m_winger == wing::left)
{
m_posit = sf::Vector2f(3.0f*m_radius, m_windims.y - 3.0f*m_radius);
}
else if (m_winger == wing::right)
{
m_posit = sf::Vector2f(m_windims.x - 3.0f*m_radius, m_windims.y - 3.0f*m_radius);
}
}
void set_square_posit()
{
m_square.setPosition(m_posit.x - m_radius, m_posit.y - m_radius);
}
void set_square()
{
m_square.setSize(m_sides);
set_square_posit();
m_square.setFillColor(m_color);
}
void horizon_check()
{
if (m_posit.x < m_radius)
{
m_posit.x = m_radius;
}
if (m_posit.x > m_windims.x - m_radius)
{
m_posit.x = m_windims.x - m_radius;
}
}
void move_right()
{
if ((sf::Keyboard::isKeyPressed(sf::Keyboard::D) && (m_winger == wing::left)) ||
(sf::Keyboard::isKeyPressed(sf::Keyboard::L) && (m_winger == wing::right)))
{
m_posit += m_speed_right;
}
horizon_check();
}
void move_left()
{
if ((sf::Keyboard::isKeyPressed(sf::Keyboard::A) && (m_winger == wing::left)) ||
(sf::Keyboard::isKeyPressed(sf::Keyboard::J) && (m_winger == wing::right)))
{
m_posit += m_speed_left;
}
horizon_check();
}
void vertigo_check(ground& earth)
{
if (m_posit.y > earth.show_posit_y() - m_radius)
{
m_posit.y = earth.show_posit_y() - m_radius;
m_speed_vert.y = 0.0f;
m_jumps = m_max_jumps;
}
}
void jump_up()
{
if ((sf::Keyboard::isKeyPressed(sf::Keyboard::W) && (m_winger == wing::left)) ||
(sf::Keyboard::isKeyPressed(sf::Keyboard::I) && (m_winger == wing::right)))
{
if (!pressing && (m_jumps > 0))
{
m_speed_vert.y = m_jump_up.y;
pressing = true;
--m_jumps;
}
}
else if (pressing)
{
pressing = false;
}
}
void gravity(ground& earth)
{
m_speed_vert += m_accel;
jump_up();
if (m_speed_vert.y > -m_jump_up.y)
{
m_speed_vert.y = -m_jump_up.y;
}
m_posit += m_speed_vert;
vertigo_check(earth);
}
void warp(square& another)
{
if ((sf::Keyboard::isKeyPressed(sf::Keyboard::S) && (m_winger == wing::left)) ||
(sf::Keyboard::isKeyPressed(sf::Keyboard::K) && (m_winger == wing::right)))
{
if (!warping)
{
const float x_pout_ = x_pout();
const float o_x_pout = another.x_pout();
if (o_x_pout > x_pout_)
{
if (o_x_pout <= m_windims.x - 2.5f*m_side)
{
m_posit.x = o_x_pout + 2.0f*m_side;
}
else if (o_x_pout >= 2.5f*m_side)
{
m_posit.x = o_x_pout - 2.0f*m_side;
}
}
if (o_x_pout < x_pout_)
{
if (o_x_pout >= 2.5f*m_side)
{
m_posit.x = o_x_pout - 2.0f*m_side;
}
else if (o_x_pout <= m_windims.x - 2.5f*m_side)
{
m_posit.x = o_x_pout + 2.0f*m_side;
}
}
warping = true;
}
}
else if (warping)
{
warping = false;
}
}
void collision_check(square& another)
{
const float delta_x{x_pout() - another.x_pout()};
const float delta_y{y_pout() - another.y_pout()};
if ((abs(delta_y) >= abs(delta_x)) && (abs(delta_y) <= m_side))
{
if (delta_y <= 0)
{
y_pin(another.y_pout() - m_side);
reset_jumps();
}
}
if ((abs(delta_x) > abs(delta_y)) && (abs(delta_x) <= m_side))
{
if (delta_x <= 0)
{
const float x_middle{another.x_pout() + 0.5f*delta_x};
x_pin(x_middle - m_radius);
another.x_pin(x_middle + m_radius);
}
}
}
void move_shots()
{
const int shot_number{static_cast<int>(m_shots.size())};
int count{0};
while (count < shot_number)
{
m_shots[count].move_shot();
++count;
}
}
void moving(ground& earth, square& another)
{
move_left();
move_right();
gravity(earth);
warp(another);
collision_check(another);
set_square_posit();
move_shots();
}
void display_square(sf::RenderWindow& window)
{
window.draw(m_square);
}
void display_shots(sf::RenderWindow& window)
{
int count = 0;
while (count < static_cast<int>(m_shots.size()))
{
m_shots[count].displaying(window);
++count;
}
}
void shooting(square& another)
{
if ((sf::Keyboard::isKeyPressed(sf::Keyboard::R) && (show_wing() == wing::left)) ||
(sf::Keyboard::isKeyPressed(sf::Keyboard::P) && (show_wing() == wing::right)))
{
if (!dressing)
{
sf::Vector2f shot_speed{m_shot_speed};
if (another.m_posit.x < m_posit.x)
{
shot_speed = -m_shot_speed;
}
const shot fired{show_side(), show_posit(), shot_speed, show_color()};
m_shots.push_back(fired);
dressing = true;
}
}
else if (dressing)
{
dressing = false;
}
}
public:
float show_side()
{
return m_side;
}
sf::Vector2f show_posit()
{
return m_posit;
}
sf::Color show_color()
{
return m_color;
}
wing show_wing()
{
return m_winger;
}
std::vector <shot> show_shots()
{
return m_shots;
}
void displaying(sf::RenderWindow& window)
{
display_square(window);
display_shots(window);
}
void acting(ground& earth, square& another)
{
moving(earth, another);
shooting(another);
}
square(const sf::Vector2f& windims, const wing& winger, const sf::Color& color)
: m_windims(windims), m_winger(winger), m_side(m_windims.y/m_divis),
m_radius(0.5f*m_side), m_sides(m_side, m_side),
m_speed_right(m_speed_mult*m_windims.x, 0.0f), m_speed_left(-m_speed_mult*m_windims.x, 0.0f),
m_jump_up(0.0f, -m_jump_mult*m_windims.y), m_accel(0.0f, m_accel_mult*m_windims.y),
m_color(color), m_square(), m_shots()
{
set_wing();
set_square();
}
~square()
{
}
// float m_health{100.0f};
};
int window_maker(const sf::Vector2f& windims, const std::string& program_name)
{
const float millis{5.0f/1000.0f};
const sf::Color black{sf::Color(0, 0, 0)};
const sf::Color light_red{sf::Color(191, 63, 63)};
const sf::Color light_green{sf::Color(63, 191, 63)}; const sf::Color light_blue{sf::Color(63, 63, 191)};
const float divis{10.0f};
square lefter(windims, wing::left, light_red);
square righter(windims, wing::right, light_blue);
std::vector <shot> shots;
ground earth(windims, divis, light_green);
sf::RenderWindow window(sf::VideoMode(windims.x, windims.y), program_name, sf::Style::Default);
sf::Clock clock;
sf::Time time;
while (window.isOpen())
{
clock.restart();
sf::Event event;
window.clear(black);
earth.displaying(window);
lefter.displaying(window);
righter.displaying(window);
time = clock.getElapsedTime();
while(time.asSeconds() < millis)
{
time = clock.getElapsedTime();
}
window.display();
lefter.acting(earth, righter);
righter.acting(earth, lefter);
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
{
window.close();
return 0;
}
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
{
window.close();
return 0;
}
}
}
return 1;
}
int main()
{
const std::string program_name{"Squaighter V0.6"};
std::cout << program_name << '\n';
const sf::Vector2f windims{800.0f, 400.0f};
return window_maker(windims, program_name);
}