From bcc0e0095aef3debb12f8e44a3206b8f9c30634e Mon Sep 17 00:00:00 2001 From: Persson-dev Date: Wed, 21 Aug 2024 10:29:42 +0200 Subject: [PATCH] network: send player velocity --- include/blitz/godot/World.h | 3 ++- include/blitz/protocol/PacketData.h | 1 + src/blitz/godot/World.cpp | 6 ++++-- src/blitz/protocol/PacketSerializer.cpp | 15 ++------------- src/client/ClientWorld.cpp | 2 +- src/server/ServerWorld.cpp | 4 ++-- 6 files changed, 12 insertions(+), 19 deletions(-) diff --git a/include/blitz/godot/World.h b/include/blitz/godot/World.h index e699a9e..272ed84 100644 --- a/include/blitz/godot/World.h +++ b/include/blitz/godot/World.h @@ -35,6 +35,7 @@ class World : public godot::Node3D, public protocol::PacketHandler { void AddPlayer(PlayerID a_PlayerId, godot::String a_PlayerName); void RemovePlayer(PlayerID a_PlayerId); - void SetPlayerPositionAndRotation(PlayerID a_PlayerId, const godot::Vector3& a_Position, const godot::Vector3& a_Rotation); + void SetPlayerPositionAndRotation( + PlayerID a_PlayerId, const godot::Vector3& a_Position, const godot::Vector3& a_Rotation, const godot::Vector3& a_Velocity); }; } // namespace blitz \ No newline at end of file diff --git a/include/blitz/protocol/PacketData.h b/include/blitz/protocol/PacketData.h index 0587af7..2c5174c 100644 --- a/include/blitz/protocol/PacketData.h +++ b/include/blitz/protocol/PacketData.h @@ -63,6 +63,7 @@ struct PlayerPositionAndRotation { PlayerID m_Player; godot::Vector3 m_Position; godot::Vector3 m_Rotation; + godot::Vector3 m_Velocity; }; struct PlayerShoot {}; diff --git a/src/blitz/godot/World.cpp b/src/blitz/godot/World.cpp index a0b0bb3..86b547f 100644 --- a/src/blitz/godot/World.cpp +++ b/src/blitz/godot/World.cpp @@ -69,7 +69,7 @@ void World::HandlePacket(const protocol::packets::PlayerPositionAndRotation& a_P if (data.m_Player == get_multiplayer()->get_unique_id() || data.m_Player != a_PlayerPos.m_Sender) return; - SetPlayerPositionAndRotation(data.m_Player, data.m_Position, data.m_Rotation); + SetPlayerPositionAndRotation(data.m_Player, data.m_Position, data.m_Rotation, data.m_Velocity); } void World::AddPlayer(PlayerID a_PlayerId, String a_PlayerName) { @@ -97,11 +97,13 @@ void World::RemovePlayer(PlayerID a_PlayerId) { } } -void World::SetPlayerPositionAndRotation(PlayerID a_PlayerId, const Vector3& a_Position, const Vector3& a_Rotation) { +void World::SetPlayerPositionAndRotation( + PlayerID a_PlayerId, const Vector3& a_Position, const Vector3& a_Rotation, const godot::Vector3& a_Velocity) { Player* player = GetPlayerById(a_PlayerId); if (player) { player->set_position(a_Position); player->SetCameraRotation(a_Rotation); + player->set_velocity(a_Velocity); } } diff --git a/src/blitz/protocol/PacketSerializer.cpp b/src/blitz/protocol/PacketSerializer.cpp index 5df73b3..0be9097 100644 --- a/src/blitz/protocol/PacketSerializer.cpp +++ b/src/blitz/protocol/PacketSerializer.cpp @@ -195,21 +195,10 @@ void Deserializer::DeserializePacketData(data::PlayerLeave& a_Packet) { void Serializer::SerializePacketData(const data::PlayerList& a_Packet) { m_Buffer << a_Packet.m_Players; - // m_Buffer << static_cast(a_Packet.m_Players.size()); - // for (auto player : a_Packet.m_Players) { - // m_Buffer << player.m_PlayerId << player.m_PlayerName; - // } } void Deserializer::DeserializePacketData(data::PlayerList& a_Packet) { m_Buffer >> a_Packet.m_Players; - // std::uint8_t playerCount; - // m_Buffer >> playerCount; - // for (std::uint8_t i = 0; i < playerCount; i++) { - // PlayerInfo player; - // m_Buffer >> player.m_PlayerId >> player.m_PlayerName; - // a_Packet.m_Players.push_back(player); - // } } @@ -273,11 +262,11 @@ void Deserializer::DeserializePacketData(data::ChatMessage& a_Packet) { void Serializer::SerializePacketData(const data::PlayerPositionAndRotation& a_Packet) { - m_Buffer << a_Packet.m_Player << a_Packet.m_Position << a_Packet.m_Rotation; + m_Buffer << a_Packet.m_Player << a_Packet.m_Position << a_Packet.m_Rotation << a_Packet.m_Velocity; } void Deserializer::DeserializePacketData(data::PlayerPositionAndRotation& a_Packet) { - m_Buffer >> a_Packet.m_Player >> a_Packet.m_Position >> a_Packet.m_Rotation; + m_Buffer >> a_Packet.m_Player >> a_Packet.m_Position >> a_Packet.m_Rotation >> a_Packet.m_Velocity; } diff --git a/src/client/ClientWorld.cpp b/src/client/ClientWorld.cpp index f140374..4f30631 100644 --- a/src/client/ClientWorld.cpp +++ b/src/client/ClientWorld.cpp @@ -32,7 +32,7 @@ void ClientWorld::UpdatePlayerPos() { Player* player = GetPlayerById(get_multiplayer()->get_unique_id()); if (player) { m_NetworkInterface->BroadcastPacket(protocol::packets::PlayerPositionAndRotation( - {get_multiplayer()->get_unique_id(), player->get_position(), player->GetCameraRotation()})); + {get_multiplayer()->get_unique_id(), player->get_position(), player->GetCameraRotation(), player->get_velocity()})); } } diff --git a/src/server/ServerWorld.cpp b/src/server/ServerWorld.cpp index 568e4cc..8c86fa7 100644 --- a/src/server/ServerWorld.cpp +++ b/src/server/ServerWorld.cpp @@ -30,8 +30,8 @@ void ServerWorld::SyncPlayersPos() { for (int i = 0; i < m_Players->get_child_count(); i++) { Player* player = Object::cast_to(m_Players->get_child(i)); DEV_ASSERT(player); - m_NetworkInterface->BroadcastPacket( - protocol::packets::PlayerPositionAndRotation({player->GetId(), player->get_position(), player->GetCameraRotation()})); + m_NetworkInterface->BroadcastPacket(protocol::packets::PlayerPositionAndRotation( + {player->GetId(), player->get_position(), player->GetCameraRotation(), player->get_velocity()})); } }