-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
157 lines (123 loc) · 5.57 KB
/
main.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
#include <iostream>
#include <vector>
#include <chrono>
#include <cmath>
/*#include "Objects/Ship.h"
#include "Objects/Asteroid.h"
#include "Objects/Bullet.h"*/
#include "View/main_funcs.h"
#include "View/main_screen_out.h"
#include <SFML/Graphics.hpp>
using std::vector;
const int WIDTH = 1200; //!< Screen width
const int HEIGHT = 800; //!< Screen height
/**
* Main loop function
*/
int main() {
float dt;
int score = 100;
sf::ContextSettings settings;
settings.antialiasingLevel = 4;
sf::RenderWindow window(sf::VideoMode(WIDTH, HEIGHT), "Asteroid", sf::Style::Default, settings);
sf::Font font;
sf::Text info_score;
sf::Text info_hp;
Ship ship{(float)WIDTH, (float)HEIGHT};
vector<Asteroid> asteroids;
vector<Asteroid> new_asteroids;
vector<Bullet> bullets;
sf::Clock sf_clock;
std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now();
std::chrono::steady_clock::time_point safe_time_start = std::chrono::steady_clock::now();
std::chrono::steady_clock::time_point respawn_time = std::chrono::steady_clock::now();
std::chrono::steady_clock::time_point bullet_time = std::chrono::steady_clock::now();
font.loadFromFile("../Fonts/arial.ttf");
info_score.setFont(font);
info_score.setString("Score: " + std::to_string(score) + "\t\tHp: " + std::to_string(ship.get_hp()));
info_score.setCharacterSize(16);
info_score.setPosition(0.f, 0.f);
while (window.isOpen()) {
window.clear();
dt = sf_clock.restart().asSeconds();
sf::Event event{};
while (window.pollEvent(event))
if (event.type == sf::Event::Closed)
window.close();
if (asteroids.size() < 100 && std::chrono::steady_clock::now() >= start + std::chrono::seconds(5)) {
start = std::chrono::steady_clock::now();
Asteroid a{(float)WIDTH, (float)HEIGHT};
if (std::pow((ship.get_x() - a.get_x()), 2) + std::pow((ship.get_y() - a.get_y()), 2) > 40000.f)
asteroids.emplace_back(Asteroid{(float)WIDTH, (float)HEIGHT, a.get_x(), a.get_y(), a.get_hp_size()});
}
if (ship.is_destroyed() && std::chrono::steady_clock::now() > respawn_time + std::chrono::seconds(2)) {
ship.respawn_ship();
safe_time_start = std::chrono::steady_clock::now();
ship.change_safe_mode();
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)) ship.rotate(dt,true);
if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)) ship.rotate(dt,false);
if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)) ship.move(dt);
if (sf::Keyboard::isKeyPressed(sf::Keyboard::P)) {
if (score > 0 && !ship.is_destroyed())
if (std::chrono::steady_clock::now() > bullet_time + std::chrono::milliseconds(200)) {
bullets.emplace_back(Bullet{(float)WIDTH, (float)HEIGHT, ship.gen_true_dots(ship.get_x(), ship.get_y())[0], ship.get_angle()});
bullet_time = std::chrono::steady_clock::now();
score -= 1;
}
}
// Check all collisions
for (auto &a : asteroids)
if (!ship.is_destroyed() && !ship.is_safe() && check_collision(a, ship)) {
ship.destroy_ship();
if (ship.is_alive())
respawn_time = std::chrono::steady_clock::now();
else
break;
}
for (auto &a : asteroids)
for (auto &dc : get_object_copies(a))
for (auto &b : bullets)
if (a.dot_inside(dc, b.get_x(), b.get_y())) {
a.gain_damage();
if (a.get_hp() == 0)
split_asteroid(new_asteroids, a);
b.make_invalid();
score += 16;
}
// =============================================================================================================
// Change of asteroid number
asteroids.erase(std::remove_if(asteroids.begin(),
asteroids.end(),
[](auto & a){return !a.is_valid();}), asteroids.end());
bullets.erase(std::remove_if(bullets.begin(),
bullets.end(),
[](auto & b){return !b.is_valid();}), bullets.end());
asteroids.insert(asteroids.end(), new_asteroids.begin(), new_asteroids.end());
new_asteroids.clear();
// =============================================================================================================
// Drawing objects and info
for (auto &a: asteroids) {
a.move(dt);
a.rotate(dt);
draw_object(window, (BaseObject &)a);
}
for (auto &b : bullets) {
b.move(dt);
draw_bullet(window, b);
}
if (!ship.is_destroyed()) {
draw_object(window, (BaseObject &)ship);
if (ship.is_safe() && std::chrono::steady_clock::now() > safe_time_start + std::chrono::seconds(2))
ship.change_safe_mode();
}
info_score.setString("Score: " + std::to_string(score) + "\t\tHp: " + std::to_string(ship.get_hp()));
window.draw(info_score);
// =============================================================================================================
window.display();
if (!ship.is_alive() || score == 0)
break;
}
std::cout << "Game over";
return 0;
}