[ChatCommandHandler] '/time <add|set>' command added.

[CelestialObject|Skybox] Moon changes its phase everyday.
This commit is contained in:
Quentin Bazin 2020-07-17 03:28:55 +02:00
parent 0f015ee75d
commit 30e3ae2054
8 changed files with 71 additions and 20 deletions

View File

@ -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);
}

View File

@ -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;

View File

@ -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()

View File

@ -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;

View File

@ -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;
}
}

View File

@ -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;
}

View File

@ -123,7 +123,53 @@ void ChatCommandHandler::stopCommand(const std::vector<std::string> &command, Cl
}
}
void ChatCommandHandler::teleportationCommand(const std::vector<std::string> &command, ClientInfo &client) const {
void ChatCommandHandler::timeCommand(const std::vector<std::string> &command, ClientInfo &client) const {
if (command.size() != 3 || (command.at(1) != "set" && command.at(1) != "add")) {
m_server.sendChatMessage(0, "Usage: /time <set|add> <value>", &client);
}
else if (command.at(1) == "set") {
static const std::unordered_map<std::string, u64> 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<std::string> &command, ClientInfo &client) const {
if (command.size() != 4) {
m_server.sendChatMessage(0, "Usage: /tp x y z", &client);
}

View File

@ -48,7 +48,8 @@ class ChatCommandHandler {
void helpCommand(const std::vector<std::string> &command, ClientInfo &client) const;
void optionCommand(const std::vector<std::string> &command, ClientInfo &client) const;
void stopCommand(const std::vector<std::string> &command, ClientInfo &client) const;
void teleportationCommand(const std::vector<std::string> &command, ClientInfo &client) const;
void timeCommand(const std::vector<std::string> &command, ClientInfo &client) const;
void tpCommand(const std::vector<std::string> &command, ClientInfo &client) const;
void tpsCommand(const std::vector<std::string> &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},
};
};