Skip to content

Commit

Permalink
Merge pull request #52 from MasterLaplace/47-feature-implement-flakka…
Browse files Browse the repository at this point in the history
…ri-protocol-v1

Implement flakkari protocol v1
  • Loading branch information
MasterLaplace authored Nov 22, 2024
2 parents c215f0f + c16f3fa commit c3755d0
Show file tree
Hide file tree
Showing 30 changed files with 1,378 additions and 924 deletions.
7 changes: 2 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ set(SOURCES

Flakkari/Engine/EntityComponentSystem/Systems/Systems.cpp
Flakkari/Engine/EntityComponentSystem/Registry.cpp
Flakkari/Engine/EntityComponentSystem/Factory.cpp

Flakkari/Server/UDPServer.cpp
Flakkari/Server/Client/Client.cpp
Expand Down Expand Up @@ -58,7 +59,7 @@ set(HEADERS
Flakkari/Engine/EntityComponentSystem/Entity.hpp
Flakkari/Engine/EntityComponentSystem/SparseArrays.hpp
Flakkari/Engine/EntityComponentSystem/Registry.hpp
Flakkari/Engine/EntityComponentSystem/EntityFactory.hpp
Flakkari/Engine/EntityComponentSystem/Factory.hpp

Flakkari/Server/UDPServer.hpp

Expand Down Expand Up @@ -269,10 +270,6 @@ add_library(flakkari_network SHARED ${SOURCES_LIB_NETWORK} ${HEADER_LIB_NETWORK}

# Include Directories:
target_include_directories(flakkari_network PRIVATE ${CMAKE_SOURCE_DIR}/Flakkari)
target_include_directories(flakkari_network PRIVATE ${singleton_SOURCE_DIR})

# Link Libraries:
target_link_libraries(flakkari_network PRIVATE nlohmann_json::nlohmann_json)

# Install dynamic library:
install(TARGETS flakkari_network DESTINATION lib)
Expand Down
10 changes: 10 additions & 0 deletions Flakkari/Engine/EntityComponentSystem/Components/3D/Control.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ LPL_PACKED_START
* _look_left: look left (give access to the look left)
* _look_right: look right (give access to the look right)
* _shoot: shoot (give access to the shoot)
* _padding: padding to align the size of the struct
*/
struct Control {
bool _move_up = false;
Expand All @@ -43,6 +44,7 @@ struct Control {
bool _look_left = false;
bool _look_right = false;
bool _shoot = false;
uint8_t _padding : 5 = 0;

Control() = default;
Control(bool m_up, bool m_down, bool m_left, bool m_right, bool m_front, bool m_back, bool l_up, bool l_down,
Expand Down Expand Up @@ -75,6 +77,14 @@ struct Control {
return *this;
}

unsigned short toSerialized()
{
return static_cast<unsigned short>((_move_up << 0) | (_move_down << 1) | (_move_left << 2) |
(_move_right << 3) | (_move_front << 4) | (_move_back << 5) |
(_look_up << 6) | (_look_down << 7) | (_look_left << 8) |
(_look_right << 9) | (_shoot << 10));
}

std::size_t size() const { return sizeof(*this); }
};

Expand Down
14 changes: 10 additions & 4 deletions Flakkari/Engine/EntityComponentSystem/Components/3D/Movable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,24 @@ LPL_PACKED_START
struct Movable {
Math::Vector3f _velocity;
Math::Vector3f _acceleration;
float _minSpeed;
float _maxSpeed;

Movable() : _velocity(0, 0), _acceleration(0, 0) {}
Movable(const Math::Vector3f &velocity, const Math::Vector3f &acceleration)
: _velocity(velocity), _acceleration(acceleration){};
Movable(const Movable &other) : _velocity(other._velocity), _acceleration(other._acceleration){};
Movable() : _velocity(0, 0), _acceleration(0, 0), _minSpeed(0), _maxSpeed(0){};
Movable(const Math::Vector3f &velocity, const Math::Vector3f &acceleration, float minSpeed, float maxSpeed)
: _velocity(velocity), _acceleration(acceleration), _minSpeed(minSpeed), _maxSpeed(maxSpeed){};
Movable(const Movable &other)
: _velocity(other._velocity), _acceleration(other._acceleration), _minSpeed(other._minSpeed),
_maxSpeed(other._maxSpeed){};

Movable &operator=(const Movable &other)
{
if (this != &other)
{
_velocity = other._velocity;
_acceleration = other._acceleration;
_minSpeed = other._minSpeed;
_maxSpeed = other._maxSpeed;
}

return *this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ struct RigidBody {
float _angularDrag;
bool _useGravity = true;
bool _isKinematic = false;
uint8_t padding : 6 = 0;

RigidBody() : _mass(0), _drag(0), _angularDrag(0), _useGravity(false), _isKinematic(false){};
RigidBody(const RigidBody &other)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ LPL_PACKED_START
struct Transform {
Math::Vector3f _position;
Math::Vector3f _scale;
Math::Vector3f _rotation;
Math::Quaternion _rotation;

Transform() : _position(0, 0, 0), _scale(1, 1, 1), _rotation(0, 0, 0){};
Transform(const Math::Vector2f &position, const Math::Vector2f &scale, const Math::Vector3f &rotation)
Transform(const Math::Vector3f &position, const Math::Vector3f &scale, const Math::Quaternion &rotation)
: _position(position), _scale(scale), _rotation(rotation){};
Transform(const Transform &other) : _position(other._position), _scale(other._scale), _rotation(other._rotation){};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,26 @@
#ifndef FLAKKARI_TEMPLATE_HPP_
#define FLAKKARI_TEMPLATE_HPP_

#include <nlohmann/json.hpp>
#include <string>

namespace Flakkari::Engine::ECS::Components::Common {

struct Template {
std::string name;
nlohmann::json content;

Template() : name("") {}
Template(const std::string &nname) : name(nname) {}
Template(const Template &other) : name(other.name) {}
Template() : name(""), content(nlohmann::json::object()) {}
Template(const std::string &name, const nlohmann::json &content) : name(name), content(content) {}
Template(const Template &other) : name(other.name), content(other.content) {}

Template &operator=(const Template &other)
{
if (this != &other)
{
name = other.name;
content = other.content;
}

return *this;
}
Expand Down
52 changes: 52 additions & 0 deletions Flakkari/Engine/EntityComponentSystem/Components/Common/Timer.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
** EPITECH PROJECT, 2024
** Title: Flakkari
** Author: MasterLaplace
** Created: 2024-11-17
** File description:
** Timer
*/

#ifndef Timer_HPP_
#define Timer_HPP_

#include <chrono>
#include <cstring>
#include <string>

#include "config.h.in"

namespace Flakkari::Engine::ECS::Components::Common {
LPL_PACKED_START

/**
* @brief Timer component for ECS entities that have a timer attached to them
*
* @details This component is used to store the current time and the maximum time
*/
struct Timer {
std::chrono::steady_clock::time_point lastTime = std::chrono::steady_clock::now();
float maxTime;

Timer() : maxTime(0) {}
Timer(bool spawed) : maxTime(spawed) {}
Timer(const Timer &other) : maxTime(other.maxTime) {}

Timer &operator=(const Timer &other)
{
if (this != &other)
{
lastTime = other.lastTime;
maxTime = other.maxTime;
}

return *this;
}

std::size_t size() const { return sizeof(*this); }
};

LPL_PACKED_END
} // namespace Flakkari::Engine::ECS::Components::Common

#endif /* !Timer_HPP_ */
26 changes: 18 additions & 8 deletions Flakkari/Engine/EntityComponentSystem/Components/Common/Weapon.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,28 +26,38 @@ LPL_PACKED_START
* It is used by the WeaponComponent to handle the firing and reloading logic.
*
* @param
* damage: The amount of damage dealt by the weapon.
* minDamage: The amount of damage dealt by the weapon.
* maxDamage: The maximum amount of damage dealt by the weapon.
* chargeTime: The time it takes to shoot a charged bullet.
* chargeMaxTime: The maximum time it takes to charge a bullet at max power.
* isCharging: A flag that indicates if the weapon is charging.
* 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 {
std::size_t damage;
std::size_t minDamage;
std::size_t maxDamage;
float chargeTime = 0;
float chargeMaxTime;
bool isCharging = false;
float timeSinceLastShot = 0;
float fireRate;
std::size_t level;

Weapon() = default;
Weapon(const Weapon &other) = default;
Weapon(std::size_t dmg, float rate, std::size_t lvl) : damage(dmg), fireRate(rate), level(lvl){};
Weapon(std::size_t min_dmg, std::size_t max_dmg, float chargeMax, float rate, std::size_t lvl)
: minDamage(min_dmg), maxDamage(max_dmg), chargeMaxTime(chargeMax), fireRate(rate), level(lvl){};

Weapon &operator=(const Weapon &other)
{
if (this != &other)
{
damage = other.damage;
minDamage = other.minDamage;
maxDamage = other.maxDamage;
chargeTime = other.chargeTime;
chargeMaxTime = other.chargeMaxTime;
isCharging = other.isCharging;
fireRate = other.fireRate;
level = other.level;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
#define FLAKKARI_COMPONENTS3D_HPP_

#include "3D/BoxCollider.hpp" // Collider component (center, size)
#include "3D/Control.hpp" // Control component (up, down, left, right, shoot)
#include "3D/Movable.hpp" // Movable component (velocity, acceleration)
#include "3D/Control.hpp" // Control component (move_[up, down, left, right, ...], look_[*] shoot)
#include "3D/Movable.hpp" // Movable component (velocity, acceleration, minSpeed, maxSpeed)
#include "3D/RigidBody.hpp" // RigidBody component (mass, drag, angularDrag, useGravity, isKinematic)
#include "3D/SphereCollider.hpp" // Collider component (center, radius)
#include "3D/Transform.hpp" // Transform component (position, rotation, scale)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "Common/Spawned.hpp" // Spawned component (spawned)
#include "Common/Tag.hpp" // Tag component (tag)
#include "Common/Template.hpp" // Template component (name)
#include "Common/Timer.hpp" // Timer component (time)
#include "Common/Weapon.hpp" // Weapon component (name)

#include "Common/NetworkEvent.hpp" // NetworkEvent component (event)
Expand Down
15 changes: 15 additions & 0 deletions Flakkari/Engine/EntityComponentSystem/Entity.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,25 @@ class Entity {
return *this;
}

bool operator==(const Entity &other) const { return _id == other._id; }

std::size_t getId() const { return _id; }

private:
std::size_t _id;
};

} // namespace Flakkari::Engine::ECS

#include <unordered_map>

namespace std {
template <> struct hash<Flakkari::Engine::ECS::Entity> {
size_t operator()(const Flakkari::Engine::ECS::Entity &entity) const noexcept
{
return std::hash<std::size_t>()(entity.getId());
}
};
} // namespace std

#endif /* !FLAKKARI_ENTITY_HPP_ */
Loading

0 comments on commit c3755d0

Please sign in to comment.