Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Animate hud and hide hud if engine is off #94

Open
wants to merge 2 commits into
base: 1.20
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,39 @@
import net.minecraftforge.client.gui.overlay.IGuiOverlay;
import net.minecraftforge.client.gui.overlay.VanillaGuiOverlay;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import org.joml.Math;

public final class HudHandler {
private static final ResourceLocation HUD_TEXTURE = new ResourceLocation(IronJetpacks.MOD_ID, "textures/gui/hud.png");

// Animation progress = 0 means that the hud is currently shown, 1 for hidden
private static double animationProgress = 0;
private static boolean wasHidden = false;
private static final IGuiOverlay HUD_OVERLAY = (gui, gfx, partialTick, width, height) -> {
var mc = Minecraft.getInstance();
if (mc.player != null && isVisible(mc)) {
var chest = JetpackUtils.getEquippedJetpack(mc.player);
var item = chest.getItem();

if (!chest.isEmpty() && item instanceof JetpackItem) {
var pos = getHudPos();

// Check if hud should be hidden
if (ModConfigs.HIDE_HUD_ON_ENGINE_OFF.get() && JetpackUtils.isEngineOn(chest) && animationProgress > 0) {
wasHidden = false;
} else if (!ModConfigs.HIDE_HUD_ON_ENGINE_OFF.get()) {
wasHidden = false;
} else if (!JetpackUtils.isEngineOn(chest) && animationProgress < 1) {
wasHidden = true;
}

// Animate the hud
var animationStep = wasHidden ? ModConfigs.HUD_ANIMATION_SPEED.get() : -ModConfigs.HUD_ANIMATION_SPEED.get();
animationProgress = Math.clamp(0, animationStep + animationProgress, 1);

if (animationProgress == 1)
return;

var pos = getHudPos(animationProgress);
if (pos != null) {
int xPos = (int) (pos.x / 0.33) - 18;
int yPos = (int) (pos.y / 0.33) - 78;
Expand Down Expand Up @@ -56,6 +77,9 @@ public final class HudHandler {
gfx.drawString(mc.font, hover, pos.x + 6, pos.y + 14, 16383998);
}
}
} else {
wasHidden = true;
animationProgress = 1;
}
}
};
Expand All @@ -65,12 +89,12 @@ public void onRegisterGuiOverlays(RegisterGuiOverlaysEvent event) {
event.registerAbove(VanillaGuiOverlay.HOTBAR.id(), "jetpack_hud", HUD_OVERLAY);
}

private static HudPos getHudPos() {
private static HudPos getHudPos(double animationProgress) {
var window = Minecraft.getInstance().getWindow();
int xOffset = ModConfigs.HUD_OFFSET_X.get();
int yOffset = ModConfigs.HUD_OFFSET_Y.get();

return switch (ModConfigs.HUD_POSITION.get()) {
var pos = switch (ModConfigs.HUD_POSITION.get()) {
case 0 -> new HudPos(10 + xOffset, 30 + yOffset, 0);
case 1 -> new HudPos(10 + xOffset, window.getGuiScaledHeight() / 2 + yOffset, 0);
case 2 -> new HudPos(10 + xOffset, window.getGuiScaledHeight() - 30 + yOffset, 0);
Expand All @@ -82,6 +106,18 @@ private static HudPos getHudPos() {
default -> null;
};

if (pos == null)
return null;

pos.x = getOffset(pos, animationProgress);
return pos;
}

private static int getOffset(HudPos pos, double progress) {
if (pos.side == 0)
return (int) (pos.x - progress * 70);
else
return (int) (pos.x + progress * 70);
}

private static int getEnergyBarScaled(ItemStack stack) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public final class ModConfigs {
public static final ForgeConfigSpec.IntValue HUD_POSITION;
public static final ForgeConfigSpec.IntValue HUD_OFFSET_X;
public static final ForgeConfigSpec.IntValue HUD_OFFSET_Y;
public static final ForgeConfigSpec.BooleanValue HIDE_HUD_ON_ENGINE_OFF;
public static final ForgeConfigSpec.DoubleValue HUD_ANIMATION_SPEED;
public static final ForgeConfigSpec.BooleanValue SHOW_HUD_OVER_CHAT;

// Client
Expand Down Expand Up @@ -46,6 +48,12 @@ public final class ModConfigs {
HUD_OFFSET_Y = client
.comment("The Y offset for the HUD.")
.defineInRange("offsetY", 0, Integer.MIN_VALUE, Integer.MAX_VALUE);
HIDE_HUD_ON_ENGINE_OFF = client
.comment("If the HUD should be hidden when the jetpack is off.")
.define("hideOnEngineOff", true);
HUD_ANIMATION_SPEED = client
.comment("The animation speed for the HUD. lower = slower, higher = faster. 1 is instant.")
.defineInRange("animationSpeed", 0.1d, 0d, 1d);
SHOW_HUD_OVER_CHAT = client
.comment("Show HUD over the chat?")
.define("showOverChat", false);
Expand Down