[ServerCommandHandler] Now supports teleportation command.

This commit is contained in:
Quentin Bazin 2020-02-22 01:44:00 +09:00
parent 3c4fae07f5
commit bb0c8e8d49
10 changed files with 82 additions and 18 deletions

View File

@ -66,6 +66,8 @@ class ClientPlayer : public Player {
float y() const { return m_y; }
float z() const { return m_z; }
void setPosition(float x, float y, float z);
const gk::Camera &camera() { return m_camera; }
private:

View File

@ -23,7 +23,11 @@
#include "ChatMessage.hpp"
ChatMessage::ChatMessage(u16 clientID, const std::string &message, u32 posY) {
m_text.setText("<Client " + std::to_string(clientID) + "> " + message);
if (clientID > 0)
m_text.setText("<Client " + std::to_string(clientID) + "> " + message);
else
m_text.setText(message);
m_text.setPosition(0, posY);
m_text.setBackgroundColor(gk::Color{0, 0, 0, 127});
m_text.setBackgroundSize(300, 10);

View File

@ -47,6 +47,7 @@ void ClientCommandHandler::sendPlayerPosUpdate() {
packet << m_player.Player::x();
packet << m_player.Player::y();
packet << m_player.Player::z();
packet << false;
m_client.send(packet);
}
@ -137,15 +138,16 @@ void ClientCommandHandler::setupCallbacks() {
m_client.setCommandCallback(Network::Command::PlayerPosUpdate, [this](sf::Packet &packet) {
s32 x, y, z;
u16 clientId;
bool isTeleportation;
packet >> clientId;
packet >> x >> y >> z;
packet >> isTeleportation;
if (clientId != m_client.id())
m_playerBoxes.at(clientId).setPosition(x, y, z);
// else {
// m_camera.setPosition(x, y, z);
// m_player.setPosition(x, y, z);
// }
else if (isTeleportation) {
m_player.setPosition(x, y, z);
}
});
m_client.setCommandCallback(Network::Command::PlayerSpawn, [this](sf::Packet &packet) {

View File

@ -37,17 +37,17 @@ ChatState::ChatState(ClientCommandHandler &clientCommandHandler, Chat &chat, gk:
m_drawBackground = false;
updateTextInputGeometry();
m_textInput.setScale(Config::guiScale, Config::guiScale);
m_textInput.setBackgroundColor(gk::Color{0, 0, 0, 127});
m_textInput.setPadding(1, 1);
updateTextInputGeometry();
m_chat.setMessageVisibility(true);
}
void ChatState::updateTextInputGeometry() {
m_textInput.setPosition(4, Config::screenHeight - 35);
m_textInput.setPosition(4, Config::screenHeight - 12 * Config::guiScale);
m_textInput.setBackgroundSize(Config::screenWidth / Config::guiScale - 4, 10);
}

View File

@ -143,6 +143,15 @@ void ClientPlayer::updatePosition(const ClientWorld &world) {
m_velocity.z = 0;
}
void ClientPlayer::setPosition(float x, float y, float z) {
m_x = x;
m_y = y;
m_z = z;
Player::setPosition(x, y, z);
m_camera.setPosition(x, y, z - 0.1);
}
void ClientPlayer::checkCollisions(const ClientWorld &world) {
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) || defined(__ANDROID__)
const float PLAYER_HEIGHT = 1.8;

View File

@ -44,7 +44,7 @@ namespace Network {
PlayerPlaceBlock = 7, // <TCP> [NetworkCommand][s32 x, y, z][u32 block] (from Client only)
PlayerDigBlock = 8, // <TCP> [NetworkCommand][s32 x, y, z] (from Client only)
PlayerInvUpdate = 9, // <TCP> [NetworkCommand][u16 client id][[std::string item][u16 amount][u8 x, y]...] (both) [FIXME]
PlayerPosUpdate = 10, // <TCP> [NetworkCommand][u16 client id][s32 x, y, z] (both) // FIXME
PlayerPosUpdate = 10, // <TCP> [NetworkCommand][u16 client id][s32 x, y, z][bool isTeleportation] (both) // FIXME
PlayerSpawn = 11, // <TCP> [NetworkCommand][u16 client id][s32 x, y, z] (from Server only)
PlayerInventory = 12, // <TCP> [NetworkCommand][u16 screenWidth, screenHeight][u8 guiScale] (from Client only)

View File

@ -44,7 +44,8 @@ class ServerCommandHandler {
void sendBlockDataUpdate(s32 x, s32 y, s32 z, const BlockData *blockData, const Client *client = nullptr) const;
void sendBlockInvUpdate(s32 x, s32 y, s32 z, const Inventory &inventory, const Client *client = nullptr) const;
void sendPlayerPosUpdate(u16 clientID, const ServerPlayer &player, const Client *client = nullptr) const;
void sendPlayerPosUpdate(u16 clientID, bool isTeleportation = false, const Client *client = nullptr) const;
void sendChatMessage(u16 clientID, const std::string &message, const Client *client = nullptr) const;
void setupCallbacks();

View File

@ -71,7 +71,7 @@ void ServerApplication::update() {
if (gk::GameClock::getTicks() % 1000 < 10) {
for (auto &it : m_players) {
m_serverCommandHandler.sendPlayerPosUpdate(it.first, it.second);
m_serverCommandHandler.sendPlayerPosUpdate(it.first);
}
}
}

View File

@ -50,11 +50,24 @@ void ServerCommandHandler::sendBlockInvUpdate(s32 x, s32 y, s32 z, const Invento
client->tcpSocket->send(packet);
}
void ServerCommandHandler::sendPlayerPosUpdate(u16 clientID, const ServerPlayer &player, const Client *client) const {
void ServerCommandHandler::sendPlayerPosUpdate(u16 clientID, bool isTeleportation, const Client *client) const {
const ServerPlayer &player = m_players.at(clientID);
sf::Packet packet;
packet << Network::Command::PlayerPosUpdate;
packet << clientID;
packet << player.x() << player.y() << player.z();
packet << isTeleportation;
if (!client)
m_server.sendToAllClients(packet);
else
client->tcpSocket->send(packet);
}
void ServerCommandHandler::sendChatMessage(u16 clientID, const std::string &message, const Client *client) const {
sf::Packet packet;
packet << Network::Command::ChatMessage << clientID << message;
if (!client)
m_server.sendToAllClients(packet);
@ -185,12 +198,45 @@ void ServerCommandHandler::setupCallbacks() {
}
});
m_server.setCommandCallback(Network::Command::ChatMessage, [this](Client &, sf::Packet &packet) {
// u16 id;
// std::string message;
// packet >> id >> message;
m_server.setCommandCallback(Network::Command::ChatMessage, [this](Client &client, sf::Packet &packet) {
u16 clientID;
std::string message;
packet >> clientID >> message;
m_server.sendToAllClients(packet);
if (message[0] != '/') {
sendChatMessage(clientID, message);
}
// FIXME: Do a proper implementation later
else {
std::stringstream sstream;
sstream << message.substr(1);
std::vector<std::string> command;
std::string line;
while (std::getline(sstream, line, ' ')) {
command.emplace_back(line);
}
if (!command.empty()) {
if (command.at(0) == "tp") {
if (command.size() != 4) {
// FIXME: ID 0 should be server messages
sendChatMessage(0, "Usage: /tp x y z", &client);
}
else {
s32 x = std::stoi(command.at(1));
s32 y = std::stoi(command.at(2));
s32 z = std::stoi(command.at(3));
m_players.at(clientID).setPosition(x, y, z);
sendPlayerPosUpdate(clientID, true);
sendChatMessage(0, "Teleported to " + std::to_string(x) + " " + std::to_string(y) + " " + std::to_string(z), &client);
}
}
}
}
});
}

View File

@ -23,7 +23,7 @@
#include "ServerInfo.hpp"
Client &ServerInfo::addClient(sf::IpAddress address, u16 port, const std::shared_ptr<sf::TcpSocket> &socket) {
m_clients.emplace_back(m_clients.size(), address, port, socket);
m_clients.emplace_back(m_clients.size() + 1, address, port, socket);
return m_clients.back();
}