From d48a504486311f3175f4ebef3b0649140e728fbb Mon Sep 17 00:00:00 2001 From: PepperCode1 <44146161+PepperCode1@users.noreply.github.com> Date: Thu, 24 Oct 2024 21:52:07 -0700 Subject: [PATCH] Add Flywheel visuals for bogey block entities --- .../simibubi/create/AllBlockEntityTypes.java | 2 + .../bogey/AbstractBogeyBlockEntity.java | 2 +- .../bogey/BogeyBlockEntityRenderer.java | 21 ++-- .../trains/bogey/BogeyBlockEntityVisual.java | 95 +++++++++++++++++++ .../content/trains/bogey/BogeyStyle.java | 7 +- .../content/trains/bogey/BogeyVisual.java | 11 ++- .../content/trains/bogey/BogeyVisualizer.java | 4 +- .../trains/bogey/StandardBogeyVisual.java | 52 +++++++--- .../entity/CarriageContraptionVisual.java | 25 +++-- 9 files changed, 179 insertions(+), 40 deletions(-) create mode 100644 src/main/java/com/simibubi/create/content/trains/bogey/BogeyBlockEntityVisual.java diff --git a/src/main/java/com/simibubi/create/AllBlockEntityTypes.java b/src/main/java/com/simibubi/create/AllBlockEntityTypes.java index d0cccbc36..74918805a 100644 --- a/src/main/java/com/simibubi/create/AllBlockEntityTypes.java +++ b/src/main/java/com/simibubi/create/AllBlockEntityTypes.java @@ -199,6 +199,7 @@ import com.simibubi.create.content.schematics.cannon.SchematicannonRenderer; import com.simibubi.create.content.schematics.cannon.SchematicannonVisual; import com.simibubi.create.content.schematics.table.SchematicTableBlockEntity; import com.simibubi.create.content.trains.bogey.BogeyBlockEntityRenderer; +import com.simibubi.create.content.trains.bogey.BogeyBlockEntityVisual; import com.simibubi.create.content.trains.bogey.StandardBogeyBlockEntity; import com.simibubi.create.content.trains.display.FlapDisplayBlockEntity; import com.simibubi.create.content.trains.display.FlapDisplayRenderer; @@ -844,6 +845,7 @@ public class AllBlockEntityTypes { public static final BlockEntityEntry BOGEY = REGISTRATE .blockEntity("bogey", StandardBogeyBlockEntity::new) + .visual(() -> BogeyBlockEntityVisual::new, false) .renderer(() -> BogeyBlockEntityRenderer::new) .validBlocks(AllBlocks.SMALL_BOGEY, AllBlocks.LARGE_BOGEY) .register(); diff --git a/src/main/java/com/simibubi/create/content/trains/bogey/AbstractBogeyBlockEntity.java b/src/main/java/com/simibubi/create/content/trains/bogey/AbstractBogeyBlockEntity.java index 6fc496c50..146a04e2e 100644 --- a/src/main/java/com/simibubi/create/content/trains/bogey/AbstractBogeyBlockEntity.java +++ b/src/main/java/com/simibubi/create/content/trains/bogey/AbstractBogeyBlockEntity.java @@ -113,6 +113,6 @@ public abstract class AbstractBogeyBlockEntity extends CachedRenderBBBlockEntity setChanged(); Level level = getLevel(); if (level != null) - getLevel().sendBlockUpdated(getBlockPos(), getBlockState(), getBlockState(), 3); + level.sendBlockUpdated(getBlockPos(), getBlockState(), getBlockState(), 3); } } diff --git a/src/main/java/com/simibubi/create/content/trains/bogey/BogeyBlockEntityRenderer.java b/src/main/java/com/simibubi/create/content/trains/bogey/BogeyBlockEntityRenderer.java index d340cc5b3..cbeaf0659 100644 --- a/src/main/java/com/simibubi/create/content/trains/bogey/BogeyBlockEntityRenderer.java +++ b/src/main/java/com/simibubi/create/content/trains/bogey/BogeyBlockEntityRenderer.java @@ -7,10 +7,9 @@ import com.simibubi.create.foundation.blockEntity.renderer.SafeBlockEntityRender import net.minecraft.client.renderer.MultiBufferSource; import net.minecraft.client.renderer.blockentity.BlockEntityRendererProvider; import net.minecraft.core.Direction; -import net.minecraft.world.level.block.entity.BlockEntity; import net.minecraft.world.level.block.state.BlockState; -public class BogeyBlockEntityRenderer extends SafeBlockEntityRenderer { +public class BogeyBlockEntityRenderer extends SafeBlockEntityRenderer { public BogeyBlockEntityRenderer(BlockEntityRendererProvider.Context context) { } @@ -18,14 +17,16 @@ public class BogeyBlockEntityRenderer extends SafeBlockEn protected void renderSafe(T be, float partialTicks, PoseStack ms, MultiBufferSource buffer, int light, int overlay) { BlockState blockState = be.getBlockState(); - if (be instanceof AbstractBogeyBlockEntity sbbe) { - float angle = sbbe.getVirtualAngle(partialTicks); - if (blockState.getBlock() instanceof AbstractBogeyBlock bogey) { - ms.translate(.5f, .5f, .5f); - if (blockState.getValue(AbstractBogeyBlock.AXIS) == Direction.Axis.X) - ms.mulPose(Axis.YP.rotationDegrees(90)); - sbbe.getStyle().render(bogey.getSize(), partialTicks, ms, buffer, light, overlay, angle, sbbe.getBogeyData(), false); - } + if (!(blockState.getBlock() instanceof AbstractBogeyBlock bogey)) { + return; } + + float angle = be.getVirtualAngle(partialTicks); + ms.pushPose(); + ms.translate(.5f, .5f, .5f); + if (blockState.getValue(AbstractBogeyBlock.AXIS) == Direction.Axis.X) + ms.mulPose(Axis.YP.rotationDegrees(90)); + be.getStyle().render(bogey.getSize(), partialTicks, ms, buffer, light, overlay, angle, be.getBogeyData(), false); + ms.popPose(); } } diff --git a/src/main/java/com/simibubi/create/content/trains/bogey/BogeyBlockEntityVisual.java b/src/main/java/com/simibubi/create/content/trains/bogey/BogeyBlockEntityVisual.java new file mode 100644 index 000000000..e3bd7191b --- /dev/null +++ b/src/main/java/com/simibubi/create/content/trains/bogey/BogeyBlockEntityVisual.java @@ -0,0 +1,95 @@ +package com.simibubi.create.content.trains.bogey; + +import java.util.function.Consumer; + +import org.jetbrains.annotations.Nullable; + +import com.mojang.blaze3d.vertex.PoseStack; +import com.mojang.math.Axis; +import com.simibubi.create.content.trains.bogey.BogeySizes.BogeySize; + +import dev.engine_room.flywheel.api.instance.Instance; +import dev.engine_room.flywheel.api.visualization.VisualizationContext; +import dev.engine_room.flywheel.lib.visual.AbstractBlockEntityVisual; +import dev.engine_room.flywheel.lib.visual.SimpleDynamicVisual; +import net.minecraft.core.BlockPos; +import net.minecraft.core.Direction; +import net.minecraft.nbt.CompoundTag; + +public class BogeyBlockEntityVisual extends AbstractBlockEntityVisual implements SimpleDynamicVisual { + private final PoseStack poseStack = new PoseStack(); + + @Nullable + private final BogeySize bogeySize; + private BogeyStyle lastStyle; + @Nullable + private BogeyVisual bogey; + + public BogeyBlockEntityVisual(VisualizationContext ctx, AbstractBogeyBlockEntity blockEntity, float partialTick) { + super(ctx, blockEntity, partialTick); + + lastStyle = blockEntity.getStyle(); + + if (!(blockState.getBlock() instanceof AbstractBogeyBlock block)) { + bogeySize = null; + return; + } + + bogeySize = block.getSize(); + + BlockPos visualPos = getVisualPosition(); + poseStack.translate(visualPos.getX(), visualPos.getY(), visualPos.getZ()); + poseStack.translate(.5f, .5f, .5f); + if (blockState.getValue(AbstractBogeyBlock.AXIS) == Direction.Axis.X) + poseStack.mulPose(Axis.YP.rotationDegrees(90)); + poseStack.translate(0, -1.5 - 1 / 128f, 0); + + bogey = lastStyle.createVisual(bogeySize, visualizationContext, partialTick, false); + } + + @Override + public void beginFrame(Context context) { + if (bogeySize == null) { + return; + } + + BogeyStyle style = blockEntity.getStyle(); + if (style != lastStyle) { + if (bogey != null) { + bogey.delete(); + bogey = null; + } + lastStyle = style; + bogey = lastStyle.createVisual(bogeySize, visualizationContext, context.partialTick(), false); + } + + if (bogey == null) { + return; + } + + CompoundTag bogeyData = blockEntity.getBogeyData(); + float angle = blockEntity.getVirtualAngle(context.partialTick()); + bogey.update(bogeyData, angle, poseStack); + } + + @Override + public void collectCrumblingInstances(Consumer<@Nullable Instance> consumer) { + if (bogey != null) { + bogey.collectCrumblingInstances(consumer); + } + } + + @Override + public void updateLight(float partialTick) { + if (bogey != null) { + bogey.updateLight(computePackedLight()); + } + } + + @Override + protected void _delete() { + if (bogey != null) { + bogey.delete(); + } + } +} diff --git a/src/main/java/com/simibubi/create/content/trains/bogey/BogeyStyle.java b/src/main/java/com/simibubi/create/content/trains/bogey/BogeyStyle.java index 9a375d7ee..d7ece27df 100644 --- a/src/main/java/com/simibubi/create/content/trains/bogey/BogeyStyle.java +++ b/src/main/java/com/simibubi/create/content/trains/bogey/BogeyStyle.java @@ -12,7 +12,6 @@ import com.mojang.blaze3d.vertex.PoseStack; import com.simibubi.create.AllBogeyStyles; import com.simibubi.create.AllSoundEvents; import com.simibubi.create.content.trains.bogey.BogeySizes.BogeySize; -import com.simibubi.create.content.trains.entity.CarriageBogey; import com.simibubi.create.foundation.utility.Lang; import dev.engine_room.flywheel.api.visualization.VisualizationContext; @@ -96,10 +95,10 @@ public class BogeyStyle { @OnlyIn(Dist.CLIENT) @Nullable - public BogeyVisual createVisual(VisualizationContext ctx, CarriageBogey bogey, float partialTick) { - SizeRenderer renderer = sizeRenderers.get(bogey.getSize()); + public BogeyVisual createVisual(BogeySize size, VisualizationContext ctx, float partialTick, boolean inContraption) { + SizeRenderer renderer = sizeRenderers.get(size); if (renderer != null) { - return renderer.visualizer.createVisual(ctx, bogey, partialTick); + return renderer.visualizer.createVisual(ctx, partialTick, inContraption); } return null; } diff --git a/src/main/java/com/simibubi/create/content/trains/bogey/BogeyVisual.java b/src/main/java/com/simibubi/create/content/trains/bogey/BogeyVisual.java index 2b0eb6897..a003116cc 100644 --- a/src/main/java/com/simibubi/create/content/trains/bogey/BogeyVisual.java +++ b/src/main/java/com/simibubi/create/content/trains/bogey/BogeyVisual.java @@ -1,13 +1,22 @@ package com.simibubi.create.content.trains.bogey; +import java.util.function.Consumer; + +import org.jetbrains.annotations.Nullable; + import com.mojang.blaze3d.vertex.PoseStack; +import dev.engine_room.flywheel.api.instance.Instance; +import net.minecraft.nbt.CompoundTag; + public interface BogeyVisual { - void update(float wheelAngle, PoseStack poseStack); + void update(CompoundTag bogeyData, float wheelAngle, PoseStack poseStack); void hide(); void updateLight(int packedLight); + void collectCrumblingInstances(Consumer<@Nullable Instance> consumer); + void delete(); } diff --git a/src/main/java/com/simibubi/create/content/trains/bogey/BogeyVisualizer.java b/src/main/java/com/simibubi/create/content/trains/bogey/BogeyVisualizer.java index 38acb77d4..08570e0a2 100644 --- a/src/main/java/com/simibubi/create/content/trains/bogey/BogeyVisualizer.java +++ b/src/main/java/com/simibubi/create/content/trains/bogey/BogeyVisualizer.java @@ -1,10 +1,8 @@ package com.simibubi.create.content.trains.bogey; -import com.simibubi.create.content.trains.entity.CarriageBogey; - import dev.engine_room.flywheel.api.visualization.VisualizationContext; @FunctionalInterface public interface BogeyVisualizer { - BogeyVisual createVisual(VisualizationContext ctx, CarriageBogey bogey, float partialTick); + BogeyVisual createVisual(VisualizationContext ctx, float partialTick, boolean inContraption); } diff --git a/src/main/java/com/simibubi/create/content/trains/bogey/StandardBogeyVisual.java b/src/main/java/com/simibubi/create/content/trains/bogey/StandardBogeyVisual.java index feec031b8..0bb70cd1d 100644 --- a/src/main/java/com/simibubi/create/content/trains/bogey/StandardBogeyVisual.java +++ b/src/main/java/com/simibubi/create/content/trains/bogey/StandardBogeyVisual.java @@ -1,24 +1,29 @@ package com.simibubi.create.content.trains.bogey; +import java.util.function.Consumer; + +import org.jetbrains.annotations.Nullable; + import com.mojang.blaze3d.vertex.PoseStack; import com.simibubi.create.AllBlocks; import com.simibubi.create.AllPartialModels; import com.simibubi.create.content.kinetics.simpleRelays.ShaftBlock; -import com.simibubi.create.content.trains.entity.CarriageBogey; import com.simibubi.create.foundation.render.VirtualRenderHelper; import com.simibubi.create.foundation.utility.AngleHelper; +import dev.engine_room.flywheel.api.instance.Instance; import dev.engine_room.flywheel.api.visualization.VisualizationContext; import dev.engine_room.flywheel.lib.instance.InstanceTypes; import dev.engine_room.flywheel.lib.instance.TransformedInstance; import dev.engine_room.flywheel.lib.model.Models; import net.minecraft.core.Direction; +import net.minecraft.nbt.CompoundTag; public class StandardBogeyVisual implements BogeyVisual { private final TransformedInstance shaft1; private final TransformedInstance shaft2; - public StandardBogeyVisual(VisualizationContext ctx, CarriageBogey bogey, float partialTick) { + public StandardBogeyVisual(VisualizationContext ctx, float partialTick, boolean inContraption) { var shaftInstancer = ctx.instancerProvider() .instancer(InstanceTypes.TRANSFORMED, VirtualRenderHelper.blockModel(AllBlocks.SHAFT.getDefaultState() .setValue(ShaftBlock.AXIS, Direction.Axis.Z))); @@ -27,7 +32,7 @@ public class StandardBogeyVisual implements BogeyVisual { } @Override - public void update(float wheelAngle, PoseStack poseStack) { + public void update(CompoundTag bogeyData, float wheelAngle, PoseStack poseStack) { shaft1.setTransform(poseStack) .translate(-.5f, .25f, 0) .center() @@ -54,6 +59,12 @@ public class StandardBogeyVisual implements BogeyVisual { shaft2.light(packedLight).setChanged(); } + @Override + public void collectCrumblingInstances(Consumer<@Nullable Instance> consumer) { + consumer.accept(shaft1); + consumer.accept(shaft2); + } + @Override public void delete() { shaft1.delete(); @@ -65,8 +76,8 @@ public class StandardBogeyVisual implements BogeyVisual { private final TransformedInstance wheel1; private final TransformedInstance wheel2; - public Small(VisualizationContext ctx, CarriageBogey bogey, float partialTick) { - super(ctx, bogey, partialTick); + public Small(VisualizationContext ctx, float partialTick, boolean inContraption) { + super(ctx, partialTick, inContraption); var wheelInstancer = ctx.instancerProvider() .instancer(InstanceTypes.TRANSFORMED, Models.partial(AllPartialModels.SMALL_BOGEY_WHEELS)); frame = ctx.instancerProvider() @@ -77,8 +88,8 @@ public class StandardBogeyVisual implements BogeyVisual { } @Override - public void update(float wheelAngle, PoseStack poseStack) { - super.update(wheelAngle, poseStack); + public void update(CompoundTag bogeyData, float wheelAngle, PoseStack poseStack) { + super.update(bogeyData, wheelAngle, poseStack); wheel1.setTransform(poseStack) .translate(0, 12 / 16f, -1) .rotateXDegrees(wheelAngle) @@ -108,6 +119,14 @@ public class StandardBogeyVisual implements BogeyVisual { wheel2.light(packedLight).setChanged(); } + @Override + public void collectCrumblingInstances(Consumer<@Nullable Instance> consumer) { + super.collectCrumblingInstances(consumer); + consumer.accept(frame); + consumer.accept(wheel1); + consumer.accept(wheel2); + } + @Override public void delete() { super.delete(); @@ -125,8 +144,8 @@ public class StandardBogeyVisual implements BogeyVisual { private final TransformedInstance wheels; private final TransformedInstance pin; - public Large(VisualizationContext ctx, CarriageBogey bogey, float partialTick) { - super(ctx, bogey, partialTick); + public Large(VisualizationContext ctx, float partialTick, boolean inContraption) { + super(ctx, partialTick, inContraption); var secondaryShaftInstancer = ctx.instancerProvider() .instancer(InstanceTypes.TRANSFORMED, VirtualRenderHelper.blockModel(AllBlocks.SHAFT.getDefaultState() .setValue(ShaftBlock.AXIS, Direction.Axis.X))); @@ -147,8 +166,8 @@ public class StandardBogeyVisual implements BogeyVisual { } @Override - public void update(float wheelAngle, PoseStack poseStack) { - super.update(wheelAngle, poseStack); + public void update(CompoundTag bogeyData, float wheelAngle, PoseStack poseStack) { + super.update(bogeyData, wheelAngle, poseStack); secondaryShaft1.setTransform(poseStack) .translate(-.5f, .25f, .5f) .center() @@ -201,6 +220,17 @@ public class StandardBogeyVisual implements BogeyVisual { pin.light(packedLight).setChanged(); } + @Override + public void collectCrumblingInstances(Consumer<@Nullable Instance> consumer) { + super.collectCrumblingInstances(consumer); + consumer.accept(secondaryShaft1); + consumer.accept(secondaryShaft2); + consumer.accept(wheels); + consumer.accept(drive); + consumer.accept(piston); + consumer.accept(pin); + } + @Override public void delete() { super.delete(); diff --git a/src/main/java/com/simibubi/create/content/trains/entity/CarriageContraptionVisual.java b/src/main/java/com/simibubi/create/content/trains/entity/CarriageContraptionVisual.java index 7fadc8e62..28ef06cb2 100644 --- a/src/main/java/com/simibubi/create/content/trains/entity/CarriageContraptionVisual.java +++ b/src/main/java/com/simibubi/create/content/trains/entity/CarriageContraptionVisual.java @@ -12,9 +12,10 @@ import com.simibubi.create.foundation.utility.Iterate; import dev.engine_room.flywheel.api.visual.DynamicVisual; import dev.engine_room.flywheel.api.visualization.VisualizationContext; import dev.engine_room.flywheel.lib.transform.TransformStack; +import net.minecraft.nbt.CompoundTag; public class CarriageContraptionVisual extends ContraptionVisual { - private final PoseStack ms = new PoseStack(); + private final PoseStack poseStack = new PoseStack(); @Nullable private Carriage carriage; @@ -59,10 +60,10 @@ public class CarriageContraptionVisual extends ContraptionVisual