forked from Mach3tryhard/Find-Rakis
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
125 lines (115 loc) · 4.64 KB
/
Copy pathmain.cpp
File metadata and controls
125 lines (115 loc) · 4.64 KB
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
#include <iostream>
#include <vector>
#include <chrono>
#include <thread>
#include <SFML/Graphics.hpp>
#include "code/Pair.h"
#include "code/Physics.h"
#include "code/Bullet.h"
#include "code/Celestial.h"
#include "code/Exceptions.h"
#include "code/Generator.h"
#include "code/GUI.h"
#include "code/ParticleSystem.h"
#include "code/Universe.h"
#include "code/Generator.h"
int main() {
try {
sf::RenderWindow window;
window.create(sf::VideoMode({1920, 1008}), "Find Rakis", sf::Style::Default);
window.setVerticalSyncEnabled(true);
sf::View view(sf::FloatRect({0, 0}, {1920, 1008}));
window.setView(view);
sf::Vector2f center = view.getCenter();
sf::Vector2f size = view.getSize();
sf::FloatRect viewRect({center.x - size.x/2.f, center.y - size.y/2.f}, {size.x, size.y});
std::cout << "Fereastra a fost creată\n";
std::mt19937 gen(std::random_device{}());
/// CREATE GUI
GUI gui{};
gui.Initialize(window);
/// CREATE PLAYER
Physics playerphysics{5};
SpaceShip player{playerphysics,10,100,100,0};
player.getShape().setPosition({center.x,center.y});
/// CREATE UNIVERSE
Universe universe(25,gen);
/// CREATE TEXTURES
Generator noise(1000,sf::Color::White,0);
sf::Clock clock;
while(window.isOpen()) {
bool shouldExit = false;
sf::Time dt = clock.restart();
while(const std::optional event = window.pollEvent()) {
if (event->is<sf::Event::Closed>())
window.close();
else
if (event->is<sf::Event::Resized>()) {
const auto* resize = event->getIf<sf::Event::Resized>();
view.setSize({1920.f, 1008.f});
view.setCenter({static_cast<float>(player.getPhysics().getPosition().x),static_cast<float>(player.getPhysics().getPosition().y)});
float windowAspect = static_cast<float>(resize->size.x) / static_cast<float>(resize->size.y);
float worldAspect = 1920.f / 1008.f;
if (windowAspect > worldAspect) {
float width = 1008.f * windowAspect;
view.setViewport(sf::FloatRect({(resize->size.x - width)/2.f / resize->size.x, 0.f},{ width / resize->size.x, 1.f}));
} else {
float height = 1920.f / windowAspect;
view.setViewport(sf::FloatRect({0.f, (resize->size.y - height)/2.f / resize->size.y} ,{ 1.f, height / resize->size.y}));
}
window.setView(view);
}
else if (event->is<sf::Event::KeyPressed>()) {
const auto* keyPressed = event->getIf<sf::Event::KeyPressed>();
if(keyPressed->scancode == sf::Keyboard::Scancode::Escape) {
shouldExit = true;
}
if(keyPressed->code == sf::Keyboard::Key::C) {
if (player.getEnergy()>10)
player.ShootBullet();
}
}
}
if(shouldExit) {
window.close();
std::cout << "Fereastra a fost închisă (shouldExit == true)\n";
break;
}
window.clear();
/// UNIVERSE STUFF
universe.Update(player,window,viewRect,noise.getTexture());
/// PARTICLE STUFF
player.getExhaust().update(dt);
window.draw(player.getExhaust());
/// PLAYER STUFF
player.UpdateBullets(dt,window,viewRect);
player.InputCheck(dt);
player.getPhysics().UpdatePhysics(player.getCap(),dt);
window.draw(player.getShape());
/// DRAW GUI
gui.DrawText(player);
gui.DrawArrowHUD(window, player);
gui.DrawMiniMap(window,universe,player);
gui.DrawBars(window,player);
window.draw(gui.getText());
window.display();
}
}
catch (const ResourceLoadException& e) {
std::cerr << e.what() << "\n";
return 1;
}
catch (const GenerationException& e) {
std::cerr << e.what() << "\n";
return 2;
}
catch (const PhysicsException& e) {
std::cerr << e.what() << "\n";
return 3;
}
catch (const std::exception& e) {
std::cerr << "Nu se stie error: " << e.what() << "\n";
return -1;
}
return 0;
}