[ClientPlayer] Now sending PlayerChunkPosUpdate.

master
Quentin Bazin 2021-05-16 19:02:50 +02:00
parent 6f97f53416
commit 5bdacba397
8 changed files with 30 additions and 0 deletions

View File

@ -274,6 +274,14 @@ Packet sent from a client when it is ready to receive chunks.
_This packet has no field._
#### PlayerChunkPosUpdate
| Field name | Field type | Notes |
| ------------- | ----------- | ---------------------------------------------------- |
| Chunk X | s32 | Chunk X coordinate |
| Chunk Y | s32 | Chunk Y coordinate |
| Chunk Z | s32 | Chunk Z coordinate |
#### BlockActivated
| Field name | Field type | Notes |

View File

@ -97,6 +97,14 @@ void ClientCommandHandler::sendPlayerReady() {
m_client.send(packet);
}
void ClientCommandHandler::sendPlayerChunkPosUpdate() const {
Network::Packet packet;
const gk::Vector3i &chunkPos = m_player.getCurrentChunk();
packet << Network::Command::PlayerChunkPosUpdate
<< s32(chunkPos.x) << s32(chunkPos.y) << s32(chunkPos.z);
m_client.send(packet);
}
void ClientCommandHandler::sendBlockActivated(const glm::ivec4 &selectedBlock) {
Network::Packet packet;
packet << Network::Command::BlockActivated

View File

@ -53,6 +53,7 @@ class ClientCommandHandler {
void sendPlayerPlaceBlock(s32 x, s32 y, s32 z, u32 block);
void sendPlayerHeldItemChanged(u8 hotbarSlot, u16 itemID);
void sendPlayerReady();
void sendPlayerChunkPosUpdate() const;
void sendBlockActivated(const glm::ivec4 &selectedBlock);
void sendBlockInvUpdate(Inventory &inventory);
void sendItemActivated(const glm::ivec4 &selectedBlock);

View File

@ -29,6 +29,7 @@
#include <gk/core/input/GamePad.hpp>
#include <gk/core/Mouse.hpp>
#include "ClientCommandHandler.hpp"
#include "ClientPlayer.hpp"
#include "ClientWorld.hpp"
#include "GameConfig.hpp"
@ -153,6 +154,12 @@ void ClientPlayer::updatePosition(const ClientWorld &world) {
else {
GameConfig::currentScreenEffect = 0;
}
// Sending PlayerChunkPosUpdate if needed
if (!m_lastChunkPos.has_value() || m_lastChunkPos.value() != getCurrentChunk()) {
world.client().sendPlayerChunkPosUpdate();
m_lastChunkPos = getCurrentChunk();
}
}
void ClientPlayer::setPosition(double x, double y, double z) {

View File

@ -88,6 +88,8 @@ class ClientPlayer : public Player {
bool m_isJumping = false;
const float m_jumpSpeed = 0.06f;
std::optional<gk::Vector3i> m_lastChunkPos;
};
#endif // CLIENTPLAYER_HPP_

View File

@ -65,6 +65,8 @@ class ClientWorld : public World, public gk::Drawable {
const ClientScene &scene() const { return m_scene; }
ClientScene &scene() { return m_scene; }
const ClientCommandHandler &client() const { return *m_client; }
void setClient(ClientCommandHandler &client) { m_client = &client; }
void setCamera(gk::Camera &camera) { m_camera = &camera; m_scene.setCamera(camera); }
void setEventHandler(gk::EventHandler &eventHandler) { m_eventHandler = &eventHandler; }

View File

@ -52,6 +52,7 @@ std::string Network::commandToString(Network::Command command) {
{Network::Command::PlayerChangeDimension, "PlayerChangeDimension"},
{Network::Command::PlayerHeldItemChanged, "PlayerHeldItemChanged"},
{Network::Command::PlayerReady, "PlayerReady"},
{Network::Command::PlayerChunkPosUpdate, "PlayerChunkPosUpdate"},
{Network::Command::BlockUpdate, "BlockUpdate"},
{Network::Command::BlockActivated, "BlockActivated"},

View File

@ -55,6 +55,7 @@ namespace Network {
PlayerChangeDimension = 0x36,
PlayerHeldItemChanged = 0x37,
PlayerReady = 0x38,
PlayerChunkPosUpdate = 0x39,
BlockUpdate = 0x40,
BlockActivated = 0x41,