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();
TransformType transformType = TransformType.NONE;
LinkedControllerModel mainModel = ((LinkedControllerModel) Minecraft.getInstance()
LinkedControllerModel mainModel = (LinkedControllerModel) Minecraft.getInstance()
.getItemRenderer()
.getModel(stack, null, null));
.getModel(stack, null, null);
PartialItemModelRenderer renderer = PartialItemModelRenderer.of(stack, transformType, ms, buffer, overlay);
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);
MatrixTransformStack msr = MatrixTransformStack.of(ms);
@ -41,7 +41,7 @@ public class LecternControllerRenderer extends SafeTileEntityRenderer<LecternCon
msr.rotateY(AngleHelper.horizontalAngle(facing) - 90);
msr.translate(0.28, 0, 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();
}

View file

@ -35,10 +35,6 @@ import net.minecraft.util.text.TextFormatting;
public class LinkedControllerClientHandler {
enum Mode {
IDLE, ACTIVE, BIND
}
public static Mode MODE = Mode.IDLE;
public static int PACKET_RATE = 5;
public static Collection<Integer> currentlyPressed = new HashSet<>();
@ -112,6 +108,8 @@ public class LinkedControllerClientHandler {
if (!currentlyPressed.isEmpty())
AllPackets.channel.sendToServer(new LinkedControllerInputPacket(currentlyPressed, false));
currentlyPressed.clear();
LinkedControllerItemRenderer.resetButtons();
}
protected static boolean isActuallyPressed(KeyBinding kb) {
@ -124,6 +122,7 @@ public class LinkedControllerClientHandler {
public static void tick() {
LinkedControllerItemRenderer.tick();
if (MODE == Mode.IDLE)
return;
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
protected void render(ItemStack stack, LinkedControllerModel model, PartialItemModelRenderer renderer,
ItemCameraTransforms.TransformType transformType, MatrixStack ms, IRenderTypeBuffer buffer, int light,
int overlay) {
renderLinkedController(stack, model, renderer, transformType, ms, light, null, null);
renderNormal(stack, model, renderer, transformType, ms, light);
}
public static void renderLinkedController(ItemStack stack, LinkedControllerModel model,
protected static void renderNormal(ItemStack stack, LinkedControllerModel model,
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();
MatrixTransformStack msr = MatrixTransformStack.of(ms);
ms.pushPose();
Minecraft mc = Minecraft.getInstance();
boolean rightHanded = mc.options.mainHand == HandSide.RIGHT;
TransformType mainHand =
rightHanded ? TransformType.FIRST_PERSON_RIGHT_HAND : TransformType.FIRST_PERSON_LEFT_HAND;
TransformType offHand =
rightHanded ? TransformType.FIRST_PERSON_LEFT_HAND : TransformType.FIRST_PERSON_RIGHT_HAND;
if (renderType == RenderType.NORMAL) {
Minecraft mc = Minecraft.getInstance();
boolean rightHanded = mc.options.mainHand == HandSide.RIGHT;
TransformType mainHand =
rightHanded ? TransformType.FIRST_PERSON_RIGHT_HAND : TransformType.FIRST_PERSON_LEFT_HAND;
TransformType offHand =
rightHanded ? TransformType.FIRST_PERSON_LEFT_HAND : TransformType.FIRST_PERSON_RIGHT_HAND;
if (active == null) {
active = false;
boolean noControllerInMain = !AllItems.LINKED_CONTROLLER.isIn(mc.player.getMainHandItem());
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;
}
@ -89,56 +110,62 @@ public class LinkedControllerItemRenderer extends CustomRenderedItemModelRendere
active = true;
}
active &= LinkedControllerClientHandler.MODE != Mode.IDLE && !LinkedControllerClientHandler.inLectern();
usedByMe = active;
active &= LinkedControllerClientHandler.MODE != Mode.IDLE;
if (active && (transformType == mainHand || transformType == offHand)) {
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);
}
renderDepression = true;
}
renderer.render(active ? model.getPartial("powered") : model.getOriginalModel(), light);
if (!active) {
ms.popPose();
return;
}
IBakedModel button = model.getPartial("button");
float s = 1 / 16f;
float b = s * -.75f;
int index = 0;
if (LinkedControllerClientHandler.MODE == Mode.BIND) {
int i = (int) MathHelper.lerp((MathHelper.sin(AnimationTickHolder.getRenderTime() / 4f) + 1) / 2, 5, 15);
light = i << 20;
if (renderType == RenderType.NORMAL) {
if (LinkedControllerClientHandler.MODE == Mode.BIND) {
int i = (int) MathHelper.lerp((MathHelper.sin(AnimationTickHolder.getRenderTime() / 4f) + 1) / 2, 5, 15);
light = i << 20;
}
}
ms.pushPose();
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);
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);
button(renderer, ms, light, pt, button, b, index++, usedByMe);
renderButton(renderer, ms, light, pt, button, b, index++, renderDepression);
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();
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);
button(renderer, ms, light, pt, button, b, index++, usedByMe);
renderButton(renderer, ms, light, pt, button, b, index++, renderDepression);
ms.popPose();
}
protected static void button(PartialItemModelRenderer renderer, MatrixStack ms, int light, float pt, IBakedModel button,
float b, int index, boolean usedByMe) {
protected static void renderButton(PartialItemModelRenderer renderer, MatrixStack ms, int light, float pt, IBakedModel button,
float b, int index, boolean renderDepression) {
ms.pushPose();
float depression = usedByMe ? b * buttons.get(index).getValue(pt) : 0;
ms.translate(0, depression, 0);
if (renderDepression) {
float depression = b * buttons.get(index).getValue(pt);
ms.translate(0, depression, 0);
}
renderer.renderSolid(button, light);
ms.popPose();
}
protected enum RenderType {
NORMAL, LECTERN;
}
}