[NetworkComponent] Now using server-side entity ID instead of an arbitrary one.

This commit is contained in:
Quentin Bazin 2020-04-29 17:58:07 +02:00
parent 1d7782c54a
commit bf7143efb3
8 changed files with 52 additions and 30 deletions

View File

@ -27,6 +27,8 @@
#include <gk/core/Debug.hpp>
#include <gk/gl/Camera.hpp>
#include <entt/entt.hpp>
#include "AnimationComponent.hpp"
#include "Client.hpp"
#include "ClientPlayer.hpp"
@ -249,7 +251,7 @@ void ClientCommandHandler::setupCallbacks() {
});
m_client.setCommandCallback(Network::Command::EntitySpawn, [this](sf::Packet &packet) {
u32 entityID;
entt::entity entityID;
packet >> entityID;
auto &registry = m_world.scene().registry();
@ -261,12 +263,12 @@ void ClientCommandHandler::setupCallbacks() {
registry.emplace<NetworkComponent>(entity, entityID);
}
else if (registry.get<NetworkComponent>(it->second).entityID != entityID) {
gkError() << "EntitySpawn: Entity ID" << entityID << "is invalid";
gkError() << "EntitySpawn: Entity ID" << std::underlying_type_t<entt::entity>(entityID) << "is invalid";
}
});
m_client.setCommandCallback(Network::Command::EntityDespawn, [this](sf::Packet &packet) {
u32 entityID;
entt::entity entityID;
packet >> entityID;
auto it = m_entityMap.find(entityID);
@ -274,11 +276,11 @@ void ClientCommandHandler::setupCallbacks() {
m_world.scene().registry().destroy(it->second);
}
else
gkError() << "EntityDespawn: Entity ID" << entityID << "is invalid";
gkError() << "EntityDespawn: Entity ID" << std::underlying_type_t<entt::entity>(entityID) << "is invalid";
});
m_client.setCommandCallback(Network::Command::EntityPosition, [this](sf::Packet &packet) {
u32 entityID;
entt::entity entityID;
packet >> entityID;
auto it = m_entityMap.find(entityID);
@ -287,11 +289,11 @@ void ClientCommandHandler::setupCallbacks() {
packet >> position.x >> position.y >> position.z;
}
else
gkError() << "EntityPosition: Entity ID" << entityID << "is invalid";
gkError() << "EntityPosition: Entity ID" << std::underlying_type_t<entt::entity>(entityID) << "is invalid";
});
m_client.setCommandCallback(Network::Command::EntityRotation, [this](sf::Packet &packet) {
u32 entityID;
entt::entity entityID;
packet >> entityID;
auto it = m_entityMap.find(entityID);
@ -303,11 +305,11 @@ void ClientCommandHandler::setupCallbacks() {
rotation.quat = glm::quat(w, x, y, z);
}
else
gkError() << "EntityRotation: Entity ID" << entityID << "is invalid";
gkError() << "EntityRotation: Entity ID" << std::underlying_type_t<entt::entity>(entityID) << "is invalid";
});
m_client.setCommandCallback(Network::Command::EntityAnimation, [this](sf::Packet &packet) {
u32 entityID;
entt::entity entityID;
packet >> entityID;
auto it = m_entityMap.find(entityID);
@ -316,11 +318,11 @@ void ClientCommandHandler::setupCallbacks() {
animation.deserialize(packet);
}
else
gkError() << "EntityAnimation: Entity ID" << entityID << "is invalid";
gkError() << "EntityAnimation: Entity ID" << std::underlying_type_t<entt::entity>(entityID) << "is invalid";
});
m_client.setCommandCallback(Network::Command::EntityDrawableDef, [this](sf::Packet &packet) {
u32 entityID;
entt::entity entityID;
packet >> entityID;
auto it = m_entityMap.find(entityID);
@ -328,7 +330,7 @@ void ClientCommandHandler::setupCallbacks() {
packet >> m_world.scene().registry().get_or_emplace<DrawableDef>(it->second);
}
else
gkError() << "EntityDrawableDef: Entity ID" << entityID << "is invalid";
gkError() << "EntityDrawableDef: Entity ID" << std::underlying_type_t<entt::entity>(entityID) << "is invalid";
});
}

View File

@ -72,7 +72,7 @@ class ClientCommandHandler {
std::unordered_map<u16, PlayerBox> &m_playerBoxes;
std::unordered_map<u32, entt::entity> m_entityMap;
std::unordered_map<entt::entity, entt::entity> m_entityMap;
bool m_isRegistryInitialized = false;

View File

@ -36,3 +36,15 @@ sf::Packet &operator>>(sf::Packet &packet, gk::Color &color) {
return packet;
}
sf::Packet &operator<<(sf::Packet &packet, const entt::entity &entity) {
packet << std::underlying_type_t<entt::entity>(entity);
return packet;
}
sf::Packet &operator>>(sf::Packet &packet, entt::entity &entity) {
std::underlying_type_t<entt::entity> id;
packet >> id;
entity = static_cast<entt::entity>(id);
return packet;
}

View File

@ -143,4 +143,12 @@ sf::Packet &operator>>(sf::Packet &packet, gk::Vector3<T> &vec) {
sf::Packet &operator<<(sf::Packet &packet, const gk::Color &color);
sf::Packet &operator>>(sf::Packet &packet, gk::Color &color);
//======================================================================================
// entt::entity
//======================================================================================
#include <entt/entt.hpp>
sf::Packet &operator<<(sf::Packet &packet, const entt::entity &entity);
sf::Packet &operator>>(sf::Packet &packet, entt::entity &entity);
#endif // NETWORKUTILS_HPP_

View File

@ -27,10 +27,10 @@
#ifndef NETWORKCOMPONENT_HPP_
#define NETWORKCOMPONENT_HPP_
#include <gk/core/IntTypes.hpp>
#include <entt/entt.hpp>
struct NetworkComponent {
u32 entityID = 0;
entt::entity entityID;
bool hasSpawned = false;
};

View File

@ -112,7 +112,7 @@ void ServerCommandHandler::sendChatMessage(u16 clientID, const std::string &mess
client->tcpSocket->send(packet);
}
void ServerCommandHandler::sendEntitySpawn(u32 entityID, const ClientInfo *client) const {
void ServerCommandHandler::sendEntitySpawn(entt::entity entityID, const ClientInfo *client) const {
sf::Packet packet;
packet << Network::Command::EntitySpawn << entityID;
@ -122,7 +122,7 @@ void ServerCommandHandler::sendEntitySpawn(u32 entityID, const ClientInfo *clien
client->tcpSocket->send(packet);
}
void ServerCommandHandler::sendEntityDespawn(u32 entityID, const ClientInfo *client) const {
void ServerCommandHandler::sendEntityDespawn(entt::entity entityID, const ClientInfo *client) const {
sf::Packet packet;
packet << Network::Command::EntityDespawn << entityID;
@ -132,7 +132,7 @@ void ServerCommandHandler::sendEntityDespawn(u32 entityID, const ClientInfo *cli
client->tcpSocket->send(packet);
}
void ServerCommandHandler::sendEntityPosition(u32 entityID, double x, double y, double z, const ClientInfo *client) const {
void ServerCommandHandler::sendEntityPosition(entt::entity entityID, double x, double y, double z, const ClientInfo *client) const {
sf::Packet packet;
packet << Network::Command::EntityPosition << entityID << x << y << z;
@ -142,7 +142,7 @@ void ServerCommandHandler::sendEntityPosition(u32 entityID, double x, double y,
client->tcpSocket->send(packet);
}
void ServerCommandHandler::sendEntityRotation(u32 entityID, float w, float x, float y, float z, const ClientInfo *client) const {
void ServerCommandHandler::sendEntityRotation(entt::entity entityID, float w, float x, float y, float z, const ClientInfo *client) const {
sf::Packet packet;
packet << Network::Command::EntityRotation << entityID << w << x << y << z;
@ -152,7 +152,7 @@ void ServerCommandHandler::sendEntityRotation(u32 entityID, float w, float x, fl
client->tcpSocket->send(packet);
}
void ServerCommandHandler::sendEntityAnimation(u32 entityID, const AnimationComponent &animation, const ClientInfo *client) const {
void ServerCommandHandler::sendEntityAnimation(entt::entity entityID, const AnimationComponent &animation, const ClientInfo *client) const {
sf::Packet packet;
packet << Network::Command::EntityAnimation << entityID << animation;
@ -162,7 +162,7 @@ void ServerCommandHandler::sendEntityAnimation(u32 entityID, const AnimationComp
client->tcpSocket->send(packet);
}
void ServerCommandHandler::sendEntityDrawableDef(u32 entityID, const DrawableDef &drawableDef, const ClientInfo *client) const {
void ServerCommandHandler::sendEntityDrawableDef(entt::entity entityID, const DrawableDef &drawableDef, const ClientInfo *client) const {
sf::Packet packet;
packet << Network::Command::EntityDrawableDef << entityID << drawableDef;

View File

@ -31,6 +31,8 @@
#include <gk/core/Vector3.hpp>
#include <entt/entt.hpp>
#include "ChatCommandHandler.hpp"
struct BlockData;
@ -58,12 +60,12 @@ class ServerCommandHandler {
void sendPlayerInvUpdate(u16 clientID, const ClientInfo *client = nullptr) const;
void sendPlayerChangeDimension(u16 clientID, s32 x, s32 y, s32 z, u16 dimension, const ClientInfo *client = nullptr) const;
void sendChatMessage(u16 clientID, const std::string &message, const ClientInfo *client = nullptr) const;
void sendEntitySpawn(u32 entityID, const ClientInfo *client = nullptr) const;
void sendEntityDespawn(u32 entityID, const ClientInfo *client = nullptr) const;
void sendEntityPosition(u32 entityID, double x, double y, double z, const ClientInfo *client = nullptr) const;
void sendEntityRotation(u32 entityID, float w, float x, float y, float z, const ClientInfo *client = nullptr) const;
void sendEntityAnimation(u32 entityID, const AnimationComponent &animation, const ClientInfo *client = nullptr) const;
void sendEntityDrawableDef(u32 entityID, const DrawableDef &drawableDef, const ClientInfo *client = nullptr) const;
void sendEntitySpawn(entt::entity entityID, const ClientInfo *client = nullptr) const;
void sendEntityDespawn(entt::entity entityID, const ClientInfo *client = nullptr) const;
void sendEntityPosition(entt::entity entityID, double x, double y, double z, const ClientInfo *client = nullptr) const;
void sendEntityRotation(entt::entity entityID, float w, float x, float y, float z, const ClientInfo *client = nullptr) const;
void sendEntityAnimation(entt::entity entityID, const AnimationComponent &animation, const ClientInfo *client = nullptr) const;
void sendEntityDrawableDef(entt::entity entityID, const DrawableDef &drawableDef, const ClientInfo *client = nullptr) const;
void setupCallbacks();

View File

@ -34,13 +34,11 @@
#include "Registry.hpp"
#include "RotationComponent.hpp"
static u32 counter = 0; // FIXME: TEMPORARY
void ItemDropFactory::create(entt::registry &registry, double x, double y, double z, const std::string &itemID, u16 amount) {
auto entity = registry.create();
registry.emplace<PositionComponent>(entity, x, y, z);
registry.emplace<RotationComponent>(entity);
registry.emplace<NetworkComponent>(entity, counter++);
registry.emplace<NetworkComponent>(entity, entity);
auto &drawableDef = registry.emplace<DrawableDef>(entity);
auto &cube = drawableDef.addInventoryCube();