Skip to content

Commit

Permalink
Merge pull request #20 from MasterLaplace/54-imlement-a-game-class-in…
Browse files Browse the repository at this point in the history
…-flakkari-server

54 implement a game class in flakkari server
  • Loading branch information
MasterLaplace authored Jan 9, 2024
2 parents 1c6b9cf + 7595d02 commit 2637a08
Show file tree
Hide file tree
Showing 19 changed files with 729 additions and 42 deletions.
27 changes: 27 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@ set(SOURCES
Flakkari/Network/IOMultiplexer.cpp
Flakkari/Protocol/Header.cpp
Flakkari/Protocol/Packet.cpp
Flakkari/Engine/Math/Vector.cpp
Flakkari/Engine/EntityComponentSystem/Systems/Systems.cpp
Flakkari/Engine/EntityComponentSystem/Registry.cpp
Flakkari/Server/UDPServer.cpp
Flakkari/Server/Client/Client.cpp
Flakkari/Server/Client/ClientManager.cpp
Flakkari/Server/Game/Game.cpp
Flakkari/Server/Internals/CommandManager.cpp
)

Expand All @@ -28,12 +31,17 @@ set(HEADERS
Flakkari/Network/IOMultiplexer.hpp
Flakkari/Protocol/Header.hpp
Flakkari/Protocol/Packet.hpp
Flakkari/Engine/Math/Vector.hpp
Flakkari/Engine/EntityComponentSystem/Components/Components2D.hpp
Flakkari/Engine/EntityComponentSystem/Components/ComponentsCommon.hpp
Flakkari/Engine/EntityComponentSystem/Systems/Systems.hpp
Flakkari/Engine/EntityComponentSystem/Entity.hpp
Flakkari/Engine/EntityComponentSystem/SparseArrays.hpp
Flakkari/Engine/EntityComponentSystem/Registry.hpp
Flakkari/Server/UDPServer.hpp
Flakkari/Server/Client/Client.hpp
Flakkari/Server/Client/ClientManager.hpp
Flakkari/Server/Game/Game.hpp
Flakkari/Server/Internals/CommandManager.hpp
)

Expand All @@ -48,6 +56,17 @@ set(HEADER_LIB_NETWORK
Flakkari/Network/IOMultiplexer.hpp
)

# CMake Modules:
INCLUDE(FetchContent)

FetchContent_Declare(
nlohmann_json
GIT_REPOSITORY https://github.com/nlohmann/json.git
GIT_TAG v3.11.3
)

FetchContent_MakeAvailable(nlohmann_json)

# Separate Build Artifacts:
set(CMAKE_BINARY_DIR ${CMAKE_SOURCE_DIR}/build)
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR})
Expand All @@ -72,6 +91,9 @@ add_executable(r-type_server ${SOURCES} ${HEADERS})
# Include Directories:
target_include_directories(r-type_server PRIVATE ${CMAKE_SOURCE_DIR}/Flakkari)

# Link Libraries:
target_link_libraries(r-type_server PRIVATE nlohmann_json::nlohmann_json)

# Documentation: sudo apt-get install graphviz
find_package(Doxygen)
if(DOXYGEN_FOUND)
Expand Down Expand Up @@ -108,6 +130,7 @@ set(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_SOURCE_DIR}/LICENSE")

set(CPACK_GENERATOR "DEB")
set(CPACK_DEBIAN_PACKAGE_MAINTAINER "R-Type Team")
set(CPACK_DEBIAN_PACKAGE_DEPENDS "nlohmann-json3-dev (>= 3.9.1)" "doxygen (>= 1.8.17)" "graphviz (>= 2.40.1)")
set(CPACK_DEBIAN_PACKAGE_ARCHITECTURE "amd64")

include(CPack)
Expand All @@ -124,6 +147,10 @@ add_library(r-type_server_network SHARED ${SOURCES_LIB_NETWORK} ${HEADER_LIB_NET

# Include Directories:
target_include_directories(r-type_server_network PRIVATE ${CMAKE_SOURCE_DIR}/Flakkari)

# Link Libraries:
target_link_libraries(r-type_server_network PRIVATE nlohmann_json::nlohmann_json)

# Install dynamic library:
install(TARGETS r-type_server_network DESTINATION lib)
install(FILES ${HEADER_LIB_NETWORK} DESTINATION include/Flakkari/Network)
30 changes: 30 additions & 0 deletions Flakkari/Engine/EntityComponentSystem/Components/2D/Movable.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
** EPITECH PROJECT, 2024
** Title: Flakkari
** Author: MasterLaplace
** Created: 2023-01-06
** File description:
** Movable
*/

#ifndef MOVABLE_HPP_
#define MOVABLE_HPP_

#include "../../../Math/Vector.hpp"

namespace Flakkari::Engine::ECS::Components::_2D {

struct Movable {
Math::Vector2d velocity; // pixels / second
double angularVelocity; // degrees / second
Math::Vector2d acceleration; // pixels / second^2
double angularAcceleration; // degrees / second^2

Movable() : velocity(0, 0), acceleration(0, 0) {};
Movable(const Math::Vector2d &velocity, const Math::Vector2d &acceleration) : velocity(velocity), acceleration(acceleration) {};
Movable(const Movable &other) : velocity(other.velocity), acceleration(other.acceleration) {};
};

} // namespace Game::ECS::Components::_2D

#endif /* !MOVABLE_HPP_ */
29 changes: 29 additions & 0 deletions Flakkari/Engine/EntityComponentSystem/Components/2D/Transform.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
** EPITECH PROJECT, 2024
** Title: Flakkari
** Author: MasterLaplace
** Created: 2023-01-06
** File description:
** Transform
*/

#ifndef TRANSFORM_HPP_
#define TRANSFORM_HPP_

#include "../../../Math/Vector.hpp"

namespace Flakkari::Engine::ECS::Components::_2D {

struct Transform {
Math::Vector2d position;
Math::Vector2d scale;
double rotation;

Transform() : position(0, 0), scale(1, 1), rotation(0) {};
Transform(const Math::Vector2d &position, const Math::Vector2d &scale, double rotation) : position(position), scale(scale), rotation(rotation) {};
Transform(const Transform &other) : position(other.position), scale(other.scale), rotation(other.rotation) {};
};

} // namespace Game::ECS::Components::_2D

#endif /* !TRANSFORM_HPP_ */
33 changes: 33 additions & 0 deletions Flakkari/Engine/EntityComponentSystem/Components/Common/Child.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
** EPITECH PROJECT, 2024
** Title: Flakkari
** Author: MasterLaplace
** Created: 2023-01-06
** File description:
** Child
*/

#ifndef CHILD_HPP_
#define CHILD_HPP_

#include <string>

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

/**
* @brief Child component for ECS entities that have a child entity attached to them
*
* @details This component is used to create a child entity based on a prefab
* and attach it to the entity that has this component
*/
struct Child {
std::string name;

Child() : name("") {}
Child(const std::string &name) : name(name) {}
Child(const Child &other) : name(other.name) {}
};

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

#endif /* !CHILD_HPP_ */
32 changes: 32 additions & 0 deletions Flakkari/Engine/EntityComponentSystem/Components/Common/Parent.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
** EPITECH PROJECT, 2024
** Title: Flakkari
** Author: MasterLaplace
** Created: 2023-01-06
** File description:
** Parent
*/

#ifndef PARENT_HPP_
#define PARENT_HPP_

#include <string>

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

/**
* @brief Parent component for ECS entities that have a parent entity attached to them
*
* @details This component is used to store the parent entity of an entity in the ECS
*/
struct Parent {
std::size_t entity;

Parent() : entity(0) {}
Parent(const std::size_t &entity) : entity(entity) {}
Parent(const Parent &other) : entity(other.entity) {}
};

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

#endif /* !PARENT_HPP_ */
32 changes: 32 additions & 0 deletions Flakkari/Engine/EntityComponentSystem/Components/Common/Tag.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
** EPITECH PROJECT, 2024
** Title: Flakkari
** Author: MasterLaplace
** Created: 2023-01-06
** File description:
** Tag
*/

#ifndef TAG_HPP_
#define TAG_HPP_

#include <string>

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

/**
* @brief Tag 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 Tag {
std::string tag;

Tag() : tag("") {}
Tag(const std::string &tag) : tag(tag) {}
Tag(const Tag &other) : tag(other.tag) {}
};

} // namespace Game::ECS::Components::Common

#endif /* !TAG_HPP_ */
22 changes: 22 additions & 0 deletions Flakkari/Engine/EntityComponentSystem/Components/Components2D.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**************************************************************************
* Flakkari Engine Library v0.1.0
*
* Flakkari Library is a C++ Library for Network.
* @file Components2D.hpp
* @brief Components2D header. Contains all 2D components.
* (Collider, Movable, RigidBody, Transform)
*
* Flakkari Library is under MIT License.
* https://opensource.org/licenses/MIT
* © 2023 @MasterLaplace
* @version 0.1.0
* @date 2023-01-06
**************************************************************************/

#ifndef COMPONENTS2D_HPP_
#define COMPONENTS2D_HPP_

#include "2D/Movable.hpp" // Movable component (velocity, angularVelocity, acceleration, angularAcceleration)
#include "2D/Transform.hpp" // Transform component (position, rotation, scale)

#endif /* !COMPONENTS2D_HPP_ */
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**************************************************************************
* Flakkari Engine Library v0.1.0
*
* Flakkari Library is a C++ Library for Network.
* @file ComponentsCommon.hpp
* @brief ComponentsCommon header. Contains all common components.
* (Parent, Tag)
*
* Flakkari Library is under MIT License.
* https://opensource.org/licenses/MIT
* © 2023 @MasterLaplace
* @version 0.1.0
* @date 2023-01-06
**************************************************************************/

#ifndef COMPONENTSCOMMON_HPP_
#define COMPONENTSCOMMON_HPP_

#include "Common/Parent.hpp" // Parent component (entity)
#include "Common/Tag.hpp" // Tag component (tag)

#endif /* !COMPONENTSCOMMON_HPP_ */
34 changes: 34 additions & 0 deletions Flakkari/Engine/EntityComponentSystem/Systems/Systems.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
** EPITECH PROJECT, 2024
** Title: Flakkari
** Author: MasterLaplace
** Created: 2023-01-06
** File description:
** Systems
*/

#include "Systems.hpp"

namespace Flakkari::Engine::ECS::Systems {

void position(Registry &r, float deltaTime)
{
if (!r.isRegistered<ECS::Components::_2D::Transform>() || !r.isRegistered<ECS::Components::_2D::Movable>())
return;
auto& positions = r.getComponents<ECS::Components::_2D::Transform>();
auto& velocities = r.getComponents<ECS::Components::_2D::Movable>();

for (Entity i(0); i < positions.size() && i < velocities.size(); ++i) {
auto& pos = positions[i];
auto& vel = velocities[i];

if (pos.has_value() && vel.has_value()) {
pos->position.x += vel->velocity.dx * deltaTime * 0.5f * vel->acceleration.x * deltaTime * deltaTime;
pos->position.y += vel->velocity.dy * deltaTime * 0.5f * vel->acceleration.y * deltaTime * deltaTime;
vel->velocity.dx += vel->acceleration.x * deltaTime;
vel->velocity.dy += vel->acceleration.y * deltaTime;
}
}
}

} // namespace Flakkari::Engine::ECS::Systems
40 changes: 40 additions & 0 deletions Flakkari/Engine/EntityComponentSystem/Systems/Systems.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**************************************************************************
* Flakkari Engine Library v0.1.0
*
* Flakkari Library is a C++ Library for Network.
* @file Systems.hpp
* @brief Systems header. Contains all systems.
* (position, rigid_body)
*
* Flakkari Library is under MIT License.
* https://opensource.org/licenses/MIT
* © 2023 @MasterLaplace
* @version 0.1.0
* @date 2023-01-06
**************************************************************************/

#ifndef SYSTEMS_HPP_
#define SYSTEMS_HPP_

#include "../Registry.hpp"
#include "../Components/Components2D.hpp"
#include "../Components/ComponentsCommon.hpp"

#include <cmath>
#include <chrono>

namespace Flakkari::Engine::ECS::Systems {

/**
* @brief Updates the position of all entities with a Position and a Movable component based on their velocity.
*
* @note The velocity vector is normalized following this advice: https://youtube.com/shorts/0cYjreg7dpg
*
* @param r The registry containing the entities to update.
* @param deltaTime The time elapsed since the last update.
*/
void position(Registry &r, float deltaTime);

} // namespace Flakkari::Engine::ECS::Systems

#endif /* !SYSTEMS_HPP_ */
1 change: 1 addition & 0 deletions Flakkari/Network/IOMultiplexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ void PSELECT::removeSocket(FileDescriptor socket)
throw std::runtime_error("Index out of range");
FD_CLR(socket, &_fds);
_sockets.erase(std::remove(_sockets.begin(), _sockets.end(), socket), _sockets.end());
_maxFd = *std::max_element(_sockets.begin(), _sockets.end());
}

int PSELECT::wait()
Expand Down
Loading

0 comments on commit 2637a08

Please sign in to comment.