-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlatformerClone.cpp
More file actions
220 lines (162 loc) · 6.01 KB
/
PlatformerClone.cpp
File metadata and controls
220 lines (162 loc) · 6.01 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
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
#include "Headers.h"
#include "Entity.h"
#include "Map.h"
// global variables
sf::Font font;
// debugging texts
sf::Text playerPosText;
sf::Text avgPhysicsTime;
sf::Vector2f viewPosition(0, 0);
Player player;
// where we do all the physics calculations, where everything is closley related to delta time
void physicsUpdate(float deltaTime, Map *map, sf::RenderWindow *window)
{
auto start = std::chrono::system_clock::now();
//map->updateBlocks(player);
// this is sorting the map blocks vector based on their distance to the player
// remove std::greater<Block*>() to make it sort based on smaller
if (! std::is_sorted(map->map.begin(), map->map.end()) );
std::sort(map->map.begin(), map->map.end(), &Block::operator<);
player.physicsUpdate(deltaTime, map, true);
// testing player collisions with all the entities on the map
for (Entity* entity : *map->entities)
{
if (entity->alive)
{
entity->physicsUpdate(deltaTime, map, true);
if (player.rect.getGlobalBounds().intersects(entity->rect.getGlobalBounds()))
{
entity->rect.setFillColor(sf::Color::Cyan);
}
else { entity->rect.setFillColor(sf::Color::Red); }
if (map->checkCollisionDirection(player.rect, entity->rect) == CollisionDirection::top)
{
entity->alive = false;
}
// to save up on computations
entity->draw(window);
}
}
auto end = std::chrono::system_clock::now();
std::chrono::duration<double> elapsed = end - start;
// debug text update
avgPhysicsTime.setString(sf::String(std::to_string(elapsed.count())));
}
// where we draw everything
void draw(sf::RenderWindow *window, sf::View *viewPort, Map *map, float deltaTime)
{
// moving the view port based on the player's position
viewPosition.x = player.rect.getPosition().x - (window->getSize().x * 0.5f);
viewPosition.y = player.rect.getPosition().y - (window->getSize().y * 0.5f);
// moving the view based on the player's position
viewPort->reset(sf::FloatRect(viewPosition.x, viewPosition.y, 700, 700));
// drawing every block on the map
for (sf::RectangleShape* block : map->map)
{
window->draw(*block);
}
// misc debug
{
std::string playerPosString = std::to_string(int(player.rect.getPosition().x)) +
"\n" + std::to_string(int(player.rect.getPosition().y));
playerPosText.setFillColor(sf::Color::White);
playerPosText.setPosition(player.rect.getPosition());
playerPosText.setString(sf::String(playerPosString));
}
player.draw(window);
window->draw(playerPosText);
window->draw(avgPhysicsTime);
}
int main()
{
// loading the font for global usage
font.loadFromFile("res\\sprites\\pix.ttf");
// the actual window where we draw all the bullshit
sf::RenderWindow window(sf::VideoMode(800, 800), "PlatfromerClone");
window.setFramerateLimit(60);
// initializing the player
player = Player(150, 150, GAME_SCALE, GAME_SCALE, sf::Color(32, 38, 54), 200);
// view port responsible for scrolling the view with the player
// think of it as the camera
sf::View viewPort;
viewPort.reset(sf::FloatRect(0, 0, window.getSize().x, window.getSize().y));
// clock for physics calucations
sf::Clock clock;
float deltaTime;
// event handler for closing the game window
sf::Event event;
// the image we use to extrapolate map data
sf::Image mapImage;
mapImage.loadFromFile("res\\sprites\\map2.png");
// intitializing the map with the map image
Map map(mapImage, &window);
map.render();
// for debugging purposes
playerPosText = sf::Text("", font, 15);
avgPhysicsTime = sf::Text("", font, 48);
avgPhysicsTime.setFillColor(sf::Color::Black);
// basic framerate text for debugging
sf::Text frameRate("", font, 48);
frameRate.setFillColor(sf::Color::Black);
while (window.isOpen())
{
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed ||
sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
window.close();
}
deltaTime = clock.getElapsedTime().asSeconds();
clock.restart();
frameRate.setString(std::to_string(int(1 / deltaTime)));
frameRate.setPosition(viewPosition);
avgPhysicsTime.setPosition(viewPosition.x + 200, viewPosition.y);
window.setView(viewPort);
window.clear(sf::Color(255, 255, 255));
map.updateBlocks(player);
physicsUpdate(deltaTime, &map, &window);
draw(&window, &viewPort, &map, deltaTime);
window.draw(frameRate);
window.display();
}
return 0;
}
// for testing purposes
int mainTest()
{
sf::RenderWindow window(sf::VideoMode(800, 800), "PlatfromerClone");
window.setFramerateLimit(60);
player = Player(150, 150, GAME_SCALE, GAME_SCALE, sf::Color(32, 38, 54), 200);
// the image we use to extrapolate map data
sf::Image mapImage;
mapImage.loadFromFile("res\\sprites\\map1.png");
// intitializing the map with the map image
Map map(mapImage, &window);
map.render();
sf::Clock clock;
float deltaTime;
// event handler for closing the game window
sf::Event event;
while (window.isOpen())
{
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed ||
sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
window.close();
}
deltaTime = clock.getElapsedTime().asSeconds();
clock.restart();
map.updateBlocks(player);
std::sort(map.map.begin(), map.map.end(), &(Block::operator<));
window.clear(sf::Color(255, 255, 255));
physicsUpdate(deltaTime, &map, &window);
window.draw(player.rect);
for (Block* block : map.map)
{
window.draw(*block);
}
window.display();
}
return 0;
}