From c3af75874c9af69d54ee6f8f9bbc2a4c47e32b1d Mon Sep 17 00:00:00 2001 From: Bixilon Date: Wed, 2 Nov 2022 13:40:01 +0100 Subject: [PATCH] day phases --- .../RainGradientSetGameEventHandler.kt | 2 +- .../ThunderGradientSetGameEventHandler.kt | 2 +- .../rain/RainStartGameEventHandler.kt | 2 +- .../handlers/rain/RainStopGameEventHandler.kt | 2 +- .../minosoft/data/world/time/DayPhases.kt | 41 ++++++++++++++++++ .../minosoft/data/world/time/WorldTime.kt | 6 +-- .../data/world/weather/WorldWeather.kt | 9 ++-- .../gui/rendering/sky/box/SkyboxRenderer.kt | 43 ++++++++++++++++--- 8 files changed, 89 insertions(+), 18 deletions(-) create mode 100644 src/main/java/de/bixilon/minosoft/data/world/time/DayPhases.kt diff --git a/src/main/java/de/bixilon/minosoft/data/registries/other/game/event/handlers/gradients/RainGradientSetGameEventHandler.kt b/src/main/java/de/bixilon/minosoft/data/registries/other/game/event/handlers/gradients/RainGradientSetGameEventHandler.kt index e794a3cc4..275c0d562 100644 --- a/src/main/java/de/bixilon/minosoft/data/registries/other/game/event/handlers/gradients/RainGradientSetGameEventHandler.kt +++ b/src/main/java/de/bixilon/minosoft/data/registries/other/game/event/handlers/gradients/RainGradientSetGameEventHandler.kt @@ -22,6 +22,6 @@ object RainGradientSetGameEventHandler : GameEventHandler { override val RESOURCE_LOCATION: ResourceLocation = "minecraft:rain_gradient_set".toResourceLocation() override fun handle(data: Float, connection: PlayConnection) { - connection.world.weather = connection.world.weather.copy(rainGradient = data) + connection.world.weather = connection.world.weather.copy(rain = data) } } diff --git a/src/main/java/de/bixilon/minosoft/data/registries/other/game/event/handlers/gradients/ThunderGradientSetGameEventHandler.kt b/src/main/java/de/bixilon/minosoft/data/registries/other/game/event/handlers/gradients/ThunderGradientSetGameEventHandler.kt index 5c140cd7c..04777bdfa 100644 --- a/src/main/java/de/bixilon/minosoft/data/registries/other/game/event/handlers/gradients/ThunderGradientSetGameEventHandler.kt +++ b/src/main/java/de/bixilon/minosoft/data/registries/other/game/event/handlers/gradients/ThunderGradientSetGameEventHandler.kt @@ -22,6 +22,6 @@ object ThunderGradientSetGameEventHandler : GameEventHandler { override val RESOURCE_LOCATION: ResourceLocation = "minecraft:thunder_gradient_set".toResourceLocation() override fun handle(data: Float, connection: PlayConnection) { - connection.world.weather = connection.world.weather.copy(thunderGradient = data) + connection.world.weather = connection.world.weather.copy(thunder = data) } } diff --git a/src/main/java/de/bixilon/minosoft/data/registries/other/game/event/handlers/rain/RainStartGameEventHandler.kt b/src/main/java/de/bixilon/minosoft/data/registries/other/game/event/handlers/rain/RainStartGameEventHandler.kt index f69e74b7e..8e6440b81 100644 --- a/src/main/java/de/bixilon/minosoft/data/registries/other/game/event/handlers/rain/RainStartGameEventHandler.kt +++ b/src/main/java/de/bixilon/minosoft/data/registries/other/game/event/handlers/rain/RainStartGameEventHandler.kt @@ -22,6 +22,6 @@ object RainStartGameEventHandler : GameEventHandler { override val RESOURCE_LOCATION: ResourceLocation = "minecraft:rain_start".toResourceLocation() override fun handle(data: Float, connection: PlayConnection) { - connection.world.weather = connection.world.weather.copy(raining = true, rainGradient = 1.0f) + connection.world.weather = connection.world.weather.copy(rain = 1.0f) } } diff --git a/src/main/java/de/bixilon/minosoft/data/registries/other/game/event/handlers/rain/RainStopGameEventHandler.kt b/src/main/java/de/bixilon/minosoft/data/registries/other/game/event/handlers/rain/RainStopGameEventHandler.kt index b5e70a202..a55d5a386 100644 --- a/src/main/java/de/bixilon/minosoft/data/registries/other/game/event/handlers/rain/RainStopGameEventHandler.kt +++ b/src/main/java/de/bixilon/minosoft/data/registries/other/game/event/handlers/rain/RainStopGameEventHandler.kt @@ -22,6 +22,6 @@ object RainStopGameEventHandler : GameEventHandler { override val RESOURCE_LOCATION: ResourceLocation = "minecraft:rain_stop".toResourceLocation() override fun handle(data: Float, connection: PlayConnection) { - connection.world.weather = connection.world.weather.copy(raining = false, rainGradient = 0.0f) + connection.world.weather = connection.world.weather.copy(rain = 0.0f) } } diff --git a/src/main/java/de/bixilon/minosoft/data/world/time/DayPhases.kt b/src/main/java/de/bixilon/minosoft/data/world/time/DayPhases.kt new file mode 100644 index 000000000..af59c5289 --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/data/world/time/DayPhases.kt @@ -0,0 +1,41 @@ +/* + * Minosoft + * Copyright (C) 2020-2022 Moritz Zwerger + * + * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with this program. If not, see . + * + * This software is not affiliated with Mojang AB, the original developer of Minecraft. + */ + +package de.bixilon.minosoft.data.world.time + +enum class DayPhases(private val progressCalculator: (Int) -> Float) { + SUNRISE({ (it - 23000) / 1000.0f }), + DAY({ it / 12000.0f }), + SUNSET({ (it - 12000) / 1000.0f }), // I love this song: https://www.youtube.com/watch?v=URma_gu1aNE + NIGHT({ (it - 13000) / 10000.9f }), + ; + + fun getProgress(time: Int): Float { + return progressCalculator(time) + } + + companion object { + fun of(time: Int): DayPhases { + if (time > 23000) { + return SUNRISE + } + if (time < 12000) { + return DAY + } + if (time in 12000 until 13000) { + return SUNSET + } + return NIGHT + } + } +} diff --git a/src/main/java/de/bixilon/minosoft/data/world/time/WorldTime.kt b/src/main/java/de/bixilon/minosoft/data/world/time/WorldTime.kt index 3f2651a96..8765dca8c 100644 --- a/src/main/java/de/bixilon/minosoft/data/world/time/WorldTime.kt +++ b/src/main/java/de/bixilon/minosoft/data/world/time/WorldTime.kt @@ -30,6 +30,7 @@ class WorldTime( val cycling = time >= 0 val moonPhase = MoonPhases[age.toInt() % ProtocolDefinition.TICKS_PER_DAY % MoonPhases.VALUES.size] // ToDo: Verify + val phase = DayPhases.of(time) val skyAngle: Float @@ -46,9 +47,8 @@ class WorldTime( base = base.clamp(0.0, 1.0) base = 1.0 - base - base *= 1.0 - ((world.weather.rainGradient * 5.0) / 16.0) - base *= 1.0 - (((world.weather.thunderGradient * world.weather.rainGradient) * 5.0) / 16.0) + base *= 1.0 - ((world.weather.rain * 5.0) / 16.0) + base *= 1.0 - (((world.weather.thunder * world.weather.rain) * 5.0) / 16.0) return base * 0.8 + 0.2 } - } diff --git a/src/main/java/de/bixilon/minosoft/data/world/weather/WorldWeather.kt b/src/main/java/de/bixilon/minosoft/data/world/weather/WorldWeather.kt index 15b8a41ee..e0d8846e0 100644 --- a/src/main/java/de/bixilon/minosoft/data/world/weather/WorldWeather.kt +++ b/src/main/java/de/bixilon/minosoft/data/world/weather/WorldWeather.kt @@ -14,7 +14,8 @@ package de.bixilon.minosoft.data.world.weather data class WorldWeather( - val raining: Boolean = false, - val rainGradient: Float = 0.0f, - val thunderGradient: Float = 0.0f, -) + val rain: Float = 0.0f, + val thunder: Float = 0.0f, +) { + val raining: Boolean get() = rain > 0.0f +} diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/sky/box/SkyboxRenderer.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/sky/box/SkyboxRenderer.kt index fb9dee77d..01872eb4d 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/sky/box/SkyboxRenderer.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/sky/box/SkyboxRenderer.kt @@ -21,6 +21,7 @@ import de.bixilon.minosoft.data.text.formatting.color.ChatColors import de.bixilon.minosoft.data.text.formatting.color.RGBColor import de.bixilon.minosoft.data.text.formatting.color.RGBColor.Companion.asColor import de.bixilon.minosoft.data.world.positions.ChunkPositionUtil.chunkPosition +import de.bixilon.minosoft.data.world.time.DayPhases import de.bixilon.minosoft.data.world.time.WorldTime import de.bixilon.minosoft.gui.rendering.sky.SkyChildRenderer import de.bixilon.minosoft.gui.rendering.sky.SkyRenderer @@ -123,6 +124,30 @@ class SkyboxRenderer( return RGBColor(red / count, green / count, blue / count) } + private fun calculateThunder(rain: Float, thunder: Float): RGBColor { + return ChatColors.DARK_GRAY + } + + private fun calculateRain(rain: Float): RGBColor { + return ChatColors.GRAY + } + + private fun calculateSunrise(progress: Float): RGBColor { + return ChatColors.YELLOW + } + + private fun calculateDaytime(progress: Float): RGBColor { + return ChatColors.BLUE + } + + private fun calculateSunset(progress: Float): RGBColor { + return ChatColors.RED + } + + private fun calculateNight(progress: Float): RGBColor { + return ChatColors.DARK_BLUE + } + private fun calculateSkyColor(): RGBColor? { val properties = sky.properties if (properties.fixedTexture != null) { @@ -135,18 +160,22 @@ class SkyboxRenderer( } val weather = sky.renderWindow.connection.world.weather - if (weather.thunderGradient > 0.0f) { - return ChatColors.DARK_GRAY + if (weather.thunder > 0.0f) { + return calculateThunder(weather.rain, weather.thunder) } if (weather.raining) { - return ChatColors.GRAY + return calculateRain(weather.rain) } - if (time.time in 13000..23000) { - return ChatColors.DARK_BLUE - } + val phase = time.phase + val progress = phase.getProgress(time.time) - return ChatColors.BLUE + return when (phase) { + DayPhases.SUNRISE -> calculateSunrise(progress) + DayPhases.DAY -> calculateDaytime(progress) + DayPhases.SUNSET -> calculateSunset(progress) + DayPhases.NIGHT -> calculateNight(progress) + } } companion object {