Skip to content

Commit

Permalink
serious smoothness
Browse files Browse the repository at this point in the history
  • Loading branch information
der-fruhling committed Aug 7, 2024
1 parent 6f122ff commit 10bf53b
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
}
Expand All @@ -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());

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit 10bf53b

Please sign in to comment.