Skip to content

Commit

Permalink
Finish sinkrate alert
Browse files Browse the repository at this point in the history
Signed-off-by: Octol1ttle <l1ttleofficial@outlook.com>
  • Loading branch information
Octol1ttle committed Oct 7, 2023
1 parent 8a7e96f commit 2c7cb19
Show file tree
Hide file tree
Showing 36 changed files with 507 additions and 257 deletions.
10 changes: 5 additions & 5 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ org.gradle.parallel=true
# gradlew --refresh-dependencies
# To remap the mixin locations:
# gradlew migrateMappings --mappings "1.16.1+build.9"
minecraft_version=1.20.1
yarn_mappings=1.20.1+build.5
minecraft_version=1.20.2
yarn_mappings=1.20.2+build.4
loader_version=0.14.7
#Fabric api
fabric_version=0.83.0
immediatelyfast_version=1.2.0
fabric_version=0.89.0
immediatelyfast_version=1.2.5
# Mod Properties
mod_version=2.0.0-alpha.1+mc1.20.1
mod_version=2.0.0-alpha.1+mc1.20.2
maven_group=net.torocraft
archives_base_name=flighthud
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https://services.gradle.org/distributions/gradle-8.1.1-bin.zip
distributionUrl=https://services.gradle.org/distributions/gradle-8.3-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import net.minecraft.entity.Entity;
import net.minecraft.sound.SoundCategory;
import net.minecraft.sound.SoundEvent;
import net.minecraft.util.math.MathHelper;

public class AlertSoundInstance extends EntityTrackingSoundInstance {
public AlertSoundInstance(SoundEvent sound, float volume, Entity entity, boolean repeat) {
Expand All @@ -13,7 +14,7 @@ public AlertSoundInstance(SoundEvent sound, float volume, Entity entity, boolean
}

public void setVolume(float volume) {
this.volume = Math.max(0, Math.min(1, volume));
this.volume = MathHelper.clamp(volume, 0.0f, 1.0f);
}

@Override
Expand Down
10 changes: 6 additions & 4 deletions src/main/java/net/torocraft/flighthud/FlightHud.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback;
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper;
import net.minecraft.client.option.KeyBinding;
import net.minecraft.client.util.InputUtil;
import net.torocraft.flighthud.commands.SwitchDisplayModeCommand;
Expand Down Expand Up @@ -42,15 +43,16 @@ public class FlightHud implements ClientModInitializer {
FlightHud.MODID + ".min.json",
config -> FlightHud.CONFIG_MIN = config);

private static KeyBinding keyBinding;
private static KeyBinding toggleDisplayMode;

private static void setupKeycCode() {
keyBinding = new KeyBinding("key.flighthud.toggleDisplayMode", InputUtil.Type.KEYSYM,
GLFW.GLFW_KEY_GRAVE_ACCENT, "category.flighthud.toggleDisplayMode");
toggleDisplayMode = new KeyBinding("key.flighthud.toggleDisplayMode", InputUtil.Type.KEYSYM,
GLFW.GLFW_KEY_GRAVE_ACCENT, "category.flighthud");

KeyBindingHelper.registerKeyBinding(toggleDisplayMode);

ClientTickEvents.END_CLIENT_TICK.register(client -> {
while (keyBinding.wasPressed()) {
while (toggleDisplayMode.wasPressed()) {
CONFIG_SETTINGS.toggleDisplayMode();
}
});
Expand Down
62 changes: 33 additions & 29 deletions src/main/java/net/torocraft/flighthud/HudComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,21 @@

import java.util.function.Consumer;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.font.TextRenderer;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.text.Text;
import net.minecraft.util.math.RotationAxis;
import net.torocraft.flighthud.config.HudConfig;

public abstract class HudComponent {
public static HudConfig CONFIG;

public static int drawFont(TextRenderer textRenderer, DrawContext context, Text text, float x, float y,
int color) {
context.drawText(textRenderer, text, i(x), i(y), color, false);
return 1;
}

public static void fill(DrawContext context, float x1, float y1, float x2, float y2, int color) {
context.fill(i(x1), i(y1), i(x2), i(y2), color);
}
Expand All @@ -27,15 +33,29 @@ public static float wrapHeading(float degrees) {
return degrees;
}

public static int drawFont(MinecraftClient mc, DrawContext context, String s, float x, float y,
public static int drawFont(TextRenderer textRenderer, DrawContext context, String text, float x, float y,
int color) {
context.drawText(mc.textRenderer, s, i(x), i(y), color, false);
context.drawText(textRenderer, text, i(x), i(y), color, false);
return 1;
}

public static void drawCenteredFont(MinecraftClient mc, DrawContext context, String s, float width, float y,
int color) {
context.drawText(mc.textRenderer, s, i(width - mc.textRenderer.getWidth(s)) / 2, i(y), color, false);
public static void drawHighlightedFont(TextRenderer textRenderer, DrawContext context, float x, float y, Text text, int textColor, int highlightColor, boolean highlight) {
if (highlight) {
HudRenderer.drawUnbatched(context, ctx -> {
HudComponent.fill(context, x - 1.5f, y - 1.5f, x + textRenderer.getWidth(text), y + 8.0f, highlightColor);
HudComponent.drawFont(textRenderer, context, text, x, y, textColor);
});
return;
}
HudComponent.drawFont(textRenderer, context, text, x, y, highlightColor);
}

public static void drawUnbatched(DrawContext context, Consumer<DrawContext> c) {
if (FabricLoader.getInstance().isModLoaded("immediatelyfast"))
net.torocraft.flighthud.compatibility.ImmediatelyFastBatchingAccessor.endHudBatching();
c.accept(context);
if (FabricLoader.getInstance().isModLoaded("immediatelyfast"))
net.torocraft.flighthud.compatibility.ImmediatelyFastBatchingAccessor.beginHudBatching();
}

public static void drawHorizontalLine(DrawContext context, float x1, float x2, float y, int color) {
Expand Down Expand Up @@ -66,25 +86,7 @@ public static void drawBox(DrawContext context, float x, float y, float w, int c
drawVerticalLine(context, x + w, y, y + 10, color);
}

public static void drawTextHighlight(TextRenderer renderer, DrawContext context, float x, float y, String text, int color) {
HudComponent.fill(context, x - 1.5f, y - 1.5f, x + renderer.getWidth(text), y + 8.0f, color);
}

public static void drawUnbatched(Consumer<DrawContext> c, DrawContext context) {
if (FabricLoader.getInstance().isModLoaded("immediatelyfast"))
net.torocraft.flighthud.compat.ImmediatelyFastBatchingAccessor.endHudBatching();
c.accept(context);
if (FabricLoader.getInstance().isModLoaded("immediatelyfast"))
net.torocraft.flighthud.compat.ImmediatelyFastBatchingAccessor.beginHudBatching();
}

public abstract void render(DrawContext context, MinecraftClient client);

protected void drawFont(MinecraftClient mc, DrawContext context, String s, float x, float y) {
drawFont(mc, context, s, x, y, CONFIG.color);
}

protected void drawPointer(DrawContext context, float x, float y, float rot) {
protected static void drawPointer(DrawContext context, float x, float y, float rot) {
context.getMatrices().push();
context.getMatrices().translate(x, y, 0);
context.getMatrices().multiply(RotationAxis.POSITIVE_Z.rotationDegrees(rot + 45));
Expand All @@ -93,13 +95,13 @@ protected void drawPointer(DrawContext context, float x, float y, float rot) {
context.getMatrices().pop();
}

protected void drawRightAlignedFont(MinecraftClient mc, DrawContext context, String s, float x,
protected static void drawRightAlignedFont(TextRenderer textRenderer, DrawContext context, String s, float x,
float y, int color) {
int w = mc.textRenderer.getWidth(s);
drawFont(mc, context, s, x - w, y, color);
int w = textRenderer.getWidth(s);
drawFont(textRenderer, context, s, x - w, y, color);
}

protected void drawHorizontalLineDashed(DrawContext context, float x1, float x2, float y,
protected static void drawHorizontalLineDashed(DrawContext context, float x1, float x2, float y,
int dashCount, int color) {
float width = x2 - x1;
int segmentCount = dashCount * 2 - 1;
Expand All @@ -118,4 +120,6 @@ protected void drawHorizontalLineDashed(DrawContext context, float x1, float x2,
drawHorizontalLine(context, dx1, dx2, y, color);
}
}

public abstract void render(DrawContext context, TextRenderer textRenderer);
}
51 changes: 33 additions & 18 deletions src/main/java/net/torocraft/flighthud/HudRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,49 @@

import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.font.TextRenderer;
import net.minecraft.client.gui.DrawContext;
import net.torocraft.flighthud.components.AltitudeIndicator;
import net.torocraft.flighthud.components.ElytraHealthIndicator;
import net.torocraft.flighthud.components.FlightPathIndicator;
import net.torocraft.flighthud.components.HeadingIndicator;
import net.torocraft.flighthud.components.LocationIndicator;
import net.torocraft.flighthud.components.PitchIndicator;
import net.torocraft.flighthud.components.SpeedIndicator;
import net.torocraft.flighthud.computers.FlightComputer;
import net.torocraft.flighthud.config.SettingsConfig.DisplayMode;
import net.torocraft.flighthud.indicators.AlertIndicator;
import net.torocraft.flighthud.indicators.AltitudeIndicator;
import net.torocraft.flighthud.indicators.ElytraHealthIndicator;
import net.torocraft.flighthud.indicators.FlightPathIndicator;
import net.torocraft.flighthud.indicators.HeadingIndicator;
import net.torocraft.flighthud.indicators.LocationIndicator;
import net.torocraft.flighthud.indicators.PitchIndicator;
import net.torocraft.flighthud.indicators.SpeedIndicator;
import org.jetbrains.annotations.NotNull;

public class HudRenderer extends HudComponent {
private static final String FULL = DisplayMode.FULL.toString();
private static final String MIN = DisplayMode.MIN.toString();
public static HudRenderer INSTANCE;
@NotNull
public final FlightComputer computer;
private final Dimensions dim = new Dimensions();
private final HudComponent[] components;

public HudRenderer(MinecraftClient mc) {
this.computer = new FlightComputer(mc);
this.components = new HudComponent[]{
new FlightPathIndicator(computer, dim), new LocationIndicator(dim),
new FlightPathIndicator(computer, dim), new LocationIndicator(computer, dim),
new HeadingIndicator(computer, dim), new SpeedIndicator(computer, dim),
new AltitudeIndicator(computer, dim), new PitchIndicator(computer, dim),
new ElytraHealthIndicator(computer, dim)
new ElytraHealthIndicator(computer, dim), new AlertIndicator(computer, dim)
};
}

private void setupConfig(MinecraftClient client) {
public static FlightComputer getComputer() {
if (INSTANCE == null) {
return null;
}
return INSTANCE.computer;
}

private void setupConfig() {
HudComponent.CONFIG = null;
if (client.player.isFallFlying()) {
if (computer.getPlayer().isFallFlying()) {
if (FlightHud.CONFIG_SETTINGS.displayModeWhenFlying.equals(FULL)) {
HudComponent.CONFIG = FlightHud.CONFIG_FULL;
} else if (FlightHud.CONFIG_SETTINGS.displayModeWhenFlying.equals(MIN)) {
Expand All @@ -48,9 +59,8 @@ private void setupConfig(MinecraftClient client) {
}
}

@Override
public void render(DrawContext context, MinecraftClient client) {
setupConfig(client);
public void render(DrawContext context, MinecraftClient mc) {
setupConfig();

if (HudComponent.CONFIG == null) {
return;
Expand All @@ -64,18 +74,23 @@ public void render(DrawContext context, MinecraftClient client) {
context.getMatrices().scale(scale, scale, scale);
}

dim.update(client);
dim.update(mc);

for (HudComponent component : components) {
if (FabricLoader.getInstance().isModLoaded("immediatelyfast"))
net.torocraft.flighthud.compat.ImmediatelyFastBatchingAccessor.beginHudBatching();
component.render(context, client);
net.torocraft.flighthud.compatibility.ImmediatelyFastBatchingAccessor.beginHudBatching();
component.render(context, mc.textRenderer);
if (FabricLoader.getInstance().isModLoaded("immediatelyfast"))
net.torocraft.flighthud.compat.ImmediatelyFastBatchingAccessor.endHudBatching();
net.torocraft.flighthud.compatibility.ImmediatelyFastBatchingAccessor.endHudBatching();
}
context.getMatrices().pop();
} catch (Exception e) {
FlightHud.LOGGER.error("Exception rendering components", e);
}
}

@Override
public void render(DrawContext context, TextRenderer textRenderer) {
throw new IllegalStateException();
}
}
31 changes: 31 additions & 0 deletions src/main/java/net/torocraft/flighthud/alerts/AlertSoundData.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package net.torocraft.flighthud.alerts;

import net.minecraft.sound.SoundEvent;
import org.jetbrains.annotations.Nullable;

public class AlertSoundData {
public static final AlertSoundData EMPTY = new AlertSoundData(
null,
Integer.MAX_VALUE,
0.0f,
false
);
public final @Nullable SoundEvent sound;
public final float volume;
public final boolean repeat;
private final int priority;

public AlertSoundData(@Nullable SoundEvent sound, int priority, float volume, boolean repeat) {
this.sound = sound;
this.priority = priority;
this.volume = volume;
this.repeat = repeat;
}

/**
* @return the alert's priority, where 0 is highest priority. The priority will go down by 0.5 if the sound is repeating
*/
public double getPriority() {
return this.priority - (this.repeat ? 0.5 : 0.0);
}
}
27 changes: 0 additions & 27 deletions src/main/java/net/torocraft/flighthud/alerts/ECAMAlert.java

This file was deleted.

7 changes: 4 additions & 3 deletions src/main/java/net/torocraft/flighthud/alerts/IAlert.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package net.torocraft.flighthud.alerts;

import net.minecraft.client.MinecraftClient;
import net.minecraft.client.font.TextRenderer;
import net.minecraft.client.gui.DrawContext;
import org.jetbrains.annotations.NotNull;

public interface IAlert {
boolean isTriggered();

void tick();
@NotNull AlertSoundData getAlertSoundData();

int drawText(MinecraftClient mc, DrawContext context, float x, float y, boolean highlight);
void renderCentered(TextRenderer textRenderer, DrawContext context, float width, float y, boolean highlight);
}
Loading

0 comments on commit 2c7cb19

Please sign in to comment.