Skip to content

Commit

Permalink
Add Flywheel visuals for bogey block entities
Browse files Browse the repository at this point in the history
  • Loading branch information
PepperCode1 committed Oct 25, 2024
1 parent 1935b79 commit d48a504
Show file tree
Hide file tree
Showing 9 changed files with 179 additions and 40 deletions.
2 changes: 2 additions & 0 deletions src/main/java/com/simibubi/create/AllBlockEntityTypes.java
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@
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;
Expand Down Expand Up @@ -844,6 +845,7 @@ public class AllBlockEntityTypes {

public static final BlockEntityEntry<StandardBogeyBlockEntity> BOGEY = REGISTRATE
.blockEntity("bogey", StandardBogeyBlockEntity::new)
.visual(() -> BogeyBlockEntityVisual::new, false)
.renderer(() -> BogeyBlockEntityRenderer::new)
.validBlocks(AllBlocks.SMALL_BOGEY, AllBlocks.LARGE_BOGEY)
.register();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,6 @@ private void markUpdated() {
setChanged();
Level level = getLevel();
if (level != null)
getLevel().sendBlockUpdated(getBlockPos(), getBlockState(), getBlockState(), 3);
level.sendBlockUpdated(getBlockPos(), getBlockState(), getBlockState(), 3);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,26 @@
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<T extends BlockEntity> extends SafeBlockEntityRenderer<T> {
public class BogeyBlockEntityRenderer<T extends AbstractBogeyBlockEntity> extends SafeBlockEntityRenderer<T> {
public BogeyBlockEntityRenderer(BlockEntityRendererProvider.Context context) {
}

@Override
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();
}
}
Original file line number Diff line number Diff line change
@@ -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<AbstractBogeyBlockEntity> 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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
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;
Expand Down Expand Up @@ -96,10 +95,10 @@ public void render(BogeySize size, float partialTick, PoseStack poseStack, Multi

@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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
@@ -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)));
Expand All @@ -27,7 +32,7 @@ public StandardBogeyVisual(VisualizationContext ctx, CarriageBogey bogey, float
}

@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()
Expand All @@ -54,6 +59,12 @@ public void updateLight(int packedLight) {
shaft2.light(packedLight).setChanged();
}

@Override
public void collectCrumblingInstances(Consumer<@Nullable Instance> consumer) {
consumer.accept(shaft1);
consumer.accept(shaft2);
}

@Override
public void delete() {
shaft1.delete();
Expand All @@ -65,8 +76,8 @@ public static class Small extends StandardBogeyVisual {
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()
Expand All @@ -77,8 +88,8 @@ public Small(VisualizationContext ctx, CarriageBogey bogey, float partialTick) {
}

@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)
Expand Down Expand Up @@ -108,6 +119,14 @@ public void updateLight(int packedLight) {
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();
Expand All @@ -125,8 +144,8 @@ public static class Large extends StandardBogeyVisual {
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)));
Expand All @@ -147,8 +166,8 @@ public Large(VisualizationContext ctx, CarriageBogey bogey, float partialTick) {
}

@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()
Expand Down Expand Up @@ -201,6 +220,17 @@ public void updateLight(int packedLight) {
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();
Expand Down
Loading

0 comments on commit d48a504

Please sign in to comment.