From 094f5b4b31b8e8a0770ef8ba503c4caffc2fb168 Mon Sep 17 00:00:00 2001 From: MasterLaplace Date: Fri, 22 Nov 2024 15:45:53 -0500 Subject: [PATCH] feat: refactor addCommonsToPacketByEntity to include additional components and improve packet serialization --- Flakkari/Protocol/PacketFactory.hpp | 263 +++++++++++----------------- 1 file changed, 99 insertions(+), 164 deletions(-) diff --git a/Flakkari/Protocol/PacketFactory.hpp b/Flakkari/Protocol/PacketFactory.hpp index 014d2307..b1f4d255 100644 --- a/Flakkari/Protocol/PacketFactory.hpp +++ b/Flakkari/Protocol/PacketFactory.hpp @@ -26,72 +26,99 @@ namespace Flakkari::Protocol { class PacketFactory { public: - // /** - // * @brief Add all the commons components of an entity to a packet. - // * - // * @tparam Id Type of the entity id. - // * @param packet Packet to add the components to. - // * @param registry Registry to get the components from. - // * @param entity Entity to get the components from. - // */ - // template - // static void addCommonsToPacketByEntity ( - // Protocol::Packet &packet, Engine::ECS::Registry ®istry, Engine::ECS::Entity entity - // ) { - // auto child = registry.getComponents(); - // auto childEntity = child[entity]; - - // if (childEntity.has_value()) { - // packet << Protocol::ComponentId::CHILD; - // packet << childEntity->size(); - // packet << childEntity->name; - // } - - // // auto evolve = registry.getComponents(); - // // auto evolveEntity = evolve[entity]; - - // // if (evolveEntity.has_value()) { - // // packet << Protocol::ComponentId::EVOLVE; - // // packet << evolveEntity->size(); - // // packet << evolveEntity->name; - // // } - - // // auto id = registry.getComponents(); - // // auto idEntity = id[entity]; - - // // if (idEntity.has_value()) { - // // packet << Protocol::ComponentId::ID; - // // packet << idEntity->size(); - // // packet << idEntity->id; - // // } - - // auto parent = registry.getComponents(); - // auto parentEntity = parent[entity]; - - // if (parentEntity.has_value()) { - // packet << Protocol::ComponentId::PARENT; - // packet << parentEntity->size(); - // packet << parentEntity->entity; - // } - - // auto tag = registry.getComponents(); - // auto tagEntity = tag[entity]; - - // if (tagEntity.has_value()) { - // packet << Protocol::ComponentId::TAG; - // packet << tagEntity->size(); - // packet << tagEntity->tag; - // } - - // auto name = registry.getComponents(); - // auto nameEntity = name[entity]; - - // if (nameEntity.has_value()) { - // packet << Protocol::ComponentId::TEMPLATE; - // packet << nameEntity->size(); - // packet << nameEntity->name; - // } - // } + /** + * @brief Add all the commons components of an entity to a packet. + * + * @tparam Id Type of the entity id. + * @param packet Packet to add the components to. + * @param registry Registry to get the components from. + * @param entity Entity to get the components from. + */ + template + static void addCommonsToPacketByEntity ( + Protocol::Packet &packet, Engine::ECS::Registry ®istry, Engine::ECS::Entity entity + ) { + auto child = registry.getComponents()[entity]; + + if (child.has_value()) { + packet << Protocol::ComponentId::CHILD; + packet.injectString(child->name); + } + + auto evolve = registry.getComponents()[entity]; + + if (evolve.has_value()) { + packet << Protocol::ComponentId::EVOLVE; + packet.injectString(evolve->name); + } + + auto health = registry.getComponents()[entity]; + + if (health.has_value()) { + packet << Protocol::ComponentId::HEALTH; + packet << health->currentHealth; + packet << health->maxHealth; + packet << health->shield; + packet << health->maxShield; + } + + auto id = registry.getComponents()[entity]; + + if (id.has_value()) { + packet << Protocol::ComponentId::ID; + packet << id->id; + } + + auto level = registry.getComponents()[entity]; + + if (level.has_value()) { + packet << Protocol::ComponentId::LEVEL; + packet << level->level; + packet.injectString(level->currentWeapon); + packet << level->currentExp; + packet << level->requiredExp; + } + + auto parent = registry.getComponents()[entity]; + + if (parent.has_value()) { + packet << Protocol::ComponentId::PARENT; + packet << parent->entity; + } + + auto tag = registry.getComponents()[entity]; + + if (tag.has_value()) { + packet << Protocol::ComponentId::TAG; + packet.injectString(tag->tag); + } + + auto template_ = registry.getComponents()[entity]; + + if (template_.has_value()) { + packet << Protocol::ComponentId::TEMPLATE; + packet.injectString(template_->name); + } + + auto timer = registry.getComponents()[entity]; + + if (timer.has_value()) { + packet << Protocol::ComponentId::TIMER; + packet << timer->lastTime.time_since_epoch().count(); + packet << timer->maxTime; + } + + auto weapon = registry.getComponents()[entity]; + + if (weapon.has_value()) { + packet << Protocol::ComponentId::WEAPON; + packet << weapon->minDamage; + packet << weapon->maxDamage; + packet << weapon->chargeMaxTime; + packet << weapon->fireRate; + packet << weapon->level; + } + } /** * @brief Add all the 2D components of an entity to a packet. @@ -159,90 +186,6 @@ class PacketFactory { packet << rigidbody->_gravityScale; packet << rigidbody->_isKinematic; } - - auto health = registry.getComponents()[entity]; - - if (health.has_value()) - { - packet << ComponentId::HEALTH; - packet << health->currentHealth; - packet << health->maxHealth; - packet << health->shield; - packet << health->maxShield; - } - - auto weapon = registry.getComponents()[entity]; - - if (weapon.has_value()) - { - packet << ComponentId::WEAPON; - packet << weapon->damage; - packet << weapon->fireRate; - packet << weapon->level; - } - - auto level = registry.getComponents()[entity]; - - if (level.has_value()) - { - packet << ComponentId::LEVEL; - packet << level->level; - packet.injectString(level->currentWeapon); - packet << level->currentExp; - packet << level->requiredExp; - } - - auto spawned = registry.getComponents()[entity]; - - if (spawned.has_value()) - { - packet << ComponentId::SPAWNED; - packet << spawned->has_spawned; - } - - auto networkEvent = registry.getComponents()[entity]; - - if (networkEvent.has_value()) - { - packet << ComponentId::NETWORK_EVENT; - packet << networkEvent->events.size(); - for (auto &event : networkEvent->events) - { - packet << event; - } - } - - auto templateName = registry.getComponents()[entity]; - - if (templateName.has_value()) - { - packet << ComponentId::TEMPLATE; - packet.injectString(templateName->name); - } - - auto parent = registry.getComponents()[entity]; - - if (parent.has_value()) - { - packet << ComponentId::PARENT; - packet << parent->entity; - } - - auto child = registry.getComponents()[entity]; - - if (child.has_value()) - { - packet << ComponentId::CHILD; - packet.injectString(child->name); - } - - auto tag = registry.getComponents()[entity]; - - if (tag.has_value()) - { - packet << ComponentId::TAG; - packet.injectString(tag->tag); - } } /** @@ -267,6 +210,7 @@ class PacketFactory { packet << transform->_rotation.vec.x; packet << transform->_rotation.vec.y; packet << transform->_rotation.vec.z; + packet << transform->_rotation.vec.w; packet << transform->_scale.vec.x; packet << transform->_scale.vec.y; packet << transform->_scale.vec.z; @@ -283,6 +227,8 @@ class PacketFactory { packet << movable->_acceleration.vec.x; packet << movable->_acceleration.vec.y; packet << movable->_acceleration.vec.z; + packet << movable->_minSpeed; + packet << movable->_maxSpeed; } auto control = registry.getComponents()[entity]; @@ -290,17 +236,7 @@ class PacketFactory { if (control.has_value()) { packet << ComponentId::CONTROL_3D; - packet << control->_move_up; - packet << control->_move_down; - packet << control->_move_left; - packet << control->_move_right; - packet << control->_move_front; - packet << control->_move_back; - packet << control->_look_up; - packet << control->_look_down; - packet << control->_look_left; - packet << control->_look_right; - packet << control->_shoot; + packet << control.value().toSerialized(); } auto boxCollider = registry.getComponents()[entity]; @@ -335,8 +271,7 @@ class PacketFactory { packet << rigidbody->_mass; packet << rigidbody->_drag; packet << rigidbody->_angularDrag; - packet << rigidbody->_useGravity; - packet << rigidbody->_isKinematic; + packet << (byte)rigidbody->_useGravity; } } @@ -354,7 +289,7 @@ class PacketFactory { { /*_ Common Components _*/ - // addCommonsToPacketByEntity(packet, registry, entity); + addCommonsToPacketByEntity(packet, registry, entity); /*_ 2D Components _*/