Compare commits
2 Commits
d3467418c7
...
bf8a6458a0
| Author | SHA1 | Date | |
|---|---|---|---|
| bf8a6458a0 | |||
| 18ddede8c0 |
15
include/blitz/game/Listeners.h
Normal file
15
include/blitz/game/Listeners.h
Normal file
@@ -0,0 +1,15 @@
|
||||
#pragma once
|
||||
|
||||
#include "blitz/common/Vector.h"
|
||||
|
||||
namespace blitz {
|
||||
namespace game {
|
||||
|
||||
class PlayerListener {
|
||||
public:
|
||||
virtual void OnPlayerJump() {}
|
||||
virtual void OnPlayerShoot(const Vec3f& position, float yaw, float pitch) {}
|
||||
};
|
||||
|
||||
} // namespace game
|
||||
} // namespace blitz
|
||||
@@ -29,6 +29,7 @@ class PacketHandler {
|
||||
virtual void HandlePacket(const PlayerListPacket* packet) {}
|
||||
virtual void HandlePacket(const PlayerLoginPacket* packet) {}
|
||||
virtual void HandlePacket(const PlayerPositionAndRotationPacket* packet) {}
|
||||
virtual void HandlePacket(const PlayerShootPacket* packet) {}
|
||||
virtual void HandlePacket(const ServerTpsPacket* packet) {}
|
||||
};
|
||||
|
||||
|
||||
@@ -7,4 +7,5 @@
|
||||
#include "packets/PlayerListPacket.h"
|
||||
#include "packets/PlayerLoginPacket.h"
|
||||
#include "packets/PlayerPositionAndRotationPacket.h"
|
||||
#include "packets/PlayerShootPacket.h"
|
||||
#include "packets/ServerTpsPacket.h"
|
||||
@@ -12,6 +12,7 @@ class PlayerLeavePacket;
|
||||
class PlayerListPacket;
|
||||
class PlayerLoginPacket;
|
||||
class PlayerPositionAndRotationPacket;
|
||||
class PlayerShootPacket;
|
||||
class ServerTpsPacket;
|
||||
|
||||
} // namespace protocol
|
||||
|
||||
@@ -35,6 +35,7 @@ enum class PacketType : std::uint8_t {
|
||||
Disconnect, /**< Corresponds to DisconnectPacket */
|
||||
Chat, /**< Corresponds to ChatPacket */
|
||||
PlayerPositionAndRotation, /**< Corresponds to PlayerPositionAndRotationPacket */
|
||||
PlayerShoot, /**< Corresponds to PlayerShootPacket */
|
||||
|
||||
PACKET_COUNT
|
||||
};
|
||||
|
||||
31
include/blitz/protocol/packets/PlayerShootPacket.h
Normal file
31
include/blitz/protocol/packets/PlayerShootPacket.h
Normal file
@@ -0,0 +1,31 @@
|
||||
#pragma once
|
||||
|
||||
#include "blitz/game/Player.h"
|
||||
#include "blitz/protocol/Protocol.h"
|
||||
|
||||
namespace blitz {
|
||||
namespace protocol {
|
||||
|
||||
class PlayerShootPacket : public Packet {
|
||||
private:
|
||||
game::PlayerID m_Player; // only used when sent to client
|
||||
Vec3f m_Position;
|
||||
float m_Yaw, m_Pitch;
|
||||
|
||||
public:
|
||||
PlayerShootPacket() {}
|
||||
PlayerShootPacket(Vec3f position, float yaw, float pitch, game::PlayerID player = 0) :
|
||||
m_Position(position), m_Yaw(yaw), m_Pitch(pitch), m_Player(player) {}
|
||||
virtual ~PlayerShootPacket() {}
|
||||
|
||||
virtual DataBuffer Serialize(bool packetID = true) const;
|
||||
virtual void Deserialize(DataBuffer& data);
|
||||
virtual void Dispatch(PacketHandler* handler) const;
|
||||
|
||||
virtual PacketType GetType() const {
|
||||
return PacketType::PlayerShoot;
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace protocol
|
||||
} // namespace blitz
|
||||
@@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "blitz/common/Defines.h"
|
||||
#include "blitz/game/Listeners.h"
|
||||
#include "blitz/misc/ObjectNotifier.h"
|
||||
#include "blitz/misc/Time.h"
|
||||
#include "blitz/protocol/packets/ChatPacket.h"
|
||||
@@ -29,7 +30,7 @@ class GuiListener {
|
||||
};
|
||||
|
||||
// Singleton
|
||||
class Client : public utils::ObjectNotifier<GuiListener> {
|
||||
class Client : public utils::ObjectNotifier<GuiListener>, public game::PlayerListener {
|
||||
private:
|
||||
std::unique_ptr<server::Server> m_Server;
|
||||
std::unique_ptr<client::ClientConnexion> m_Connexion;
|
||||
@@ -55,6 +56,8 @@ class Client : public utils::ObjectNotifier<GuiListener> {
|
||||
|
||||
void SendPlayerPosAndLook(const Vec3f& position, float yaw, float pitch);
|
||||
|
||||
virtual void OnPlayerShoot(const Vec3f& position, float yaw, float pitch) override;
|
||||
|
||||
game::PlayerID GetPlayerID() const;
|
||||
|
||||
client::ClientGame* GetGame() {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "blitz/common/Smoothing.h"
|
||||
#include "blitz/game/Listeners.h"
|
||||
#include "blitz/misc/ObjectNotifier.h"
|
||||
#include "blitz/misc/Time.h"
|
||||
|
||||
@@ -13,13 +14,7 @@ class Player;
|
||||
|
||||
namespace input {
|
||||
|
||||
class PlayerListener {
|
||||
public:
|
||||
virtual void OnPlayerJump() {}
|
||||
virtual void OnPlayerShoot() {}
|
||||
};
|
||||
|
||||
class PlayerController : public utils::ObjectNotifier<PlayerListener> {
|
||||
class PlayerController : public utils::ObjectNotifier<game::PlayerListener> {
|
||||
private:
|
||||
game::Player* m_Player;
|
||||
utils::CooldownTimer<float> m_ShootTimer{1.0f};
|
||||
|
||||
@@ -19,7 +19,7 @@ class GunShader;
|
||||
|
||||
namespace render {
|
||||
|
||||
class MainRenderer : public GuiListener, public input::PlayerListener {
|
||||
class MainRenderer : public GuiListener, public game::PlayerListener {
|
||||
private:
|
||||
Client* m_Client;
|
||||
ModelLoader::Model m_PlayerModel;
|
||||
@@ -39,7 +39,7 @@ class MainRenderer : public GuiListener, public input::PlayerListener {
|
||||
|
||||
virtual void OnSpectatorChange(const game::PlayerID player) override;
|
||||
|
||||
virtual void OnPlayerShoot() override;
|
||||
virtual void OnPlayerShoot(const Vec3f& position, float yaw, float pitch) override;
|
||||
|
||||
void Update();
|
||||
void Render();
|
||||
|
||||
@@ -42,6 +42,7 @@ class ServerConnexion : public network::Connexion {
|
||||
virtual void HandlePacket(const protocol::DisconnectPacket* packet) override;
|
||||
virtual void HandlePacket(const protocol::ChatPacket* packet) override;
|
||||
virtual void HandlePacket(const protocol::PlayerPositionAndRotationPacket* packet) override;
|
||||
virtual void HandlePacket(const protocol::PlayerShootPacket* packet) override;
|
||||
|
||||
std::uint8_t GetID() const {
|
||||
return m_ID;
|
||||
|
||||
@@ -23,6 +23,7 @@ static std::array<PacketPtr, static_cast<std::size_t>(PacketType::PACKET_COUNT)>
|
||||
std::make_unique<DisconnectPacket>(),
|
||||
std::make_unique<ChatPacket>(),
|
||||
std::make_unique<PlayerPositionAndRotationPacket>(),
|
||||
std::make_unique<PlayerShootPacket>(),
|
||||
};
|
||||
|
||||
const Packet* CreatePacket(PacketType type, DataBuffer& buffer) {
|
||||
|
||||
@@ -2,7 +2,9 @@
|
||||
#include "blitz/protocol/Packets.h"
|
||||
|
||||
#define REGISTER_DISPATCH_CLASS(className) \
|
||||
void className::Dispatch(PacketHandler* handler) const { handler->HandlePacket(this); }
|
||||
void className::Dispatch(PacketHandler* handler) const { \
|
||||
handler->HandlePacket(this); \
|
||||
}
|
||||
|
||||
namespace blitz {
|
||||
namespace protocol {
|
||||
@@ -22,6 +24,7 @@ REGISTER_DISPATCH_CLASS(DisconnectPacket)
|
||||
REGISTER_DISPATCH_CLASS(ServerTpsPacket)
|
||||
REGISTER_DISPATCH_CLASS(ChatPacket)
|
||||
REGISTER_DISPATCH_CLASS(PlayerPositionAndRotationPacket)
|
||||
REGISTER_DISPATCH_CLASS(PlayerShootPacket);
|
||||
|
||||
} // namespace protocol
|
||||
} // namespace blitz
|
||||
|
||||
19
src/blitz/protocol/packets/PlayerShootPacket.cpp
Normal file
19
src/blitz/protocol/packets/PlayerShootPacket.cpp
Normal file
@@ -0,0 +1,19 @@
|
||||
#include "blitz/protocol/packets/PlayerShootPacket.h"
|
||||
|
||||
namespace blitz {
|
||||
namespace protocol {
|
||||
|
||||
DataBuffer PlayerShootPacket::Serialize(bool packetID) const {
|
||||
DataBuffer data;
|
||||
|
||||
WritePacketID(data, packetID);
|
||||
data << m_Player << m_Position << m_Yaw << m_Pitch;
|
||||
return data;
|
||||
}
|
||||
|
||||
void PlayerShootPacket::Deserialize(DataBuffer& data) {
|
||||
data >> m_Player >> m_Position >> m_Yaw >> m_Pitch;
|
||||
}
|
||||
|
||||
} // namespace protocol
|
||||
} // namespace blitz
|
||||
@@ -3,6 +3,7 @@
|
||||
#include "blitz/protocol/packets/ChatPacket.h"
|
||||
#include "blitz/protocol/packets/DisconnectPacket.h"
|
||||
#include "blitz/protocol/packets/PlayerPositionAndRotationPacket.h"
|
||||
#include "blitz/protocol/packets/PlayerShootPacket.h"
|
||||
#include "client/ClientConnexion.h"
|
||||
#include "client/game/ClientGame.h"
|
||||
#include "server/Server.h"
|
||||
@@ -78,6 +79,13 @@ void Client::SendPlayerPosAndLook(const Vec3f& position, float yaw, float pitch)
|
||||
m_Connexion->SendPacket(&packet);
|
||||
}
|
||||
|
||||
|
||||
void Client::OnPlayerShoot(const Vec3f& position, float yaw, float pitch) {
|
||||
protocol::PlayerShootPacket packet(position, yaw, pitch);
|
||||
m_Connexion->SendPacket(&packet);
|
||||
}
|
||||
|
||||
|
||||
game::PlayerID Client::GetPlayerID() const {
|
||||
return m_Connexion->GetPlayerID();
|
||||
}
|
||||
|
||||
@@ -27,8 +27,7 @@ PlayerController::PlayerController() :
|
||||
m_MaxDz(DEFAULT_JUMP_VEL),
|
||||
m_Dz(0.0),
|
||||
m_G(DEFAULT_GRAVITY),
|
||||
m_OnGround(true)
|
||||
{
|
||||
m_OnGround(true) {
|
||||
m_DxSmoother.Current = 0.0f;
|
||||
m_DySmoother.Current = 0.0f;
|
||||
m_DxSmoother.SetSmoothingTime(DEFAULT_LR_SPEED_SMOOTHING_TIME);
|
||||
@@ -64,12 +63,12 @@ void PlayerController::Update(float delta) {
|
||||
|
||||
if (ImGui::IsKeyDown(ImGuiKey::ImGuiKey_Space) && m_OnGround) {
|
||||
m_Dz = m_MaxDz;
|
||||
NotifyListeners(&PlayerListener::OnPlayerJump);
|
||||
NotifyListeners(&game::PlayerListener::OnPlayerJump);
|
||||
}
|
||||
|
||||
bool canShoot = m_ShootTimer.Update(delta);
|
||||
if (ImGui::IsMouseDown(ImGuiMouseButton_Left) && canShoot) {
|
||||
NotifyListeners(&PlayerListener::OnPlayerShoot);
|
||||
NotifyListeners(&game::PlayerListener::OnPlayerShoot, m_Player->GetPosition(), m_Player->GetYaw(), m_Player->GetPitch());
|
||||
m_ShootTimer.ApplyCooldown();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,6 +31,7 @@ MainRenderer::MainRenderer(Client* client) : m_Client(client), m_ShootTime(0) {
|
||||
|
||||
client->BindListener(this);
|
||||
m_PlayerController.BindListener(this);
|
||||
m_PlayerController.BindListener(client);
|
||||
|
||||
m_EntityShader = std::make_unique<shader::EntityShader>();
|
||||
blitz_debug_assert(m_EntityShader->LoadShader());
|
||||
@@ -100,7 +101,7 @@ void MainRenderer::RenderPlayers() {
|
||||
}
|
||||
}
|
||||
|
||||
void MainRenderer::OnPlayerShoot() {
|
||||
void MainRenderer::OnPlayerShoot(const Vec3f& position, float yaw, float pitch) {
|
||||
m_ShootTime = 1.0f;
|
||||
}
|
||||
|
||||
|
||||
@@ -46,6 +46,7 @@ void ServerConnexion::RegisterHandlers() {
|
||||
GetDispatcher()->RegisterHandler(protocol::PacketType::KeepAlive, this);
|
||||
GetDispatcher()->RegisterHandler(protocol::PacketType::PlayerLogin, this);
|
||||
GetDispatcher()->RegisterHandler(protocol::PacketType::Chat, this);
|
||||
GetDispatcher()->RegisterHandler(protocol::PacketType::PlayerShoot, this);
|
||||
GetDispatcher()->RegisterHandler(protocol::PacketType::PlayerPositionAndRotation, this);
|
||||
}
|
||||
|
||||
@@ -139,6 +140,10 @@ void ServerConnexion::HandlePacket(const protocol::PlayerPositionAndRotationPack
|
||||
m_Player->SetPitch(packet->GetPitch());
|
||||
}
|
||||
|
||||
void ServerConnexion::HandlePacket(const protocol::PlayerShootPacket* packet) {
|
||||
utils::LOGD(utils::Format("Le joueur %s a essayé de tirer", m_Player->GetName().c_str()).c_str());
|
||||
}
|
||||
|
||||
void ServerConnexion::Start() {
|
||||
InitConnection();
|
||||
SendKeepAlive();
|
||||
|
||||
Reference in New Issue
Block a user