Skip to content

Commit

Permalink
銃のADS時の揺れを調整
Browse files Browse the repository at this point in the history
  • Loading branch information
uTen2c committed Sep 30, 2023
1 parent feefbdd commit 1a48457
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import dev.uten2c.raincoat.util.GunState;
import dev.uten2c.raincoat.util.StackUtils;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.network.AbstractClientPlayerEntity;
import net.minecraft.client.option.SimpleOption;
import net.minecraft.client.render.GameRenderer;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.entity.player.PlayerEntity;
Expand All @@ -19,13 +21,10 @@
@Mixin(GameRenderer.class)
public abstract class MixinGameRenderer {
@Unique
private long lastNoBobbingTime = 0L;
private float worldBobbingMultiplier = 0f;

@Unique
private float lastScale = 0L;

@Unique
private static final int MARGIN_MILLIS = 50;
private float handBobbingMultiplier = 0f;

@Shadow
protected abstract void bobView(MatrixStack matrices, float tickDelta);
Expand All @@ -34,34 +33,74 @@ public abstract class MixinGameRenderer {
@Final
MinecraftClient client;

@Redirect(method = "renderWorld", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/render/GameRenderer;bobView(Lnet/minecraft/client/util/math/MatrixStack;F)V"))
private void bobView1(GameRenderer instance, MatrixStack matrices, float tickDelta) {
var cameraEntity = client.getCameraEntity();
if (!States.isOnServer() || !(cameraEntity instanceof AbstractClientPlayerEntity player) || !StackUtils.isGun(player.getMainHandStack())) {
bobView(matrices, tickDelta);
return;
}

final var mainHandStack = player.getMainHandStack();
final var gunState = StackUtils.getGunState(mainHandStack);

final var lastFrameDuration = this.client.getLastFrameDuration();
if (gunState == GunState.ADS) {
worldBobbingMultiplier -= 0.1f * lastFrameDuration;
} else {
worldBobbingMultiplier += 0.1f * lastFrameDuration;
}
worldBobbingMultiplier = MathHelper.clamp(worldBobbingMultiplier, 0f, 1f);

raincoat$bobView(matrices, tickDelta, worldBobbingMultiplier);
}

@Redirect(method = "renderHand", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/render/GameRenderer;bobView(Lnet/minecraft/client/util/math/MatrixStack;F)V"))
private void bobView(GameRenderer instance, MatrixStack matrices, float tickDelta) {
private void bobView2(GameRenderer instance, MatrixStack matrices, float tickDelta) {
var cameraEntity = client.getCameraEntity();
if (States.isOnServer() && cameraEntity instanceof PlayerEntity player && StackUtils.isGun(player.getMainHandStack())) {
var mainHandStack = player.getMainHandStack();
var gunState = StackUtils.getGunState(mainHandStack);

final var isFirstPerson = client.options.getPerspective().isFirstPerson();
var gunModelState = StackUtils.getGunModelState(isFirstPerson, mainHandStack);
var targetScale = StackUtils.getGunModelScale(gunModelState);

if (gunState == GunState.ADS || System.currentTimeMillis() - lastNoBobbingTime <= MARGIN_MILLIS) {
targetScale *= 0.01f;
if (gunState == GunState.ADS) {
lastNoBobbingTime = System.currentTimeMillis();
}
}
var scale = MathHelper.lerp(tickDelta, lastScale, targetScale);
lastScale = scale;

float f = (player.horizontalSpeed - player.prevHorizontalSpeed);
float g = -(player.horizontalSpeed + f * tickDelta);
float h = MathHelper.lerp(tickDelta, player.prevStrideDistance, player.strideDistance) * scale;
matrices.translate(MathHelper.sin(g * (float) Math.PI) * h * 0.5f, -Math.abs(MathHelper.cos(g * (float) Math.PI) * h), 0.0);
matrices.multiply(RotationAxis.POSITIVE_Z.rotationDegrees(MathHelper.sin(g * (float) Math.PI) * h * 3.0f));
matrices.multiply(RotationAxis.POSITIVE_X.rotationDegrees(Math.abs(MathHelper.cos(g * (float) Math.PI - 0.2f) * h) * 5.0f));
if (!States.isOnServer() || !(cameraEntity instanceof AbstractClientPlayerEntity player) || !StackUtils.isGun(player.getMainHandStack())) {
bobView(matrices, tickDelta);
return;
}

final var mainHandStack = player.getMainHandStack();
final var gunState = StackUtils.getGunState(mainHandStack);

final var isFirstPerson = client.options.getPerspective().isFirstPerson();
final var gunModelState = StackUtils.getGunModelState(isFirstPerson, mainHandStack);
final var targetScale = StackUtils.getGunModelScale(gunModelState);
final var minMultiplier = player.getFovMultiplier() * 0.0075f;

final var lastFrameDuration = this.client.getLastFrameDuration();
if (gunState == GunState.ADS) {
handBobbingMultiplier = 0f;
} else {
handBobbingMultiplier += 0.1f * targetScale * lastFrameDuration;
}
handBobbingMultiplier = MathHelper.clamp(handBobbingMultiplier, minMultiplier, targetScale);

raincoat$bobView(matrices, tickDelta, handBobbingMultiplier);
}

@Redirect(method = "renderHand", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/option/SimpleOption;getValue()Ljava/lang/Object;"))
private Object forceEnableIfItemIsGun(SimpleOption<Boolean> instance) {
var cameraEntity = client.getCameraEntity();
if (!States.isOnServer() || !(cameraEntity instanceof AbstractClientPlayerEntity player) || !StackUtils.isGun(player.getMainHandStack())) {
return instance.getValue();
}
return true;
}

@Unique
private void raincoat$bobView(MatrixStack matrices, float tickDelta, float multiplier) {
if (!(this.client.getCameraEntity() instanceof PlayerEntity player)) {
return;
}
bobView(matrices, tickDelta);
float f = (player.horizontalSpeed - player.prevHorizontalSpeed);
float g = -(player.horizontalSpeed + f * tickDelta);
float h = MathHelper.lerp(tickDelta, player.prevStrideDistance, player.strideDistance) * multiplier;
matrices.translate(MathHelper.sin(g * (float) Math.PI) * h * 0.5f, -Math.abs(MathHelper.cos(g * (float) Math.PI) * h), 0.0);
matrices.multiply(RotationAxis.POSITIVE_Z.rotationDegrees(MathHelper.sin(g * (float) Math.PI) * h * 3.0f));
matrices.multiply(RotationAxis.POSITIVE_X.rotationDegrees(Math.abs(MathHelper.cos(g * (float) Math.PI - 0.2f) * h) * 5.0f));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ class RaincoatMod : ClientModInitializer {
ResourceManagerHelper.get(ResourceType.CLIENT_RESOURCES).registerReloadListener(RaincoatModelReloadListener())

ModelLoadingRegistry.INSTANCE.registerResourceProvider(::RaincoatModelProvider)

}
}

0 comments on commit 1a48457

Please sign in to comment.