diff --git a/client/include/world/ClientPlayer.hpp b/client/include/world/ClientPlayer.hpp index 751dbca07..613bd910d 100644 --- a/client/include/world/ClientPlayer.hpp +++ b/client/include/world/ClientPlayer.hpp @@ -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: diff --git a/client/source/hud/ChatMessage.cpp b/client/source/hud/ChatMessage.cpp index 3da918fd9..7ef2d5cec 100644 --- a/client/source/hud/ChatMessage.cpp +++ b/client/source/hud/ChatMessage.cpp @@ -23,7 +23,11 @@ #include "ChatMessage.hpp" ChatMessage::ChatMessage(u16 clientID, const std::string &message, u32 posY) { - m_text.setText(" " + message); + if (clientID > 0) + m_text.setText(" " + 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); diff --git a/client/source/network/ClientCommandHandler.cpp b/client/source/network/ClientCommandHandler.cpp index f45d7d625..6af9be5b7 100644 --- a/client/source/network/ClientCommandHandler.cpp +++ b/client/source/network/ClientCommandHandler.cpp @@ -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) { diff --git a/client/source/states/ChatState.cpp b/client/source/states/ChatState.cpp index 06079d177..aef5f2ad7 100644 --- a/client/source/states/ChatState.cpp +++ b/client/source/states/ChatState.cpp @@ -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); } diff --git a/client/source/world/ClientPlayer.cpp b/client/source/world/ClientPlayer.cpp index 690573a1d..2fb909e5a 100644 --- a/client/source/world/ClientPlayer.cpp +++ b/client/source/world/ClientPlayer.cpp @@ -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; diff --git a/common/include/network/Network.hpp b/common/include/network/Network.hpp index 12cb6aeec..f2e397fdb 100644 --- a/common/include/network/Network.hpp +++ b/common/include/network/Network.hpp @@ -44,7 +44,7 @@ namespace Network { PlayerPlaceBlock = 7, // [NetworkCommand][s32 x, y, z][u32 block] (from Client only) PlayerDigBlock = 8, // [NetworkCommand][s32 x, y, z] (from Client only) PlayerInvUpdate = 9, // [NetworkCommand][u16 client id][[std::string item][u16 amount][u8 x, y]...] (both) [FIXME] - PlayerPosUpdate = 10, // [NetworkCommand][u16 client id][s32 x, y, z] (both) // FIXME + PlayerPosUpdate = 10, // [NetworkCommand][u16 client id][s32 x, y, z][bool isTeleportation] (both) // FIXME PlayerSpawn = 11, // [NetworkCommand][u16 client id][s32 x, y, z] (from Server only) PlayerInventory = 12, // [NetworkCommand][u16 screenWidth, screenHeight][u8 guiScale] (from Client only) diff --git a/server/include/network/ServerCommandHandler.hpp b/server/include/network/ServerCommandHandler.hpp index b56ccf8c6..e981b5fe1 100644 --- a/server/include/network/ServerCommandHandler.hpp +++ b/server/include/network/ServerCommandHandler.hpp @@ -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(); diff --git a/server/source/core/ServerApplication.cpp b/server/source/core/ServerApplication.cpp index 2ab5eb728..6b110ebc6 100644 --- a/server/source/core/ServerApplication.cpp +++ b/server/source/core/ServerApplication.cpp @@ -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); } } } diff --git a/server/source/network/ServerCommandHandler.cpp b/server/source/network/ServerCommandHandler.cpp index 7c13a1541..ceea3d058 100644 --- a/server/source/network/ServerCommandHandler.cpp +++ b/server/source/network/ServerCommandHandler.cpp @@ -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 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); + } + } + } + } }); } diff --git a/server/source/network/ServerInfo.cpp b/server/source/network/ServerInfo.cpp index 4dafbee39..9ee896745 100644 --- a/server/source/network/ServerInfo.cpp +++ b/server/source/network/ServerInfo.cpp @@ -23,7 +23,7 @@ #include "ServerInfo.hpp" Client &ServerInfo::addClient(sf::IpAddress address, u16 port, const std::shared_ptr &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(); }