world border: interpolation

This commit is contained in:
Bixilon 2022-04-26 14:10:57 +02:00
parent 09e8071262
commit 3f84b769ef
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
4 changed files with 49 additions and 3 deletions

View File

@ -184,6 +184,7 @@ class World(
chunk.tick(connection, chunkPosition)
}
chunks.lock.release()
border.tick()
}
fun randomTick() {

View File

@ -17,6 +17,8 @@ import de.bixilon.kotlinglm.vec2.Vec2d
import de.bixilon.kotlinglm.vec3.Vec3
import de.bixilon.kotlinglm.vec3.Vec3d
import de.bixilon.kotlinglm.vec3.Vec3i
import de.bixilon.kutil.math.interpolation.DoubleInterpolation.interpolateLinear
import de.bixilon.kutil.time.TimeUtil
import de.bixilon.minosoft.data.world.World
import de.bixilon.minosoft.gui.rendering.util.vec.vec2.Vec2dUtil.EMPTY
@ -27,7 +29,13 @@ class WorldBorder {
var warningBlocks = 0
var portalBound = 0
val state = WorldBorderState.STATIC
var state = WorldBorderState.STATIC
private set
private var lerpStart = -1L
private var lerpEnd = -1L
private var oldRadius = World.MAX_SIZE.toDouble()
private var newRadius = World.MAX_SIZE.toDouble()
fun isOutside(blockPosition: Vec3i): Boolean {
return isOutside(blockPosition.x.toDouble(), blockPosition.z.toDouble())
@ -51,7 +59,41 @@ class WorldBorder {
return true
}
fun lerp(oldRadius: Double, newRadius: Double, speed: Long) {
fun stopLerp() {
lerpStart = -1L
}
fun lerp(oldRadius: Double, newRadius: Double, speed: Long) {
val time = TimeUtil.millis
lerpStart = time
lerpEnd = time + speed
this.oldRadius = oldRadius
this.newRadius = newRadius
}
fun tick() {
if (lerpStart < 0L) {
return
}
val time = TimeUtil.millis
if (lerpEnd > time) {
state = WorldBorderState.STATIC
lerpStart = -1
return
}
val remaining = lerpEnd - time
val delta = (lerpEnd - lerpStart)
val oldRadius = radius
val radius = interpolateLinear(remaining.toDouble() / delta.toDouble(), this.oldRadius, this.newRadius)
this.radius = radius
state = if (oldRadius > radius) {
WorldBorderState.SHRINKING
} else {
WorldBorderState.GROWING
}
if (oldRadius == radius) {
lerpStart = -1
state = WorldBorderState.STATIC
}
}
}

View File

@ -13,6 +13,7 @@
package de.bixilon.minosoft.gui.rendering.world.border
import de.bixilon.kotlinglm.vec2.Vec2
import de.bixilon.kutil.latch.CountUpAndDownLatch
import de.bixilon.minosoft.data.registries.ResourceLocation
import de.bixilon.minosoft.data.text.RGBColor.Companion.asColor
@ -50,7 +51,6 @@ class WorldBorderRenderer(
override fun postInit(latch: CountUpAndDownLatch) {
renderWindow.textureManager.staticTextures.use(shader)
shader.setUInt("uIndexLayer", texture.renderData.shaderTextureId)
shader.setFloat("uRadius", 200.0f)
}
override fun setupTranslucent() {
@ -79,6 +79,8 @@ class WorldBorderRenderer(
WorldBorderState.STATIC -> STATIC_COLOR
}
shader.setRGBColor("uTintColor", color)
shader.setFloat("uRadius", border.radius.toFloat())
shader.setVec2("uCenter", Vec2(border.center))
}
override fun drawTranslucent() {

View File

@ -25,6 +25,7 @@ class SizeWorldBorderS2CP(buffer: PlayInByteBuffer) : WorldBorderS2CP {
val radius = buffer.readDouble()
override fun handle(connection: PlayConnection) {
connection.world.border.stopLerp()
connection.world.border.radius = radius
}