Fix linked controller rendering

- Fix unequip animation
- Fix button progress not resetting on unequip
- Optimize code
This commit is contained in:
PepperBell 2021-07-31 19:10:17 -07:00
parent 99be93e8a6
commit c674b5df8d
3 changed files with 72 additions and 42 deletions

View file

@ -26,12 +26,12 @@ public class LecternControllerRenderer extends SafeTileEntityRenderer<LecternCon
ItemStack stack = AllItems.LINKED_CONTROLLER.asStack(); ItemStack stack = AllItems.LINKED_CONTROLLER.asStack();
TransformType transformType = TransformType.NONE; TransformType transformType = TransformType.NONE;
LinkedControllerModel mainModel = ((LinkedControllerModel) Minecraft.getInstance() LinkedControllerModel mainModel = (LinkedControllerModel) Minecraft.getInstance()
.getItemRenderer() .getItemRenderer()
.getModel(stack, null, null)); .getModel(stack, null, null);
PartialItemModelRenderer renderer = PartialItemModelRenderer.of(stack, transformType, ms, buffer, overlay); PartialItemModelRenderer renderer = PartialItemModelRenderer.of(stack, transformType, ms, buffer, overlay);
boolean active = te.hasUser(); boolean active = te.hasUser();
boolean usedByMe = te.isUsedBy(Minecraft.getInstance().player); boolean renderDepression = te.isUsedBy(Minecraft.getInstance().player);
Direction facing = te.getBlockState().getValue(LecternControllerBlock.FACING); Direction facing = te.getBlockState().getValue(LecternControllerBlock.FACING);
MatrixTransformStack msr = MatrixTransformStack.of(ms); MatrixTransformStack msr = MatrixTransformStack.of(ms);
@ -41,7 +41,7 @@ public class LecternControllerRenderer extends SafeTileEntityRenderer<LecternCon
msr.rotateY(AngleHelper.horizontalAngle(facing) - 90); msr.rotateY(AngleHelper.horizontalAngle(facing) - 90);
msr.translate(0.28, 0, 0); msr.translate(0.28, 0, 0);
msr.rotateZ(-22.0); msr.rotateZ(-22.0);
LinkedControllerItemRenderer.renderLinkedController(stack, mainModel, renderer, transformType, ms, light, active, usedByMe); LinkedControllerItemRenderer.renderInLectern(stack, mainModel, renderer, transformType, ms, light, active, renderDepression);
ms.popPose(); ms.popPose();
} }

View file

@ -35,10 +35,6 @@ import net.minecraft.util.text.TextFormatting;
public class LinkedControllerClientHandler { public class LinkedControllerClientHandler {
enum Mode {
IDLE, ACTIVE, BIND
}
public static Mode MODE = Mode.IDLE; public static Mode MODE = Mode.IDLE;
public static int PACKET_RATE = 5; public static int PACKET_RATE = 5;
public static Collection<Integer> currentlyPressed = new HashSet<>(); public static Collection<Integer> currentlyPressed = new HashSet<>();
@ -112,6 +108,8 @@ public class LinkedControllerClientHandler {
if (!currentlyPressed.isEmpty()) if (!currentlyPressed.isEmpty())
AllPackets.channel.sendToServer(new LinkedControllerInputPacket(currentlyPressed, false)); AllPackets.channel.sendToServer(new LinkedControllerInputPacket(currentlyPressed, false));
currentlyPressed.clear(); currentlyPressed.clear();
LinkedControllerItemRenderer.resetButtons();
} }
protected static boolean isActuallyPressed(KeyBinding kb) { protected static boolean isActuallyPressed(KeyBinding kb) {
@ -124,6 +122,7 @@ public class LinkedControllerClientHandler {
public static void tick() { public static void tick() {
LinkedControllerItemRenderer.tick(); LinkedControllerItemRenderer.tick();
if (MODE == Mode.IDLE) if (MODE == Mode.IDLE)
return; return;
if (packetCooldown > 0) if (packetCooldown > 0)
@ -269,4 +268,8 @@ public class LinkedControllerClientHandler {
} }
public enum Mode {
IDLE, ACTIVE, BIND
}
} }

View file

@ -50,35 +50,56 @@ public class LinkedControllerItemRenderer extends CustomRenderedItemModelRendere
} }
} }
static void resetButtons() {
for (int i = 0; i < buttons.size(); i++) {
buttons.get(i).startWithValue(0);
}
}
@Override @Override
protected void render(ItemStack stack, LinkedControllerModel model, PartialItemModelRenderer renderer, protected void render(ItemStack stack, LinkedControllerModel model, PartialItemModelRenderer renderer,
ItemCameraTransforms.TransformType transformType, MatrixStack ms, IRenderTypeBuffer buffer, int light, ItemCameraTransforms.TransformType transformType, MatrixStack ms, IRenderTypeBuffer buffer, int light,
int overlay) { int overlay) {
renderNormal(stack, model, renderer, transformType, ms, light);
renderLinkedController(stack, model, renderer, transformType, ms, light, null, null);
} }
public static void renderLinkedController(ItemStack stack, LinkedControllerModel model, protected static void renderNormal(ItemStack stack, LinkedControllerModel model,
PartialItemModelRenderer renderer, ItemCameraTransforms.TransformType transformType, MatrixStack ms, PartialItemModelRenderer renderer, ItemCameraTransforms.TransformType transformType, MatrixStack ms,
int light, Boolean active, Boolean usedByMe) { int light) {
render(stack, model, renderer, transformType, ms, light, RenderType.NORMAL, false, false);
}
public static void renderInLectern(ItemStack stack, LinkedControllerModel model,
PartialItemModelRenderer renderer, ItemCameraTransforms.TransformType transformType, MatrixStack ms,
int light, boolean active, boolean renderDepression) {
render(stack, model, renderer, transformType, ms, light, RenderType.LECTERN, active, renderDepression);
}
protected static void render(ItemStack stack, LinkedControllerModel model,
PartialItemModelRenderer renderer, ItemCameraTransforms.TransformType transformType, MatrixStack ms,
int light, RenderType renderType, boolean active, boolean renderDepression) {
float pt = AnimationTickHolder.getPartialTicks(); float pt = AnimationTickHolder.getPartialTicks();
MatrixTransformStack msr = MatrixTransformStack.of(ms); MatrixTransformStack msr = MatrixTransformStack.of(ms);
ms.pushPose(); ms.pushPose();
Minecraft mc = Minecraft.getInstance(); if (renderType == RenderType.NORMAL) {
boolean rightHanded = mc.options.mainHand == HandSide.RIGHT; Minecraft mc = Minecraft.getInstance();
TransformType mainHand = boolean rightHanded = mc.options.mainHand == HandSide.RIGHT;
rightHanded ? TransformType.FIRST_PERSON_RIGHT_HAND : TransformType.FIRST_PERSON_LEFT_HAND; TransformType mainHand =
TransformType offHand = rightHanded ? TransformType.FIRST_PERSON_RIGHT_HAND : TransformType.FIRST_PERSON_LEFT_HAND;
rightHanded ? TransformType.FIRST_PERSON_LEFT_HAND : TransformType.FIRST_PERSON_RIGHT_HAND; TransformType offHand =
rightHanded ? TransformType.FIRST_PERSON_LEFT_HAND : TransformType.FIRST_PERSON_RIGHT_HAND;
if (active == null) {
active = false; active = false;
boolean noControllerInMain = !AllItems.LINKED_CONTROLLER.isIn(mc.player.getMainHandItem()); boolean noControllerInMain = !AllItems.LINKED_CONTROLLER.isIn(mc.player.getMainHandItem());
if (transformType == mainHand || (transformType == offHand && noControllerInMain)) { if (transformType == mainHand || (transformType == offHand && noControllerInMain)) {
float equip = equipProgress.getValue(pt);
int handModifier = transformType == TransformType.FIRST_PERSON_LEFT_HAND ? -1 : 1;
msr.translate(0, equip / 4, equip / 4 * handModifier);
msr.rotateY(equip * -30 * handModifier);
msr.rotateZ(equip * -30);
active = true; active = true;
} }
@ -89,56 +110,62 @@ public class LinkedControllerItemRenderer extends CustomRenderedItemModelRendere
active = true; active = true;
} }
active &= LinkedControllerClientHandler.MODE != Mode.IDLE && !LinkedControllerClientHandler.inLectern(); active &= LinkedControllerClientHandler.MODE != Mode.IDLE;
usedByMe = active;
if (active && (transformType == mainHand || transformType == offHand)) { renderDepression = true;
float equip = equipProgress.getValue(pt);
int handModifier = transformType == TransformType.FIRST_PERSON_LEFT_HAND ? -1 : 1;
msr.translate(0, equip / 4, equip / 4 * handModifier);
msr.rotateY(equip * -30 * handModifier);
msr.rotateZ(equip * -30);
}
} }
renderer.render(active ? model.getPartial("powered") : model.getOriginalModel(), light); renderer.render(active ? model.getPartial("powered") : model.getOriginalModel(), light);
if (!active) {
ms.popPose();
return;
}
IBakedModel button = model.getPartial("button"); IBakedModel button = model.getPartial("button");
float s = 1 / 16f; float s = 1 / 16f;
float b = s * -.75f; float b = s * -.75f;
int index = 0; int index = 0;
if (LinkedControllerClientHandler.MODE == Mode.BIND) { if (renderType == RenderType.NORMAL) {
int i = (int) MathHelper.lerp((MathHelper.sin(AnimationTickHolder.getRenderTime() / 4f) + 1) / 2, 5, 15); if (LinkedControllerClientHandler.MODE == Mode.BIND) {
light = i << 20; int i = (int) MathHelper.lerp((MathHelper.sin(AnimationTickHolder.getRenderTime() / 4f) + 1) / 2, 5, 15);
light = i << 20;
}
} }
ms.pushPose(); ms.pushPose();
msr.translate(2 * s, 0, 8 * s); msr.translate(2 * s, 0, 8 * s);
button(renderer, ms, light, pt, button, b, index++, usedByMe); renderButton(renderer, ms, light, pt, button, b, index++, renderDepression);
msr.translate(4 * s, 0, 0); msr.translate(4 * s, 0, 0);
button(renderer, ms, light, pt, button, b, index++, usedByMe); renderButton(renderer, ms, light, pt, button, b, index++, renderDepression);
msr.translate(-2 * s, 0, 2 * s); msr.translate(-2 * s, 0, 2 * s);
button(renderer, ms, light, pt, button, b, index++, usedByMe); renderButton(renderer, ms, light, pt, button, b, index++, renderDepression);
msr.translate(0, 0, -4 * s); msr.translate(0, 0, -4 * s);
button(renderer, ms, light, pt, button, b, index++, usedByMe); renderButton(renderer, ms, light, pt, button, b, index++, renderDepression);
ms.popPose(); ms.popPose();
msr.translate(3 * s, 0, 3 * s); msr.translate(3 * s, 0, 3 * s);
button(renderer, ms, light, pt, button, b, index++, usedByMe); renderButton(renderer, ms, light, pt, button, b, index++, renderDepression);
msr.translate(2 * s, 0, 0); msr.translate(2 * s, 0, 0);
button(renderer, ms, light, pt, button, b, index++, usedByMe); renderButton(renderer, ms, light, pt, button, b, index++, renderDepression);
ms.popPose(); ms.popPose();
} }
protected static void button(PartialItemModelRenderer renderer, MatrixStack ms, int light, float pt, IBakedModel button, protected static void renderButton(PartialItemModelRenderer renderer, MatrixStack ms, int light, float pt, IBakedModel button,
float b, int index, boolean usedByMe) { float b, int index, boolean renderDepression) {
ms.pushPose(); ms.pushPose();
float depression = usedByMe ? b * buttons.get(index).getValue(pt) : 0; if (renderDepression) {
ms.translate(0, depression, 0); float depression = b * buttons.get(index).getValue(pt);
ms.translate(0, depression, 0);
}
renderer.renderSolid(button, light); renderer.renderSolid(button, light);
ms.popPose(); ms.popPose();
} }
protected enum RenderType {
NORMAL, LECTERN;
}
} }