[RotationComponent] Added.

This commit is contained in:
Quentin Bazin 2020-04-28 22:57:24 +02:00
parent 6ed5bd2577
commit 5baec80fb0
8 changed files with 110 additions and 15 deletions

View File

@ -36,6 +36,7 @@
#include "LuaGUIState.hpp"
#include "NetworkComponent.hpp"
#include "PositionComponent.hpp"
#include "RotationComponent.hpp"
#include "Registry.hpp"
void ClientCommandHandler::sendPlayerInvUpdate() {
@ -275,7 +276,7 @@ void ClientCommandHandler::setupCallbacks() {
gkError() << "EntityDespawn: Entity ID" << entityID << "is invalid";
});
m_client.setCommandCallback(Network::Command::EntityPosUpdate, [this](sf::Packet &packet) {
m_client.setCommandCallback(Network::Command::EntityPosition, [this](sf::Packet &packet) {
u32 entityID;
packet >> entityID;
@ -285,7 +286,23 @@ void ClientCommandHandler::setupCallbacks() {
packet >> position.x >> position.y >> position.z;
}
else
gkError() << "EntityPosUpdate: Entity ID" << entityID << "is invalid";
gkError() << "EntityPosition: Entity ID" << entityID << "is invalid";
});
m_client.setCommandCallback(Network::Command::EntityRotation, [this](sf::Packet &packet) {
u32 entityID;
packet >> entityID;
auto it = m_entityMap.find(entityID);
if (it != m_entityMap.end()) {
float w, x, y, z;
packet >> w >> x >> y >> z;
auto &rotation = m_world.scene().registry().get_or_assign<RotationComponent>(it->second);
rotation.quat = glm::quat(w, x, y, z);
}
else
gkError() << "EntityRotation: Entity ID" << entityID << "is invalid";
});
m_client.setCommandCallback(Network::Command::EntityDrawableDef, [this](sf::Packet &packet) {

View File

@ -24,9 +24,12 @@
*
* =====================================================================================
*/
#include <glm/gtx/quaternion.hpp>
#include "AnimationComponent.hpp"
#include "AnimationController.hpp"
#include "PositionComponent.hpp"
#include "RotationComponent.hpp"
void AnimationController::update(entt::registry &registry) {
registry.view<PositionComponent, AnimationComponent>().each([](auto, auto &position, auto &animation) {
@ -60,8 +63,14 @@ void AnimationController::update(entt::registry &registry) {
it.translation.dy = dy;
it.translation.dz = dz;
}
// else if (it.type == AnimationType::Rotation)
// transformable.rotate(it.rotation.angle, {it.rotation.axisX, it.rotation.axisY, it.rotation.axisZ});
}
});
registry.view<RotationComponent, AnimationComponent>().each([](auto, auto &rotation, auto &animation) {
for (auto &it : animation.list) {
if (it.type == AnimationType::Rotation) {
rotation.quat += glm::angleAxis(it.rotation.angle, glm::vec3{it.rotation.axisX, it.rotation.axisY, it.rotation.axisZ});
}
}
});
}

View File

@ -63,7 +63,8 @@ std::string Network::commandToString(Network::Command command) {
{Network::Command::EntitySpawn, "EntitySpawn"},
{Network::Command::EntityDespawn, "EntityDespawn"},
{Network::Command::EntityPosUpdate, "EntityPosUpdate"},
{Network::Command::EntityPosition, "EntityPosition"},
{Network::Command::EntityRotation, "EntityRotation"},
{Network::Command::EntityDrawableDef, "EntityDrawableDef"},
};

View File

@ -70,8 +70,9 @@ namespace Network {
// Entity commands
EntitySpawn = 22, // <TCP> [NetworkCommand][u32 entity id] (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)
EntityPosition = 24, // <TCP> [NetworkCommand][u32 entity id][double x, double y, double z] (from Server only)
EntityRotation = 25, // <TCP> [NetworkCommand][u32 entity id][Rotation rot] (from Server only)
EntityDrawableDef = 26, // <TCP> [NetworkCommand][u32 entity id][DrawableDef def] (from Server only)
};
std::string commandToString(Command command);

View File

@ -0,0 +1,38 @@
/*
* =====================================================================================
*
* OpenMiner
*
* Copyright (C) 2018-2020 Unarelith, Quentin Bazin <openminer@unarelith.net>
* Copyright (C) 2019-2020 the OpenMiner contributors (see CONTRIBUTORS.md)
*
* This file is part of OpenMiner.
*
* OpenMiner is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* OpenMiner is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with OpenMiner; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* =====================================================================================
*/
#ifndef ROTATIONCOMPONENT_HPP_
#define ROTATIONCOMPONENT_HPP_
#include <glm/gtc/quaternion.hpp>
struct RotationComponent {
glm::quat quat;
bool isUpdated = true;
};
#endif // ROTATIONCOMPONENT_HPP_

View File

@ -115,9 +115,19 @@ void ServerCommandHandler::sendEntityDespawn(u32 entityID, const ClientInfo *cli
client->tcpSocket->send(packet);
}
void ServerCommandHandler::sendEntityPosUpdate(u32 entityID, double x, double y, double z, const ClientInfo *client) const {
void ServerCommandHandler::sendEntityPosition(u32 entityID, double x, double y, double z, const ClientInfo *client) const {
sf::Packet packet;
packet << Network::Command::EntityPosUpdate << entityID << x << y << z;
packet << Network::Command::EntityPosition << entityID << x << y << z;
if (!client)
m_server.sendToAllClients(packet);
else
client->tcpSocket->send(packet);
}
void ServerCommandHandler::sendEntityRotation(u32 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;
if (!client)
m_server.sendToAllClients(packet);

View File

@ -58,7 +58,8 @@ class ServerCommandHandler {
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 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 sendEntityDrawableDef(u32 entityID, DrawableDef &drawableDef, const ClientInfo *client = nullptr) const;
void setupCallbacks();

View File

@ -28,22 +28,36 @@
#include "NetworkComponent.hpp"
#include "NetworkController.hpp"
#include "PositionComponent.hpp"
#include "RotationComponent.hpp"
#include "ServerCommandHandler.hpp"
void NetworkController::update(entt::registry &registry) {
registry.view<NetworkComponent>().each([this] (auto, auto &network) {
if (!network.hasSpawned)
if (!network.hasSpawned) {
m_server->sendEntitySpawn(network.entityID);
network.hasSpawned = true;
}
});
registry.view<NetworkComponent, PositionComponent>().each([this] (auto, auto &network, auto &position) {
if (position.isUpdated)
m_server->sendEntityPosUpdate(network.entityID, position.x, position.y, position.z);
if (position.isUpdated) {
m_server->sendEntityPosition(network.entityID, position.x, position.y, position.z);
position.isUpdated = false;
}
});
registry.view<NetworkComponent, RotationComponent>().each([this] (auto, auto &network, auto &rotation) {
if (rotation.isUpdated) {
m_server->sendEntityRotation(network.entityID, rotation.quat.w, rotation.quat.x, rotation.quat.y, rotation.quat.z);
rotation.isUpdated = false;
}
});
registry.view<NetworkComponent, DrawableDef>().each([this] (auto, auto &network, auto &drawableDef) {
if (drawableDef.isUpdated)
if (drawableDef.isUpdated) {
m_server->sendEntityDrawableDef(network.entityID, drawableDef);
drawableDef.isUpdated = false;
}
});
}
@ -52,7 +66,11 @@ void NetworkController::sendEntities(entt::registry &registry, const ClientInfo
m_server->sendEntitySpawn(network.entityID, &client);
if (auto *position = registry.try_get<PositionComponent>(entity) ; position) {
m_server->sendEntityPosUpdate(network.entityID, position->x, position->y, position->z);
m_server->sendEntityPosition(network.entityID, position->x, position->y, position->z);
}
if (auto *rotation = registry.try_get<RotationComponent>(entity) ; rotation) {
m_server->sendEntityRotation(network.entityID, rotation->quat.w, rotation->quat.x, rotation->quat.y, rotation->quat.z);
}
if (auto *drawableDef = registry.try_get<DrawableDef>(entity) ; drawableDef) {