-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
329 lines (264 loc) · 8.63 KB
/
main.cpp
File metadata and controls
329 lines (264 loc) · 8.63 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
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
#include <iostream>
// Basic includes
#include "Engine.hpp"
#include "Graphics/WindowHandler.hpp"
// --------------
// Testing includes
#include "SFML/Graphics.hpp"
#include "TGUI/Widgets/Label.hpp"
#include "Utils/funcHelper.hpp"
#include "Graphics/Renderer.hpp"
#include "Graphics/Camera.hpp"
#include "Graphics/CanvasManager.hpp"
#include "Physics/Collider.hpp"
#include "Physics/FixtureDef.hpp"
#include "Physics/Filter.hpp"
#include "Particles/ParticleEmitter.hpp"
#include "UpdateInterface.hpp"
#include "Object.hpp"
#include "Input.hpp"
// -----------------
using namespace std;
// TODO make animation class
// TODO implement fixture and joint isValid functions that connect the the collider with the body that relates to them
// TODO update managers to be singletons
class Wall : public virtual Object, public Collider, public Renderer<sf::RectangleShape>
{
public:
inline Wall(const Vector2& pos, const Vector2& size)
{
setPosition(pos);
Fixture::Shape::Polygon shape;
shape.makeBox(size.x, size.y);
FixtureDef fixtureDef;
fixtureDef.setFriction(1);
Collider::createFixture(shape, fixtureDef);
Collider::setType(b2BodyType::b2_staticBody);
setSize({size.x,size.y});
setOrigin({size.x/2,size.y/2});
setFillColor(sf::Color::Red);
}
};
class Player : public virtual Object, public Collider, public Renderer<sf::RectangleShape>, public UpdateInterface
{
public:
std::string name = "Random Name";
ParticleEmitter::Ptr m_hitParticle = nullptr;
static sf::RectangleShape m_particle;
static Camera::Ptr m_camera; // default is nullptr
static std::set<Object*> m_players;
inline Player()
{
m_players.emplace(this);
Fixture::Shape::Polygon shape;
shape.makeBox(5,5);
FixtureDef fixtureDef;
fixtureDef.enablePreSolveEvents(true);
fixtureDef.setFriction(1);
Collider::createFixture(shape, fixtureDef);
setSize({5,5});
setOrigin({2.5,2.5});
setFillColor(sf::Color::White);
m_particle.setFillColor(sf::Color::Green);
m_particle.setSize({5,5});
m_particle.setOrigin({2.5,2.5});
m_hitParticle.set(new ParticleEmitter(&m_particle, {0,0}, 10, 0, 0, 1, 10, 0.5, 360));
if (!m_camera)
{
m_camera = new Camera();
m_camera->setMainCamera();
m_camera->setRotationLocked(true);
}
}
inline ~Player()
{
m_players.erase(this);
}
inline virtual void Update(float deltaTime) override
{
if (Input::get().isPressed(sf::Keyboard::Key::W))
{
applyForceToCenter({0,-120000*deltaTime});
}
if (Input::get().isPressed(sf::Keyboard::Key::A))
{
applyForceToCenter({-120000*deltaTime,0});
}
if (Input::get().isPressed(sf::Keyboard::Key::S))
{
applyForceToCenter({0,120000*deltaTime});
}
if (Input::get().isPressed(sf::Keyboard::Key::D))
{
applyForceToCenter({120000*deltaTime,0});
}
if (Input::get().isPressed(sf::Keyboard::Key::E))
{
applyTorque(500000*deltaTime);
}
if (Input::get().isPressed(sf::Keyboard::Key::Q))
{
applyTorque(-500000*deltaTime);
}
}
inline void LateUpdate(float delta) override
{
Vector2 pos(0,0);
for (auto player: m_players)
{
pos += player->getPosition();
}
pos /= m_players.size();
m_camera->setPosition(Vector2::lerp(m_camera->getPosition(), pos, 0.97*delta)); // since late update is called after physics update
m_camera->setRotation(getRotation());
}
void BeginContact(ContactData data) override
{
if (data.getPointCount() > 0 && getLinearVelocity().lengthSquared() > 250 && data.getCollider()->cast<Wall>())
{
m_hitParticle->setPosition(data.getContactPoint(0));
m_hitParticle->emit();
}
}
};
Camera::Ptr Player::m_camera = nullptr;
sf::RectangleShape Player::m_particle;
std::set<Object*> Player::m_players;
class Sensor : public virtual Object, public Renderer<sf::RectangleShape>, public Collider
{
public:
Sensor(funcHelper::func<void> onEnter, const Object::Ptr<>& object = Object::Ptr<>(nullptr))
{
Fixture::Shape::Polygon shape;
shape.makeBox(20,20);
Collider::createFixtureSensor(shape);
Collider::setType(b2_staticBody);
setSize({20,20});
setOrigin({10,10});
setFillColor(sf::Color::Transparent);
setOutlineColor(sf::Color::White);
setOutlineThickness(1);
m_onEnter = onEnter;
m_object = object;
}
~Sensor() noexcept = default;
void BeginContact(ContactData data) override
{
if (!m_object || data.getCollider() == m_object.get())
{
m_onEnter.invoke();
}
}
private:
funcHelper::func<> m_onEnter;
Object::Ptr<> m_object;
};
class OneWay : public virtual Object, public Renderer<sf::RectangleShape>, public Collider, public UpdateInterface
{
public:
inline OneWay(const Vector2& pos, const Vector2& size)
{
Fixture::Shape::Polygon shape;
shape.makeBox(size.x, size.y);
FixtureDef fixtureDef;
fixtureDef.setFilter({0x0001, 0x0000, 0});
Collider::createFixture(shape);
setSize({size.x,size.y});
setOrigin({size.x/2,size.y/2});
setFillColor(sf::Color::Transparent);
setOutlineThickness(0.5);
setOutlineColor(sf::Color::Blue);
auto opening = new Renderer<sf::RectangleShape>();
opening->setParent(this);
opening->setSize({size.x, size.y/8});
opening->setFillColor(sf::Color::Green);
opening->setOrigin({size.x/2, -size.y/2});
setPosition(pos);
}
void Update(float delta) override
{
// b2Vec2 mousePos = WindowHandler::getMousePos();
// if (sf::Mouse::isButtonPressed(sf::Mouse::Left))
// {
// mousePos -= getPosition();
// mousePos.Normalize();
// mousePos *= 7500000.f*delta;
// applyForceToCenter(mousePos);
// }
}
bool PreSolve(PreSolveData data) override
{
if (data.getCollider()->cast<Wall>() != nullptr)
return true;
if (getLocalVector(data.getNormal()).y < -0.5f) // if the object is colliding from the bottom
{
return false;
m_freeFlow.emplace(data.getCollider());
}
else if (m_freeFlow.find(data.getCollider()) != m_freeFlow.end())
{
return false;
}
return true;
}
void EndContact(ContactData data) override
{
m_freeFlow.erase(data.getCollider());
}
private:
std::set<const Collider*> m_freeFlow;
};
int main()
{
Engine::get().init(sf::VideoMode::getDesktopMode(), "Game Framework", sf::Style::Default, sf::State::Fullscreen);
//* init code
new Wall({96,108}, {192,10});
new Wall({96,0}, {192,10});
new Wall({192, 54}, {10, 108});
new Wall({0, 54}, {10, 108});
for (int i = 0; i < 100; i++)
{
auto p = new Player();
p->setPosition({15,10});
}
auto p = new Player();
p->setPosition({15,10});
new Sensor([](){}, p);
sf::RectangleShape particleShape;
particleShape.setSize({10,10});
particleShape.setOrigin({5,5});
particleShape.setFillColor(sf::Color::Magenta);
new OneWay({40,25}, {40,10});
Canvas* gui = new Canvas();
float secondTimer = 0;
int fps = 0;
auto fpsLabel = tgui::Label::create("FPS");
gui->add(fpsLabel);
// -------------
Engine::get().preLoop();
while (WindowHandler::getRenderWindow()->isOpen())
{
Engine::get().preEventHandling();
secondTimer += Engine::get().getDeltaTime();
while (const std::optional<sf::Event> event = WindowHandler::getRenderWindow()->pollEvent())
{
if (const auto* keyPressed = event->getIf<sf::Event::KeyPressed>())
if (WindowHandler::getRenderWindow()->isOpen() && keyPressed->code == sf::Keyboard::Key::Escape)
WindowHandler::getRenderWindow()->close();
Engine::get().handleEvent(event.value());
}
Engine::get().preUserCode();
//* Write code here
fps++;
if (secondTimer >= 1)
{
fpsLabel->setText("FPS: " + std::to_string(fps));
secondTimer = 0;
fps = 0;
}
//* ---------------
Engine::get().postUserCode();
}
Engine::get().close();
return EXIT_SUCCESS;
}