Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLIENT-NETWORK: Add system receive broadcast position absolute #104

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/Client/Systems/Network/ClientNetwork.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,30 @@ namespace Systems {
Systems::createMissile(position, missileType);
}

void receiveBroadcastAbsolutePosition(std::any &any, boost::asio::ip::udp::endpoint & /* unused*/)
{
std::lock_guard<std::mutex> lock(Registry::getInstance().mutex);

const struct msgPositionAbsoluteBroadcast_s &msg =
std::any_cast<struct msgPositionAbsoluteBroadcast_s>(any);
struct Types::Position position = {
static_cast<float>(msg.pos.x),
static_cast<float>(msg.pos.y),
};
auto &arrPos = Registry::getInstance().getComponents<Types::Position>();
auto &arrOtherPlayers = Registry::getInstance().getComponents<Types::OtherPlayer>();
auto ids = Registry::getInstance().getEntitiesByComponents(
{typeid(Types::Position), typeid(Types::OtherPlayer)});
auto otherPlayer = std::find_if(ids.begin(), ids.end(), [&arrOtherPlayers, &msg](std::size_t id) {
return arrOtherPlayers[id].constId == msg.playerId;
});

if (otherPlayer == ids.end()) {
return;
}
arrPos[*otherPlayer] = position;
}

std::vector<std::function<void(std::size_t, std::size_t)>> getNetworkSystems()
{
return {sendPositionRelative, sendPositionAbsolute};
romainpanno marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
1 change: 1 addition & 0 deletions src/Client/Systems/Network/ClientNetwork.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ namespace Systems {
void receiveNewAllie(std::any &any, boost::asio::ip::udp::endpoint &);
void sendPositionRelative(std::size_t /* unused */, std::size_t /* unused */);
void receiveNewBullet(std::any &any, boost::asio::ip::udp::endpoint &endpoint);
void receiveBroadcastAbsolutePosition(std::any &any, boost::asio::ip::udp::endpoint &endpoint);
std::vector<std::function<void(std::size_t, std::size_t)>> getNetworkSystems();
} // namespace Systems
11 changes: 11 additions & 0 deletions src/Nitwork/NitworkClient.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,17 @@ namespace Nitwork {
Systems::receiveNewBullet(any, endpoint);
}
}
},
{
POSITION_ABSOLUTE_BROADCAST,
{
[this](actionHandler &handler, const struct header_s &header) {
handleBody<struct msgPositionAbsolute_s>(handler, header);
},
[](std::any &any, boost::asio::ip::udp::endpoint &endpoint) {
Systems::receiveBroadcastAbsolutePosition(any, endpoint);
}
}
}
};
std::map<
Expand Down
2 changes: 1 addition & 1 deletion src/Nitwork/NitworkServer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ namespace Nitwork {
}},
{POSITION_ABSOLUTE_BROADCAST,
[this](Packet &packet) {
sendData<struct packetPositionAbsolute_s>(packet);
sendData<struct packetPositionAbsoluteBroadcast_s>(packet);
}},
};
};
Expand Down