Skip to content

Commit

Permalink
refactor: implement assignment operators for various component structs
Browse files Browse the repository at this point in the history
  • Loading branch information
MasterLaplace committed Nov 7, 2024
1 parent 7a5d15b commit 6e2c1d6
Show file tree
Hide file tree
Showing 20 changed files with 155 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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); }
};

Expand Down
13 changes: 13 additions & 0 deletions Flakkari/Engine/EntityComponentSystem/Components/2D/Control.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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); }
};

Expand Down
10 changes: 10 additions & 0 deletions Flakkari/Engine/EntityComponentSystem/Components/2D/Movable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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); }
};

Expand Down
14 changes: 14 additions & 0 deletions Flakkari/Engine/EntityComponentSystem/Components/2D/RigidBody.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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); }
};

Expand Down
11 changes: 11 additions & 0 deletions Flakkari/Engine/EntityComponentSystem/Components/2D/Transform.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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); }
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(); }
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(); }
};

Expand Down
12 changes: 12 additions & 0 deletions Flakkari/Engine/EntityComponentSystem/Components/Common/Health.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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); }
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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); }
};

Expand Down
13 changes: 13 additions & 0 deletions Flakkari/Engine/EntityComponentSystem/Components/Common/Level.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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); }
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ struct NetworkEvent {
NetworkEvent(const NetworkEvent &other) : events(other.events){};
NetworkEvent(const std::vector<unsigned short> &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); }
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(); }
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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); }
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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); }
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(); }
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(); }
};

Expand Down
11 changes: 11 additions & 0 deletions Flakkari/Engine/EntityComponentSystem/Components/Common/Weapon.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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); };
};

Expand Down
16 changes: 0 additions & 16 deletions Flakkari/Protocol/PacketFactory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,22 +266,6 @@ class PacketFactory {
add2dToPacketByEntity<Id>(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 <typename Id>
static void addInfoToPacket(Packet<Id> &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;
Expand Down
16 changes: 0 additions & 16 deletions Flakkari/Server/Game/GameManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,22 +72,6 @@ int GameManager::addGame(const std::string &gameName)
return 0;
}

const std::shared_ptr<Game> &GameManager::getGame(const std::string &gameName)
{
if (_gamesStore.find(gameName) == _gamesStore.end())
return FLAKKARI_LOG_ERROR("game not found"), nullptr;
return std::make_shared<Game>(gameName, _gamesStore[gameName]);
}

const std::vector<std::shared_ptr<Game>> &GameManager::getGamesInstances()
{
std::vector<std::shared_ptr<Game>> 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())
Expand Down
17 changes: 0 additions & 17 deletions Flakkari/Server/Game/GameManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,23 +65,6 @@ class GameManager : public Singleton<GameManager> {
*/
int addGame(const std::string &gameName);

/**
* @brief Get the Game object
*
* @param gameName Game to get
* @return std::shared_ptr<Game> Game
*
* @deprecated Use getGameInstance instead
*/
const std::shared_ptr<Game> &getGame(const std::string &gameName);

/**
* @brief Get the Games Instances object (all games loaded)
*
* @return std::vector<std::shared_ptr<Game>> Games Instances
*/
const std::vector<std::shared_ptr<Game>> &getGamesInstances();

/**
* @brief Update a game from the GameManager
*
Expand Down

0 comments on commit 6e2c1d6

Please sign in to comment.