/
Gravtacles_V0-7.cpp
992 lines (585 loc) · 14.9 KB
/
Gravtacles_V0-7.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
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
#include <iostream>
#include <sstream>
#include <cassert>
#include <string>
#include <vector>
#include <cmath>
#include <random>
#include <SFML/Graphics.hpp>
float absuvect(const sf::Vector2f& vecator)
{
return std::sqrt(vecator.x*vecator.x + vecator.y*vecator.y);
}
std::string score_to_string(const int skore)
{
std::stringstream stringer;
stringer << skore;
return stringer.str();
}
class gravitor
{
const std::string m_file_name{"Field.png"};
float m_radius{0.0f};
const float m_strength{1.0f};
const sf::Color m_light_red{sf::Color(255, 63, 63, 255)};
const sf::Color m_light_blue{sf::Color(63, 63, 255, 255)};
sf::Texture m_texture;
sf::Sprite m_sprite;
void set_texture()
{
assert(m_file_name != "");
if (!m_texture.loadFromFile(m_file_name))
{
std::cout << m_file_name << " not found!\n";
}
}
void set_sprite_texture()
{
m_sprite.setTexture(m_texture);
}
void set_origin()
{
const float mult{0.27f};
assert(mult > 0.0f);
const sf::FloatRect m_image_bounds{m_sprite.getLocalBounds()};
m_sprite.setOrigin(0.5f*m_image_bounds.width, 0.5f*m_image_bounds.height);
m_radius = mult*(m_image_bounds.width + m_image_bounds.height);
}
void set_position(const sf::Vector2f& position)
{
m_sprite.setPosition(position);
}
void set_color()
{
int opaque{abs(static_cast<int>(m_strength*255.0f))};
if (opaque > 255)
{
opaque = 255;
}
if (m_strength < 0.0f)
{
sf::Color light_red{m_light_red};
light_red.a = opaque;
m_sprite.setColor(light_red);
}
else
{
sf::Color light_blue{m_light_blue};
light_blue.a = opaque;
m_sprite.setColor(light_blue);
}
}
public:
void show_gravitor(sf::RenderWindow& window)
{
window.draw(m_sprite);
}
void set_spriterator()
{
set_texture();
set_sprite_texture();
}
float show_radius()
{
return m_radius;
}
float show_strength()
{
return m_strength;
}
sf::Vector2f get_position()
{
return m_sprite.getPosition();
}
gravitor(const float strength, const sf::Vector2f& position)
: m_strength(strength), m_texture(), m_sprite()
{
set_texture();
set_sprite_texture();
set_origin();
set_position(position);
set_color();
}
~gravitor()
{
}
};
class cargo
{
const std::string m_file_name{"Ball.png"};
float m_radius{0.0f};
const sf::Color m_light_orange{sf::Color(255, 196, 63, 255)};
sf::Vector2f m_posit{0.0f, 0.0f};
sf::Vector2f m_speed{0.0f, 0.0f};
sf::Vector2f m_accel{0.0f, 0.0f};
sf::Texture m_texture;
sf::Sprite m_sprite;
void set_texture()
{
assert(m_file_name != "");
if (!m_texture.loadFromFile(m_file_name))
{
std::cout << m_file_name << " not found!\n";
}
}
void set_sprite_texture()
{
m_sprite.setTexture(m_texture);
}
void set_origin()
{
const float mult{0.2f};
assert(mult > 0.0f);
const sf::FloatRect m_image_bounds{m_sprite.getLocalBounds()};
m_sprite.setOrigin(0.5f*m_image_bounds.width, 0.5f*m_image_bounds.height);
m_radius = mult*(m_image_bounds.width + m_image_bounds.height);
}
void set_color()
{
m_sprite.setColor(m_light_orange);
}
public:
void set_position()
{
m_sprite.setPosition(m_posit);
}
void reset_speed()
{
m_speed = sf::Vector2f(0.0f, 0.0f);
}
void set_position(const sf::Vector2f& position)
{
m_posit = position;
set_position();
}
void show_cargo(sf::RenderWindow& window)
{
window.draw(m_sprite);
}
void set_spriterator()
{
set_texture();
set_sprite_texture();
}
void reset_accel()
{
m_accel = sf::Vector2f(0.0f, 0.0f);
}
void set_click_speed(sf::RenderWindow& window)
{
const float mult{300.0f};
assert(mult > 0.0f);
const sf::Vector2f direction{static_cast<sf::Vector2f>(sf::Mouse::getPosition(window)) - m_posit};
m_speed = mult*direction/absuvect(direction);
}
void add_accel(const sf::Vector2f& accel)
{
m_accel = m_accel + accel;
}
void speed_add_accel(const float delta_time)
{
m_speed = m_speed + delta_time*m_accel;
}
void posit_add_speed(const float delta_time)
{
m_posit = m_posit + delta_time*m_speed;
}
void decrease_speed()
{
m_speed *= 0.9999f;
}
void bounce(const sf::Vector2f& window_sizes)
{
if (m_posit.x < 0.0f)
{
m_speed.x = -m_speed.x;
m_posit.x = -m_posit.x;
}
if (m_posit.x > window_sizes.x)
{
m_speed.x = -m_speed.x;
m_posit.x = window_sizes.x + window_sizes.x - m_posit.x;
}
if (m_posit.y < 0.0f)
{
m_speed.y = -m_speed.y;
m_posit.y = -m_posit.y;
}
if (m_posit.y > window_sizes.y)
{
m_speed.y = -m_speed.y;
m_posit.y = window_sizes.y + window_sizes.y - m_posit.y;
}
set_position();
}
float show_radius()
{
return m_radius;
}
sf::Vector2f get_position()
{
return m_sprite.getPosition();
}
sf::FloatRect get_sizes()
{
return m_sprite.getLocalBounds();
}
cargo(const sf::Vector2f& position)
: m_posit(position),
m_texture(), m_sprite()
{
set_texture();
set_sprite_texture();
set_origin();
set_position(m_posit);
set_color();
}
~cargo()
{
}
};
class pointer
{
const sf::Color m_light_cyan{sf::Color(63, 255, 255, 63)};
sf::ConvexShape m_polygon;
void init_polygon(cargo& ball)
{
const sf::Vector2f position{ball.get_position()};
const float mult{0.33f};
assert(mult > 0.0f);
const sf::FloatRect image_bounds{ball.get_sizes()};
const float width{mult*image_bounds.width};
const float height{mult*image_bounds.height};
m_polygon.setPointCount(3);
m_polygon.setPoint(0, position + sf::Vector2f(width, height));
m_polygon.setPoint(1, position + sf::Vector2f(width, -height));
m_polygon.setPoint(2, position + sf::Vector2f(-width, height));
m_polygon.setFillColor(m_light_cyan);
}
public:
void morph_polygon(sf::RenderWindow& window)
{
m_polygon.setPoint(0, static_cast<sf::Vector2f>(sf::Mouse::getPosition(window)));
}
void show_polygon(sf::RenderWindow& window)
{
window.draw(m_polygon);
}
pointer(cargo& ball)
: m_polygon()
{
init_polygon(ball);
}
~pointer()
{
}
};
class target
{
const std::string m_file_name{"Ball.png"};
float m_radius{0.0f};
const sf::Color m_light_purple{sf::Color(255, 63, 255, 255)};
sf::Texture m_texture;
sf::Sprite m_sprite;
void set_texture()
{
assert(m_file_name != "");
if (!m_texture.loadFromFile(m_file_name))
{
std::cout << m_file_name << " not found!\n";
}
}
void set_sprite_texture()
{
m_sprite.setTexture(m_texture);
}
void set_origin()
{
const float mult{0.2f};
assert(mult > 0.0f);
const sf::FloatRect m_image_bounds{m_sprite.getLocalBounds()};
m_sprite.setOrigin(0.5f*m_image_bounds.width, 0.5f*m_image_bounds.height);
m_radius = mult*(m_image_bounds.width + m_image_bounds.height);
}
void set_color()
{
m_sprite.setColor(m_light_purple);
}
void set_position(const sf::Vector2f& position)
{
m_sprite.setPosition(position);
}
public:
void show_target(sf::RenderWindow& window)
{
window.draw(m_sprite);
}
float show_radius()
{
return m_radius;
}
sf::Vector2f get_position()
{
return m_sprite.getPosition();
}
target(const sf::Vector2f& position)
: m_texture(), m_sprite()
{
set_texture();
set_sprite_texture();
set_origin();
set_position(position);
set_color();
}
~target()
{
}
};
class score
{
const std::string m_file_name{"VeraMono-Bold.ttf"};
const float m_mult{0.07f};
const float m_text_size{10.0f};
const sf::Color m_creme{sf::Color(255, 224, 196, 255)};
const std::string m_score_name{"Score: "};
int m_score{0};
sf::Font m_font;
sf::Text m_text;
void set_font()
{
assert(m_file_name != "");
if (!m_font.loadFromFile(m_file_name))
{
std::cout << m_file_name << " not found!\n";
}
}
void set_text_font()
{
m_text.setFont(m_font);
}
void set_text_size()
{
assert(m_text_size > 0.0f);
m_text.setCharacterSize(m_text_size);
}
void set_color()
{
m_text.setColor(m_creme);
}
void set_origin()
{
const sf::FloatRect rectangle{m_text.getLocalBounds()};
m_text.setOrigin(0.5f*rectangle.width, 0.95f*rectangle.height);
}
void set_position(const sf::Vector2f& position)
{
m_text.setPosition(position);
}
public:
void add_score()
{
++m_score;
}
void textify_score()
{
m_text.setString(m_score_name + score_to_string(m_score));
}
void show_score(sf::RenderWindow& window)
{
window.draw(m_text);
}
score(const sf::Vector2f& position)
: m_text_size(m_mult*(position.x + position.y)), m_font(), m_text()
{
assert(m_score_name != "");
assert(m_score == 0);
set_font();
set_text_font();
set_text_size();
set_color();
textify_score();
set_origin();
set_position(position);
}
~score()
{
}
};
class start_text
{
const std::string m_string{"Welcome to Gravtacles, a simple and fun point and click game.\n"};
};
sf::Vector2f gravector(const sf::Vector2f& distance, const float dist, const float radius,
const float strength)
{
const float mult{-20000.0f};
assert(mult < 0.0f);
const sf::Vector2f gravecs{(mult*strength*(radius - dist)*(radius - dist)/(radius*radius*dist))*distance};
return gravecs;
}
void gravitas(cargo& ball, gravitor& grav)
{
const sf::Vector2f distance{ball.get_position() - grav.get_position()};
const float dist{absuvect(distance) + 0.0001f};
if (dist < grav.show_radius())
{
ball.add_accel(gravector(distance, dist, grav.show_radius(), grav.show_strength()));
}
}
bool cargo_reach_target(cargo& ball, target& goal)
{
const float dist{absuvect(ball.get_position() - goal.get_position())};
const float radius{ball.show_radius() + goal.show_radius()};
if (dist <= radius)
{
return true;
}
return false;
}
float random_fraction()
{
unsigned int max_unsigned {static_cast<unsigned int>(-1)};
std::random_device rand;
return static_cast<float>(rand())/static_cast<float>(max_unsigned);
}
float random_strength()
{
return -1.0f + 2.0f*random_fraction();
}
sf::Vector2f random_posit(const sf::Vector2f& window_sizes)
{
const float boundary{0.1f};
const float range{1.0f - 2.0f*boundary};
return sf::Vector2f(window_sizes.x*(boundary + range*random_fraction()),
window_sizes.y*(boundary + range*random_fraction()));
}
int main()
{
const std::string program_name{"Gravtacles V0.7"};
assert(program_name != "");
const float delta_delta_time{0.001f};
const float delta_time{0.025f};
const float window_x{704.0f};
const float window_y{704.0f};
assert(window_x > 0.0f);
assert(window_y > 0.0f);
const sf::Vector2f window_sizes{window_x, window_y};
const sf::Color white{sf::Color(255, 255, 255)};
const sf::Color black{sf::Color(0, 0, 0)};
const sf::Color orange{sf::Color(255, 127, 0)};
const sf::Color purple{sf::Color(127, 0, 255)};
const sf::Color cyan{sf::Color(127, 255, 255)};
const sf::Color red{sf::Color(255, 0, 0)};
const sf::Color yellow{sf::Color(255, 255, 0)};
const sf::Color green{sf::Color(0, 255, 0)};
const sf::Color blue{sf::Color(127, 127, 255)};
const sf::Vector2f begin_posit{0.05f*window_sizes};
const sf::Vector2f end_posit{0.95f*window_sizes};
cargo ball{begin_posit};
pointer triforce{ball};
target goal{end_posit};
std::vector <float> strengths{-0.25f};
std::vector <sf::Vector2f> pozitions{sf::Vector2f(0.5f*window_x, 0.5f*window_y)};
std::vector <gravitor> gravs;
int grav_number{1};
gravs.push_back(gravitor (strengths[grav_number - 1], pozitions[grav_number - 1]));
gravs[grav_number - 1].set_spriterator();
score game_score{sf::Vector2f (0.48f*window_x, 0.035f*window_y)};
sf::RenderWindow window{sf::VideoMode(window_x, window_y), program_name, sf::Style::Default};
const int max_level{10000};
int current_level{1};
while (window.isOpen())
{
while (current_level <= max_level)
{
bool stay_in_level{true};
bool show_triforce{true};
bool move_in_level{false};
ball.set_position(begin_posit);
ball.reset_speed();
while (stay_in_level)
{
sf::Clock clock;
sf::Event event;
window.clear(black);
for (int count{0}; count < grav_number; ++count)
{
gravs[count].show_gravitor(window);
}
goal.show_target(window);
ball.show_cargo(window);
game_score.show_score(window);
if (show_triforce)
{
triforce.show_polygon(window);
}
window.display();
triforce.morph_polygon(window);
bool cargo_target{false};
while ((clock.getElapsedTime().asSeconds() < delta_time) &&
!cargo_target)
{
sf::Clock delta_clock;
while (delta_clock.getElapsedTime().asSeconds() < delta_delta_time)
{
}
if (move_in_level)
{
const float delta_temp{delta_clock.getElapsedTime().asSeconds()};
ball.reset_accel();
for (int count{0}; count < grav_number; ++count)
{
gravitas(ball, gravs[count]);
}
ball.speed_add_accel(delta_temp);
ball.decrease_speed();
ball.posit_add_speed(delta_temp);
ball.set_position();
ball.bounce(window_sizes);
if (!show_triforce &&
cargo_reach_target(ball, goal))
{
cargo_target = true;
stay_in_level = false;
game_score.add_score();
game_score.textify_score();
}
}
}
if (sf::Mouse::isButtonPressed(sf::Mouse::Left) && show_triforce)
{
show_triforce = false;
move_in_level = true;
ball.set_click_speed(window);
}
if (!show_triforce &&
sf::Mouse::isButtonPressed(sf::Mouse::Right))
{
stay_in_level = false;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
{
window.close();
return 1;
}
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
{
window.close();
return 2;
}
}
}
++current_level;
++grav_number;
gravs.push_back(gravitor (random_strength(), random_posit(window_sizes)));
for (int count{0}; count < grav_number; ++count)
{
gravs[count].set_spriterator();
}
}
window.close();
}
return 0;
}