Skip to content

Commit

Permalink
Feat(ShootSystem): Send packet create shoot
Browse files Browse the repository at this point in the history
I've send the packet from the system who create the shoot to communicate with other client
  • Loading branch information
AxelF44 committed Oct 9, 2024
1 parent 0326980 commit b9efcf0
Show file tree
Hide file tree
Showing 9 changed files with 179 additions and 15 deletions.
16 changes: 13 additions & 3 deletions Engine/Client/Src/Network/NetworkClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,22 @@ void Network::Client::_startReceive(ECS::Registry& reg)
if (this->_messageHandler)
this->_messageHandler(message, reg);
} catch (const std::exception& e) {
_startReceive(reg);
std::cerr << "Exception in message handler: " << e.what() << std::endl;
}
} else {
} else if (error) {
std::cerr << "Error receiving message: " << error.message() << std::endl;
if (_udp_socket) {
asio::error_code ec;
_udp_socket->cancel(ec);
_udp_socket->close(ec);
return;
}
}
_startReceive(reg);

asio::post(_udp_socket->get_executor(),
[this, &reg]() {
_startReceive(reg);
});
});
}

Expand Down
12 changes: 6 additions & 6 deletions Engine/Server/Src/GameEngine/Application/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ void GameEngine::Application::_handleArrowKey(uint8_t keyCode, int idxPlayerPack

bool status = false;
if (arrowKeyMap.find(keyCode) != arrowKeyMap.end())
_sceneManager->processInput(arrowKeyMap[keyCode], idxPlayerPacket);
if (status == true) {
status = _sceneManager->processInput(arrowKeyMap[keyCode], idxPlayerPacket);
if ((status == true) && (this->_registries->messageType != 0) && (this->_registries->payload.size() != 0)) {
std::vector<uint8_t> packet = this->_server->createPacket(this->_registries->messageType, this->_registries->payload);
for (auto it = this->_server->getClientsList().begin(); it != this->_server->getClientsList().end(); ++it) {
asio::ip::udp::endpoint& endpoint = it->second;
Expand All @@ -44,7 +44,7 @@ void GameEngine::Application::_handleAlphaKey(uint8_t keyCode, int idxPlayerPack
bool status = false;
if (alphaKeyMap.find(keyCode) != alphaKeyMap.end())
status = _sceneManager->processInput(alphaKeyMap[keyCode], idxPlayerPacket);
if (status == true) {
if ((status == true) && (this->_registries->messageType != 0) && (this->_registries->payload.size() != 0)) {
std::vector<uint8_t> packet = this->_server->createPacket(this->_registries->messageType, this->_registries->payload);
for (auto it = this->_server->getClientsList().begin(); it != this->_server->getClientsList().end(); ++it) {
asio::ip::udp::endpoint& endpoint = it->second;
Expand All @@ -64,7 +64,7 @@ void GameEngine::Application::_handleNumberKey(uint8_t keyCode, int idxPlayerPac
bool status = false;
if (numberKeyMap.find(keyCode) != numberKeyMap.end())
status = _sceneManager->processInput(numberKeyMap[keyCode], idxPlayerPacket);
if (status == true) {
if ((status == true) && (this->_registries->messageType != 0) && (this->_registries->payload.size() != 0)) {
std::vector<uint8_t> packet = this->_server->createPacket(this->_registries->messageType, this->_registries->payload);
for (auto it = this->_server->getClientsList().begin(); it != this->_server->getClientsList().end(); ++it) {
asio::ip::udp::endpoint& endpoint = it->second;
Expand All @@ -85,8 +85,8 @@ void GameEngine::Application::_handleSpecialKey(uint8_t keyCode, int idxPlayerPa

bool status = false;
if (specialKeyMap.find(keyCode) != specialKeyMap.end())
_sceneManager->processInput(specialKeyMap[keyCode], idxPlayerPacket);
if (status == true) {
status = _sceneManager->processInput(specialKeyMap[keyCode], idxPlayerPacket);
if ((status == true) && (this->_registries->messageType != 0) && (this->_registries->payload.size() != 0)) {
std::vector<uint8_t> packet = this->_server->createPacket(this->_registries->messageType, this->_registries->payload);
for (auto it = this->_server->getClientsList().begin(); it != this->_server->getClientsList().end(); ++it) {
asio::ip::udp::endpoint& endpoint = it->second;
Expand Down
2 changes: 1 addition & 1 deletion Engine/Shared/Ecs/Src/Registry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ class Registry {



uint8_t messageType; // Message Type
uint8_t messageType = 0x00; // Message Type
std::vector<uint8_t> payload; // Payload

private:
Expand Down
4 changes: 4 additions & 0 deletions Game/GameData/Scenes/firstScene.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@
{
"component": "Position2DComponent",
"path": "libupdate_position_component.so"
},
{
"component": "ShootSystem",
"path": "libupdate_shoot_system.so"
}
],
"menus": {
Expand Down
Empty file.
41 changes: 36 additions & 5 deletions R-TypeGame/SystemNetworkUpdate/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,49 @@ include_directories(
../Systems/Shoot/
../../Engine/Shared/Ecs/Src/
${CMAKE_SOURCE_DIR}/Engine/Shared/Enum/
Shoot/
${CMAKE_SOURCE_DIR}/Engine/Shared/Ecs/Src/
Shoot/
${CMAKE_SOURCE_DIR}/Engine/Shared/Component/
${CMAKE_SOURCE_DIR}/Engine/Shared/Component/
${CMAKE_SOURCE_DIR}/Engine/Shared/Component/
../Components/Speed/
../Components/Velocity/
../Components/Shoot/
../Parser/TextureRectParser/
../Parser/Position2DParser/
../Parser/SpeedParser/
../Parser/VelocityParser/
../Parser/ShootParser/
../Parser/ScaleParser/
)

# Add a shared library target 'system_initPlayer'
add_library(update_position_component SHARED
UpdatePositionSystem/updatePositionSystem.cpp
${CMAKE_SOURCE_DIR}/Engine/Shared/Network/Packet/NetworkPacket.cpp
)

find_package(asio CONFIG REQUIRED)
target_link_libraries(update_position_component PRIVATE asio::asio JsonCpp::JsonCpp)

target_link_libraries(
update_position_component
PRIVATE asio::asio
JsonCpp::JsonCpp
add_library(update_shoot_system SHARED
UpdateShootSystem/updateShootSystem.cpp
../Systems/Shoot/ShootSystem.cpp
${CMAKE_SOURCE_DIR}/Engine/Shared/Ecs/Src/Registry.cpp
../Systems/Shoot/ShootInitSystem.cpp
${CMAKE_SOURCE_DIR}/Engine/Shared/Component/Position2DComponent.cpp
${CMAKE_SOURCE_DIR}/Engine/Shared/Component/ScaleComponent.cpp
${CMAKE_SOURCE_DIR}/Engine/Shared/Component/TextureRectComponent.cpp
../Components/Speed/SpeedComponent.cpp
../Components/Velocity/VelocityComponent.cpp
../Components/Shoot/ShootComponent.cpp
../Parser/TextureRectParser/TextureRectParser.cpp
../Parser/Position2DParser/Position2DParser.cpp
../Parser/SpeedParser/SpeedParser.cpp
../Parser/VelocityParser/VelocityParser.cpp
../Parser/ShootParser/ShootParser.cpp
../Parser/ScaleParser/ScaleParser.cpp
${CMAKE_SOURCE_DIR}/Engine/Shared/Network/Packet/NetworkPacket.cpp
)
find_package(asio CONFIG REQUIRED)
target_link_libraries(update_shoot_system PRIVATE asio::asio JsonCpp::JsonCpp)
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
** EPITECH PROJECT, 2024
** UpdateShootComponent
** File description:
** UpdateShootComponent
*/

#include "ShootSystem.hpp"
#include "ShootInitSystem.hpp"
#include "PlayerComponent.hpp"
#include "updateShootSystem.hpp"
#include "Position2DComponent.hpp"

void UpdateShootSystem::_updateShootSystem(Network::UDPPacket packet, ECS::Registry& reg)
{
try {
uint32_t componentTypeLength = static_cast<size_t>(packet.getPayload()[0]);

if (packet.getPayload().size() < 1 + componentTypeLength + 1 * sizeof(int)) {
std::cerr << "Payload is too small." << std::endl;
return;
}

std::string componentType(packet.getPayload().begin() + 1, packet.getPayload().begin() + 1 + componentTypeLength);

int idxPacketEntities = (packet.getPayload()[1 + componentTypeLength] << 24) |
(packet.getPayload()[2 + componentTypeLength] << 16) |
(packet.getPayload()[3 + componentTypeLength] << 8) |
packet.getPayload()[4 + componentTypeLength];

ECS::entity_t shoot = reg.spawn_entity();
ShootInitSystem().getFunction()(reg, shoot);

ECS::SparseArray<IComponent> positions = reg.get_components<IComponent>("Position2DComponent");

if (positions.size() > shoot && positions.size() > idxPacketEntities) {
std::shared_ptr<Position2DComponent> position = std::dynamic_pointer_cast<Position2DComponent>(positions[shoot]);
std::shared_ptr<Position2DComponent> positionPlayer = std::dynamic_pointer_cast<Position2DComponent>(positions[idxPacketEntities]);

if (position && positionPlayer) {
position->x = positionPlayer->x + POS_PLAYER_X;
position->y = positionPlayer->y + POS_PLAYER_Y;
}
}

} catch (std::exception e){
std::cerr << "Error: " << e.what() << std::endl;
}
}

extern "C" ISystemNetworkUpdate* loadUpdateInstance()
{
return new UpdateShootSystem();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
** EPITECH PROJECT, 2024
** UpdateShootSystem
** File description:
** UpdateShootSystem
*/

#pragma once

#include <iostream>

#include "ISystemNetworkUpdate.hpp"

/**
* @brief Update shoot system.
*
*/
class UpdateShootSystem : public ISystemNetworkUpdate {

public:

/**
* @brief Construct a new UpdateShootSystem System object.
*
*/
UpdateShootSystem() = default;

/**
* @brief Destroy the UpdateShootSystem System object.
*
*/
~UpdateShootSystem() = default;

/**
* @brief Get the Function object.
*
* @return std::function<void(ECS::Registry& reg, int idxPacketEntities)>
*/
std::function<void(Network::UDPPacket, ECS::Registry&)> getFunction()
{
return [this](Network::UDPPacket packet, ECS::Registry& reg) {
_updateShootSystem(packet, reg);
};
}

private:

void _updateShootSystem(Network::UDPPacket packet, ECS::Registry& reg); //The system to update shoot.
};
16 changes: 16 additions & 0 deletions R-TypeGame/Systems/Shoot/ShootSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,23 @@ void ShootSystem::_shoot(ECS::Registry& reg, int idxPacketEntities)
position->y = positionPlayer->y + POS_PLAYER_Y;
}
}


reg.messageType = 0x02;
reg.payload.clear();

std::string componentType = "ShootSystem";
reg.payload.push_back(static_cast<uint8_t>(componentType.size()));
reg.payload.insert(reg.payload.end(), componentType.begin(), componentType.end());


reg.payload.push_back(static_cast<uint8_t>(idxPacketEntities >> 24) & 0xFF);
reg.payload.push_back(static_cast<uint8_t>((idxPacketEntities >> 16) & 0xFF));
reg.payload.push_back(static_cast<uint8_t>((idxPacketEntities >> 8) & 0xFF));
reg.payload.push_back(static_cast<uint8_t>((idxPacketEntities) & 0xFF));

} catch (std::exception e){
std::cerr << "Error in Shoot system: " << e.what() << std::endl;
}
}

Expand Down

0 comments on commit b9efcf0

Please sign in to comment.