From 6e2c1d65eeae991a19d499746b3fd3b7e8c54007 Mon Sep 17 00:00:00 2001 From: MasterLaplace Date: Thu, 7 Nov 2024 09:37:05 -0500 Subject: [PATCH] refactor: implement assignment operators for various component structs --- .../Components/2D/Collider.hpp | 7 +++++++ .../Components/2D/Control.hpp | 13 +++++++++++++ .../Components/2D/Movable.hpp | 10 ++++++++++ .../Components/2D/RigidBody.hpp | 14 ++++++++++++++ .../Components/2D/Transform.hpp | 11 +++++++++++ .../Components/Common/Child.hpp | 7 +++++++ .../Components/Common/Evolve.hpp | 7 +++++++ .../Components/Common/Health.hpp | 12 ++++++++++++ .../Components/Common/Id.hpp | 7 +++++++ .../Components/Common/Level.hpp | 13 +++++++++++++ .../Components/Common/NetworkEvent.hpp | 7 +++++++ .../Components/Common/NetworkIp.hpp | 7 +++++++ .../Components/Common/Parent.hpp | 8 ++++++++ .../Components/Common/Spawned.hpp | 7 +++++++ .../Components/Common/Tag.hpp | 7 +++++++ .../Components/Common/Template.hpp | 7 +++++++ .../Components/Common/Weapon.hpp | 11 +++++++++++ Flakkari/Protocol/PacketFactory.hpp | 16 ---------------- Flakkari/Server/Game/GameManager.cpp | 16 ---------------- Flakkari/Server/Game/GameManager.hpp | 17 ----------------- 20 files changed, 155 insertions(+), 49 deletions(-) diff --git a/Flakkari/Engine/EntityComponentSystem/Components/2D/Collider.hpp b/Flakkari/Engine/EntityComponentSystem/Components/2D/Collider.hpp index 04f4606c..da5d5115 100644 --- a/Flakkari/Engine/EntityComponentSystem/Components/2D/Collider.hpp +++ b/Flakkari/Engine/EntityComponentSystem/Components/2D/Collider.hpp @@ -28,6 +28,13 @@ struct Collider { Collider(Math::Vector2f nsize) : _size(nsize) {} Collider(const Collider &other) : _size(other._size) {} + Collider& operator=(const Collider& other) { + if (this != &other) + _size = other._size; + + return *this; + } + std::size_t size() const { return sizeof(_size); } }; diff --git a/Flakkari/Engine/EntityComponentSystem/Components/2D/Control.hpp b/Flakkari/Engine/EntityComponentSystem/Components/2D/Control.hpp index a0290018..12a1297a 100644 --- a/Flakkari/Engine/EntityComponentSystem/Components/2D/Control.hpp +++ b/Flakkari/Engine/EntityComponentSystem/Components/2D/Control.hpp @@ -38,6 +38,19 @@ struct Control { Control(const Control &other) : up(other.up), down(other.down), left(other.left), right(other.right), shoot(other.shoot){}; + Control& operator=(const Control& other) { + if (this != &other) + { + up = other.up; + down = other.down; + left = other.left; + right = other.right; + shoot = other.shoot; + } + + return *this; + } + std::size_t size() const { return sizeof(*this); } }; diff --git a/Flakkari/Engine/EntityComponentSystem/Components/2D/Movable.hpp b/Flakkari/Engine/EntityComponentSystem/Components/2D/Movable.hpp index 372deedb..deeed31d 100644 --- a/Flakkari/Engine/EntityComponentSystem/Components/2D/Movable.hpp +++ b/Flakkari/Engine/EntityComponentSystem/Components/2D/Movable.hpp @@ -24,6 +24,16 @@ struct Movable { : velocity(velocity), acceleration(acceleration){}; Movable(const Movable &other) : velocity(other.velocity), acceleration(other.acceleration){}; + Movable& operator=(const Movable& other) { + if (this != &other) + { + velocity = other.velocity; + acceleration = other.acceleration; + } + + return *this; + } + std::size_t size() const { return sizeof(*this); } }; diff --git a/Flakkari/Engine/EntityComponentSystem/Components/2D/RigidBody.hpp b/Flakkari/Engine/EntityComponentSystem/Components/2D/RigidBody.hpp index 2122a01c..d2a5b5e2 100644 --- a/Flakkari/Engine/EntityComponentSystem/Components/2D/RigidBody.hpp +++ b/Flakkari/Engine/EntityComponentSystem/Components/2D/RigidBody.hpp @@ -35,6 +35,20 @@ struct RigidBody { : mass(mass), restitution(restitution), friction(friction), gravityScale(gravityScale), isGravityAffected(true), isKinematic(false){}; + RigidBody& operator=(const RigidBody& other) { + if (this != &other) + { + mass = other.mass; + restitution = other.restitution; + friction = other.friction; + gravityScale = other.gravityScale; + isGravityAffected = other.isGravityAffected; + isKinematic = other.isKinematic; + } + + return *this; + } + std::size_t size() const { return sizeof(*this); } }; diff --git a/Flakkari/Engine/EntityComponentSystem/Components/2D/Transform.hpp b/Flakkari/Engine/EntityComponentSystem/Components/2D/Transform.hpp index 19c1e280..23c8f435 100644 --- a/Flakkari/Engine/EntityComponentSystem/Components/2D/Transform.hpp +++ b/Flakkari/Engine/EntityComponentSystem/Components/2D/Transform.hpp @@ -25,6 +25,17 @@ struct Transform { : position(position), scale(scale), rotation(rotation){}; Transform(const Transform &other) : position(other.position), scale(other.scale), rotation(other.rotation){}; + Transform& operator=(const Transform& other) { + if (this != &other) + { + position = other.position; + scale = other.scale; + rotation = other.rotation; + } + + return *this; + } + std::size_t size() const { return sizeof(*this); } }; diff --git a/Flakkari/Engine/EntityComponentSystem/Components/Common/Child.hpp b/Flakkari/Engine/EntityComponentSystem/Components/Common/Child.hpp index f749edfc..44964b18 100644 --- a/Flakkari/Engine/EntityComponentSystem/Components/Common/Child.hpp +++ b/Flakkari/Engine/EntityComponentSystem/Components/Common/Child.hpp @@ -27,6 +27,13 @@ struct Child { Child(const std::string &nname) : name(nname) {} Child(const Child &other) : name(other.name) {} + Child &operator=(const Child &other) { + if (this != &other) + name = other.name; + + return *this; + } + std::size_t size() const { return name.size(); } }; diff --git a/Flakkari/Engine/EntityComponentSystem/Components/Common/Evolve.hpp b/Flakkari/Engine/EntityComponentSystem/Components/Common/Evolve.hpp index 3433c786..f53ffb4b 100644 --- a/Flakkari/Engine/EntityComponentSystem/Components/Common/Evolve.hpp +++ b/Flakkari/Engine/EntityComponentSystem/Components/Common/Evolve.hpp @@ -27,6 +27,13 @@ struct Evolve { Evolve(const std::string &nname) : name(nname) {} Evolve(const Evolve &other) : name(other.name) {} + Evolve &operator=(const Evolve &other) { + if (this != &other) + name = other.name; + + return *this; + } + std::size_t size() const { return name.size(); } }; diff --git a/Flakkari/Engine/EntityComponentSystem/Components/Common/Health.hpp b/Flakkari/Engine/EntityComponentSystem/Components/Common/Health.hpp index 910678b1..f8e25062 100644 --- a/Flakkari/Engine/EntityComponentSystem/Components/Common/Health.hpp +++ b/Flakkari/Engine/EntityComponentSystem/Components/Common/Health.hpp @@ -34,6 +34,18 @@ struct Health { : currentHealth(other.currentHealth), maxHealth(other.maxHealth), shield(other.shield), maxShield(other.maxShield){}; + Health& operator=(const Health& other) { + if (this != &other) + { + currentHealth = other.currentHealth; + maxHealth = other.maxHealth; + shield = other.shield; + maxShield = other.maxShield; + } + + return *this; + } + std::size_t size() const { return sizeof(*this); } }; diff --git a/Flakkari/Engine/EntityComponentSystem/Components/Common/Id.hpp b/Flakkari/Engine/EntityComponentSystem/Components/Common/Id.hpp index d8dd97ba..fc6f6105 100644 --- a/Flakkari/Engine/EntityComponentSystem/Components/Common/Id.hpp +++ b/Flakkari/Engine/EntityComponentSystem/Components/Common/Id.hpp @@ -24,6 +24,13 @@ struct Id { Id(std::size_t id) : id(id) {} Id(const Id &other) : id(other.id) {} + Id &operator=(const Id &other) { + if (this != &other) + id = other.id; + + return *this; + } + std::size_t size() const { return sizeof(id); } }; diff --git a/Flakkari/Engine/EntityComponentSystem/Components/Common/Level.hpp b/Flakkari/Engine/EntityComponentSystem/Components/Common/Level.hpp index f6805123..aeb5e6c0 100644 --- a/Flakkari/Engine/EntityComponentSystem/Components/Common/Level.hpp +++ b/Flakkari/Engine/EntityComponentSystem/Components/Common/Level.hpp @@ -38,6 +38,19 @@ struct Level { { } + Level &operator=(const Level &other) + { + if (this != &other) + { + level = other.level; + currentWeapon = other.currentWeapon; + currentExp = other.currentExp; + requiredExp = other.requiredExp; + } + + return *this; + } + std::size_t size() const { return sizeof(level) + currentWeapon.size() + sizeof(currentExp) + sizeof(requiredExp); } }; diff --git a/Flakkari/Engine/EntityComponentSystem/Components/Common/NetworkEvent.hpp b/Flakkari/Engine/EntityComponentSystem/Components/Common/NetworkEvent.hpp index 70e33bef..521ce009 100644 --- a/Flakkari/Engine/EntityComponentSystem/Components/Common/NetworkEvent.hpp +++ b/Flakkari/Engine/EntityComponentSystem/Components/Common/NetworkEvent.hpp @@ -21,6 +21,13 @@ struct NetworkEvent { NetworkEvent(const NetworkEvent &other) : events(other.events){}; NetworkEvent(const std::vector &events) : events(events){}; + NetworkEvent &operator=(const NetworkEvent &other) { + if (this != &other) + events = other.events; + + return *this; + } + std::size_t size() const { return events.size() * sizeof(unsigned short); } }; diff --git a/Flakkari/Engine/EntityComponentSystem/Components/Common/NetworkIp.hpp b/Flakkari/Engine/EntityComponentSystem/Components/Common/NetworkIp.hpp index f6283090..98af95fe 100644 --- a/Flakkari/Engine/EntityComponentSystem/Components/Common/NetworkIp.hpp +++ b/Flakkari/Engine/EntityComponentSystem/Components/Common/NetworkIp.hpp @@ -21,6 +21,13 @@ struct NetworkIp { NetworkIp(std::string ip) : ip(ip) {} NetworkIp(const NetworkIp &other) : ip(other.ip) {} + NetworkIp &operator=(const NetworkIp &other) { + if (this != &other) + ip = other.ip; + + return *this; + } + std::size_t size() const { return ip.size(); } }; diff --git a/Flakkari/Engine/EntityComponentSystem/Components/Common/Parent.hpp b/Flakkari/Engine/EntityComponentSystem/Components/Common/Parent.hpp index f4517f92..a531e8f3 100644 --- a/Flakkari/Engine/EntityComponentSystem/Components/Common/Parent.hpp +++ b/Flakkari/Engine/EntityComponentSystem/Components/Common/Parent.hpp @@ -28,6 +28,14 @@ struct Parent { Parent() : entity(0) {} Parent(const std::size_t &entity) : entity(entity) {} Parent(const Parent &other) : entity(other.entity) {} + + Parent &operator=(const Parent &other) { + if (this != &other) + entity = other.entity; + + return *this; + } + std::size_t size() const { return sizeof(*this); } }; diff --git a/Flakkari/Engine/EntityComponentSystem/Components/Common/Spawned.hpp b/Flakkari/Engine/EntityComponentSystem/Components/Common/Spawned.hpp index 97ccf8a2..8b17f207 100644 --- a/Flakkari/Engine/EntityComponentSystem/Components/Common/Spawned.hpp +++ b/Flakkari/Engine/EntityComponentSystem/Components/Common/Spawned.hpp @@ -30,6 +30,13 @@ struct Spawned { Spawned(bool spawed) : has_spawned(spawed) {} Spawned(const Spawned &other) : has_spawned(other.has_spawned) {} + Spawned& operator=(const Spawned& other) { + if (this != &other) + has_spawned = other.has_spawned; + + return *this; + } + std::size_t size() const { return sizeof(has_spawned); } }; diff --git a/Flakkari/Engine/EntityComponentSystem/Components/Common/Tag.hpp b/Flakkari/Engine/EntityComponentSystem/Components/Common/Tag.hpp index eb4e0c85..0c2ce994 100644 --- a/Flakkari/Engine/EntityComponentSystem/Components/Common/Tag.hpp +++ b/Flakkari/Engine/EntityComponentSystem/Components/Common/Tag.hpp @@ -26,6 +26,13 @@ struct Tag { Tag(const std::string &ntag) : tag(ntag) {} Tag(const Tag &other) : tag(other.tag) {} + Tag &operator=(const Tag &other) { + if (this != &other) + tag = other.tag; + + return *this; + } + std::size_t size() const { return tag.size(); } }; diff --git a/Flakkari/Engine/EntityComponentSystem/Components/Common/Template.hpp b/Flakkari/Engine/EntityComponentSystem/Components/Common/Template.hpp index 30e6f9d2..0c8074e1 100644 --- a/Flakkari/Engine/EntityComponentSystem/Components/Common/Template.hpp +++ b/Flakkari/Engine/EntityComponentSystem/Components/Common/Template.hpp @@ -21,6 +21,13 @@ struct Template { Template(const std::string &nname) : name(nname) {} Template(const Template &other) : name(other.name) {} + Template &operator=(const Template &other) { + if (this != &other) + name = other.name; + + return *this; + } + std::size_t size() const { return name.size(); } }; diff --git a/Flakkari/Engine/EntityComponentSystem/Components/Common/Weapon.hpp b/Flakkari/Engine/EntityComponentSystem/Components/Common/Weapon.hpp index cd31d2e0..2ff81d2a 100644 --- a/Flakkari/Engine/EntityComponentSystem/Components/Common/Weapon.hpp +++ b/Flakkari/Engine/EntityComponentSystem/Components/Common/Weapon.hpp @@ -43,6 +43,17 @@ struct Weapon { Weapon(const Weapon &other) = default; Weapon(std::size_t dmg, float rate, std::size_t lvl) : damage(dmg), fireRate(rate), level(lvl){}; + Weapon &operator=(const Weapon &other) { + if (this != &other) + { + damage = other.damage; + fireRate = other.fireRate; + level = other.level; + } + + return *this; + } + std::size_t size() const { return sizeof(*this); }; }; diff --git a/Flakkari/Protocol/PacketFactory.hpp b/Flakkari/Protocol/PacketFactory.hpp index a412867e..94e18aed 100644 --- a/Flakkari/Protocol/PacketFactory.hpp +++ b/Flakkari/Protocol/PacketFactory.hpp @@ -266,22 +266,6 @@ class PacketFactory { add2dToPacketByEntity(packet, registry, entity); } - /** - * @brief Add requirement information for Command that use components. - * @tparam Id Type of the entity id. - * @param packet Packet to add the components to. - * @param size Size of the packet. - * @param sceneName Name of the scene. - * @param entity Entity to get the components from. - */ - template - static void addInfoToPacket(Packet &packet, std::size_t size, const std::string &sceneName, - Engine::ECS::Entity entity) - { - packet.injectString(sceneName); - packet << entity; - } - struct UpdateMovement { Engine::ECS::Entity entity; Engine::ECS::Components::_2D::Transform pos; diff --git a/Flakkari/Server/Game/GameManager.cpp b/Flakkari/Server/Game/GameManager.cpp index 817299bd..dd1bce8a 100644 --- a/Flakkari/Server/Game/GameManager.cpp +++ b/Flakkari/Server/Game/GameManager.cpp @@ -72,22 +72,6 @@ int GameManager::addGame(const std::string &gameName) return 0; } -const std::shared_ptr &GameManager::getGame(const std::string &gameName) -{ - if (_gamesStore.find(gameName) == _gamesStore.end()) - return FLAKKARI_LOG_ERROR("game not found"), nullptr; - return std::make_shared(gameName, _gamesStore[gameName]); -} - -const std::vector> &GameManager::getGamesInstances() -{ - std::vector> gamesInstances(_gamesInstances.size()); - - for (auto &game : _gamesInstances) - gamesInstances.insert(gamesInstances.end(), game.second.begin(), game.second.end()); - return gamesInstances; -} - int GameManager::updateGame(const std::string &gameName) { if (_gamesStore.find(gameName) == _gamesStore.end()) diff --git a/Flakkari/Server/Game/GameManager.hpp b/Flakkari/Server/Game/GameManager.hpp index b392fb33..1f132c89 100644 --- a/Flakkari/Server/Game/GameManager.hpp +++ b/Flakkari/Server/Game/GameManager.hpp @@ -65,23 +65,6 @@ class GameManager : public Singleton { */ int addGame(const std::string &gameName); - /** - * @brief Get the Game object - * - * @param gameName Game to get - * @return std::shared_ptr Game - * - * @deprecated Use getGameInstance instead - */ - const std::shared_ptr &getGame(const std::string &gameName); - - /** - * @brief Get the Games Instances object (all games loaded) - * - * @return std::vector> Games Instances - */ - const std::vector> &getGamesInstances(); - /** * @brief Update a game from the GameManager *