Implement some logic for three rotation freedom of blocks, make holo sign have three rotation freedoms

This commit is contained in:
DBotThePony 2023-01-30 14:08:01 +07:00
parent e2791add3b
commit 192144cc27
Signed by: DBot
GPG Key ID: DCC23B5715498507
7 changed files with 66 additions and 16 deletions

View File

@ -31,6 +31,11 @@ private fun initialTransform(it: BlockState, modelPath: String, builder: Configu
builder.rotationX(it.primary.toXRotBlockstate()) 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 { it.getValueNullable(WorkerState.WORKER_STATE)?.let {
modelPath += "_" + it.name.lowercase() modelPath += "_" + it.name.lowercase()
} }

View File

@ -1,5 +1,6 @@
package ru.dbotthepony.mc.otm.block package ru.dbotthepony.mc.otm.block
import net.minecraft.core.Direction
import net.minecraft.world.item.context.BlockPlaceContext import net.minecraft.world.item.context.BlockPlaceContext
import net.minecraft.world.level.block.Block import net.minecraft.world.level.block.Block
import net.minecraft.world.level.block.Rotation import net.minecraft.world.level.block.Rotation
@ -20,16 +21,30 @@ abstract class RotatableMatteryBlock @JvmOverloads constructor(properties: Prope
} }
override fun getStateForPlacement(context: BlockPlaceContext): BlockState? { override fun getStateForPlacement(context: BlockPlaceContext): BlockState? {
if (rotationFreedom() == BlockRotationFreedom.ONE) { return when (val freedom = rotationFreedom()) {
return defaultBlockState().setValue( BlockRotationFreedom.ONE -> defaultBlockState().setValue(
rotationProperty, freedom.property,
BlockRotation.of(if (faceToPlayer(context)) context.horizontalDirection.opposite else context.horizontalDirection) BlockRotation.of(if (faceToPlayer(context)) context.horizontalDirection.opposite else context.horizontalDirection)
) )
} else {
return defaultBlockState().setValue( BlockRotationFreedom.TWO -> defaultBlockState().setValue(
rotationProperty, freedom.property,
BlockRotation.of(if (faceToPlayer(context)) context.nearestLookingDirection.opposite else context.nearestLookingDirection) 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)
)
}
} }
} }

View File

@ -15,7 +15,7 @@ import ru.dbotthepony.mc.otm.shapes.BlockShapes
class HoloSignBlock : RotatableMatteryBlock(), EntityBlock { class HoloSignBlock : RotatableMatteryBlock(), EntityBlock {
override fun rotationFreedom(): BlockRotationFreedom { override fun rotationFreedom(): BlockRotationFreedom {
return BlockRotationFreedom.TWO return BlockRotationFreedom.THREE
} }
override fun newBlockEntity(p_153215_: BlockPos, p_153216_: BlockState): BlockEntity { override fun newBlockEntity(p_153215_: BlockPos, p_153216_: BlockState): BlockEntity {

View File

@ -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.RGBAColor
import ru.dbotthepony.mc.otm.core.math.facingTwo import ru.dbotthepony.mc.otm.core.math.facingTwo
import ru.dbotthepony.mc.otm.core.math.rotateWithBlockFacing import ru.dbotthepony.mc.otm.core.math.rotateWithBlockFacing
import ru.dbotthepony.mc.otm.core.math.rotationThree
class HoloSignRenderer(private val context: BlockEntityRendererProvider.Context) : BlockEntityRenderer<HoloSignBlockEntity> { class HoloSignRenderer(private val context: BlockEntityRendererProvider.Context) : BlockEntityRenderer<HoloSignBlockEntity> {
override fun render( override fun render(
@ -29,7 +30,7 @@ class HoloSignRenderer(private val context: BlockEntityRendererProvider.Context)
return return
poseStack.pushPose() poseStack.pushPose()
poseStack.rotateWithBlockFacing(tile.blockState.facingTwo) poseStack.rotateWithBlockFacing(tile.blockState.rotationThree)
poseStack.translate(0.5f, 0.5f, 0.6f) poseStack.translate(0.5f, 0.5f, 0.6f)
poseStack.scale(0.01f, 0.01f, 0.01f) poseStack.scale(0.01f, 0.01f, 0.01f)

View File

@ -24,20 +24,21 @@ operator fun BlockPos.plus(other: BlockRotation): BlockPos {
return this + other.normal return this + other.normal
} }
/**
* [secondary] clarifies about block's top facing direction, NOT bottom
*/
enum class BlockRotation(val primary: Direction, val secondary: Direction?) : StringRepresentable { enum class BlockRotation(val primary: Direction, val secondary: Direction?) : StringRepresentable {
DOWN(Direction.DOWN, null), DOWN(Direction.DOWN, Direction.NORTH),
UP(Direction.UP, null), UP(Direction.UP, Direction.NORTH),
NORTH(Direction.NORTH, null), NORTH(Direction.NORTH, null),
SOUTH(Direction.SOUTH, null), SOUTH(Direction.SOUTH, null),
WEST(Direction.WEST, null), WEST(Direction.WEST, null),
EAST(Direction.EAST, null), EAST(Direction.EAST, null),
DOWN_NORTH(Direction.DOWN, Direction.NORTH),
DOWN_SOUTH(Direction.DOWN, Direction.SOUTH), DOWN_SOUTH(Direction.DOWN, Direction.SOUTH),
DOWN_WEST(Direction.DOWN, Direction.WEST), DOWN_WEST(Direction.DOWN, Direction.WEST),
DOWN_EAST(Direction.DOWN, Direction.EAST), DOWN_EAST(Direction.DOWN, Direction.EAST),
UP_NORTH(Direction.UP, Direction.NORTH),
UP_SOUTH(Direction.UP, Direction.SOUTH), UP_SOUTH(Direction.UP, Direction.SOUTH),
UP_WEST(Direction.UP, Direction.WEST), UP_WEST(Direction.UP, Direction.WEST),
UP_EAST(Direction.UP, Direction.EAST); UP_EAST(Direction.UP, Direction.EAST);
@ -113,7 +114,6 @@ enum class BlockRotation(val primary: Direction, val secondary: Direction?) : St
Direction.UP -> { Direction.UP -> {
when (secondary) { when (secondary) {
null -> UP null -> UP
Direction.NORTH -> UP_NORTH
Direction.SOUTH -> UP_SOUTH Direction.SOUTH -> UP_SOUTH
Direction.WEST -> UP_WEST Direction.WEST -> UP_WEST
Direction.EAST -> UP_EAST Direction.EAST -> UP_EAST
@ -124,7 +124,6 @@ enum class BlockRotation(val primary: Direction, val secondary: Direction?) : St
Direction.DOWN -> { Direction.DOWN -> {
when (secondary) { when (secondary) {
null -> DOWN null -> DOWN
Direction.NORTH -> DOWN_NORTH
Direction.SOUTH -> DOWN_SOUTH Direction.SOUTH -> DOWN_SOUTH
Direction.WEST -> DOWN_WEST Direction.WEST -> DOWN_WEST
Direction.EAST -> DOWN_EAST 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
}
}
}
}
} }
} }

View File

@ -35,11 +35,9 @@ enum class BlockRotationFreedom(vararg values: BlockRotation) {
BlockRotation.SOUTH, BlockRotation.SOUTH,
BlockRotation.WEST, BlockRotation.WEST,
BlockRotation.EAST, BlockRotation.EAST,
BlockRotation.DOWN_NORTH,
BlockRotation.DOWN_SOUTH, BlockRotation.DOWN_SOUTH,
BlockRotation.DOWN_WEST, BlockRotation.DOWN_WEST,
BlockRotation.DOWN_EAST, BlockRotation.DOWN_EAST,
BlockRotation.UP_NORTH,
BlockRotation.UP_SOUTH, BlockRotation.UP_SOUTH,
BlockRotation.UP_WEST, BlockRotation.UP_WEST,
BlockRotation.UP_EAST, BlockRotation.UP_EAST,

View File

@ -647,6 +647,10 @@ fun PoseStack.rotateWithBlockFacing(rotation: Direction, clarifyingAxis: Directi
return this return this
} }
fun PoseStack.rotateWithBlockFacing(rotation: BlockRotation): PoseStack {
return rotateWithBlockFacing(rotation = rotation.primary, clarifyingAxis = rotation.secondary)
}
fun PoseStack.rotateYDegrees(rotation: Float): PoseStack { fun PoseStack.rotateYDegrees(rotation: Float): PoseStack {
mulPose(Quaternionf(AxisAngle4f(toRadians(rotation), 0f, 1f, 0f))) mulPose(Quaternionf(AxisAngle4f(toRadians(rotation), 0f, 1f, 0f)))
return this return this