chest block model: update position and rotation on change

This commit is contained in:
Moritz Zwerger 2023-10-19 21:01:41 +02:00
parent ecba02d533
commit 6858bd87a6
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
7 changed files with 34 additions and 16 deletions

View File

@ -31,11 +31,11 @@ abstract class BlockEntity(
open fun tick(connection: PlayConnection, blockState: BlockState, blockPosition: Vec3i, random: Random) = Unit
open fun getRenderer(context: RenderContext, blockState: BlockState, blockPosition: Vec3i, light: Int): BlockEntityRenderer<out BlockEntity>? {
if (this.renderer?.blockState != blockState) {
this.renderer = createRenderer(context, blockState, blockPosition, light)
open fun getRenderer(context: RenderContext, state: BlockState, position: Vec3i, light: Int): BlockEntityRenderer<out BlockEntity>? {
if (this.renderer?.state != state) {
this.renderer = createRenderer(context, state, position, light)
} else {
this.renderer?.light = light
this.renderer?.update(position, state, light)
}
return this.renderer
}

View File

@ -22,12 +22,12 @@ import de.bixilon.minosoft.protocol.network.connection.play.PlayConnection
abstract class MeshedBlockEntity(connection: PlayConnection) : BlockEntity(connection) {
override fun getRenderer(context: RenderContext, blockState: BlockState, blockPosition: Vec3i, light: Int): MeshedEntityRenderer<*> {
override fun getRenderer(context: RenderContext, state: BlockState, position: Vec3i, light: Int): MeshedEntityRenderer<*> {
var renderer = this.renderer
if (renderer is MeshedEntityRenderer && renderer.blockState == blockState) {
if (renderer is MeshedEntityRenderer && renderer.state == state) {
return renderer
}
renderer = createMeshedRenderer(context, blockState, blockPosition)
renderer = createMeshedRenderer(context, state, position)
this.renderer = renderer
return renderer
}

View File

@ -15,12 +15,13 @@ package de.bixilon.minosoft.gui.rendering.chunk.entities
import de.bixilon.minosoft.data.entities.block.BlockEntity
import de.bixilon.minosoft.data.registries.blocks.state.BlockState
import de.bixilon.minosoft.data.world.positions.BlockPosition
import de.bixilon.minosoft.gui.rendering.RenderContext
interface BlockEntityRenderer<E : BlockEntity> {
val enabled: Boolean get() = true
val blockState: BlockState
var state: BlockState
var light: Int
get() = 0
set(value) = Unit
@ -29,4 +30,9 @@ interface BlockEntityRenderer<E : BlockEntity> {
fun unload() = Unit
fun load() = Unit
fun update(position: BlockPosition, state: BlockState, light: Int) {
this.light = light
this.state = state
}
}

View File

@ -48,18 +48,19 @@ import java.util.*
class SignBlockEntityRenderer(
val sign: SignBlockEntity,
val context: RenderContext,
override val blockState: BlockState,
override var state: BlockState,
) : MeshedEntityRenderer<SignBlockEntity> {
override val enabled: Boolean get() = false
private fun getRotation(): Float {
if (blockState !is PropertyBlockState) return 0.0f
val rotation = blockState.properties[BlockProperties.ROTATION]?.toFloat() ?: return 0.0f
val state = this.state
if (state !is PropertyBlockState) return 0.0f
val rotation = state.properties[BlockProperties.ROTATION]?.toFloat() ?: return 0.0f
return rotation * 22.5f
}
override fun render(position: BlockPosition, offset: FloatArray, mesh: ChunkMesh, random: Random?, state: BlockState, neighbours: Array<BlockState?>, light: ByteArray, tints: IntArray?): Boolean {
val block = this.blockState.block
val block = this.state.block
if (block is StandingSignBlock) {
renderStandingText(offset, mesh, light[SELF_LIGHT_INDEX].toInt())
} else if (block is WallSignBlock) {
@ -97,7 +98,7 @@ class SignBlockEntityRenderer(
}
private fun renderWallText(position: FloatArray, mesh: ChunkMesh, light: Int) {
val yRotation = -when (val rotation = this.blockState.getFacing()) {
val yRotation = -when (val rotation = this.state.getFacing()) {
Directions.SOUTH -> 0.0f
Directions.EAST -> 90.0f
Directions.NORTH -> 180.0f

View File

@ -20,6 +20,7 @@ import de.bixilon.minosoft.data.registries.blocks.properties.BlockProperties.get
import de.bixilon.minosoft.data.registries.blocks.state.BlockState
import de.bixilon.minosoft.data.registries.identified.Namespaces.minecraft
import de.bixilon.minosoft.data.registries.identified.ResourceLocation
import de.bixilon.minosoft.data.world.positions.BlockPosition
import de.bixilon.minosoft.gui.rendering.RenderContext
import de.bixilon.minosoft.gui.rendering.chunk.entities.EntityRendererRegister
import de.bixilon.minosoft.gui.rendering.models.loader.ModelLoader
@ -41,7 +42,11 @@ class DoubleChestRenderer(
) {
init {
skeletal?.update(blockPosition, blockState.getFacing())
update(blockPosition, state, light)
}
override fun update(position: BlockPosition, state: BlockState, light: Int) {
skeletal?.update(position, state.getFacing())
}
companion object {

View File

@ -20,6 +20,7 @@ import de.bixilon.minosoft.data.registries.blocks.properties.BlockProperties.get
import de.bixilon.minosoft.data.registries.blocks.state.BlockState
import de.bixilon.minosoft.data.registries.identified.Namespaces.minecraft
import de.bixilon.minosoft.data.registries.identified.ResourceLocation
import de.bixilon.minosoft.data.world.positions.BlockPosition
import de.bixilon.minosoft.gui.rendering.RenderContext
import de.bixilon.minosoft.gui.rendering.chunk.entities.EntityRendererRegister
import de.bixilon.minosoft.gui.rendering.models.loader.ModelLoader
@ -39,8 +40,13 @@ class SingleChestRenderer(
model.createInstance(context),
light,
) {
init {
skeletal?.update(blockPosition, blockState.getFacing())
update(blockPosition, state, light)
}
override fun update(position: BlockPosition, state: BlockState, light: Int) {
skeletal?.update(position, state.getFacing())
}
companion object {

View File

@ -20,7 +20,7 @@ import de.bixilon.minosoft.gui.rendering.chunk.entities.BlockEntityRenderer
import de.bixilon.minosoft.gui.rendering.skeletal.instance.SkeletalInstance
abstract class StorageBlockEntityRenderer<E : StorageBlockEntity>(
override val blockState: BlockState,
override var state: BlockState,
protected val skeletal: SkeletalInstance?,
override var light: Int,
) : BlockEntityRenderer<E> {