Skip to content

Commit

Permalink
feat: refactor addCommonsToPacketByEntity to include additional compo…
Browse files Browse the repository at this point in the history
…nents and improve packet serialization
  • Loading branch information
MasterLaplace committed Nov 22, 2024
1 parent 44b3410 commit 094f5b4
Showing 1 changed file with 99 additions and 164 deletions.
263 changes: 99 additions & 164 deletions Flakkari/Protocol/PacketFactory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<typename Id>
// static void addCommonsToPacketByEntity (
// Protocol::Packet<Id> &packet, Engine::ECS::Registry &registry, Engine::ECS::Entity entity
// ) {
// auto child = registry.getComponents<Engine::ECS::Components::Common::Child>();
// auto childEntity = child[entity];

// if (childEntity.has_value()) {
// packet << Protocol::ComponentId::CHILD;
// packet << childEntity->size();
// packet << childEntity->name;
// }

// // auto evolve = registry.getComponents<Engine::ECS::Components::Common::Evolve>();
// // auto evolveEntity = evolve[entity];

// // if (evolveEntity.has_value()) {
// // packet << Protocol::ComponentId::EVOLVE;
// // packet << evolveEntity->size();
// // packet << evolveEntity->name;
// // }

// // auto id = registry.getComponents<Engine::ECS::Components::Common::Id>();
// // auto idEntity = id[entity];

// // if (idEntity.has_value()) {
// // packet << Protocol::ComponentId::ID;
// // packet << idEntity->size();
// // packet << idEntity->id;
// // }

// auto parent = registry.getComponents<Engine::ECS::Components::Common::Parent>();
// auto parentEntity = parent[entity];

// if (parentEntity.has_value()) {
// packet << Protocol::ComponentId::PARENT;
// packet << parentEntity->size();
// packet << parentEntity->entity;
// }

// auto tag = registry.getComponents<Engine::ECS::Components::Common::Tag>();
// auto tagEntity = tag[entity];

// if (tagEntity.has_value()) {
// packet << Protocol::ComponentId::TAG;
// packet << tagEntity->size();
// packet << tagEntity->tag;
// }

// auto name = registry.getComponents<Engine::ECS::Components::Common::Template>();
// 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<typename Id>
static void addCommonsToPacketByEntity (
Protocol::Packet<Id> &packet, Engine::ECS::Registry &registry, Engine::ECS::Entity entity
) {
auto child = registry.getComponents<Engine::ECS::Components::Common::Child>()[entity];

if (child.has_value()) {
packet << Protocol::ComponentId::CHILD;
packet.injectString(child->name);
}

auto evolve = registry.getComponents<Engine::ECS::Components::Common::Evolve>()[entity];

if (evolve.has_value()) {
packet << Protocol::ComponentId::EVOLVE;
packet.injectString(evolve->name);
}

auto health = registry.getComponents<Engine::ECS::Components::Common::Health>()[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<Engine::ECS::Components::Common::Id>()[entity];

if (id.has_value()) {
packet << Protocol::ComponentId::ID;
packet << id->id;
}

auto level = registry.getComponents<Engine::ECS::Components::Common::Level>()[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<Engine::ECS::Components::Common::Parent>()[entity];

if (parent.has_value()) {
packet << Protocol::ComponentId::PARENT;
packet << parent->entity;
}

auto tag = registry.getComponents<Engine::ECS::Components::Common::Tag>()[entity];

if (tag.has_value()) {
packet << Protocol::ComponentId::TAG;
packet.injectString(tag->tag);
}

auto template_ = registry.getComponents<Engine::ECS::Components::Common::Template>()[entity];

if (template_.has_value()) {
packet << Protocol::ComponentId::TEMPLATE;
packet.injectString(template_->name);
}

auto timer = registry.getComponents<Engine::ECS::Components::Common::Timer>()[entity];

if (timer.has_value()) {
packet << Protocol::ComponentId::TIMER;
packet << timer->lastTime.time_since_epoch().count();
packet << timer->maxTime;
}

auto weapon = registry.getComponents<Engine::ECS::Components::Common::Weapon>()[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.
Expand Down Expand Up @@ -159,90 +186,6 @@ class PacketFactory {
packet << rigidbody->_gravityScale;
packet << rigidbody->_isKinematic;
}

auto health = registry.getComponents<Engine::ECS::Components::Common::Health>()[entity];

if (health.has_value())
{
packet << ComponentId::HEALTH;
packet << health->currentHealth;
packet << health->maxHealth;
packet << health->shield;
packet << health->maxShield;
}

auto weapon = registry.getComponents<Engine::ECS::Components::Common::Weapon>()[entity];

if (weapon.has_value())
{
packet << ComponentId::WEAPON;
packet << weapon->damage;
packet << weapon->fireRate;
packet << weapon->level;
}

auto level = registry.getComponents<Engine::ECS::Components::Common::Level>()[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<Engine::ECS::Components::Common::Spawned>()[entity];

if (spawned.has_value())
{
packet << ComponentId::SPAWNED;
packet << spawned->has_spawned;
}

auto networkEvent = registry.getComponents<Engine::ECS::Components::Common::NetworkEvent>()[entity];

if (networkEvent.has_value())
{
packet << ComponentId::NETWORK_EVENT;
packet << networkEvent->events.size();
for (auto &event : networkEvent->events)
{
packet << event;
}
}

auto templateName = registry.getComponents<Engine::ECS::Components::Common::Template>()[entity];

if (templateName.has_value())
{
packet << ComponentId::TEMPLATE;
packet.injectString(templateName->name);
}

auto parent = registry.getComponents<Engine::ECS::Components::Common::Parent>()[entity];

if (parent.has_value())
{
packet << ComponentId::PARENT;
packet << parent->entity;
}

auto child = registry.getComponents<Engine::ECS::Components::Common::Child>()[entity];

if (child.has_value())
{
packet << ComponentId::CHILD;
packet.injectString(child->name);
}

auto tag = registry.getComponents<Engine::ECS::Components::Common::Tag>()[entity];

if (tag.has_value())
{
packet << ComponentId::TAG;
packet.injectString(tag->tag);
}
}

/**
Expand All @@ -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;
Expand All @@ -283,24 +227,16 @@ 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<Engine::ECS::Components::_3D::Control>()[entity];

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<Engine::ECS::Components::_3D::BoxCollider>()[entity];
Expand Down Expand Up @@ -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;
}
}

Expand All @@ -354,7 +289,7 @@ class PacketFactory {
{
/*_ Common Components _*/

// addCommonsToPacketByEntity<Id>(packet, registry, entity);
addCommonsToPacketByEntity<Id>(packet, registry, entity);

/*_ 2D Components _*/

Expand Down

0 comments on commit 094f5b4

Please sign in to comment.