Skip to content

Commit 094f5b4

Browse files
committed
feat: refactor addCommonsToPacketByEntity to include additional components and improve packet serialization
1 parent 44b3410 commit 094f5b4

File tree

1 file changed

+99
-164
lines changed

1 file changed

+99
-164
lines changed

Flakkari/Protocol/PacketFactory.hpp

Lines changed: 99 additions & 164 deletions
Original file line numberDiff line numberDiff line change
@@ -26,72 +26,99 @@ namespace Flakkari::Protocol {
2626

2727
class PacketFactory {
2828
public:
29-
// /**
30-
// * @brief Add all the commons components of an entity to a packet.
31-
// *
32-
// * @tparam Id Type of the entity id.
33-
// * @param packet Packet to add the components to.
34-
// * @param registry Registry to get the components from.
35-
// * @param entity Entity to get the components from.
36-
// */
37-
// template<typename Id>
38-
// static void addCommonsToPacketByEntity (
39-
// Protocol::Packet<Id> &packet, Engine::ECS::Registry &registry, Engine::ECS::Entity entity
40-
// ) {
41-
// auto child = registry.getComponents<Engine::ECS::Components::Common::Child>();
42-
// auto childEntity = child[entity];
43-
44-
// if (childEntity.has_value()) {
45-
// packet << Protocol::ComponentId::CHILD;
46-
// packet << childEntity->size();
47-
// packet << childEntity->name;
48-
// }
49-
50-
// // auto evolve = registry.getComponents<Engine::ECS::Components::Common::Evolve>();
51-
// // auto evolveEntity = evolve[entity];
52-
53-
// // if (evolveEntity.has_value()) {
54-
// // packet << Protocol::ComponentId::EVOLVE;
55-
// // packet << evolveEntity->size();
56-
// // packet << evolveEntity->name;
57-
// // }
58-
59-
// // auto id = registry.getComponents<Engine::ECS::Components::Common::Id>();
60-
// // auto idEntity = id[entity];
61-
62-
// // if (idEntity.has_value()) {
63-
// // packet << Protocol::ComponentId::ID;
64-
// // packet << idEntity->size();
65-
// // packet << idEntity->id;
66-
// // }
67-
68-
// auto parent = registry.getComponents<Engine::ECS::Components::Common::Parent>();
69-
// auto parentEntity = parent[entity];
70-
71-
// if (parentEntity.has_value()) {
72-
// packet << Protocol::ComponentId::PARENT;
73-
// packet << parentEntity->size();
74-
// packet << parentEntity->entity;
75-
// }
76-
77-
// auto tag = registry.getComponents<Engine::ECS::Components::Common::Tag>();
78-
// auto tagEntity = tag[entity];
79-
80-
// if (tagEntity.has_value()) {
81-
// packet << Protocol::ComponentId::TAG;
82-
// packet << tagEntity->size();
83-
// packet << tagEntity->tag;
84-
// }
85-
86-
// auto name = registry.getComponents<Engine::ECS::Components::Common::Template>();
87-
// auto nameEntity = name[entity];
88-
89-
// if (nameEntity.has_value()) {
90-
// packet << Protocol::ComponentId::TEMPLATE;
91-
// packet << nameEntity->size();
92-
// packet << nameEntity->name;
93-
// }
94-
// }
29+
/**
30+
* @brief Add all the commons components of an entity to a packet.
31+
*
32+
* @tparam Id Type of the entity id.
33+
* @param packet Packet to add the components to.
34+
* @param registry Registry to get the components from.
35+
* @param entity Entity to get the components from.
36+
*/
37+
template<typename Id>
38+
static void addCommonsToPacketByEntity (
39+
Protocol::Packet<Id> &packet, Engine::ECS::Registry &registry, Engine::ECS::Entity entity
40+
) {
41+
auto child = registry.getComponents<Engine::ECS::Components::Common::Child>()[entity];
42+
43+
if (child.has_value()) {
44+
packet << Protocol::ComponentId::CHILD;
45+
packet.injectString(child->name);
46+
}
47+
48+
auto evolve = registry.getComponents<Engine::ECS::Components::Common::Evolve>()[entity];
49+
50+
if (evolve.has_value()) {
51+
packet << Protocol::ComponentId::EVOLVE;
52+
packet.injectString(evolve->name);
53+
}
54+
55+
auto health = registry.getComponents<Engine::ECS::Components::Common::Health>()[entity];
56+
57+
if (health.has_value()) {
58+
packet << Protocol::ComponentId::HEALTH;
59+
packet << health->currentHealth;
60+
packet << health->maxHealth;
61+
packet << health->shield;
62+
packet << health->maxShield;
63+
}
64+
65+
auto id = registry.getComponents<Engine::ECS::Components::Common::Id>()[entity];
66+
67+
if (id.has_value()) {
68+
packet << Protocol::ComponentId::ID;
69+
packet << id->id;
70+
}
71+
72+
auto level = registry.getComponents<Engine::ECS::Components::Common::Level>()[entity];
73+
74+
if (level.has_value()) {
75+
packet << Protocol::ComponentId::LEVEL;
76+
packet << level->level;
77+
packet.injectString(level->currentWeapon);
78+
packet << level->currentExp;
79+
packet << level->requiredExp;
80+
}
81+
82+
auto parent = registry.getComponents<Engine::ECS::Components::Common::Parent>()[entity];
83+
84+
if (parent.has_value()) {
85+
packet << Protocol::ComponentId::PARENT;
86+
packet << parent->entity;
87+
}
88+
89+
auto tag = registry.getComponents<Engine::ECS::Components::Common::Tag>()[entity];
90+
91+
if (tag.has_value()) {
92+
packet << Protocol::ComponentId::TAG;
93+
packet.injectString(tag->tag);
94+
}
95+
96+
auto template_ = registry.getComponents<Engine::ECS::Components::Common::Template>()[entity];
97+
98+
if (template_.has_value()) {
99+
packet << Protocol::ComponentId::TEMPLATE;
100+
packet.injectString(template_->name);
101+
}
102+
103+
auto timer = registry.getComponents<Engine::ECS::Components::Common::Timer>()[entity];
104+
105+
if (timer.has_value()) {
106+
packet << Protocol::ComponentId::TIMER;
107+
packet << timer->lastTime.time_since_epoch().count();
108+
packet << timer->maxTime;
109+
}
110+
111+
auto weapon = registry.getComponents<Engine::ECS::Components::Common::Weapon>()[entity];
112+
113+
if (weapon.has_value()) {
114+
packet << Protocol::ComponentId::WEAPON;
115+
packet << weapon->minDamage;
116+
packet << weapon->maxDamage;
117+
packet << weapon->chargeMaxTime;
118+
packet << weapon->fireRate;
119+
packet << weapon->level;
120+
}
121+
}
95122

96123
/**
97124
* @brief Add all the 2D components of an entity to a packet.
@@ -159,90 +186,6 @@ class PacketFactory {
159186
packet << rigidbody->_gravityScale;
160187
packet << rigidbody->_isKinematic;
161188
}
162-
163-
auto health = registry.getComponents<Engine::ECS::Components::Common::Health>()[entity];
164-
165-
if (health.has_value())
166-
{
167-
packet << ComponentId::HEALTH;
168-
packet << health->currentHealth;
169-
packet << health->maxHealth;
170-
packet << health->shield;
171-
packet << health->maxShield;
172-
}
173-
174-
auto weapon = registry.getComponents<Engine::ECS::Components::Common::Weapon>()[entity];
175-
176-
if (weapon.has_value())
177-
{
178-
packet << ComponentId::WEAPON;
179-
packet << weapon->damage;
180-
packet << weapon->fireRate;
181-
packet << weapon->level;
182-
}
183-
184-
auto level = registry.getComponents<Engine::ECS::Components::Common::Level>()[entity];
185-
186-
if (level.has_value())
187-
{
188-
packet << ComponentId::LEVEL;
189-
packet << level->level;
190-
packet.injectString(level->currentWeapon);
191-
packet << level->currentExp;
192-
packet << level->requiredExp;
193-
}
194-
195-
auto spawned = registry.getComponents<Engine::ECS::Components::Common::Spawned>()[entity];
196-
197-
if (spawned.has_value())
198-
{
199-
packet << ComponentId::SPAWNED;
200-
packet << spawned->has_spawned;
201-
}
202-
203-
auto networkEvent = registry.getComponents<Engine::ECS::Components::Common::NetworkEvent>()[entity];
204-
205-
if (networkEvent.has_value())
206-
{
207-
packet << ComponentId::NETWORK_EVENT;
208-
packet << networkEvent->events.size();
209-
for (auto &event : networkEvent->events)
210-
{
211-
packet << event;
212-
}
213-
}
214-
215-
auto templateName = registry.getComponents<Engine::ECS::Components::Common::Template>()[entity];
216-
217-
if (templateName.has_value())
218-
{
219-
packet << ComponentId::TEMPLATE;
220-
packet.injectString(templateName->name);
221-
}
222-
223-
auto parent = registry.getComponents<Engine::ECS::Components::Common::Parent>()[entity];
224-
225-
if (parent.has_value())
226-
{
227-
packet << ComponentId::PARENT;
228-
packet << parent->entity;
229-
}
230-
231-
auto child = registry.getComponents<Engine::ECS::Components::Common::Child>()[entity];
232-
233-
if (child.has_value())
234-
{
235-
packet << ComponentId::CHILD;
236-
packet.injectString(child->name);
237-
}
238-
239-
auto tag = registry.getComponents<Engine::ECS::Components::Common::Tag>()[entity];
240-
241-
if (tag.has_value())
242-
{
243-
packet << ComponentId::TAG;
244-
packet.injectString(tag->tag);
245-
}
246189
}
247190

248191
/**
@@ -267,6 +210,7 @@ class PacketFactory {
267210
packet << transform->_rotation.vec.x;
268211
packet << transform->_rotation.vec.y;
269212
packet << transform->_rotation.vec.z;
213+
packet << transform->_rotation.vec.w;
270214
packet << transform->_scale.vec.x;
271215
packet << transform->_scale.vec.y;
272216
packet << transform->_scale.vec.z;
@@ -283,24 +227,16 @@ class PacketFactory {
283227
packet << movable->_acceleration.vec.x;
284228
packet << movable->_acceleration.vec.y;
285229
packet << movable->_acceleration.vec.z;
230+
packet << movable->_minSpeed;
231+
packet << movable->_maxSpeed;
286232
}
287233

288234
auto control = registry.getComponents<Engine::ECS::Components::_3D::Control>()[entity];
289235

290236
if (control.has_value())
291237
{
292238
packet << ComponentId::CONTROL_3D;
293-
packet << control->_move_up;
294-
packet << control->_move_down;
295-
packet << control->_move_left;
296-
packet << control->_move_right;
297-
packet << control->_move_front;
298-
packet << control->_move_back;
299-
packet << control->_look_up;
300-
packet << control->_look_down;
301-
packet << control->_look_left;
302-
packet << control->_look_right;
303-
packet << control->_shoot;
239+
packet << control.value().toSerialized();
304240
}
305241

306242
auto boxCollider = registry.getComponents<Engine::ECS::Components::_3D::BoxCollider>()[entity];
@@ -335,8 +271,7 @@ class PacketFactory {
335271
packet << rigidbody->_mass;
336272
packet << rigidbody->_drag;
337273
packet << rigidbody->_angularDrag;
338-
packet << rigidbody->_useGravity;
339-
packet << rigidbody->_isKinematic;
274+
packet << (byte)rigidbody->_useGravity;
340275
}
341276
}
342277

@@ -354,7 +289,7 @@ class PacketFactory {
354289
{
355290
/*_ Common Components _*/
356291

357-
// addCommonsToPacketByEntity<Id>(packet, registry, entity);
292+
addCommonsToPacketByEntity<Id>(packet, registry, entity);
358293

359294
/*_ 2D Components _*/
360295

0 commit comments

Comments
 (0)