Skip to content

Commit

Permalink
Stall warning, protection and automatic recovery
Browse files Browse the repository at this point in the history
Signed-off-by: Octol1ttle <l1ttleofficial@outlook.com>
  • Loading branch information
Octol1ttle committed Oct 8, 2023
1 parent 2ee44e7 commit 2390a93
Show file tree
Hide file tree
Showing 28 changed files with 246 additions and 114 deletions.
10 changes: 5 additions & 5 deletions src/main/java/net/torocraft/flighthud/Dimensions.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,17 @@ public void update(MinecraftClient client) {
wScreen = wScreen * c.scale;
}

degreesPerPixel = hScreen / client.options.getFov().getValue();
xMid = wScreen / 2;
yMid = hScreen / 2;
degreesPerPixel = hScreen / (float) client.options.getFov().getValue();
xMid = wScreen * 0.5f;
yMid = hScreen * 0.5f;

wFrame = wScreen * c.width;
hFrame = hScreen * c.height;

lFrame = ((wScreen - wFrame) / 2) + c.xOffset;
lFrame = ((wScreen - wFrame) * 0.5f) + c.xOffset;
rFrame = lFrame + wFrame;

tFrame = ((hScreen - hFrame) / 2) + c.yOffset;
tFrame = ((hScreen - hFrame) * 0.5f) + c.yOffset;
bFrame = tFrame + hFrame;
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/torocraft/flighthud/FlightHud.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ private static void setupKeycCode() {

ClientTickEvents.END_CLIENT_TICK.register(client -> {
while (toggleDisplayMode.wasPressed()) {
CONFIG_SETTINGS.toggleDisplayMode();
CONFIG_SETTINGS.toggleDisplayMode(client);
}
});
}
Expand Down
14 changes: 3 additions & 11 deletions src/main/java/net/torocraft/flighthud/HudComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,17 @@ protected static int i(float f) {
return Math.round(f);
}

public static float wrapHeading(float degrees) {
degrees = degrees % 360;
while (degrees < 0) {
degrees += 360;
}
return degrees;
}

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

public static void drawHighlightedFont(TextRenderer textRenderer, DrawContext context, float x, float y, Text text, int textColor, int highlightColor, boolean highlight) {
public static void drawHighlightedFont(TextRenderer textRenderer, DrawContext context, float x, float y, Text text, 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);
HudComponent.drawFont(textRenderer, context, text, x, y, CONFIG.white);
});
return;
}
Expand Down Expand Up @@ -105,7 +97,7 @@ protected static void drawHorizontalLineDashed(DrawContext context, float x1, fl
int dashCount, int color) {
float width = x2 - x1;
int segmentCount = dashCount * 2 - 1;
float dashSize = width / segmentCount;
float dashSize = width / (float) segmentCount;
for (int i = 0; i < segmentCount; i++) {
if (i % 2 != 0) {
continue;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/torocraft/flighthud/HudRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public static FlightComputer getComputer() {

private void setupConfig() {
HudComponent.CONFIG = null;
if (computer.getPlayer().isFallFlying()) {
if (computer.player.isFallFlying()) {
if (FlightHud.CONFIG_SETTINGS.displayModeWhenFlying.equals(FULL)) {
HudComponent.CONFIG = FlightHud.CONFIG_FULL;
} else if (FlightHud.CONFIG_SETTINGS.displayModeWhenFlying.equals(MIN)) {
Expand Down
9 changes: 0 additions & 9 deletions src/main/java/net/torocraft/flighthud/Util.java

This file was deleted.

2 changes: 1 addition & 1 deletion src/main/java/net/torocraft/flighthud/alerts/IAlert.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ public interface IAlert {

@NotNull AlertSoundData getAlertSoundData();

void renderCentered(TextRenderer textRenderer, DrawContext context, float width, float y, boolean highlight);
boolean renderCentered(TextRenderer textRenderer, DrawContext context, float width, float y, boolean highlight);
}
50 changes: 50 additions & 0 deletions src/main/java/net/torocraft/flighthud/alerts/StallAlert.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package net.torocraft.flighthud.alerts;

import net.minecraft.client.font.TextRenderer;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.sound.SoundEvent;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import net.torocraft.flighthud.HudComponent;
import net.torocraft.flighthud.computers.FlightComputer;
import net.torocraft.flighthud.computers.GPWSComputer;
import net.torocraft.flighthud.computers.StallComputer;
import org.jetbrains.annotations.NotNull;

import static net.torocraft.flighthud.HudComponent.CONFIG;

public class StallAlert implements IAlert {
private static final AlertSoundData STALL = new AlertSoundData(
SoundEvent.of(new Identifier("flighthud:stall")),
0,
0.75f,
true
);
private final FlightComputer computer;

public StallAlert(FlightComputer computer) {
this.computer = computer;
}

@Override
public boolean isTriggered() {
return computer.stall.stalling >= StallComputer.STATUS_APPROACHING_STALL;
}

@Override
public @NotNull AlertSoundData getAlertSoundData() {
return STALL;
}

@Override
public boolean renderCentered(TextRenderer textRenderer, DrawContext context, float width, float y, boolean highlight) {
Text text = Text.translatable("alerts.flighthud.stall");
float startX = (width - textRenderer.getWidth(text)) * 0.5f;
HudComponent.drawHighlightedFont(textRenderer, context, startX, y, text,
-computer.velocityPerSecond.y >= GPWSComputer.MAX_SAFE_SINK_RATE ?
CONFIG.alertColor :
CONFIG.amberColor, highlight);

return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,24 @@

import static net.torocraft.flighthud.HudComponent.CONFIG;

public class UnsafeSinkrateAlert implements IAlert {
public static final float SINKRATE_THRESHOLD = 7.5f;
public static final AlertSoundData SINKRATE = new AlertSoundData(
SoundEvent.of(new Identifier("flighthud:sinkrate")),
public class ExcessiveDescentRateAlert implements IAlert {
private static final float SINK_RATE_THRESHOLD = 7.5f;
private static final AlertSoundData SINK_RATE = new AlertSoundData(
SoundEvent.of(new Identifier("flighthud:sink_rate")),
1,
0.5f,
0.75f,
false
);
public static final float PULL_UP_THRESHOLD = 5.0f;
public static final AlertSoundData PULL_UP = new AlertSoundData(
private static final float PULL_UP_THRESHOLD = 5.0f;
private static final AlertSoundData PULL_UP = new AlertSoundData(
SoundEvent.of(new Identifier("flighthud:pull_up")),
1,
0.5f,
0.75f,
false
);
private final FlightComputer computer;

public UnsafeSinkrateAlert(FlightComputer computer) {
public ExcessiveDescentRateAlert(FlightComputer computer) {
this.computer = computer;
}

Expand All @@ -41,30 +41,35 @@ public boolean isTriggered() {
}

@Override
public void renderCentered(TextRenderer textRenderer, DrawContext context, float width, float y, boolean highlight) {
public boolean renderCentered(TextRenderer textRenderer, DrawContext context, float width, float y, boolean highlight) {
if (computer.gpws.impactTime <= PULL_UP_THRESHOLD) {
Text text = Text.translatable("alerts.flighthud.pull_up");
float startX = (width - textRenderer.getWidth(text)) / 2;
float startX = (width - textRenderer.getWidth(text)) * 0.5f;
HudComponent.drawHighlightedFont(textRenderer, context, startX, y, text,
CONFIG.white, CONFIG.alertColor, highlight);
return;
CONFIG.alertColor, highlight);

return true;
}

if (computer.gpws.impactTime <= SINKRATE_THRESHOLD) {
Text text = Text.translatable("alerts.flighthud.sinkrate");
float startX = (width - textRenderer.getWidth(text)) / 2;
if (computer.gpws.impactTime <= SINK_RATE_THRESHOLD) {
Text text = Text.translatable("alerts.flighthud.sink_rate");
float startX = (width - textRenderer.getWidth(text)) * 0.5f;
HudComponent.drawHighlightedFont(textRenderer, context, startX, y, text,
CONFIG.white, CONFIG.amberColor, highlight);
CONFIG.amberColor, highlight);

return true;
}

return false;
}

@Override
public @NotNull AlertSoundData getAlertSoundData() {
if (computer.gpws.impactTime <= PULL_UP_THRESHOLD) {
return PULL_UP;
}
if (computer.gpws.impactTime <= SINKRATE_THRESHOLD) {
return SINKRATE;
if (computer.gpws.impactTime <= SINK_RATE_THRESHOLD) {
return SINK_RATE;
}

return AlertSoundData.EMPTY;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class SwitchDisplayModeCommand implements Command<FabricClientCommandSour

@Override
public int run(CommandContext<FabricClientCommandSource> context) {
FlightHud.CONFIG_SETTINGS.toggleDisplayMode();
FlightHud.CONFIG_SETTINGS.toggleDisplayMode(context.getSource().getClient());
return 0;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@
import net.torocraft.flighthud.AlertSoundInstance;
import net.torocraft.flighthud.alerts.AlertSoundData;
import net.torocraft.flighthud.alerts.IAlert;
import net.torocraft.flighthud.alerts.nav.gpws.UnsafeSinkrateAlert;
import net.torocraft.flighthud.alerts.StallAlert;
import net.torocraft.flighthud.alerts.nav.gpws.ExcessiveDescentRateAlert;
import org.jetbrains.annotations.Nullable;

import static net.minecraft.SharedConstants.TICKS_PER_SECOND;

public class AlertController {
private static final int SOUND_DELAY = (int) (TICKS_PER_SECOND * 0.2f);
public final List<IAlert> activeAlerts;
private final FlightComputer computer;
private final SoundManager manager;
Expand All @@ -22,7 +26,7 @@ public AlertController(FlightComputer computer, SoundManager manager) {
this.computer = computer;
this.manager = manager;
ALERTS = new IAlert[]{
new UnsafeSinkrateAlert(computer)
new ExcessiveDescentRateAlert(computer), new StallAlert(computer)
};
activeAlerts = new ArrayList<>();
}
Expand Down Expand Up @@ -56,8 +60,8 @@ public void tick() {
if (activeSound != null) {
manager.stop(activeSound);
}
activeSound = new AlertSoundInstance(data.sound, data.volume, computer.getPlayer(), data.repeat);
manager.play(activeSound);
activeSound = new AlertSoundInstance(data.sound, data.volume, computer.player, data.repeat);
manager.play(activeSound, SOUND_DELAY);
}

break;
Expand Down
Loading

0 comments on commit 2390a93

Please sign in to comment.