From 10bf53b39568418c9bbf7f3db9925b350e6535fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?der=5Ffr=C3=BChling?= Date: Wed, 7 Aug 2024 16:48:33 -0700 Subject: [PATCH] serious smoothness --- .../CreateTrainPerspectiveMod.java | 7 +-- .../create/trainperspective/Perspective.java | 31 +++++++--- .../trainperspective/mixin/EntityMixin.java | 57 ++++++++----------- 3 files changed, 50 insertions(+), 45 deletions(-) diff --git a/common/src/main/java/net/derfruhling/minecraft/create/trainperspective/CreateTrainPerspectiveMod.java b/common/src/main/java/net/derfruhling/minecraft/create/trainperspective/CreateTrainPerspectiveMod.java index 25c81f9..761a1a0 100644 --- a/common/src/main/java/net/derfruhling/minecraft/create/trainperspective/CreateTrainPerspectiveMod.java +++ b/common/src/main/java/net/derfruhling/minecraft/create/trainperspective/CreateTrainPerspectiveMod.java @@ -36,7 +36,7 @@ public void onEntityMount(Perspective persp, CarriageContraptionEntity contrapti persp.setRotationState(state); var carriage = state.getContraption(); assert carriage != null; - persp.enable(carriage.pitch, carriage.yaw); + persp.enable(carriage); } else { var state = persp.getRotationState(); state.onMounted(); @@ -60,7 +60,7 @@ public void tickStandingEntity(final CarriageContraptionEntity contraption, fina persp.setRotationState(state); var carriage = state.getContraption(); assert carriage != null; - persp.enable(carriage.pitch, carriage.yaw); + persp.enable(carriage); } else { state.update(); } @@ -69,8 +69,7 @@ public void tickStandingEntity(final CarriageContraptionEntity contraption, fina private void tickPerspectiveState(Entity player, Perspective persp, RotationState state) { var carriage = state.getContraption(); if (carriage == null) return; - persp.setLean(carriage.pitch); - persp.setYaw(carriage.yaw); + persp.setReference(carriage); player.setYRot(player.getYRot() + state.getYawDelta()); player.setYBodyRot(player.getYRot()); diff --git a/common/src/main/java/net/derfruhling/minecraft/create/trainperspective/Perspective.java b/common/src/main/java/net/derfruhling/minecraft/create/trainperspective/Perspective.java index 5042d74..38a3cbd 100644 --- a/common/src/main/java/net/derfruhling/minecraft/create/trainperspective/Perspective.java +++ b/common/src/main/java/net/derfruhling/minecraft/create/trainperspective/Perspective.java @@ -1,31 +1,46 @@ package net.derfruhling.minecraft.create.trainperspective; +import com.simibubi.create.content.trains.entity.CarriageContraptionEntity; import net.minecraft.util.Mth; +import net.minecraft.world.entity.Entity; import org.jetbrains.annotations.Nullable; public interface Perspective { - void enable(float initialLean, float initialYaw); + void enable(CarriageContraptionEntity entity); void disable(); boolean isEnabled(); - void setLean(float lean); + void setReference(CarriageContraptionEntity entity); - void setYaw(float yaw); + @Nullable + CarriageContraptionEntity getReference(); - float getLean(float f); + default float getLean(float f) { + var ref = getReference(); + if (ref == null) return 0.0f; + if (f == 1.0f) return ref.pitch * getScale(); + return Mth.lerp(f, ref.prevPitch * getPrevScale(), ref.pitch * getScale()); + } - float getYaw(float f); + default float getYaw(float f) { + var ref = getReference(); + if (ref == null) return 0.0f; + if (f == 1.0f) return ref.yaw * getScale(); + return Mth.lerp(f, ref.prevYaw * getPrevScale(), ref.yaw * getScale()); + } @Nullable RotationState getRotationState(); void setRotationState(@Nullable RotationState state); - default void diminish() { - setLean(getLean(1.0f) * 0.9f); - } + void diminish(); + + float getPrevScale(); + + float getScale(); default boolean isDiminished() { return Mth.abs(getLean(1.0f)) < 0.01f; diff --git a/common/src/main/java/net/derfruhling/minecraft/create/trainperspective/mixin/EntityMixin.java b/common/src/main/java/net/derfruhling/minecraft/create/trainperspective/mixin/EntityMixin.java index bded9c6..f916823 100644 --- a/common/src/main/java/net/derfruhling/minecraft/create/trainperspective/mixin/EntityMixin.java +++ b/common/src/main/java/net/derfruhling/minecraft/create/trainperspective/mixin/EntityMixin.java @@ -1,6 +1,7 @@ package net.derfruhling.minecraft.create.trainperspective.mixin; import com.llamalad7.mixinextras.sugar.Local; +import com.simibubi.create.content.trains.entity.CarriageContraptionEntity; import net.derfruhling.minecraft.create.trainperspective.*; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; @@ -28,7 +29,9 @@ public abstract class EntityMixin { @Unique private boolean ctp$perspectiveActive = false; @Unique - private float ctp$lean = 0.0f, ctp$yaw = 0.0f, ctp$oldLean = 0.0f, ctp$oldYaw = 0.0f; + private @Nullable CarriageContraptionEntity ctp$reference = null; + @Unique + private float ctp$scale = 1.0f, ctp$prevScale = 1.0f; @Unique private @Nullable RotationState ctp$currentState = null; @@ -74,54 +77,42 @@ public float modifyYaw(float yaw, @Local(argsOnly = true, index = 1) float pitch } else return yaw; } - public void ctp$enable(float initialLean, float initialYaw) { + public void ctp$enable(CarriageContraptionEntity entity) { ctp$perspectiveActive = true; - ctp$lean = initialLean; - ctp$yaw = initialYaw; - ctp$oldLean = initialLean; - ctp$oldYaw = initialYaw; + ctp$reference = entity; } public void ctp$disable() { ctp$perspectiveActive = false; - ctp$lean = 0.0f; - ctp$yaw = 0.0f; - ctp$oldLean = 0.0f; - ctp$oldYaw = 0.0f; + ctp$reference = null; } - public boolean ctp$isEnabled() { - return ctp$perspectiveActive; + public void ctp$setReference(CarriageContraptionEntity entity) { + ctp$reference = entity; + ctp$prevScale = ctp$scale; + ctp$scale = 1.0f; } - public void ctp$setLean(float lean) { - ctp$oldLean = ctp$lean; - ctp$lean = lean; + public CarriageContraptionEntity ctp$getReference() { + return ctp$reference; } - public void ctp$setYaw(float yaw) { - // some configurations flip between 0 and 360 constantly - // adjust accordingly - ctp$oldYaw = ctp$yaw; - ctp$yaw = yaw; - - while (ctp$yaw - ctp$oldYaw < -180.0f) { - ctp$oldYaw -= 360.0f; - } + public void ctp$diminish() { + if (ctp$scale <= 0.0f) return; + ctp$prevScale = ctp$scale; + ctp$scale -= 0.99f; + } - while (ctp$yaw - ctp$oldYaw >= 180.0f) { - ctp$oldYaw += 360.0f; - } + public float ctp$getScale() { + return ctp$scale; } - public float ctp$getLean(float f) { - if (f == 1.0f) return ctp$lean; - return Mth.lerp(f, ctp$oldLean, ctp$lean); + public float ctp$getPrevScale() { + return ctp$prevScale; } - public float ctp$getYaw(float f) { - if (f == 1.0f) return ctp$yaw; - return Mth.lerp(f, ctp$oldYaw, ctp$yaw); + public boolean ctp$isEnabled() { + return ctp$perspectiveActive; } public @Nullable RotationState ctp$getRotationState() {