From 192144cc2708df04a7456d28a50ec338fcf54e09 Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Mon, 30 Jan 2023 14:08:01 +0700 Subject: [PATCH] Implement some logic for three rotation freedom of blocks, make holo sign have three rotation freedoms --- .../blocks/MatteryBlockStateProvider.kt | 5 +++ .../mc/otm/block/RotatableMatteryBlock.kt | 27 ++++++++++--- .../mc/otm/block/decorative/HoloSignBlock.kt | 2 +- .../render/blockentity/HoloSignRenderer.kt | 3 +- .../mc/otm/core/math/BlockRotation.kt | 39 ++++++++++++++++--- .../mc/otm/core/math/BlockRotationFreedom.kt | 2 - .../mc/otm/core/math/EuclidMath.kt | 4 ++ 7 files changed, 66 insertions(+), 16 deletions(-) diff --git a/src/data/kotlin/ru/dbotthepony/mc/otm/datagen/blocks/MatteryBlockStateProvider.kt b/src/data/kotlin/ru/dbotthepony/mc/otm/datagen/blocks/MatteryBlockStateProvider.kt index e41abe868..ed3aba803 100644 --- a/src/data/kotlin/ru/dbotthepony/mc/otm/datagen/blocks/MatteryBlockStateProvider.kt +++ b/src/data/kotlin/ru/dbotthepony/mc/otm/datagen/blocks/MatteryBlockStateProvider.kt @@ -31,6 +31,11 @@ private fun initialTransform(it: BlockState, modelPath: String, builder: Configu builder.rotationX(it.primary.toXRotBlockstate()) } + it.getValueNullable(BlockRotationFreedom.THREE.property)?.let { + builder.rotationY(it.primary.toYRotBlockstate() + (it.secondary?.toYRotBlockstate() ?: 0)) + builder.rotationX(it.primary.toXRotBlockstate()) + } + it.getValueNullable(WorkerState.WORKER_STATE)?.let { modelPath += "_" + it.name.lowercase() } diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/block/RotatableMatteryBlock.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/block/RotatableMatteryBlock.kt index 248123fbf..fa7689090 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/block/RotatableMatteryBlock.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/block/RotatableMatteryBlock.kt @@ -1,5 +1,6 @@ package ru.dbotthepony.mc.otm.block +import net.minecraft.core.Direction import net.minecraft.world.item.context.BlockPlaceContext import net.minecraft.world.level.block.Block import net.minecraft.world.level.block.Rotation @@ -20,16 +21,30 @@ abstract class RotatableMatteryBlock @JvmOverloads constructor(properties: Prope } override fun getStateForPlacement(context: BlockPlaceContext): BlockState? { - if (rotationFreedom() == BlockRotationFreedom.ONE) { - return defaultBlockState().setValue( - rotationProperty, + return when (val freedom = rotationFreedom()) { + BlockRotationFreedom.ONE -> defaultBlockState().setValue( + freedom.property, BlockRotation.of(if (faceToPlayer(context)) context.horizontalDirection.opposite else context.horizontalDirection) ) - } else { - return defaultBlockState().setValue( - rotationProperty, + + BlockRotationFreedom.TWO -> defaultBlockState().setValue( + freedom.property, BlockRotation.of(if (faceToPlayer(context)) context.nearestLookingDirection.opposite else context.nearestLookingDirection) ) + + BlockRotationFreedom.THREE -> { + val primary = if (faceToPlayer(context)) context.nearestLookingDirection.opposite else context.nearestLookingDirection + var secondary = if (faceToPlayer(context)) context.horizontalDirection else context.horizontalDirection.opposite + + if (primary == Direction.DOWN) { + secondary = secondary.opposite + } + + defaultBlockState().setValue( + freedom.property, + BlockRotation.ofSafe(primary, secondary) + ) + } } } diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/block/decorative/HoloSignBlock.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/block/decorative/HoloSignBlock.kt index 80c0b51db..92f861ff4 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/block/decorative/HoloSignBlock.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/block/decorative/HoloSignBlock.kt @@ -15,7 +15,7 @@ import ru.dbotthepony.mc.otm.shapes.BlockShapes class HoloSignBlock : RotatableMatteryBlock(), EntityBlock { override fun rotationFreedom(): BlockRotationFreedom { - return BlockRotationFreedom.TWO + return BlockRotationFreedom.THREE } override fun newBlockEntity(p_153215_: BlockPos, p_153216_: BlockState): BlockEntity { diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/client/render/blockentity/HoloSignRenderer.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/client/render/blockentity/HoloSignRenderer.kt index 1c89e54de..5a5ed2bc9 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/client/render/blockentity/HoloSignRenderer.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/client/render/blockentity/HoloSignRenderer.kt @@ -15,6 +15,7 @@ import ru.dbotthepony.mc.otm.core.get import ru.dbotthepony.mc.otm.core.math.RGBAColor import ru.dbotthepony.mc.otm.core.math.facingTwo import ru.dbotthepony.mc.otm.core.math.rotateWithBlockFacing +import ru.dbotthepony.mc.otm.core.math.rotationThree class HoloSignRenderer(private val context: BlockEntityRendererProvider.Context) : BlockEntityRenderer { override fun render( @@ -29,7 +30,7 @@ class HoloSignRenderer(private val context: BlockEntityRendererProvider.Context) return poseStack.pushPose() - poseStack.rotateWithBlockFacing(tile.blockState.facingTwo) + poseStack.rotateWithBlockFacing(tile.blockState.rotationThree) poseStack.translate(0.5f, 0.5f, 0.6f) poseStack.scale(0.01f, 0.01f, 0.01f) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/core/math/BlockRotation.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/core/math/BlockRotation.kt index b67604aa2..b77eec949 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/core/math/BlockRotation.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/core/math/BlockRotation.kt @@ -24,20 +24,21 @@ operator fun BlockPos.plus(other: BlockRotation): BlockPos { return this + other.normal } +/** + * [secondary] clarifies about block's top facing direction, NOT bottom + */ enum class BlockRotation(val primary: Direction, val secondary: Direction?) : StringRepresentable { - DOWN(Direction.DOWN, null), - UP(Direction.UP, null), + DOWN(Direction.DOWN, Direction.NORTH), + UP(Direction.UP, Direction.NORTH), NORTH(Direction.NORTH, null), SOUTH(Direction.SOUTH, null), WEST(Direction.WEST, null), EAST(Direction.EAST, null), - DOWN_NORTH(Direction.DOWN, Direction.NORTH), DOWN_SOUTH(Direction.DOWN, Direction.SOUTH), DOWN_WEST(Direction.DOWN, Direction.WEST), DOWN_EAST(Direction.DOWN, Direction.EAST), - UP_NORTH(Direction.UP, Direction.NORTH), UP_SOUTH(Direction.UP, Direction.SOUTH), UP_WEST(Direction.UP, Direction.WEST), UP_EAST(Direction.UP, Direction.EAST); @@ -113,7 +114,6 @@ enum class BlockRotation(val primary: Direction, val secondary: Direction?) : St Direction.UP -> { when (secondary) { null -> UP - Direction.NORTH -> UP_NORTH Direction.SOUTH -> UP_SOUTH Direction.WEST -> UP_WEST Direction.EAST -> UP_EAST @@ -124,7 +124,6 @@ enum class BlockRotation(val primary: Direction, val secondary: Direction?) : St Direction.DOWN -> { when (secondary) { null -> DOWN - Direction.NORTH -> DOWN_NORTH Direction.SOUTH -> DOWN_SOUTH Direction.WEST -> DOWN_WEST Direction.EAST -> DOWN_EAST @@ -133,5 +132,33 @@ enum class BlockRotation(val primary: Direction, val secondary: Direction?) : St } } } + + @JvmStatic + fun ofSafe(primary: Direction, secondary: Direction?): BlockRotation { + return when (primary) { + Direction.NORTH -> NORTH + Direction.SOUTH -> SOUTH + Direction.WEST -> WEST + Direction.EAST -> EAST + + Direction.UP -> { + when (secondary) { + Direction.SOUTH -> UP_SOUTH + Direction.WEST -> UP_WEST + Direction.EAST -> UP_EAST + else -> UP + } + } + + Direction.DOWN -> { + when (secondary) { + Direction.SOUTH -> DOWN_SOUTH + Direction.WEST -> DOWN_WEST + Direction.EAST -> DOWN_EAST + else -> DOWN + } + } + } + } } } diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/core/math/BlockRotationFreedom.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/core/math/BlockRotationFreedom.kt index fc0dd01fb..2f131ba8e 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/core/math/BlockRotationFreedom.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/core/math/BlockRotationFreedom.kt @@ -35,11 +35,9 @@ enum class BlockRotationFreedom(vararg values: BlockRotation) { BlockRotation.SOUTH, BlockRotation.WEST, BlockRotation.EAST, - BlockRotation.DOWN_NORTH, BlockRotation.DOWN_SOUTH, BlockRotation.DOWN_WEST, BlockRotation.DOWN_EAST, - BlockRotation.UP_NORTH, BlockRotation.UP_SOUTH, BlockRotation.UP_WEST, BlockRotation.UP_EAST, diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/core/math/EuclidMath.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/core/math/EuclidMath.kt index 5dd4654a1..3b8c60b5f 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/core/math/EuclidMath.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/core/math/EuclidMath.kt @@ -647,6 +647,10 @@ fun PoseStack.rotateWithBlockFacing(rotation: Direction, clarifyingAxis: Directi return this } +fun PoseStack.rotateWithBlockFacing(rotation: BlockRotation): PoseStack { + return rotateWithBlockFacing(rotation = rotation.primary, clarifyingAxis = rotation.secondary) +} + fun PoseStack.rotateYDegrees(rotation: Float): PoseStack { mulPose(Quaternionf(AxisAngle4f(toRadians(rotation), 0f, 1f, 0f))) return this