From bc2a51508f36c6ee21437a6b5b200fba7c660bbb Mon Sep 17 00:00:00 2001 From: Christian Mesh Date: Mon, 11 Dec 2023 14:18:49 -0500 Subject: [PATCH] Smooth diesel locomotive throttles between notches Same code as the RPM sound code --- .../entity/LocomotiveDiesel.java | 20 +++++++++---------- .../model/DieselLocomotiveModel.java | 6 +++--- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/main/java/cam72cam/immersiverailroading/entity/LocomotiveDiesel.java b/src/main/java/cam72cam/immersiverailroading/entity/LocomotiveDiesel.java index 54d2ee60c..5182b9f05 100644 --- a/src/main/java/cam72cam/immersiverailroading/entity/LocomotiveDiesel.java +++ b/src/main/java/cam72cam/immersiverailroading/entity/LocomotiveDiesel.java @@ -21,7 +21,7 @@ public class LocomotiveDiesel extends Locomotive { - private float soundThrottle; + private float relativeRPM; private float internalBurn = 0; private int turnOnOffDelay = 0; @@ -163,7 +163,7 @@ public double getAppliedTractiveEffort(Speed speed) { double efficiency = 0.82; // Similar to a *lot* of imperial references double speed_M_S = (Math.abs(speed.metric())/3.6); double maxPowerAtSpeed = maxPower_W * efficiency / Math.max(0.001, speed_M_S); - return maxPowerAtSpeed * getThrottle() * getReverser(); + return maxPowerAtSpeed * relativeRPM * getReverser(); } return 0; } @@ -176,13 +176,13 @@ public void onTick() { turnOnOffDelay -= 1; } + float absThrottle = Math.abs(this.getThrottle()); + if (this.relativeRPM > absThrottle) { + this.relativeRPM -= Math.min(0.01f, this.relativeRPM - absThrottle); + } else if (this.relativeRPM < absThrottle) { + this.relativeRPM += Math.min(0.01f, absThrottle - this.relativeRPM); + } if (getWorld().isClient) { - float absThrottle = Math.abs(this.getThrottle()); - if (this.soundThrottle > absThrottle) { - this.soundThrottle -= Math.min(0.01f, this.soundThrottle - absThrottle); - } else if (this.soundThrottle < absThrottle) { - this.soundThrottle += Math.min(0.01f, absThrottle - this.soundThrottle); - } return; } @@ -261,8 +261,8 @@ public void onDissassemble() { setTurnedOn(false); } - public float getSoundThrottle() { - return soundThrottle; + public float getRelativeRPM() { + return relativeRPM; } @Override diff --git a/src/main/java/cam72cam/immersiverailroading/model/DieselLocomotiveModel.java b/src/main/java/cam72cam/immersiverailroading/model/DieselLocomotiveModel.java index 247b118ee..2d58a36b9 100644 --- a/src/main/java/cam72cam/immersiverailroading/model/DieselLocomotiveModel.java +++ b/src/main/java/cam72cam/immersiverailroading/model/DieselLocomotiveModel.java @@ -76,14 +76,14 @@ protected void effects(LocomotiveDiesel stock) { : 0); if (idle != null) { if (stock.isRunning()) { - float volume = Math.max(0.1f, stock.getSoundThrottle()); + float volume = Math.max(0.1f, stock.getRelativeRPM()); float pitchRange = stock.getDefinition().getEnginePitchRange(); - float pitch = (1-pitchRange) + stock.getSoundThrottle() * pitchRange; + float pitch = (1-pitchRange) + stock.getRelativeRPM() * pitchRange; if (running == null) { // Simple idle.effects(stock, volume, pitch); } else { - boolean isThrottledUp = stock.getSoundThrottle() > 0.01; + boolean isThrottledUp = stock.getRelativeRPM() > 0.01; float fade = runningFade.getOrDefault(stock.getUUID(), 0f); fade += 0.05f * (isThrottledUp ? 1 : -1); fade = Math.min(Math.max(fade, 0), 1);