From 30e3ae2054c62f939309ed318b963226fbac99b0 Mon Sep 17 00:00:00 2001 From: Quentin Bazin Date: Fri, 17 Jul 2020 03:28:55 +0200 Subject: [PATCH] [ChatCommandHandler] '/time ' command added. [CelestialObject|Skybox] Moon changes its phase everyday. --- source/client/graphics/CelestialObject.cpp | 4 +- source/client/graphics/CelestialObject.hpp | 4 +- source/client/graphics/Skybox.cpp | 2 + source/client/hud/DebugOverlay.cpp | 2 + source/common/core/GameTime.cpp | 17 +++---- source/common/core/GameTime.hpp | 8 +++- source/server/network/ChatCommandHandler.cpp | 48 +++++++++++++++++++- source/server/network/ChatCommandHandler.hpp | 6 ++- 8 files changed, 71 insertions(+), 20 deletions(-) diff --git a/source/client/graphics/CelestialObject.cpp b/source/client/graphics/CelestialObject.cpp index d639d8fc..dc5d8e86 100644 --- a/source/client/graphics/CelestialObject.cpp +++ b/source/client/graphics/CelestialObject.cpp @@ -69,8 +69,8 @@ void CelestialObject::updateVertexBuffer() const { if (m_phaseCount && m_phaseSize && m_currentPhase < m_phaseCount) { u16 currentPhaseX = m_currentPhase % (m_texture->getSize().x / m_phaseSize); u16 currentPhaseY = m_currentPhase / (m_texture->getSize().x / m_phaseSize); - texRect.x = currentPhaseX / float(m_texture->getSize().x); - texRect.y = currentPhaseY / float(m_texture->getSize().y); + texRect.x = currentPhaseX * m_phaseSize / float(m_texture->getSize().x); + texRect.y = currentPhaseY * m_phaseSize / float(m_texture->getSize().y); texRect.sizeX = m_phaseSize / float(m_texture->getSize().x); texRect.sizeY = m_phaseSize / float(m_texture->getSize().y); } diff --git a/source/client/graphics/CelestialObject.hpp b/source/client/graphics/CelestialObject.hpp index b2fccf76..fa1c90f9 100644 --- a/source/client/graphics/CelestialObject.hpp +++ b/source/client/graphics/CelestialObject.hpp @@ -43,7 +43,7 @@ class CelestialObject : public gk::Drawable, public gk::Transformable { void setSize(float width, float height) { m_width = width; m_height = height; m_isUpdateNeeded = true; } void setTexture(const std::string &textureName); void setPhaseCount(u16 phaseCount, u16 phaseSize) { m_phaseCount = phaseCount; m_phaseSize = phaseSize; m_isUpdateNeeded = true; } - void setCurrentPhase(u16 currentPhase) { m_currentPhase = currentPhase; m_isUpdateNeeded = true; } + void setCurrentPhase(u16 currentPhase) const { if (m_currentPhase != currentPhase) { m_currentPhase = currentPhase; m_isUpdateNeeded = true; } } void setRotationOffset(u16 rotationOffset) { m_rotationOffset = rotationOffset; } void setRotationSpeed(float rotationSpeed) { m_rotationSpeed = rotationSpeed; } void setRotationAxis(const gk::Vector3f &rotationAxis) { m_rotationAxis = rotationAxis; } @@ -66,7 +66,7 @@ class CelestialObject : public gk::Drawable, public gk::Transformable { u16 m_phaseCount = 0; u16 m_phaseSize = 0; - u16 m_currentPhase = 0; + mutable u16 m_currentPhase = 0; u16 m_rotationOffset = 0; float m_rotationSpeed = 1.f; diff --git a/source/client/graphics/Skybox.cpp b/source/client/graphics/Skybox.cpp index b38cdca1..99835099 100644 --- a/source/client/graphics/Skybox.cpp +++ b/source/client/graphics/Skybox.cpp @@ -63,6 +63,8 @@ void Skybox::draw(gk::RenderTarget &target, gk::RenderStates states) const { gk::Shader::bind(nullptr); } + m_moon.setCurrentPhase((GameTime::getTicks() / 24000) % 8); + states.shader = &m_shader; // Subtract the camera position - see comment in ClientWorld::draw() diff --git a/source/client/hud/DebugOverlay.cpp b/source/client/hud/DebugOverlay.cpp index 9e31cd5d..15ef4b30 100644 --- a/source/client/hud/DebugOverlay.cpp +++ b/source/client/hud/DebugOverlay.cpp @@ -92,8 +92,10 @@ void DebugOverlay::update() { stream << '\n'; stream << "Game time: "; + u32 day = GameTime::getCurrentDay(); u16 hour = GameTime::getCurrentHour(); u16 minute = GameTime::getCurrentMinute(); + stream << "Day " << day << " "; stream << (hour < 10 ? "0" : "") << hour << ":"; stream << (minute < 10 ? "0" : "") << minute; diff --git a/source/common/core/GameTime.cpp b/source/common/core/GameTime.cpp index 969fcf6d..ce5749de 100644 --- a/source/common/core/GameTime.cpp +++ b/source/common/core/GameTime.cpp @@ -55,23 +55,18 @@ gk::Color GameTime::getSkyColorFromTime(const Sky &sky, float time) { return skyColor; } -void GameTime::incrementTicks() { - ++s_ticks; - - updateTpsCounter(); -} - void GameTime::updateTpsCounter() { static u64 tpsTimer = gk::GameClock::getInstance().getTicks(true); - static u8 tpsCount = 0; + static u64 tpsStart = s_ticks; + + if (tpsStart > s_ticks) + tpsStart = s_ticks; u64 currentClockTicks = gk::GameClock::getInstance().getTicks(true); - ++tpsCount; - if (currentClockTicks - tpsTimer > 1000) { - s_ticksPerSecond = floor(tpsCount / ((currentClockTicks - tpsTimer) / 1000.0f) + 0.5f); + s_ticksPerSecond = floor((s_ticks - tpsStart) / ((currentClockTicks - tpsTimer) / 1000.0f) + 0.5f); tpsTimer = currentClockTicks; - tpsCount = 0; + tpsStart = s_ticks; } } diff --git a/source/common/core/GameTime.hpp b/source/common/core/GameTime.hpp index 8db896c9..f9139cee 100644 --- a/source/common/core/GameTime.hpp +++ b/source/common/core/GameTime.hpp @@ -42,12 +42,16 @@ class GameTime { static float getSunlightIntensityFromTime(float time); static gk::Color getSkyColorFromTime(const Sky &sky, float time); - static void incrementTicks(); - static void setTicks(u64 ticks) { s_ticks = ticks; } + static void incrementTicks() { ++s_ticks; updateTpsCounter(); } + static void setTicks(u64 ticks) { s_ticks = ticks; updateTpsCounter(); } static u16 getTicksPerSecond() { return s_ticksPerSecond; } static u64 getTicks() { return s_ticks; } + static u32 getCurrentDay() { + return (s_ticks + dayStartOffset + 3000.f) / 1000.f / 24 + 1; + } + static u8 getCurrentHour() { return u64((s_ticks + dayStartOffset + 3000.f) / 1000.f) % 24; } diff --git a/source/server/network/ChatCommandHandler.cpp b/source/server/network/ChatCommandHandler.cpp index 5a37e9fc..72471593 100644 --- a/source/server/network/ChatCommandHandler.cpp +++ b/source/server/network/ChatCommandHandler.cpp @@ -123,7 +123,53 @@ void ChatCommandHandler::stopCommand(const std::vector &command, Cl } } -void ChatCommandHandler::teleportationCommand(const std::vector &command, ClientInfo &client) const { +void ChatCommandHandler::timeCommand(const std::vector &command, ClientInfo &client) const { + if (command.size() != 3 || (command.at(1) != "set" && command.at(1) != "add")) { + m_server.sendChatMessage(0, "Usage: /time ", &client); + } + else if (command.at(1) == "set") { + static const std::unordered_map values = { + {"day", 1000}, + {"noon", 6000}, + {"sunset", 12000}, + {"night", 13000}, + {"midnight", 18000}, + {"sunrise", 23000}, + }; + + if (auto it = values.find(command.at(2)) ; it != values.end()) { + GameTime::setTicks(it->second); + + m_server.sendChatMessage(0, "Time set to " + std::to_string(it->second), &client); + } + else { + try { + u64 ticks = std::stoull(command.at(2)); + + GameTime::setTicks(ticks); + + m_server.sendChatMessage(0, "Time set to " + std::to_string(ticks), &client); + } + catch (std::out_of_range &e) { + m_server.sendChatMessage(0, "Invalid time", &client); + } + } + } + else if (command.at(1) == "add") { + try { + u64 ticks = std::stoull(command.at(2)); + + GameTime::setTicks(GameTime::getTicks() + ticks); + + m_server.sendChatMessage(0, "Added " + std::to_string(ticks) + " to the time", &client); + } + catch (std::out_of_range &e) { + m_server.sendChatMessage(0, "Invalid time", &client); + } + } +} + +void ChatCommandHandler::tpCommand(const std::vector &command, ClientInfo &client) const { if (command.size() != 4) { m_server.sendChatMessage(0, "Usage: /tp x y z", &client); } diff --git a/source/server/network/ChatCommandHandler.hpp b/source/server/network/ChatCommandHandler.hpp index 98f56bda..023095c8 100644 --- a/source/server/network/ChatCommandHandler.hpp +++ b/source/server/network/ChatCommandHandler.hpp @@ -48,7 +48,8 @@ class ChatCommandHandler { void helpCommand(const std::vector &command, ClientInfo &client) const; void optionCommand(const std::vector &command, ClientInfo &client) const; void stopCommand(const std::vector &command, ClientInfo &client) const; - void teleportationCommand(const std::vector &command, ClientInfo &client) const; + void timeCommand(const std::vector &command, ClientInfo &client) const; + void tpCommand(const std::vector &command, ClientInfo &client) const; void tpsCommand(const std::vector &command, ClientInfo &client) const; ServerCommandHandler &m_server; @@ -58,7 +59,8 @@ class ChatCommandHandler { {"help", &ChatCommandHandler::helpCommand}, {"option", &ChatCommandHandler::optionCommand}, {"stop", &ChatCommandHandler::stopCommand}, - {"tp", &ChatCommandHandler::teleportationCommand}, + {"time", &ChatCommandHandler::timeCommand}, + {"tp", &ChatCommandHandler::tpCommand}, {"tps", &ChatCommandHandler::tpsCommand}, }; };