Skip to content

Commit 64598d4

Browse files
committed
wip rigidbody
1 parent 04091f9 commit 64598d4

File tree

3 files changed

+73
-15
lines changed

3 files changed

+73
-15
lines changed

proj-BirdEngine/include/containers.h

+47-6
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,16 @@ struct Vector2 {
1010
float x, y;
1111
};
1212
struct Direction : public Vector2 {
13-
public:
13+
public:
14+
Direction() = default;
1415
Direction(float x = 0, float y = 0) : Vector2{ x, y } {}
1516
Direction(int x = 0, int y = 0) : Vector2{ (float)x, (float)y } {}
1617
};
1718
struct Size : public Vector2 {
1819
public:
19-
Size(float width = 0, float height = 0) : Vector2{ width, height } {}
20-
Size(int width = 0, int height = 0) : Vector2{ (float)width, (float)height } {}
20+
Size() = default;
21+
Size(float width, float height) : Vector2{ width, height } {}
22+
Size(int width, int height) : Vector2{ (float)width, (float)height } {}
2123
};
2224
struct Position : public Vector2 {
2325
public:
@@ -78,7 +80,7 @@ class Transform : public Component {
7880
Size size;
7981
float rotation = 0;
8082

81-
Transform(GameObjectID owner_id, Position pos, Size sz, float rot)
83+
Transform(GameObjectID owner_id, Position pos, Size sz, float rot = 0)
8284
: position(pos), size(sz), rotation(rot), Component { owner_id } {}
8385

8486
void set(int x) {
@@ -116,6 +118,37 @@ struct less_than_compare_key_RenderSystemComponent {
116118
}
117119
};
118120

121+
class PhysicsSystemComponent : public Component {
122+
public:
123+
PhysicsSystemComponent(GameObjectID owner_id)
124+
: Component{ owner_id } {}
125+
virtual Transform* transform() = 0;
126+
};
127+
128+
class Collider : public Component {
129+
private:
130+
Transform* _transform;
131+
Size _size;
132+
public:
133+
Collider(GameObjectID owner_id, Transform* transform)
134+
: Component{ owner_id }, _transform{ transform } {}
135+
const Size& size() { return _size; }
136+
Transform* transform() {
137+
return _transform;
138+
}
139+
};
140+
141+
class Rigidbody : public PhysicsSystemComponent {
142+
Vector2 velocity;
143+
public:
144+
Collider* _collider;
145+
Rigidbody(GameObjectID owner_id, Transform* transform)
146+
: PhysicsSystemComponent{ owner_id }, _collider{ new Collider{owner_id, transform} } {}
147+
Collider* collider() {
148+
return _collider;
149+
}
150+
};
151+
119152
class Sprite : public RenderSystemComponent {
120153
public:
121154
Transform* _transform;
@@ -139,7 +172,15 @@ class Sprite : public RenderSystemComponent {
139172
class GameObject {
140173
private:
141174
GameObjectID _id = next_id();
142-
std::vector<Component*> _component_list = { new Transform {_id, Position{10, 10}, Size{10, 10}, 0} };
175+
std::vector<Component*> _component_list = {
176+
new Transform {
177+
_id,
178+
Position{10, 10},
179+
Size{10, 10},
180+
0
181+
}
182+
};
183+
143184
public:
144185
const GameObjectID& id() { return _id; };
145186
const std::vector<Component*>& component_list() { return _component_list; };
@@ -153,7 +194,7 @@ class GameObject {
153194
T* add_component(Args&&... args) {
154195
static_assert(std::is_base_of<Component, T>::value, "T must inherit Component");
155196

156-
// Check if a component of type T already exists and is unique
197+
// Check if a component of type T already exists and should be unique
157198
for (Component* comp : _component_list) {
158199
T* t = dynamic_cast<T*>(comp);
159200
if (t && t->unique_on_gameobject()) {

proj-BirdEngine/src/engine_core.cpp

+22-5
Original file line numberDiff line numberDiff line change
@@ -57,19 +57,35 @@ namespace WING_API {
5757

5858
namespace WING {
5959

60-
class PhysicsSystemComponent {
60+
class RenderSystem {
61+
private:
62+
std::shared_ptr<DeviceInterface> _device_interface;
63+
public:
64+
RenderSystem(std::shared_ptr<DeviceInterface> device_interface_)
65+
: _device_interface{ device_interface_ } {}
6166

62-
};
67+
void update(std::vector<RenderSystemComponent*> comps) {
68+
69+
//_device_interface->renderer()->draw();
70+
71+
_device_interface->renderer()->draw_background();
6372

64-
class PhysicsSystem {
73+
std::sort(comps.begin(), comps.end(), less_than_compare_key_RenderSystemComponent());
74+
for (RenderSystemComponent* c : comps)
75+
{
76+
auto d = Drawable{ c->texture_id(), c->transform(), c->layer_index() };
77+
_device_interface->renderer()->draw_drawable(d);
78+
}
6579

80+
_device_interface->renderer()->apply_draw();
81+
}
6682
};
6783

68-
class RenderSystem {
84+
class PsysicsSystem {
6985
private:
7086
std::shared_ptr<DeviceInterface> _device_interface;
7187
public:
72-
RenderSystem(std::shared_ptr<DeviceInterface> device_interface_)
88+
PsysicsSystem(std::shared_ptr<DeviceInterface> device_interface_)
7389
: _device_interface{ device_interface_ } {}
7490

7591
void update(std::vector<RenderSystemComponent*> comps) {
@@ -93,6 +109,7 @@ namespace WING {
93109
std::shared_ptr<WING::Registry> _registry = std::make_shared<WING::Registry>();
94110
bool initialized = false;
95111
bool game_initialized = false;
112+
PsysicsSystem psysics_system;
96113
RenderSystem render_system{ _device_interface };
97114
Scene* current_scene = nullptr;
98115

proj-FlappyBird/src/main.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include <SDL.h>
1+
//#include <SDL.h>
22
#include <wing.h>
33
//#include <game/game.h>
44
#include <res_manager/texture_manager.h>
@@ -48,7 +48,7 @@ bool init() {
4848
Registry::set_start_scene(main_scene_id);
4949
pipe = Registry::create_new_gameobject();
5050
pipe->get_component<Transform>()->set(100, 100, 100, 100);
51-
pipe->add_component<Sprite>(pipe->get_component<Transform>(), texture_manager.get_texture_id(TEXTURE_KEY::PIPE));
51+
pipe->add_component<Sprite>(pipe->get_component<Transform>(), texture_manager.get_texture_id(TEXTURE_KEY::TEXTURE_FLAPPY_BIRD_UP_WING));
5252
Registry::add_gameobject_to_scene(pipe, main_scene_id);
5353

5454

@@ -61,8 +61,8 @@ void run_game() {
6161
//////////////program_state = BirdEngine::instance()->update();
6262

6363
// Move
64-
auto p = pipe->get_component<Transform>()->position;
65-
pipe->get_component<Transform>()->set(p.x + 1, 100, 100, 100);
64+
//auto p = pipe->get_component<Transform>()->position;
65+
//pipe->get_component<Transform>()->set(p.x + 1, 100, 100, 100);
6666

6767
Wing::run_game();
6868
// TEST

0 commit comments

Comments
 (0)