From d045fe3af01390d1f8c81da27af967c10cb5a99a Mon Sep 17 00:00:00 2001 From: MasterLaplace Date: Sun, 14 Jan 2024 17:30:31 +0100 Subject: [PATCH] feat(Components): Add new components: NetworkEvent, Collider, and Spawned --- .../Components/2D/Collider.hpp | 41 +++++++++++++ .../Components/2D/RigidBody.hpp | 44 ++++++++++++++ .../Components/Common/Health.hpp | 43 ++++++++++++++ .../Components/Common/Level.hpp | 51 ++++++++++++++++ .../Components/Common/NetworkEvent.hpp | 31 ++++++++++ .../Components/Common/Spawned.hpp | 41 +++++++++++++ .../Components/Common/Weapon.hpp | 55 +++++++++++++++++ .../Components/Components2D.hpp | 5 +- .../Components/ComponentsCommon.hpp | 9 ++- .../EntityComponentSystem/EntityFactory.hpp | 59 +++++++++++++++++++ 10 files changed, 376 insertions(+), 3 deletions(-) create mode 100644 Flakkari/Engine/EntityComponentSystem/Components/2D/Collider.hpp create mode 100644 Flakkari/Engine/EntityComponentSystem/Components/2D/RigidBody.hpp create mode 100644 Flakkari/Engine/EntityComponentSystem/Components/Common/Health.hpp create mode 100644 Flakkari/Engine/EntityComponentSystem/Components/Common/Level.hpp create mode 100644 Flakkari/Engine/EntityComponentSystem/Components/Common/NetworkEvent.hpp create mode 100644 Flakkari/Engine/EntityComponentSystem/Components/Common/Spawned.hpp create mode 100644 Flakkari/Engine/EntityComponentSystem/Components/Common/Weapon.hpp diff --git a/Flakkari/Engine/EntityComponentSystem/Components/2D/Collider.hpp b/Flakkari/Engine/EntityComponentSystem/Components/2D/Collider.hpp new file mode 100644 index 00000000..1910b406 --- /dev/null +++ b/Flakkari/Engine/EntityComponentSystem/Components/2D/Collider.hpp @@ -0,0 +1,41 @@ +/* +** EPITECH PROJECT, 2024 +** Title: Flakkari +** Author: MasterLaplace +** Created: 2023-01-14 +** File description: +** Collider +*/ + +#ifndef COLLIDER_HPP_ +#define COLLIDER_HPP_ + +#include +#include "../../../Math/Vector.hpp" + +#include "Network/Packed.hpp" + +namespace Flakkari::Engine::ECS::Components::_2D { +PACKED_START + +/** + * @brief Collider component for ECS entities that have a script attached to them + * + * @details This component is used to store the path to the script that will be executed + */ +struct Collider { + Math::Vector2f _size; + + Collider() : _size() {} + Collider(Math::Vector2f nsize) : _size(nsize) {} + Collider(const Collider &other) : _size(other._size) {} + + std::size_t size() const { + return sizeof(_size); + } +}; + +PACKED_END +} // namespace Flakkari::Engine::ECS::Components::_2D + +#endif /* !COLLIDER_HPP_ */ diff --git a/Flakkari/Engine/EntityComponentSystem/Components/2D/RigidBody.hpp b/Flakkari/Engine/EntityComponentSystem/Components/2D/RigidBody.hpp new file mode 100644 index 00000000..c050db8b --- /dev/null +++ b/Flakkari/Engine/EntityComponentSystem/Components/2D/RigidBody.hpp @@ -0,0 +1,44 @@ +/* +** EPITECH PROJECT, 2024 +** Title: Flakkari +** Author: MasterLaplace +** Created: 2023-01-14 +** File description: +** RigidBody +*/ + +#ifndef RIGIDBODY_HPP_ +#define RIGIDBODY_HPP_ + +#include "../../../Math/Vector.hpp" + +#include "Network/Packed.hpp" + +namespace Flakkari::Engine::ECS::Components::_2D { +PACKED_START + +/** + * @brief RigidBody represent the physical properties of a rigid body in a game engine + * + */ +struct RigidBody { + float mass; + float restitution; + float friction; + float gravityScale; + bool isGravityAffected = true; + bool isKinematic = false; + + RigidBody() : mass(0), restitution(0), friction(0), gravityScale(0), isGravityAffected(false), isKinematic(false) {}; + RigidBody(const RigidBody &other) : mass(other.mass), restitution(other.restitution), friction(other.friction), gravityScale(other.gravityScale), isGravityAffected(other.isGravityAffected), isKinematic(other.isKinematic) {}; + RigidBody(float mass, float restitution, float friction, float gravityScale) : mass(mass), restitution(restitution), friction(friction), gravityScale(gravityScale), isGravityAffected(true), isKinematic(false) {}; + + std::size_t size() const { + return sizeof(*this); + } +}; + +PACKED_END +} // namespace Flakkari::Engine::ECS::Components::_2D + +#endif /* !RIGIDBODY_HPP_ */ diff --git a/Flakkari/Engine/EntityComponentSystem/Components/Common/Health.hpp b/Flakkari/Engine/EntityComponentSystem/Components/Common/Health.hpp new file mode 100644 index 00000000..148f69a9 --- /dev/null +++ b/Flakkari/Engine/EntityComponentSystem/Components/Common/Health.hpp @@ -0,0 +1,43 @@ +/* +** EPITECH PROJECT, 2024 +** Title: Flakkari +** Author: MasterLaplace +** Created: 2023-01-14 +** File description: +** Health +*/ + +#ifndef Health_HPP_ +#define Health_HPP_ + +#include + +#include "Network/Packed.hpp" + +namespace Flakkari::Engine::ECS::Components::Common { +PACKED_START + +/** + * @brief Health is a structure that represents the life of an "living object" + * + */ +struct Health { + unsigned int currentHealth; + unsigned int maxHealth = 100; + unsigned int shield = 0; + unsigned int maxShield = 100; + + Health() : currentHealth(100), maxHealth(100), shield(0), maxShield(100) {}; + Health(unsigned int currentHealth, unsigned int maxHealth, unsigned int shield, unsigned int maxShield) : + currentHealth(currentHealth), maxHealth(maxHealth), shield(shield), maxShield(maxShield) {}; + Health(const Health &other) : currentHealth(other.currentHealth), maxHealth(other.maxHealth), shield(other.shield), maxShield(other.maxShield) {}; + + unsigned int size() const { + return sizeof(*this); + } +}; + +PACKED_END +} // namespace Flakkari::Engine::ECS::Components::Common + +#endif /* !Health_HPP_ */ diff --git a/Flakkari/Engine/EntityComponentSystem/Components/Common/Level.hpp b/Flakkari/Engine/EntityComponentSystem/Components/Common/Level.hpp new file mode 100644 index 00000000..150feae1 --- /dev/null +++ b/Flakkari/Engine/EntityComponentSystem/Components/Common/Level.hpp @@ -0,0 +1,51 @@ +/* +** EPITECH PROJECT, 2024 +** Title: Flakkari +** Author: MasterLaplace +** Created: 2023-01-14 +** File description: +** Level +*/ + +#ifndef LEVEL_HPP +#define LEVEL_HPP + +#include +#include + +#include "Network/Packed.hpp" + +namespace Flakkari::Engine::ECS::Components::Common { +PACKED_START + +/** + * @brief FontInfo is a structure that holds information about a Font + * + */ +struct Level { + unsigned int level; + const char *currentWeapon; + unsigned int currentExp; + unsigned int requiredExp; + + Level() : level(1), currentWeapon(""), currentExp(0), requiredExp(100) {} + Level(unsigned int level, std::string currentWeapon, unsigned int currentExp, unsigned int requiredExp) + : level(level), + currentWeapon(currentWeapon.c_str()), + currentExp(currentExp), + requiredExp(requiredExp) {} + Level(const Level &other) + : level(other.level), + currentWeapon(other.currentWeapon), + currentExp(other.currentExp), + requiredExp(other.requiredExp) {} + + std::size_t size() const { + return sizeof(level) + std::strlen(currentWeapon) + sizeof(currentExp) + sizeof(requiredExp); + } +}; + +PACKED_END +} // namespace Flakkari::Engine::ECS::Components::Common + +#endif //LEVEL_HPP diff --git a/Flakkari/Engine/EntityComponentSystem/Components/Common/NetworkEvent.hpp b/Flakkari/Engine/EntityComponentSystem/Components/Common/NetworkEvent.hpp new file mode 100644 index 00000000..2a19a5f7 --- /dev/null +++ b/Flakkari/Engine/EntityComponentSystem/Components/Common/NetworkEvent.hpp @@ -0,0 +1,31 @@ +/* +** EPITECH PROJECT, 2024 +** Title: Flakkari +** Author: MasterLaplace +** Created: 2023-01-14 +** File description: +** NetworkEvent +*/ + +#ifndef NETWORKEVENT_HPP_ +#define NETWORKEVENT_HPP_ + +#include + +namespace Flakkari::Engine::ECS::Components::Common { + + struct NetworkEvent { + std::vector events; + + NetworkEvent() = default; + NetworkEvent(const NetworkEvent &other) : events(other.events) {}; + NetworkEvent(const std::vector &events) : events(events) {}; + + std::size_t size() const { + return events.size() * sizeof(unsigned short); + } + }; + +} /* namespace Flakkari::Engine::ECS::Components::Common */ + +#endif /* !NETWORKEVENT_HPP_ */ diff --git a/Flakkari/Engine/EntityComponentSystem/Components/Common/Spawned.hpp b/Flakkari/Engine/EntityComponentSystem/Components/Common/Spawned.hpp new file mode 100644 index 00000000..3fc4fdf9 --- /dev/null +++ b/Flakkari/Engine/EntityComponentSystem/Components/Common/Spawned.hpp @@ -0,0 +1,41 @@ +/* +** EPITECH PROJECT, 2024 +** Title: Flakkari +** Author: MasterLaplace +** Created: 2023-01-14 +** File description: +** Spawned +*/ + +#ifndef SPAWNED_HPP_ +#define SPAWNED_HPP_ + +#include +#include + +#include "Network/Packed.hpp" + +namespace Flakkari::Engine::ECS::Components::Common { +PACKED_START + +/** + * @brief Spawned component for ECS entities that have a script attached to them + * + * @details This component is used to store the path to the script that will be executed + */ +struct Spawned { + bool has_spawned; + + Spawned() : has_spawned(false) {} + Spawned(bool spawed) : has_spawned(spawed) {} + Spawned(const Spawned &other) : has_spawned(other.has_spawned) {} + + std::size_t size() const { + return sizeof(has_spawned); + } +}; + +PACKED_END +} // namespace Flakkari::Engine::ECS::Components::Common + +#endif /* !SPAWNED_HPP_ */ \ No newline at end of file diff --git a/Flakkari/Engine/EntityComponentSystem/Components/Common/Weapon.hpp b/Flakkari/Engine/EntityComponentSystem/Components/Common/Weapon.hpp new file mode 100644 index 00000000..ffef650e --- /dev/null +++ b/Flakkari/Engine/EntityComponentSystem/Components/Common/Weapon.hpp @@ -0,0 +1,55 @@ +/* +** EPITECH PROJECT, 2024 +** Title: Flakkari +** Author: MasterLaplace +** Created: 2023-01-14 +** File description: +** Weapon +*/ + +#ifndef WEAPON_HPP_ +#define WEAPON_HPP_ + +#include +#include + +#include "Network/Packed.hpp" + +namespace Flakkari::Engine::ECS::Components::Common { +PACKED_START + +/** + * @brief Weapon is a structure that defines the characteristics of a weapon. + * + * @details + * The Weapon structure is used to define the characteristics of a weapon. + * It is used by the WeaponComponent to handle the firing and reloading logic. + * + * @param + * damage: The amount of damage dealt by the weapon. + * fireRate: The rate of fire, shots per second. + * currentAmmo: Current ammunition in the magazine. + * reloadTime: Time it takes to reload the weapon in seconds. + * timeSinceLastShot: Time elapsed since the last shot was fired. + * isReloading: Is the weapon currently reloading. + * maxAmmo: Maximum ammunition the weapon can hold. + */ +struct Weapon { + unsigned int damage; + float fireRate; + unsigned int level; + + Weapon() = default; + Weapon(const Weapon &other) = default; + Weapon(unsigned int dmg, float rate, unsigned int lvl) + : damage(dmg), fireRate(rate), level(lvl){}; + + unsigned int size() const { + return sizeof(*this); + }; +}; + +PACKED_END +} // namespace Flakkari::Engine::ECS::Components::Common + +#endif /* !WEAPON_HPP_ */ diff --git a/Flakkari/Engine/EntityComponentSystem/Components/Components2D.hpp b/Flakkari/Engine/EntityComponentSystem/Components/Components2D.hpp index aac18bd8..499002c3 100644 --- a/Flakkari/Engine/EntityComponentSystem/Components/Components2D.hpp +++ b/Flakkari/Engine/EntityComponentSystem/Components/Components2D.hpp @@ -4,7 +4,7 @@ * Flakkari Library is a C++ Library for Network. * @file Components2D.hpp * @brief Components2D header. Contains all 2D components. - * (Collider, Movable, RigidBody, Transform) + * (Collider, Control, Movable, RigidBody, Transform) * * Flakkari Library is under MIT License. * https://opensource.org/licenses/MIT @@ -16,9 +16,10 @@ #ifndef FLAKKARI_COMPONENTS2D_HPP_ #define FLAKKARI_COMPONENTS2D_HPP_ -// #include "2D/Collider.hpp" // Collider component (shape, position, rotation, scale) +#include "2D/Collider.hpp" // Collider component (size) #include "2D/Control.hpp" // Control component (up, down, left, right, shoot) #include "2D/Movable.hpp" // Movable component (velocity, angularVelocity, acceleration, angularAcceleration) +#include "2D/RigidBody.hpp" // RigidBody component (mass, inertia, restitution, friction) #include "2D/Transform.hpp" // Transform component (position, rotation, scale) #endif /* !FLAKKARI_COMPONENTS2D_HPP_ */ diff --git a/Flakkari/Engine/EntityComponentSystem/Components/ComponentsCommon.hpp b/Flakkari/Engine/EntityComponentSystem/Components/ComponentsCommon.hpp index c31659b8..b1f8d809 100644 --- a/Flakkari/Engine/EntityComponentSystem/Components/ComponentsCommon.hpp +++ b/Flakkari/Engine/EntityComponentSystem/Components/ComponentsCommon.hpp @@ -4,7 +4,8 @@ * Flakkari Library is a C++ Library for Network. * @file ComponentsCommon.hpp * @brief ComponentsCommon header. Contains all common components. - * (Parent, Tag) + * (Parent, Tag, Id, Template, Child, Evolve, Health, Level, + * Script, Spawned, Weapon) * * Flakkari Library is under MIT License. * https://opensource.org/licenses/MIT @@ -18,9 +19,15 @@ #include "Common/Child.hpp" // Child component (name) #include "Common/Evolve.hpp" // Evolve component (name) +#include "Common/Health.hpp" // Health component (health) #include "Common/Id.hpp" // Id component (id) +#include "Common/Level.hpp" // Level component (level) #include "Common/Parent.hpp" // Parent component (entity) +#include "Common/Spawned.hpp" // Spawned component (spawned) #include "Common/Tag.hpp" // Tag component (tag) #include "Common/Template.hpp" // Template component (name) +#include "Common/Weapon.hpp" // Weapon component (name) + +#include "Common/NetworkEvent.hpp" // NetworkEvent component (event) #endif /* !FLAKKARI_COMPONENTSCOMMON_HPP_ */ diff --git a/Flakkari/Engine/EntityComponentSystem/EntityFactory.hpp b/Flakkari/Engine/EntityComponentSystem/EntityFactory.hpp index 5f2ddaa0..25a05bb5 100644 --- a/Flakkari/Engine/EntityComponentSystem/EntityFactory.hpp +++ b/Flakkari/Engine/EntityComponentSystem/EntityFactory.hpp @@ -77,6 +77,14 @@ class EntityFactory { //*_ 2D Components _*// + if (componentName == "Collider") { + registry.registerComponent(); + Engine::ECS::Components::_2D::Collider collider; + collider._size = Engine::Math::Vector2f(componentContent["size"]["x"], componentContent["size"]["y"]); + registry.add_component(entity, std::move(collider)); + continue; + } + if (componentName == "Control") { registry.registerComponent(); Engine::ECS::Components::_2D::Control control; @@ -98,6 +106,19 @@ class EntityFactory { continue; } + if (componentName == "RigidBody") { + registry.registerComponent(); + Engine::ECS::Components::_2D::RigidBody rigidBody; + rigidBody.mass = componentContent["mass"]; + rigidBody.restitution = componentContent["restitution"]; + rigidBody.friction = componentContent["friction"]; + rigidBody.gravityScale = componentContent["gravityScale"]; + rigidBody.isGravityAffected = componentContent["isGravityAffected"]; + rigidBody.isKinematic = componentContent["isKinematic"]; + registry.add_component(entity, std::move(rigidBody)); + continue; + } + if (componentName == "Transform") { registry.registerComponent(); Engine::ECS::Components::_2D::Transform transform; @@ -124,6 +145,17 @@ class EntityFactory { continue; } + if (componentName == "Health") { + registry.registerComponent(); + Engine::ECS::Components::Common::Health health; + health.maxHealth = componentContent["maxHealth"]; + health.currentHealth = componentContent["currentHealth"]; + health.maxShield = componentContent["maxShield"]; + health.shield = componentContent["shield"]; + registry.add_component(entity, std::move(health)); + continue; + } + if (componentName == "Parent") { registry.registerComponent(); Engine::ECS::Components::Common::Parent parent(componentContent["entity"]); @@ -131,6 +163,23 @@ class EntityFactory { continue; } + if (componentName == "Level") { + registry.registerComponent(); + Engine::ECS::Components::Common::Level level; + level.level = componentContent["level"]; + level.currentExp = componentContent["currentExp"]; + level.requiredExp = componentContent["requiredExp"]; + level.currentWeapon = componentContent["currentWeapon"].get().c_str(); + registry.add_component(entity, std::move(level)); + continue; + } + + if (componentName == "Spawned") { + registry.registerComponent(); + Engine::ECS::Components::Common::Spawned spawned(componentContent["has_spawned"]); + registry.add_component(entity, std::move(spawned)); + continue; + } if (componentName == "Tag") { registry.registerComponent(); @@ -145,6 +194,16 @@ class EntityFactory { registry.add_component(entity, std::move(template_)); continue; } + + if (componentName == "Weapon") { + registry.registerComponent(); + Engine::ECS::Components::Common::Weapon weapon; + weapon.damage = componentContent["damage"]; + weapon.fireRate = componentContent["fireRate"]; + weapon.level = componentContent["level"]; + registry.add_component(entity, std::move(weapon)); + continue; + } } } };