From 56d942432b08454c9aaab26520da82e90876b0a9 Mon Sep 17 00:00:00 2001 From: Bixilon Date: Mon, 22 Mar 2021 15:09:43 +0100 Subject: [PATCH] improve code a bit --- .../de/bixilon/minosoft/data/Directions.kt | 9 +++- .../gui/main/StartProgressWindow.java | 2 +- .../chunk/models/renderable/FluidRenderer.kt | 52 ++++++++++++------- 3 files changed, 42 insertions(+), 21 deletions(-) diff --git a/src/main/java/de/bixilon/minosoft/data/Directions.kt b/src/main/java/de/bixilon/minosoft/data/Directions.kt index a3fb2e4f5..1d4e12515 100644 --- a/src/main/java/de/bixilon/minosoft/data/Directions.kt +++ b/src/main/java/de/bixilon/minosoft/data/Directions.kt @@ -22,7 +22,8 @@ enum class Directions(direction: Vec3) { WEST(Vec3(-1, 0, 0)), EAST(Vec3(1, 0, 0)); - val inverse: Directions = inverse() + lateinit var inverse: Directions + private set private fun inverse(): Directions { val ordinal = ordinal @@ -69,5 +70,11 @@ enum class Directions(direction: Vec3) { } return minDirection } + + init { + for (direction in DIRECTIONS) { + direction.inverse = direction.inverse() + } + } } } diff --git a/src/main/java/de/bixilon/minosoft/gui/main/StartProgressWindow.java b/src/main/java/de/bixilon/minosoft/gui/main/StartProgressWindow.java index 478c324b1..e84910891 100644 --- a/src/main/java/de/bixilon/minosoft/gui/main/StartProgressWindow.java +++ b/src/main/java/de/bixilon/minosoft/gui/main/StartProgressWindow.java @@ -88,7 +88,7 @@ public class StartProgressWindow extends Application { }); } hideDialog(); - }).start(); + }, "JavaFX Launch Thread").start(); } public static void start() throws InterruptedException { diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/chunk/models/renderable/FluidRenderer.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/chunk/models/renderable/FluidRenderer.kt index 0f32a85dd..a5e9f435a 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/chunk/models/renderable/FluidRenderer.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/chunk/models/renderable/FluidRenderer.kt @@ -18,7 +18,11 @@ import glm_.vec2.Vec2 import glm_.vec3.Vec3 import glm_.vec4.Vec4 -class FluidRenderer(private val stillTextureName: String, private val flowingTextureName: String, val regex: String): BlockRenderInterface { +class FluidRenderer( + private val stillTextureName: String, + private val flowingTextureName: String, + private val regex: String, +) : BlockRenderInterface { override val fullFaceDirections: Array = arrayOfNulls(Directions.DIRECTIONS.size) override val transparentFaces: Array = arrayOfNulls(Directions.DIRECTIONS.size) private var still: Texture? = null @@ -29,11 +33,17 @@ class FluidRenderer(private val stillTextureName: String, private val flowingTex val lightLevel = lightAccessor.getLightLevel(position) val heights = calculateHeights(neighbourBlocks, blockState, world, position) val isFlowing = isLiquidFlowing(heights) - val (texture, angle) = if (isFlowing) { - Pair(flowing, getRotationAngle(heights)) + + val texture: Texture + val angle: Float + if (isFlowing) { + texture = flowing!! + angle = getRotationAngle(heights) } else { - Pair(still, 0f) + texture = still!! + angle = 0f } + val positions = calculatePositions(heights) for (direction in Directions.DIRECTIONS) { if (isBlockSameFluid(neighbourBlocks[direction.ordinal]) || neighbourBlocks[direction.ordinal]?.getBlockRenderer(position + direction)?.fullFaceDirections?.let { it[direction.inverse.ordinal] != null } == true && direction != Directions.UP) { @@ -46,7 +56,7 @@ class FluidRenderer(private val stillTextureName: String, private val flowingTex face.rotate(angle) val positionTemplate = BlockModelElement.FACE_POSITION_MAP_TEMPLATE[direction.ordinal] val drawPositions = arrayOf(positions[positionTemplate[0]], positions[positionTemplate[1]], positions[positionTemplate[2]], positions[positionTemplate[3]]) - createQuad(drawPositions, face.getTexturePositionArray(direction), texture!!, modelMatrix, mesh, tintColor, lightLevel) + createQuad(drawPositions, face.getTexturePositionArray(direction), texture, modelMatrix, mesh, tintColor, lightLevel) } } @@ -67,20 +77,24 @@ class FluidRenderer(private val stillTextureName: String, private val flowingTex } val minHeight = heights.minOrNull() val position = heights.indexOfFirst { it == minHeight } - val directions = HEIGHT_POSITIONS_REVERSED[position] + val directions = HEIGHT_POSITIONS_REVERSED[position]!! var angle = 0f - for (direction in directions!!) { + for (direction in directions) { angle += getRotationAngle(direction) } - return if (position == 1) { angle / directions.size } else { angle / directions.size + glm.PI.toFloat() } + return if (position == 1) { + angle / directions.size + } else { + angle / directions.size + glm.PIf + } } private fun getRotationAngle(direction: Directions): Float { return when (direction) { - Directions.SOUTH -> glm.PI.toFloat() + Directions.SOUTH -> glm.PIf Directions.NORTH -> 0f - Directions.WEST -> glm.PI.toFloat() * 0.5f - Directions.EAST -> glm.PI.toFloat() * 1.5f + Directions.WEST -> glm.PIf * 0.5f + Directions.EAST -> glm.PIf * 1.5f else -> error("Unexpected value: $direction") } } @@ -207,15 +221,15 @@ class FluidRenderer(private val stillTextureName: String, private val flowingTex ElementRenderer.POSITION_2, ElementRenderer.POSITION_3, ElementRenderer.POSITION_4 - ) - - val HEIGHT_POSITIONS = mapOf( - Pair(setOf(Directions.NORTH, Directions.WEST), 0), - Pair(setOf(Directions.NORTH, Directions.EAST), 1), - Pair(setOf(Directions.SOUTH, Directions.WEST), 2), - Pair(setOf(Directions.SOUTH, Directions.EAST), 3), ) - val HEIGHT_POSITIONS_REVERSED = HEIGHT_POSITIONS.entries.associate{(k,v)-> v to k} + val HEIGHT_POSITIONS = mapOf( + setOf(Directions.NORTH, Directions.WEST) to 0, + setOf(Directions.NORTH, Directions.EAST) to 1, + setOf(Directions.SOUTH, Directions.WEST) to 2, + setOf(Directions.SOUTH, Directions.EAST) to 3, + ) + + val HEIGHT_POSITIONS_REVERSED = HEIGHT_POSITIONS.entries.associate { (k, v) -> v to k } } }