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

Add HUD overlays for charged items #1425

Draft
wants to merge 1 commit into
base: dev
Choose a base branch
from
Draft
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 @@ -48,4 +48,12 @@ public class VIngameOverlays {
public static final ResourceLocation BAT = VResourceLocation.mod("bat");
public static final ResourceLocation DISGUISE = VResourceLocation.mod("disguise");
public static final ResourceLocation SUN = VResourceLocation.mod("sun");
/**
* Horizontal blood bar overlay for each held item
*/
public static final ResourceLocation BLOOD_CHARGED = VResourceLocation.mod("blood_charged");
/**
* Vertical charged crossbow bolts overlay for each held item
*/
public static final ResourceLocation TECH_CROSSBOW_CHARGED = VResourceLocation.mod("tech_crossbow_charged");
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package de.teamlapen.vampirism.api.items;

import de.teamlapen.vampirism.api.VampirismDataComponents;
import de.teamlapen.vampirism.api.components.IBloodCharged;
import net.minecraft.world.item.ItemStack;

public interface IBloodChargeable {
Expand All @@ -26,6 +28,7 @@ public interface IBloodChargeable {
* @return the charge percentage of the item
*/
default float getChargePercentage(ItemStack stack) {
return canBeCharged(stack) ? 0 : 1;
IBloodCharged bloodCharge = stack.get(VampirismDataComponents.BLOOD_CHARGED.get());
return bloodCharge != null ? bloodCharge.charged() : 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,7 @@ static void registerScreenOverlays(@NotNull RegisterGuiLayersEvent event) {
event.registerAbove(VanillaGuiLayers.CAMERA_OVERLAYS, VIngameOverlays.BAT, new BatOverlay());
event.registerAbove(VanillaGuiLayers.CAMERA_OVERLAYS, VIngameOverlays.DISGUISE, new DisguiseOverlay());
event.registerAbove(VanillaGuiLayers.CAMERA_OVERLAYS, VIngameOverlays.SUN, new SunOverlay());
event.registerAboveAll(VIngameOverlays.BLOOD_CHARGED, new BloodChargedOverlay());
event.registerAboveAll(VIngameOverlays.TECH_CROSSBOW_CHARGED, new TechCrossbowChargedOverlay());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package de.teamlapen.vampirism.client.gui.overlay;

import de.teamlapen.vampirism.api.items.IBloodChargeable;
import de.teamlapen.vampirism.api.util.VResourceLocation;
import net.minecraft.client.DeltaTracker;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.ItemStack;
import org.jetbrains.annotations.NotNull;

public class BloodChargedOverlay extends HeldItemOverlay {

public static final ResourceLocation OUTER = VResourceLocation.mod("widget/blood_bar");
public static final ResourceLocation INNER = VResourceLocation.mod( "widget/blood_bar_content");
private static final int WIDTH = 90;
private static final int HEIGHT = 12;

@Override
protected void renderMainHand(@NotNull GuiGraphics pGuiGraphics, @NotNull DeltaTracker deltaTracker, ItemStack stack) {
if (stack.getItem() instanceof IBloodChargeable item) {
int y = pGuiGraphics.guiHeight() - 45;
int x = pGuiGraphics.guiWidth() / 2 + 91;
x = (x + (pGuiGraphics.guiWidth() - x) / 2) - WIDTH/2;
pGuiGraphics.blitSprite(OUTER, x, y, WIDTH, HEIGHT);
pGuiGraphics.blitSprite(INNER, x, y, (int) (WIDTH * item.getChargePercentage(stack)), HEIGHT);
}
}

@Override
protected void renderOffHand(@NotNull GuiGraphics pGuiGraphics, @NotNull DeltaTracker deltaTracker, ItemStack stack) {
if (stack.getItem() instanceof IBloodChargeable item) {
int y = pGuiGraphics.guiHeight() - 45;
int x = (pGuiGraphics.guiWidth() / 2 - 91) / 2 - WIDTH/2;
pGuiGraphics.blitSprite(OUTER, x, y, WIDTH, HEIGHT);
pGuiGraphics.blitSprite(INNER, x, y, (int) (WIDTH * item.getChargePercentage(stack)), HEIGHT);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package de.teamlapen.vampirism.client.gui.overlay;

import net.minecraft.client.DeltaTracker;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.LayeredDraw;
import net.minecraft.client.player.LocalPlayer;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.item.ItemStack;
import org.jetbrains.annotations.NotNull;

public abstract class HeldItemOverlay implements LayeredDraw.Layer {

protected final Minecraft mc = Minecraft.getInstance();

@Override
public void render(@NotNull GuiGraphics pGuiGraphics, @NotNull DeltaTracker deltaTracker) {
LocalPlayer player = mc.player;
ItemStack mainItem = player.getItemInHand(InteractionHand.MAIN_HAND);
ItemStack offHand = player.getItemInHand(InteractionHand.OFF_HAND);

renderMainHand(pGuiGraphics, deltaTracker, mainItem);

renderOffHand(pGuiGraphics, deltaTracker, offHand);
}

protected abstract void renderMainHand(@NotNull GuiGraphics pGuiGraphics, @NotNull DeltaTracker deltaTracker, ItemStack stack);

protected abstract void renderOffHand(@NotNull GuiGraphics pGuiGraphics, @NotNull DeltaTracker deltaTracker, ItemStack stack);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package de.teamlapen.vampirism.client.gui.overlay;

import de.teamlapen.vampirism.api.util.VResourceLocation;
import de.teamlapen.vampirism.items.crossbow.TechCrossbowItem;
import net.minecraft.client.DeltaTracker;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.core.component.DataComponents;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.component.ChargedProjectiles;
import org.jetbrains.annotations.NotNull;

import java.util.List;

public class TechCrossbowChargedOverlay extends HeldItemOverlay {
public static final ResourceLocation OUTER = VResourceLocation.mod("widget/arrow");
public static final ResourceLocation INNER = VResourceLocation.mc("widget/arrows");
private final Minecraft mc = Minecraft.getInstance();

@Override
protected void renderMainHand(@NotNull GuiGraphics pGuiGraphics, @NotNull DeltaTracker deltaTracker, ItemStack stack) {
if (stack.getItem() instanceof TechCrossbowItem) {
List<ItemStack> projectiles = stack.getOrDefault(DataComponents.CHARGED_PROJECTILES, ChargedProjectiles.EMPTY).getItems();
int x = pGuiGraphics.guiWidth() - 30;
int y = pGuiGraphics.guiHeight() - 60;
pGuiGraphics.blitSprite(OUTER, x, y, 20, 20);
if (projectiles.size() > 0) {
pGuiGraphics.blitSprite(INNER, 15,37 , 0,0, x + 3, y - 3 * projectiles.size() + 13, 15, 3 * projectiles.size());
}
pGuiGraphics.drawString(mc.font, String.valueOf(projectiles.size()), x + 8, y + 12, -1, false);
}
}

@Override
protected void renderOffHand(@NotNull GuiGraphics pGuiGraphics, @NotNull DeltaTracker deltaTracker, ItemStack stack) {
if (stack.getItem() instanceof TechCrossbowItem) {
List<ItemStack> projectiles = stack.getOrDefault(DataComponents.CHARGED_PROJECTILES, ChargedProjectiles.EMPTY).getItems();
int x = 15;
int y = pGuiGraphics.guiHeight() - 60;
pGuiGraphics.blitSprite(OUTER, x, y, 20, 20);
if (projectiles.size() > 0) {
pGuiGraphics.blitSprite(INNER, 15,37 , 0,0, x + 3, y - 3 * projectiles.size() + 13, 15, 3 * projectiles.size());
}
pGuiGraphics.drawString(mc.font, String.valueOf(projectiles.size()), x + 8, y + 12, -1, false);
}
}
}
10 changes: 0 additions & 10 deletions src/main/java/de/teamlapen/vampirism/items/VampireSwordItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -277,16 +277,6 @@ protected float getSpeedModifier(@NotNull ItemStack stack) {
*/
protected abstract float getChargeUsage();

/**
* Gets the charged value from the tag compound
*
* @return Value between 0 and 1
*/
@Override
public float getChargePercentage(@NotNull ItemStack stack) {
return stack.getOrDefault(ModDataComponents.BLOOD_CHARGED, BloodCharged.EMPTY).charged();
}

/**
* @return Charging factor multiplied with amount to get charge percentage
*/
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"gui": {
"scaling": {
"type": "nine_slice",
"width": 75,
"height": 12,
"border": 3
}
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"gui": {
"scaling": {
"type": "nine_slice",
"width": 75,
"height": 12,
"border": 3
}
}
}