[Network] 'EntityDespawn' packet added.
This commit is contained in:
parent
580789deee
commit
597e60b716
@ -263,15 +263,25 @@ void ClientCommandHandler::setupCallbacks() {
|
||||
}
|
||||
});
|
||||
|
||||
m_client.setCommandCallback(Network::Command::EntityDespawn, [this](sf::Packet &packet) {
|
||||
u32 entityID;
|
||||
packet >> entityID;
|
||||
|
||||
auto it = m_entityMap.find(entityID);
|
||||
if (it != m_entityMap.end()) {
|
||||
m_world.scene().registry().destroy(it->second);
|
||||
}
|
||||
else
|
||||
gkError() << "EntityDespawn: Entity ID" << entityID << "is invalid";
|
||||
});
|
||||
|
||||
m_client.setCommandCallback(Network::Command::EntityPosUpdate, [this](sf::Packet &packet) {
|
||||
u32 entityID;
|
||||
packet >> entityID;
|
||||
|
||||
auto ®istry = m_world.scene().registry();
|
||||
|
||||
auto it = m_entityMap.find(entityID);
|
||||
if (it != m_entityMap.end()) {
|
||||
auto &position = registry.get_or_assign<PositionComponent>(it->second);
|
||||
auto &position = m_world.scene().registry().get_or_assign<PositionComponent>(it->second);
|
||||
packet >> position.x >> position.y >> position.z;
|
||||
}
|
||||
else
|
||||
@ -282,11 +292,9 @@ void ClientCommandHandler::setupCallbacks() {
|
||||
u32 entityID;
|
||||
packet >> entityID;
|
||||
|
||||
auto ®istry = m_world.scene().registry();
|
||||
|
||||
auto it = m_entityMap.find(entityID);
|
||||
if (it != m_entityMap.end()) {
|
||||
packet >> registry.get_or_assign<DrawableDef>(it->second);
|
||||
packet >> m_world.scene().registry().get_or_assign<DrawableDef>(it->second);
|
||||
}
|
||||
else
|
||||
gkError() << "EntityDrawableDef: Entity ID" << entityID << "is invalid";
|
||||
|
@ -62,6 +62,7 @@ std::string Network::commandToString(Network::Command command) {
|
||||
{Network::Command::ChatMessage, "ChatMessage"},
|
||||
|
||||
{Network::Command::EntitySpawn, "EntitySpawn"},
|
||||
{Network::Command::EntityDespawn, "EntityDespawn"},
|
||||
{Network::Command::EntityPosUpdate, "EntityPosUpdate"},
|
||||
{Network::Command::EntityDrawableDef, "EntityDrawableDef"},
|
||||
};
|
||||
|
@ -69,8 +69,9 @@ namespace Network {
|
||||
|
||||
// Entity commands
|
||||
EntitySpawn = 22, // <TCP> [NetworkCommand][u32 entity id] (from Server only)
|
||||
EntityPosUpdate = 23, // <TCP> [NetworkCommand][u32 entity id][double x, double y, double z] (from Server only)
|
||||
EntityDrawableDef = 24, // <TCP> [NetworkCommand][u32 entity id][DrawableDef def] (from Server only)
|
||||
EntityDespawn = 23, // <TCP> [NetworkCommand][u32 entity id] (from Server only)
|
||||
EntityPosUpdate = 24, // <TCP> [NetworkCommand][u32 entity id][double x, double y, double z] (from Server only)
|
||||
EntityDrawableDef = 25, // <TCP> [NetworkCommand][u32 entity id][DrawableDef def] (from Server only)
|
||||
};
|
||||
|
||||
std::string commandToString(Command command);
|
||||
|
@ -105,6 +105,16 @@ void ServerCommandHandler::sendEntitySpawn(u32 entityID, const ClientInfo *clien
|
||||
client->tcpSocket->send(packet);
|
||||
}
|
||||
|
||||
void ServerCommandHandler::sendEntityDespawn(u32 entityID, const ClientInfo *client) const {
|
||||
sf::Packet packet;
|
||||
packet << Network::Command::EntityDespawn << entityID;
|
||||
|
||||
if (!client)
|
||||
m_server.sendToAllClients(packet);
|
||||
else
|
||||
client->tcpSocket->send(packet);
|
||||
}
|
||||
|
||||
void ServerCommandHandler::sendEntityPosUpdate(u32 entityID, double x, double y, double z, const ClientInfo *client) const {
|
||||
sf::Packet packet;
|
||||
packet << Network::Command::EntityPosUpdate << entityID << x << y << z;
|
||||
|
@ -57,6 +57,7 @@ class ServerCommandHandler {
|
||||
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 sendEntityPosUpdate(u32 entityID, double x, double y, double z, const ClientInfo *client = nullptr) const;
|
||||
void sendEntityDrawableDef(u32 entityID, DrawableDef &drawableDef, const ClientInfo *client = nullptr) const;
|
||||
|
||||
|
@ -24,20 +24,22 @@
|
||||
*
|
||||
* =====================================================================================
|
||||
*/
|
||||
#include <gk/gl/Transformable.hpp>
|
||||
|
||||
#include "CollisionController.hpp"
|
||||
#include "ItemStack.hpp"
|
||||
#include "NetworkComponent.hpp"
|
||||
#include "PlayerList.hpp"
|
||||
#include "PositionComponent.hpp"
|
||||
#include "ServerCommandHandler.hpp"
|
||||
|
||||
void CollisionController::update(entt::registry ®istry) {
|
||||
registry.view<gk::Transformable, gk::DoubleBox, ItemStack>().each([&](auto entity, auto &transformable, auto &box, auto &itemStack) {
|
||||
registry.view<PositionComponent, gk::DoubleBox, ItemStack, NetworkComponent>().each([&](auto entity, auto &position, auto &box, auto &itemStack, auto &network) {
|
||||
for (auto &it : m_players) {
|
||||
gk::DoubleBox hitbox = box + transformable.getPosition();
|
||||
gk::DoubleBox hitbox = box + gk::Vector3d{position.x, position.y, position.z};
|
||||
gk::DoubleBox playerHitbox = it.second.hitbox() + gk::Vector3d{it.second.x(), it.second.y(), it.second.z()};
|
||||
if (hitbox.intersects(playerHitbox)) {
|
||||
it.second.inventory().addStack(itemStack.item().stringID(), itemStack.amount());
|
||||
// FIXME: Send inventory update here
|
||||
m_server->sendEntityDespawn(network.entityID);
|
||||
registry.destroy(entity);
|
||||
}
|
||||
}
|
||||
|
@ -30,6 +30,7 @@
|
||||
#include "AbstractController.hpp"
|
||||
|
||||
class PlayerList;
|
||||
class ServerCommandHandler;
|
||||
|
||||
class CollisionController : public AbstractController {
|
||||
public:
|
||||
@ -37,7 +38,11 @@ class CollisionController : public AbstractController {
|
||||
|
||||
void update(entt::registry ®istry);
|
||||
|
||||
void setServer(ServerCommandHandler *server) { m_server = server; }
|
||||
|
||||
private:
|
||||
ServerCommandHandler *m_server = nullptr;
|
||||
|
||||
PlayerList &m_players;
|
||||
};
|
||||
|
||||
|
@ -29,15 +29,16 @@
|
||||
#include "ServerScene.hpp"
|
||||
|
||||
ServerScene::ServerScene(PlayerList &players) {
|
||||
m_controllers.emplace_back(new CollisionController(players));
|
||||
m_network = static_cast<NetworkController *>(m_controllers.emplace_back(new NetworkController).get());
|
||||
m_collisionController = static_cast<CollisionController *>(m_controllers.emplace_back(new CollisionController(players)).get());
|
||||
m_networkController = static_cast<NetworkController *>(m_controllers.emplace_back(new NetworkController).get());
|
||||
}
|
||||
|
||||
void ServerScene::sendEntities(const ClientInfo &client) {
|
||||
m_network->sendEntities(m_registry, client);
|
||||
m_networkController->sendEntities(m_registry, client);
|
||||
}
|
||||
|
||||
void ServerScene::setServer(ServerCommandHandler *server) {
|
||||
m_network->setServer(server);
|
||||
m_collisionController->setServer(server);
|
||||
m_networkController->setServer(server);
|
||||
}
|
||||
|
||||
|
@ -30,6 +30,7 @@
|
||||
#include "Scene.hpp"
|
||||
|
||||
class ClientInfo;
|
||||
class CollisionController;
|
||||
class NetworkController;
|
||||
class PlayerList;
|
||||
class ServerCommandHandler;
|
||||
@ -43,7 +44,8 @@ class ServerScene : public Scene {
|
||||
void setServer(ServerCommandHandler *server);
|
||||
|
||||
private:
|
||||
NetworkController *m_network = nullptr;
|
||||
CollisionController *m_collisionController = nullptr;
|
||||
NetworkController *m_networkController = nullptr;
|
||||
};
|
||||
|
||||
#endif // SERVERSCENE_HPP_
|
||||
|
Loading…
x
Reference in New Issue
Block a user