diff --git a/include/blitz/godot/World.h b/include/blitz/godot/World.h index 24476d7..6b19941 100644 --- a/include/blitz/godot/World.h +++ b/include/blitz/godot/World.h @@ -25,13 +25,15 @@ class World : public godot::Node3D, public protocol::PacketHandler { void HandlePacket(const protocol::packets::PlayerJoin&) override; void HandlePacket(const protocol::packets::PlayerLeave&) override; + void HandlePacket(const protocol::packets::PlayerShoot&) override; protected: NetworkInterface* m_NetworkInterface; godot::Node* m_Players; float m_PassedTime; - + virtual void AddProjectile( + PlayerID a_Shooter, const godot::Vector3& a_Position, const godot::Vector3& a_Rotation, const godot::Vector3& a_Velocity); virtual void AddPlayer(PlayerID a_PlayerId, godot::String a_PlayerName); virtual void RemovePlayer(PlayerID a_PlayerId); virtual void SetPlayerPositionAndRotation( diff --git a/include/blitz/protocol/PacketData.h b/include/blitz/protocol/PacketData.h index 2c5174c..4dbc841 100644 --- a/include/blitz/protocol/PacketData.h +++ b/include/blitz/protocol/PacketData.h @@ -1,9 +1,9 @@ #pragma once #include -#include #include #include +#include namespace blitz { namespace protocol { @@ -66,7 +66,12 @@ struct PlayerPositionAndRotation { godot::Vector3 m_Velocity; }; -struct PlayerShoot {}; +struct PlayerShoot { + PlayerID m_Sender; + godot::Vector3 m_Position; + godot::Vector3 m_Rotation; + godot::Vector3 m_Velocity; +}; } // namespace data } // namespace protocol diff --git a/include/client/PlayerController.h b/include/client/PlayerController.h index 9bdea60..286fa10 100644 --- a/include/client/PlayerController.h +++ b/include/client/PlayerController.h @@ -5,6 +5,8 @@ namespace blitz { +class NetworkInterface; + class PlayerController : public godot::Node { GDCLASS(PlayerController, godot::Node) protected: @@ -25,6 +27,7 @@ class PlayerController : public godot::Node { float m_Speed; Player* m_Player; godot::Node3D* m_Head; + NetworkInterface* m_NetworkInterface; void UpdateBobbing(float delta); void UpdateFOV(float delta); diff --git a/src/blitz/godot/World.cpp b/src/blitz/godot/World.cpp index 6e9eb6a..afb3a0c 100644 --- a/src/blitz/godot/World.cpp +++ b/src/blitz/godot/World.cpp @@ -1,6 +1,7 @@ #include #include +#include #include #include #include @@ -28,6 +29,7 @@ void World::_ready() { m_NetworkInterface->RegisterHandler(protocol::PacketType::PlayerJoin, *this); m_NetworkInterface->RegisterHandler(protocol::PacketType::PlayerLeave, *this); m_NetworkInterface->RegisterHandler(protocol::PacketType::PlayerPositionAndRotation, *this); + m_NetworkInterface->RegisterHandler(protocol::PacketType::PlayerShoot, *this); } @@ -60,6 +62,11 @@ void World::HandlePacket(const protocol::packets::PlayerLeave& a_PlayerLeave) { RemovePlayer(a_PlayerLeave.m_Data.m_PlayerId); } +void World::HandlePacket(const protocol::packets::PlayerShoot& a_PlayerShoot) { + const protocol::data::PlayerShoot& playerShoot = a_PlayerShoot.m_Data; + AddProjectile(playerShoot.m_Sender, playerShoot.m_Position, playerShoot.m_Rotation, playerShoot.m_Velocity); +} + void World::AddPlayer(PlayerID a_PlayerId, String a_PlayerName) { UtilityFunctions::print("New Player with id : ", a_PlayerId, " and name ", a_PlayerName); @@ -93,4 +100,22 @@ void World::SetPlayerPositionAndRotation( } } +void World::AddProjectile( + PlayerID a_Shooter, const godot::Vector3& a_Position, const godot::Vector3& a_Rotation, const godot::Vector3& a_Velocity) { + + Player* shooter = GetPlayerById(a_Shooter); + if (!shooter) + return; + + Bullet* bullet = ProjectileFactory::CreatePen(); + + bullet->set_position(a_Position); + Transform3D bulletTransform = bullet->get_transform(); + bulletTransform.basis.set_euler(a_Rotation); + bullet->set_transform(bulletTransform); + + Node* entities = get_node("WorldContent/Entities"); + entities->add_child(bullet); +} + } // namespace blitz \ No newline at end of file diff --git a/src/blitz/protocol/PacketSerializer.cpp b/src/blitz/protocol/PacketSerializer.cpp index 0be9097..3da6ff3 100644 --- a/src/blitz/protocol/PacketSerializer.cpp +++ b/src/blitz/protocol/PacketSerializer.cpp @@ -272,9 +272,13 @@ void Deserializer::DeserializePacketData(data::PlayerPositionAndRotation& a_Pack -void Serializer::SerializePacketData(const data::PlayerShoot& a_Packet) {} +void Serializer::SerializePacketData(const data::PlayerShoot& a_Packet) { + m_Buffer << a_Packet.m_Sender << a_Packet.m_Position << a_Packet.m_Rotation << a_Packet.m_Velocity; +} -void Deserializer::DeserializePacketData(data::PlayerShoot& a_Packet) {} +void Deserializer::DeserializePacketData(data::PlayerShoot& a_Packet) { + m_Buffer >> a_Packet.m_Sender >> a_Packet.m_Position >> a_Packet.m_Rotation >> a_Packet.m_Velocity; +} diff --git a/src/client/PlayerController.cpp b/src/client/PlayerController.cpp index 60408f0..63e83bf 100644 --- a/src/client/PlayerController.cpp +++ b/src/client/PlayerController.cpp @@ -1,6 +1,7 @@ #include #include +#include #include #include #include @@ -68,6 +69,9 @@ void PlayerController::_ready() { m_Player->SetPosition({0, 1, 0}); m_Player->SetVelocity({0, 0, 0}); + + m_NetworkInterface = m_Player->get_node("../../../../Network"); + DEV_ASSERT(m_NetworkInterface); } void PlayerController::_unhandled_input(const godot::Ref& a_Event) { @@ -109,13 +113,10 @@ void PlayerController::_process(float a_Delta) { void PlayerController::Shoot() { if (Input::get_singleton()->is_action_pressed("shoot")) { - Bullet* bullet = ProjectileFactory::CreatePen(); - - bullet->set_position(m_Camera->get_global_position()); - bullet->set_transform(m_Camera->get_global_transform()); - - Node* entities = m_Player->get_node("../../Entities"); - entities->add_child(bullet); + // we don't use velocity yet + protocol::packets::PlayerShoot packet( + {m_Player->GetId(), m_Camera->get_global_position(), m_Camera->get_global_transform().basis.get_euler(), {}}); + m_NetworkInterface->BroadcastPacket(packet); } }