diff --git a/docs/network/rfc/RFC.md b/docs/network/rfc/RFC.md index 954b8aae..f662f793 100644 --- a/docs/network/rfc/RFC.md +++ b/docs/network/rfc/RFC.md @@ -37,10 +37,11 @@ Table of Contents 3.2.2. READY 3.2.3. POSITION_RELATIVE 3.2.4. POSITION_ABSOLUTE - 3.2.5. NEW_BULLET + 3.2.5. NEW_MISSILE 3.2.6. LIFE_UPDATE 3.2.7. ENEMY_DEATH 3.2.8. PLAYER_DEATH + 3.2.9. MISSILE_DEATH 3.3. Main Server -> Client 3.3.1. CONNECT_MAIN_SERVER_RESP 3.3.2. LIST_LOBBY @@ -50,10 +51,11 @@ Table of Contents 3.4.3. ENEMY_DEATH 3.4.4. NEW_ENEMY 3.4.5. NEW_PLAYER - 3.4.6. NEW_BULLET + 3.4.6. NEW_MISSILE 3.4.7. POSITION_ABSOLUTE_BROADCAST 3.4.8. POSITION_RELATIVE_BROADCAST 3.4.9. PLAYER_DEATH + 3.4.10. MISSILE_DEATH 3.5. Lobby Server -> Main Server 3.5.1. INFO_LOBBY 4. References @@ -190,7 +192,7 @@ Table of Contents 3.1.1. CONNECT_MAIN_SERVER - To understand this action, the action header `magick` must be equal to `18` + To understand this action, the action header `magick` must be equal to `19` The action body is composed of the following fields: - `magick` @@ -220,7 +222,7 @@ Table of Contents 3.1.3. CREATE_LOBBY - To understand this action, the action header `magick` must be equal to `15` + To understand this action, the action header `magick` must be equal to `17` The action body is composed of the following fields: - `magick` @@ -391,7 +393,7 @@ Table of Contents This field must be of size 4 bytes. This field is signed (so starting from -((2^32)/2) to +(((2^32)/2)-1)) -3.2.5. NEW_BULLET +3.2.5. NEW_MISSILE To understand this action, the action header `magick` must be equal to `9` @@ -399,6 +401,8 @@ Table of Contents - `magick` - `x` - `y` + - `missileId` + - `missileHealth` - `missileType` *** Magick @@ -423,6 +427,24 @@ Table of Contents This field must be of size 4 bytes. This field is signed (so starting from -((2^32)/2) to +(((2^32)/2)-1)) + *** Missile ID + + This field correspond to the ID of the missile. + This is useless because this is the server that will set the ID of the + missile. + + This field must be of size 4 byte. + This field must be equal to `0` + + *** Missile Health + + This field correspond to the health of the missile. + This is useless because this is the server that will set the health of the + missile. + + This field must be of size 4 byte. + This field must be equal to `0` + *** Missile Type This field correspond to the type of the missile. @@ -515,11 +537,35 @@ Table of Contents This field is unsigned (so starting from 0 to 2^32) This field is unique for each player. +3.2.9. MISSILE_DEATH + + To understant This action, the aciton header `magick` must be equal to `15` + + The action body is composed of ther following fields: + - `magick` + - `missileId` + + *** Magick + + This field help to know the packet is realy missile death action + + This field must be of size 1 byte. + This field is unsigned (so starting from 0 to 2^8) + This field must be equal to the ascii `\x16` + + *** Missile ID + + This field correspond to the ID of the missile that died + + This field must be of size 4 bytes. + This field is unsigned (so starting from 0 to 2^32) + This field is unique for each missile. + 3.3. Main Server -> Client 3.3.1. CONNECT_MAIN_SERVER_RESP - To understand this action, the action header `magick` must be equal to `19` + To understand this action, the action header `magick` must be equal to `20` The action body is composed of the following fields: - `magick` @@ -820,7 +866,7 @@ Table of Contents This field is equal to `1` if it is you This field is equal to `0` if it is not you -3.4.6. NEW_BULLET +3.4.6. NEW_MISSILE To understand this action, the action header `magick` must be equal to `9` @@ -828,6 +874,8 @@ Table of Contents - `magick` - `x` - `y` + - `missileId` + - `missileHealth` - `missileType` *** Magick @@ -852,6 +900,20 @@ Table of Contents This field must be of size 4 bytes. This field is signed (so starting from -((2^32)/2) to +(((2^32)/2)-1)) + *** Missile ID + + This field correspond to the ID of the missile. + + This field must be of size 4 bytes. + This field is signed (so starting from -((2^32)/2) to +(((2^32)/2)-1)) + + *** Missile Health + + This field correspond to the health of the missile. + + This field must be of size 4 bytes. + This field is signed (so starting from -((2^32)/2) to +(((2^32)/2)-1)) + *** Missile Type This field correspond to the type of the bullet. @@ -972,11 +1034,35 @@ Table of Contents This field is unsigned (so starting from 0 to 2^32) This field is unique for each player. +3.2.10. MISSILE_DEATH + + To understant This action, the aciton header `magick` must be equal to `15` + + The action body is composed of ther following fields: + - `magick` + - `missileId` + + *** Magick + + This field help to know the packet is realy missile death action + + This field must be of size 1 byte. + This field is unsigned (so starting from 0 to 2^8) + This field must be equal to the ascii `\x16` + + *** Missile ID + + This field correspond to the ID of the missile that died + + This field must be of size 4 bytes. + This field is unsigned (so starting from 0 to 2^32) + This field is unique for each missile. + 3.5. Lobby Server -> Main Server 3.5.1. INFO_LOBBY - To understand this action, the action header `magick` must be equal to `17` + To understand this action, the action header `magick` must be equal to `18` This action is used to let the Main Server know the current Lobby Server exists. @@ -1079,4 +1165,4 @@ Table of Contents Structures used by the R-Bus game inside the network -``` +``` \ No newline at end of file diff --git a/src/Client/Systems/Events/EventsSystems.cpp b/src/Client/Systems/Events/EventsSystems.cpp index c3c1e4e3..d7ba7b09 100644 --- a/src/Client/Systems/Events/EventsSystems.cpp +++ b/src/Client/Systems/Events/EventsSystems.cpp @@ -133,7 +133,6 @@ namespace Systems { Nitwork::NitworkClient::getInstance().addNewBulletMsg( {Maths::removeIntDecimals(pos.x), Maths::removeIntDecimals(pos.y)}, missile.type); - createMissile(pos, missile); clock_.restart(getClockIdFromMissileType(missile.type)); } } diff --git a/src/Client/Systems/Network/ClientNetwork.cpp b/src/Client/Systems/Network/ClientNetwork.cpp index e3f4ac7a..c5d0fed7 100644 --- a/src/Client/Systems/Network/ClientNetwork.cpp +++ b/src/Client/Systems/Network/ClientNetwork.cpp @@ -49,6 +49,28 @@ namespace Systems { } } + void receiveMissileDeath(std::any &any, boost::asio::ip::udp::endpoint & /* unused */) + { + std::lock_guard lock(Registry::getInstance().mutex); + const auto missileDeath = std::any_cast(any); + auto &missiles = Registry::getInstance().getComponents(); + auto &arrHealth = Registry::getInstance().getComponents(); + std::vector ids = missiles.getExistingsId(); + + for (auto id : ids) { + if (missiles[id].constId == missileDeath.missileId) { + if (arrHealth.exist(id)) { + arrHealth[id].hp = 0; + } else { + Logger::fatal("\n\n\n!!!! Missile has no health component, but is alive !!!!\n\n\n"); + // TODO : remove missile + Registry::getInstance().removeEntity(id); + } + return; + } + } + } + void handleStartWave(std::any &any, boost::asio::ip::udp::endpoint & /* unused */) { auto &director = SystemManagersDirector::getInstance(); @@ -210,6 +232,8 @@ namespace Systems { void receiveNewBullet(std::any &any, boost::asio::ip::udp::endpoint & /* unused*/) { std::lock_guard lock(Registry::getInstance().mutex); + auto missiles = Registry::getInstance().getComponents(); + auto health = Registry::getInstance().getComponents(); const struct msgNewBullet_s &msgNewBullet = std::any_cast(any); @@ -218,7 +242,9 @@ namespace Systems { Maths::addIntDecimals(msgNewBullet.pos.y), }; struct Types::Missiles missileType = {static_cast(msgNewBullet.missileType)}; - Systems::createMissile(position, missileType); + auto id = Systems::createMissile(position, missileType); + missiles[id].constId = msgNewBullet.id; + health[id].hp = msgNewBullet.life; } void receiveBroadcastAbsolutePosition(std::any &any, boost::asio::ip::udp::endpoint & /* unused*/) diff --git a/src/Client/Systems/Network/ClientNetwork.hpp b/src/Client/Systems/Network/ClientNetwork.hpp index e27efe40..c9f175b2 100644 --- a/src/Client/Systems/Network/ClientNetwork.hpp +++ b/src/Client/Systems/Network/ClientNetwork.hpp @@ -15,6 +15,7 @@ namespace Systems { void receiveRelativePosition(std::any &any, boost::asio::ip::udp::endpoint &); void receiveBroadcastAbsolutePosition(std::any &any, boost::asio::ip::udp::endpoint &endpoint); void receivePlayerDeath(std::any &any, boost::asio::ip::udp::endpoint &endpoint); + void receiveMissileDeath(std::any &any, boost::asio::ip::udp::endpoint & /* unused */); void handleNewLobbyMsg(std::any &data, boost::asio::ip::udp::endpoint &endpoint); void receiveConnectMainServerResp(std::any &data, boost::asio::ip::udp::endpoint &endpoint); std::vector> getNetworkSystems(); diff --git a/src/ECS/ECSCustomTypes.cpp b/src/ECS/ECSCustomTypes.cpp index c4cd261f..d42e6aea 100644 --- a/src/ECS/ECSCustomTypes.cpp +++ b/src/ECS/ECSCustomTypes.cpp @@ -2,6 +2,8 @@ unsigned int Types::Enemy::_enemyNb = 0; std::mutex Types::Enemy::_mutex; +unsigned int Types::Missiles::_missileNb = 0; +std::mutex Types::Missiles::_mutex; namespace Types { diff --git a/src/ECS/ECSCustomTypes.hpp b/src/ECS/ECSCustomTypes.hpp index 07eee38a..2d55268d 100644 --- a/src/ECS/ECSCustomTypes.hpp +++ b/src/ECS/ECSCustomTypes.hpp @@ -83,7 +83,30 @@ namespace Types { }; struct Missiles { + public: + Missiles(enum missileTypes_e _type = CLASSIC) : type(_type), constId(0) + { + } + + static void setMissileNb(unsigned int nb) + { + std::lock_guard lock(_mutex); + + _missileNb = nb; + } + + static unsigned int getMissileNb() + { + std::lock_guard lock(_mutex); + return _missileNb; + } + missileTypes_e type; + unsigned int constId; + + private: + static unsigned int _missileNb; + static std::mutex _mutex; }; class Physics { diff --git a/src/ECS/Systems/BulletsSystems.cpp b/src/ECS/Systems/BulletsSystems.cpp index 37869f49..0beea57c 100644 --- a/src/ECS/Systems/BulletsSystems.cpp +++ b/src/ECS/Systems/BulletsSystems.cpp @@ -103,10 +103,10 @@ namespace Systems { } } - void createMissile(Types::Position pos, Types::Missiles &typeOfMissile) + std::size_t createMissile(Types::Position pos, Types::Missiles &typeOfMissile) { - Json &json = Json::getInstance(); - Registry::getInstance().addEntity(); + Json &json = Json::getInstance(); + std::size_t id = Registry::getInstance().addEntity(); nlohmann::json bulletData = json.getJsonObjectById(JsonType::BULLETS, getMissileId(typeOfMissile.type), "bullets"); Types::CollisionRect collisionRect = @@ -132,6 +132,7 @@ namespace Systems { Registry::getInstance().getComponents().insertBack(healthComp); Registry::getInstance().getComponents().insertBack(damageComp); Registry::getInstance().getComponents().insertBack(deadComp); + return id; } static void updateBouncePhysics(std::vector ids) diff --git a/src/ECS/Systems/Systems.cpp b/src/ECS/Systems/Systems.cpp index 139e4f29..488b9c56 100644 --- a/src/ECS/Systems/Systems.cpp +++ b/src/ECS/Systems/Systems.cpp @@ -385,7 +385,8 @@ namespace Systems { static void sendDeathMsg(std::size_t arrId) { - auto &arrEnemies = Registry::getInstance().getComponents(); + auto &arrEnemies = Registry::getInstance().getComponents(); + auto &arrMissiles = Registry::getInstance().getComponents(); #ifndef CLIENT auto &arrOtherPlayer = Registry::getInstance().getComponents(); @@ -393,6 +394,15 @@ namespace Systems { Nitwork::NitworkServer::getInstance().addPlayerDeathMsg(arrOtherPlayer[arrId].constId); } #endif + + if (arrMissiles.exist(arrId)) { +#ifdef CLIENT + Nitwork::NitworkClient::getInstance().addMissileDeathMsg(arrMissiles[arrId].constId); +#else + Nitwork::NitworkServer::getInstance().addMissileDeathMsg(arrMissiles[arrId].constId); +#endif + } + if (arrEnemies.exist(arrId)) { #ifdef CLIENT Nitwork::NitworkClient::getInstance().addEnemyDeathMsg(arrEnemies[arrId].getConstId().id); diff --git a/src/ECS/Systems/Systems.hpp b/src/ECS/Systems/Systems.hpp index 28e7d81d..94a6bf77 100644 --- a/src/ECS/Systems/Systems.hpp +++ b/src/ECS/Systems/Systems.hpp @@ -35,7 +35,7 @@ namespace Systems { const struct position_absolute_s &pos, const struct health_s &life, bool otherPlayer = false); - void createMissile(Types::Position pos, Types::Missiles &typeOfMissile); + std::size_t createMissile(Types::Position pos, Types::Missiles &typeOfMissile); std::vector> getECSSystems(); std::vector> getBulletSystems(); } // namespace Systems diff --git a/src/Nitwork/Nitwork.h b/src/Nitwork/Nitwork.h index 57b6131b..5d5bbf4d 100644 --- a/src/Nitwork/Nitwork.h +++ b/src/Nitwork/Nitwork.h @@ -34,7 +34,7 @@ #define MAGICK_LIFE_UPDATE '\x0b' #define MAGICK_ENEMY_DEATH '\x0c' #define MAGICK_NEW_ENEMY '\x0e' - #define MAGICK_NEW_BULLET '\x0d' + #define MAGICK_NEW_MISSILE '\x0d' #define MAGICK_POSITION_RELATIVE_BROADCAST '\x0f' #define MAGICK_POSITION_ABSOLUTE_BROADCAST '\x10' #define MAGICK_NEW_PLAYER '\x0a' @@ -45,6 +45,7 @@ #define MAGICK_CONNECT_MAIN_SERVER '\x18' #define MAGICK_CONNECT_MAIN_SERVER_RESP '\x14' #define MAGICK_INFO_LOBBY '\x15' + #define MAGICK_MISSILE_DEATH '\x16' typedef unsigned char n_magick_t; typedef int n_idsReceived_t; @@ -64,17 +65,18 @@ enum n_actionType_t { ENEMY_DEATH = 6, POSITION_RELATIVE = 7, POSITION_ABSOLUTE = 8, - NEW_BULLET = 9, + NEW_MISSILE = 9, NEW_ENEMY = 10, NEW_PLAYER = 11, POSITION_RELATIVE_BROADCAST = 12, POSITION_ABSOLUTE_BROADCAST = 13, PLAYER_DEATH = 14, - CREATE_LOBBY = 15, - LIST_LOBBY = 16, - INFO_LOBBY = 17, - CONNECT_MAIN_SERVER = 18, - CONNECT_MAIN_SERVER_RESP = 19, + MISSILE_DEATH = 15, + CREATE_LOBBY = 16, + LIST_LOBBY = 17, + INFO_LOBBY = 18, + CONNECT_MAIN_SERVER = 19, + CONNECT_MAIN_SERVER_RESP = 20, N_ACTION_TYPE_MAX, }; @@ -178,7 +180,9 @@ PACK(struct packetNewEnemy_s { PACK(struct msgNewBullet_s { n_magick_t magick; struct position_absolute_s pos; - enum missileTypes_e missileType; + n_id_t id; + int life; + missileTypes_e missileType; }); PACK(struct packetNewBullet_s { @@ -252,7 +256,19 @@ PACK(struct packetPlayerDeath_s { struct msgPlayerDeath_s msg; }); -/* Message Request List Lobby */ +/* Message Missile Death */ +PACK(struct msgMissileDeath_s { + n_magick_t magick; + n_id_t missileId; +}); + +PACK(struct packetMissileDeath_s { + struct header_s header; + struct action_s action; + struct msgMissileDeath_s msgMissileDeath; +}); + + /* Message Request List Lobby */ PACK(struct msgRequestListLobby_s { n_magick_t magick; }); diff --git a/src/Nitwork/NitworkClient.cpp b/src/Nitwork/NitworkClient.cpp index 09d51b07..67208a76 100644 --- a/src/Nitwork/NitworkClient.cpp +++ b/src/Nitwork/NitworkClient.cpp @@ -196,12 +196,14 @@ namespace Nitwork { .header = {0, 0, 0, 0, 1, 0}, .action = { - .magick = NEW_BULLET, + .magick = NEW_MISSILE, }, .msg = { - .magick = MAGICK_NEW_BULLET, + .magick = MAGICK_NEW_MISSILE, .pos = pos, + .id = 0, + .life = 0, .missileType = missileType, }, }; @@ -299,6 +301,27 @@ namespace Nitwork { addPacketToSend(packet); } + void NitworkClient::addMissileDeathMsg(n_id_t id) + { + std::lock_guard lock(_receivedPacketsIdsMutex); + struct packetMissileDeath_s packetMissileDeath = { + .header = {0, 0, 0, 0, 1, 0}, + .action = + { + .magick = MISSILE_DEATH, + }, + .msgMissileDeath = + { + .magick = MAGICK_MISSILE_DEATH, + .missileId = id, + }, + }; + Packet packet( + packetMissileDeath.action.magick, + std::make_any(packetMissileDeath), + _serverEndpoint); + } + void NitworkClient::addListLobbyMsg() { std::lock_guard lock(_receivedPacketsIdsMutex); diff --git a/src/Nitwork/NitworkClient.hpp b/src/Nitwork/NitworkClient.hpp index d77dd806..dab909a9 100644 --- a/src/Nitwork/NitworkClient.hpp +++ b/src/Nitwork/NitworkClient.hpp @@ -90,6 +90,7 @@ namespace Nitwork { * @param id The id of the dead player */ void addPlayerDeathMsg(n_id_t id); + void addMissileDeathMsg(n_id_t id); /** * @brief Add a connect main server message to the packet @@ -281,7 +282,7 @@ namespace Nitwork { } }, { - NEW_BULLET, + NEW_MISSILE, { [this](actionHandler &handler, const struct header_s &header) { handleBody(handler, header); @@ -324,6 +325,17 @@ namespace Nitwork { } } }, + { + MISSILE_DEATH, + { + [this](actionHandler &handler, const struct header_s &header) { + handleBody(handler, header); + }, + [](std::any &any, boost::asio::ip::udp::endpoint &endpoint) { + Systems::receiveMissileDeath(any, endpoint); + } + } + }, { LIST_LOBBY, { @@ -376,7 +388,7 @@ namespace Nitwork { } }, { - NEW_BULLET, + NEW_MISSILE, [this](Packet &packet) { sendData(packet); } diff --git a/src/Nitwork/NitworkServer.cpp b/src/Nitwork/NitworkServer.cpp index 7a598853..d692f763 100644 --- a/src/Nitwork/NitworkServer.cpp +++ b/src/Nitwork/NitworkServer.cpp @@ -298,13 +298,13 @@ namespace Nitwork { _playersIds[endpoint] = playerMsg.playerId; } - void NitworkServer::addStarWaveMessage(n_id_t enemyId) + void NitworkServer::addStarWaveMessage(n_id_t enemyNb) { std::lock_guard lock(_receivedPacketsIdsMutex); struct packetMsgStartWave_s packetMsgStartWave = { .header = {0, 0, 0, 0, 1, 0}, .action = {.magick = START_WAVE}, - .msgStartWave = {.magick = MAGICK_START_WAVE, .enemyNb = enemyId} + .msgStartWave = {.magick = MAGICK_START_WAVE, .enemyNb = enemyNb} }; Packet packet( packetMsgStartWave.action.magick, @@ -361,20 +361,18 @@ namespace Nitwork { addPacketToSend(packet); } - void NitworkServer::broadcastNewBulletMsg( - const struct msgNewBullet_s &msg, - boost::asio::ip::udp::endpoint &senderEndpoint) + void NitworkServer::broadcastNewBulletMsg(const struct msgNewBullet_s &msg) { std::lock_guard lock(_receivedPacketsIdsMutex); struct packetNewBullet_s packetNewBullet = { .header = {0, 0, 0, 0, 1, 0}, - .action = {.magick = NEW_BULLET}, + .action = {.magick = NEW_MISSILE}, .msg = msg }; Packet packet( packetNewBullet.action.magick, std::make_any(packetNewBullet)); - sendToAllClientsButNotOne(packet, senderEndpoint); + sendToAllClients(packet); } void NitworkServer::broadcastAbsolutePositionMsg( @@ -442,6 +440,27 @@ namespace Nitwork { addPacketToSend(packet); } + void NitworkServer::addMissileDeathMsg(n_id_t id) + { + std::lock_guard lock(_receivedPacketsIdsMutex); + struct packetMissileDeath_s packetMissileDeath = { + .header = {0, 0, 0, 0, 1, 0}, + .action = + { + .magick = MISSILE_DEATH, + }, + .msgMissileDeath = + { + .magick = MAGICK_MISSILE_DEATH, + .missileId = id, + }, + }; + Packet packet( + packetMissileDeath.action.magick, + std::make_any(packetMissileDeath)); + sendToAllClients(packet); + } + n_id_t NitworkServer::getPlayerId(const boost::asio::ip::udp::endpoint &endpoint) const { return _playersIds.at(endpoint); diff --git a/src/Nitwork/NitworkServer.hpp b/src/Nitwork/NitworkServer.hpp index b71e4057..d0135c1f 100644 --- a/src/Nitwork/NitworkServer.hpp +++ b/src/Nitwork/NitworkServer.hpp @@ -54,6 +54,7 @@ namespace Nitwork { struct lobby_s getServerInfos() const; /* Messages creation methods */ + /** * @brief Add a msg that contain the lobby infos to the main server */ @@ -63,7 +64,7 @@ namespace Nitwork { * @brief Add a msg packet that contain the start wave msg to the clients * @param enemyId The id of the enemy that will be created */ - void addStarWaveMessage(n_id_t enemyId); + void addStarWaveMessage(n_id_t enemyNb); /** * @brief Add a msg packet that contain the life update msg to the clients @@ -102,13 +103,10 @@ namespace Nitwork { const msgCreatePlayer_s &playerMsg); /** - * @brief Add a msg packet that contain the new bullet msg to the clients - * @param msg The infos of the bullet that will be created - * @param senderEndpoint The endpoint of the client that sent the msg of hie new bullet + * @brief Add a msg packet that contain the new missile msg to the clients + * @param msg The infos of the missile that will be created */ - void broadcastNewBulletMsg( - const struct msgNewBullet_s &msg, - boost::asio::ip::udp::endpoint &senderEndpoint); + void broadcastNewBulletMsg(const struct msgNewBullet_s &msg); /** * @brief Add a msg packet that contain the new bullet msg to the clients @@ -135,6 +133,12 @@ namespace Nitwork { boost::asio::ip::udp::endpoint &endpoint, const struct msgCreatePlayer_s &playerMsg); + /** + * @brief Add a msg packet that contain the new missile msg to the clients + * @param id The id of the player that died + */ + void addMissileDeathMsg(n_id_t id); + /** * @brief Add a msg packet that contain the new bullet msg to the clients * @param endpoint The endpoint of the client that will receive the new player @@ -272,7 +276,7 @@ namespace Nitwork { [](std::any &msg, boost::asio::ip::udp::endpoint &endpoint) { Systems::handleClientEnemyDeath(msg, endpoint); }}}, - {NEW_BULLET, + {NEW_MISSILE, {[this](actionHandler &actionHandler, const struct header_s &header) { handleBody(actionHandler, header); }, @@ -293,6 +297,13 @@ namespace Nitwork { [](std::any &msg, boost::asio::ip::udp::endpoint &endpoint) { Systems::receivePlayerDeathMsg(msg, endpoint); }}}, + {MISSILE_DEATH, + {[this](actionHandler &actionHandler, const struct header_s &header) { + handleBody(actionHandler, header); + }, + [](std::any &msg, boost::asio::ip::udp::endpoint &endpoint) { + Systems::handleClientMissileDeath(msg, endpoint); + }}}, }; /** * @brief a map that will be used to handle the actions, in order to send them @@ -318,7 +329,7 @@ namespace Nitwork { [this](Packet &packet) { sendData(packet); } }, - {NEW_BULLET, + {NEW_MISSILE, [this](Packet &packet) { sendData(packet); } }, @@ -338,6 +349,10 @@ namespace Nitwork { [this](Packet &packet) { sendData(packet); } }, + {MISSILE_DEATH, + [this](Packet &packet) { + sendData(packet); + } }, {INFO_LOBBY, [this](Packet &packet) { sendData(packet); }} diff --git a/src/Server/Systems/Network/ServerNetwork.cpp b/src/Server/Systems/Network/ServerNetwork.cpp index ed98e9c1..f318e6d0 100644 --- a/src/Server/Systems/Network/ServerNetwork.cpp +++ b/src/Server/Systems/Network/ServerNetwork.cpp @@ -70,20 +70,27 @@ namespace Systems { } } - void receiveNewBulletMsg(const std::any &msg, boost::asio::ip::udp::endpoint &endpoint) + void receiveNewBulletMsg(const std::any &msg, boost::asio::ip::udp::endpoint & /* unused */) { std::lock_guard lock(Registry::getInstance().mutex); + auto &arrMissiles = Registry::getInstance().getComponents(); + auto &arrHealth = Registry::getInstance().getComponents(); - const struct msgNewBullet_s &msgNewBullet = std::any_cast(msg); + struct msgNewBullet_s msgNewBullet = std::any_cast(msg); struct Types::Position position = { Maths::addIntDecimals(msgNewBullet.pos.x), Maths::addIntDecimals(msgNewBullet.pos.y), }; struct Types::Missiles missileType = {static_cast(msgNewBullet.missileType)}; - Systems::createMissile(position, missileType); - // send bullet to clients but not the sender - Nitwork::NitworkServer::getInstance().broadcastNewBulletMsg(msgNewBullet, endpoint); + auto id = Systems::createMissile(position, missileType); + if (!arrMissiles.exist(id) || !arrHealth.exist(id)) { + Logger::error("Error: missile not created"); + return; + } + msgNewBullet.id = arrMissiles[id].constId; + msgNewBullet.life = arrHealth[id].hp; + Nitwork::NitworkServer::getInstance().broadcastNewBulletMsg(msgNewBullet); } void receiveAbsolutePositionMsg(const std::any &msg, boost::asio::ip::udp::endpoint &endpoint) @@ -148,4 +155,38 @@ namespace Systems { } Nitwork::NitworkServer::getInstance().addPlayerDeathMsg(msgPlayerDeath.playerId); } + + void handleClientMissileDeath(const std::any &msg, boost::asio::ip::udp::endpoint & /* us=nused */) + { + std::lock_guard lock(Registry::getInstance().mutex); + const struct msgMissileDeath_s &msgMissileDeath = std::any_cast(msg); + auto ®istry = Registry::getInstance(); + + if (msgMissileDeath.magick != MAGICK_MISSILE_DEATH) { + Logger::error("Error: magick is not CLIENT_MISSILE_DEATH"); + return; + } + auto &arrMissiles = registry.getComponents(); + auto arrHealth = registry.getComponents(); + auto arrPos = registry.getComponents(); + auto ids = arrMissiles.getExistingsId(); + + for (auto &id : ids) { + if (arrMissiles[id].constId == msgMissileDeath.missileId) { + if (arrHealth.exist(id) && arrPos.exist(id)) { + Nitwork::NitworkServer::getInstance().broadcastNewBulletMsg({ + .magick = MAGICK_NEW_MISSILE, + .pos = + {static_cast(Maths::removeIntDecimals(arrPos[id].x)), + static_cast(Maths::removeIntDecimals(arrPos[id].y))}, + .id = msgMissileDeath.missileId, + .life = arrHealth[id].hp, + .missileType = arrMissiles[id].type, + }); + } + return; + } + } + } + } // namespace Systems diff --git a/src/Server/Systems/Network/ServerNetwork.hpp b/src/Server/Systems/Network/ServerNetwork.hpp index bd814462..02c73ef3 100644 --- a/src/Server/Systems/Network/ServerNetwork.hpp +++ b/src/Server/Systems/Network/ServerNetwork.hpp @@ -16,4 +16,5 @@ namespace Systems { void receiveNewBulletMsg(const std::any &msg, boost::asio::ip::udp::endpoint &endpoint); void receiveAbsolutePositionMsg(const std::any &msg, boost::asio::ip::udp::endpoint &endpoint); void receivePlayerDeathMsg(const std::any &msg, boost::asio::ip::udp::endpoint &endpoint); + void handleClientMissileDeath(const std::any &msg, boost::asio::ip::udp::endpoint &endpoint); } // namespace Systems