Added zoom functionality
This commit is contained in:
52
src/client/java/wtf/hak/survivalfabric/features/Zoom.java
Normal file
52
src/client/java/wtf/hak/survivalfabric/features/Zoom.java
Normal file
@ -0,0 +1,52 @@
|
||||
package wtf.hak.survivalfabric.features;
|
||||
|
||||
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
|
||||
import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper;
|
||||
import net.minecraft.client.MinecraftClient;
|
||||
import net.minecraft.client.option.KeyBinding;
|
||||
import net.minecraft.client.util.InputUtil;
|
||||
import net.minecraft.text.Text;
|
||||
import org.lwjgl.glfw.GLFW;
|
||||
import wtf.hak.survivalfabric.utils.MathUtils;
|
||||
|
||||
public class Zoom {
|
||||
|
||||
private static final KeyBinding ZOOM_BIND = KeyBindingHelper.registerKeyBinding(new KeyBinding("key.survivalfabric.zoom", InputUtil.Type.KEYSYM, GLFW.GLFW_KEY_C, "category.survivalfabric.survivalfabric"));
|
||||
private static final float INITIAL_ZOOM = 20f;
|
||||
|
||||
private static boolean SHOULD_ZOOM = false;
|
||||
private static float ZOOM_STEP_VALUE = 2.5f;
|
||||
private static int ZOOM_STEP = 0;
|
||||
|
||||
public static void register() {
|
||||
ClientTickEvents.END_CLIENT_TICK.register(client -> {
|
||||
if(ZOOM_BIND.isPressed())
|
||||
SHOULD_ZOOM = true;
|
||||
else {
|
||||
SHOULD_ZOOM = false;
|
||||
ZOOM_STEP = 0;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static boolean isZooming() {
|
||||
return SHOULD_ZOOM;
|
||||
}
|
||||
|
||||
public static float getZoomFov() {
|
||||
return INITIAL_ZOOM - -ZOOM_STEP * ZOOM_STEP_VALUE;
|
||||
}
|
||||
|
||||
public static void modifyStep(int step) {
|
||||
ZOOM_STEP += step;
|
||||
|
||||
// Clamp the zoom level so the FOV stays within [1, 110]
|
||||
float zoomFov = getZoomFov();
|
||||
if (zoomFov < 1) {
|
||||
ZOOM_STEP = Math.round((1 - INITIAL_ZOOM) / ZOOM_STEP_VALUE)+1;
|
||||
} else if (zoomFov > 110) {
|
||||
ZOOM_STEP = Math.round((110 - INITIAL_ZOOM) / ZOOM_STEP_VALUE);
|
||||
}
|
||||
MinecraftClient.getInstance().player.sendMessage(Text.literal(ZOOM_STEP + ""), true);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user