@@ -10,14 +10,16 @@ struct Vector2 {
10
10
float x, y;
11
11
};
12
12
struct Direction : public Vector2 {
13
- public:
13
+ public:
14
+ Direction () = default ;
14
15
Direction (float x = 0 , float y = 0 ) : Vector2{ x, y } {}
15
16
Direction (int x = 0 , int y = 0 ) : Vector2{ (float )x, (float )y } {}
16
17
};
17
18
struct Size : public Vector2 {
18
19
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 } {}
21
23
};
22
24
struct Position : public Vector2 {
23
25
public:
@@ -78,7 +80,7 @@ class Transform : public Component {
78
80
Size size;
79
81
float rotation = 0 ;
80
82
81
- Transform (GameObjectID owner_id, Position pos, Size sz, float rot)
83
+ Transform (GameObjectID owner_id, Position pos, Size sz, float rot = 0 )
82
84
: position(pos), size(sz), rotation(rot), Component { owner_id } {}
83
85
84
86
void set (int x) {
@@ -116,6 +118,37 @@ struct less_than_compare_key_RenderSystemComponent {
116
118
}
117
119
};
118
120
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
+
119
152
class Sprite : public RenderSystemComponent {
120
153
public:
121
154
Transform* _transform;
@@ -139,7 +172,15 @@ class Sprite : public RenderSystemComponent {
139
172
class GameObject {
140
173
private:
141
174
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
+
143
184
public:
144
185
const GameObjectID& id () { return _id; };
145
186
const std::vector<Component*>& component_list () { return _component_list; };
@@ -153,7 +194,7 @@ class GameObject {
153
194
T* add_component (Args&&... args) {
154
195
static_assert (std::is_base_of<Component, T>::value, " T must inherit Component" );
155
196
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
157
198
for (Component* comp : _component_list) {
158
199
T* t = dynamic_cast <T*>(comp);
159
200
if (t && t->unique_on_gameobject ()) {
0 commit comments