Skip to content

Commit

Permalink
feat: Added UI for rendering the substitution count and progress
Browse files Browse the repository at this point in the history
  • Loading branch information
sekwah41 committed May 5, 2022
1 parent a813057 commit b2d0cd2
Show file tree
Hide file tree
Showing 8 changed files with 191 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import net.minecraft.nbt.Tag;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.phys.Vec3;
import net.minecraftforge.common.util.INBTSerializable;

import java.util.HashMap;
Expand All @@ -26,6 +27,8 @@ public interface INinjaData extends INBTSerializable<Tag> {
void addChakra(float amount);
void addStamina(float amount);

Vec3 getSubstitutionLoc();

DoubleJumpData getDoubleJumpData();

HashMap<String, CooldownTickEvent> getCooldownEvents();
Expand Down
16 changes: 14 additions & 2 deletions src/main/java/com/sekwah/narutomod/capabilities/NinjaData.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import net.minecraft.resources.ResourceLocation;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.phys.Vec3;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.ICapabilityProvider;
import net.minecraftforge.common.util.LazyOptional;
Expand All @@ -33,7 +34,7 @@ public class NinjaData implements INinjaData, ICapabilityProvider {
@Sync(minTicks = 1)
private float stamina;

@Sync
@Sync(minTicks = 1)
private float substitutions;

@Sync
Expand All @@ -42,9 +43,12 @@ public class NinjaData implements INinjaData, ICapabilityProvider {
@Sync
private float maxStamina;

@Sync
// Unless the player needs to know the max for rendering, no point in rendering.
private float maxSubstitutions;

@Sync
private Vec3 substitutionLocation;

/**
* If the player can double jump, will be updated by underlying values server side.
*
Expand Down Expand Up @@ -112,6 +116,7 @@ public boolean canRegen() {
private static final String STAMINA_TAG = "stamina";
private static final String SAVE_TIME = "save_time";
private static final String COOLDOWN_TAG = "cooldowns";
private static final String SUBSTITUTION_TAG = "substitutions";

private final LazyOptional<INinjaData> holder = LazyOptional.of(() -> this);

Expand Down Expand Up @@ -177,6 +182,11 @@ public void addStamina(float amount) {
this.stamina = Math.min(Math.max(this.stamina + amount, 0), maxStamina);
}

@Override
public Vec3 getSubstitutionLoc() {
return this.substitutionLocation;
}

@Override
public DoubleJumpData getDoubleJumpData() {
return this.doubleJumpData;
Expand Down Expand Up @@ -314,6 +324,7 @@ public Tag serializeNBT() {
cooldownData.putInt(key, event.ticks);
}
nbt.put(COOLDOWN_TAG, cooldownData);
nbt.putFloat(SUBSTITUTION_TAG, this.substitutions);
return nbt;
}

Expand All @@ -329,6 +340,7 @@ public void deserializeNBT(Tag tag) {
for (String key : cooldownData.getAllKeys()) {
this.cooldownTickEvents.put(key, new CooldownTickEvent(cooldownData.getInt(key) - ticksPassed));
}
this.substitutions = compoundTag.getFloat(SUBSTITUTION_TAG);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,6 @@ public class ChakraAndStaminaGUI extends GuiComponent {
private int screenWidth;
private int screenHeight;

/**
* Will be false if the entity is not a ninja
*/
private boolean shouldRender;

private float chakra;
private float stamina;

Expand All @@ -40,9 +35,6 @@ public ChakraAndStaminaGUI(Minecraft mc) {
}

public void render(PoseStack matrixStack) {
if(!shouldRender) {
return;
}
this.screenWidth = this.minecraft.getWindow().getGuiScaledWidth();
this.screenHeight = this.minecraft.getWindow().getGuiScaledHeight();
int barDesign = NarutoConfig.chakraBarDesign;
Expand Down Expand Up @@ -137,20 +129,13 @@ private void centeredTextOutlined(PoseStack matrixStack, String text, int x, int
this.getFont().draw(matrixStack, text, (float) x - width, y, color);
}

public void tick() {
if(this.minecraft.getCameraEntity() instanceof Player player) {
shouldRender = true;
if(player != null) {
player.getCapability(NinjaCapabilityHandler.NINJA_DATA).ifPresent(ninjaData -> {
chakra = ninjaData.getChakra();
stamina = ninjaData.getStamina();
maxChakra = ninjaData.getMaxChakra();
maxStamina = ninjaData.getMaxStamina();
});
}
} else {
shouldRender = false;
}
public void tick(Player player) {
player.getCapability(NinjaCapabilityHandler.NINJA_DATA).ifPresent(ninjaData -> {
this.chakra = ninjaData.getChakra();
this.stamina = ninjaData.getStamina();
this.maxChakra = ninjaData.getMaxChakra();
this.maxStamina = ninjaData.getMaxStamina();
});
}

private void setColor(Color color) {
Expand Down
26 changes: 23 additions & 3 deletions src/main/java/com/sekwah/narutomod/client/gui/NarutoInGameGUI.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.sekwah.narutomod.client.gui;

import com.mojang.blaze3d.vertex.PoseStack;
import com.sekwah.narutomod.capabilities.NinjaCapabilityHandler;
import net.minecraft.client.Minecraft;
import net.minecraft.world.entity.player.Player;
import net.minecraftforge.client.event.RenderGameOverlayEvent;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.TickEvent;
Expand All @@ -10,25 +12,43 @@
public class NarutoInGameGUI {

private final ChakraAndStaminaGUI charkaOverlay;
private final SubstitutionGUI substitutionOverlay;
private final Minecraft minecraft;

/**
* Will be false if the entity is not a ninja
*/
private boolean shouldRender;

public NarutoInGameGUI(){
Minecraft mc = Minecraft.getInstance();
this.charkaOverlay = new ChakraAndStaminaGUI(mc);
this.minecraft = Minecraft.getInstance();
this.charkaOverlay = new ChakraAndStaminaGUI(this.minecraft);
this.substitutionOverlay = new SubstitutionGUI(this.minecraft);
MinecraftForge.EVENT_BUS.addListener(this::renderGameOverlay);
MinecraftForge.EVENT_BUS.addListener(this::clientTickEvent);
}

@SubscribeEvent
public void clientTickEvent(TickEvent.ClientTickEvent event) {
this.charkaOverlay.tick();
if(this.minecraft.getCameraEntity() instanceof Player player) {
shouldRender = true;
this.charkaOverlay.tick(player);
this.substitutionOverlay.tick(player);
} else {
shouldRender = false;
}
}

@SubscribeEvent
public void renderGameOverlay(RenderGameOverlayEvent event) {
if(event.getType() != RenderGameOverlayEvent.ElementType.ALL) {
return;
}
if(!shouldRender) {
return;
}
PoseStack stack = event.getMatrixStack();
this.charkaOverlay.render(stack);
this.substitutionOverlay.render(stack);
}
}
110 changes: 110 additions & 0 deletions src/main/java/com/sekwah/narutomod/client/gui/SubstitutionGUI.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package com.sekwah.narutomod.client.gui;

import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.PoseStack;
import com.sekwah.narutomod.NarutoMod;
import com.sekwah.narutomod.capabilities.NinjaCapabilityHandler;
import com.sekwah.narutomod.config.NarutoConfig;
import com.sekwah.narutomod.util.ColorUtil;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Font;
import net.minecraft.client.gui.GuiComponent;
import net.minecraft.client.renderer.GameRenderer;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.player.Player;

import java.awt.*;

public class SubstitutionGUI extends GuiComponent {

private static final ResourceLocation LOG_TEXTURE = new ResourceLocation(NarutoMod.MOD_ID, "textures/gui/jutsu/jutsu_substiutution.png");

private final Minecraft minecraft;
private int screenWidth;
private int screenHeight;

private final int intStaminaColor;
private final int intChakraColor;

private float substitutions;

public SubstitutionGUI(Minecraft mc) {
this.minecraft = mc;

Color chakraColor = new Color(255, 255, 255);
Color chakraColorDarker = new Color(0, 0, 0);
this.intStaminaColor = ColorUtil.toMCColor(chakraColor).getValue();
this.intChakraColor = ColorUtil.toMCColor(chakraColorDarker).getValue();
}

public void render(PoseStack matrixStack) {
this.screenWidth = this.minecraft.getWindow().getGuiScaledWidth();
this.screenHeight = this.minecraft.getWindow().getGuiScaledHeight();
int screenMid = this.screenWidth / 2;

int xPos = screenMid + 102;
int yOffset = 23;
float brightness = 0.4f;

int textureWidth = 19;
int textureHeight = 18;

float height = this.substitutions % 1;

if(height == 0) {
height = 1;
}

int pixelHeight = Math.round(textureHeight * height);

// stack, x, y, tx, ty, width, height, textureWidth, textureHeight
RenderSystem.setShader(GameRenderer::getPositionTexShader);
RenderSystem.setShaderTexture(0, LOG_TEXTURE);
RenderSystem.setShaderColor(brightness, brightness, brightness, 1.0F);
blit(matrixStack, xPos, this.screenHeight - yOffset,
6, 7,
textureWidth, textureHeight,
32, 32);
RenderSystem.setShaderColor(1.0f, 1.0f,1.0f,1.0F);
blit(matrixStack, xPos, this.screenHeight - yOffset + textureHeight - pixelHeight,
6, 7 + textureHeight - pixelHeight,
textureWidth, pixelHeight,
32, 32);



this.centeredTextOutlined(matrixStack,
String.valueOf((int) Math.floor(substitutions)),
xPos + 15,
this.screenHeight - yOffset + 12,
intStaminaColor,
intChakraColor);

}

private void centeredTextOutlined(PoseStack matrixStack, String text, int x, int y, int color, int backgroundColor) {
int width = this.getFont().width(text) / 2;
this.getFont().draw(matrixStack, text, (float) x+1 - width, y, backgroundColor);
this.getFont().draw(matrixStack, text, (float) x-1 - width, y, backgroundColor);
this.getFont().draw(matrixStack, text, (float) x - width, (float) y+1, backgroundColor);
this.getFont().draw(matrixStack, text, (float) x - width, (float) y-1, backgroundColor);
this.getFont().draw(matrixStack, text, (float) x - width, y, color);
}

public void tick(Player player) {
player.getCapability(NinjaCapabilityHandler.NINJA_DATA).ifPresent(ninjaData -> {
this.substitutions = ninjaData.getSubstitutionCount();
});
}

private void setColor(Color color) {
RenderSystem.setShaderColor(color.getRed() / 255f,
color.getGreen() / 255f,
color.getBlue() / 255f,
1.0F);
}

private Font getFont() {
return this.minecraft.font;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public class NarutoConfig {
.defineInRange("maxSubstitutions", 3 , 0, Integer.MAX_VALUE);

CONFIG_SUBSTITUTION_REGEN_TIME = configBuilder.comment("Substitution Regen Time (Seconds)")
.defineInRange("substitutionRegenRate", 60 , 0, Integer.MAX_VALUE);
.defineInRange("substitutionRegenTime", 60 , 0, Integer.MAX_VALUE);

configBuilder.pop();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.sekwah.sekclib.capabilitysync.capabilitysync.tracker.implemented;

import com.sekwah.sekclib.capabilitysync.capabilitysync.tracker.SyncTrackerSerializer;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.world.phys.Vec3;

/**
* Sync structure
* boolean isNull
* double posX
* double posX
* double posX
*/
public class Vec3SyncTracker implements SyncTrackerSerializer<Vec3> {

@Override
public void encode(Vec3 objectToSend, FriendlyByteBuf outBuffer) {
outBuffer.writeBoolean(objectToSend == null);
if(objectToSend != null) {
outBuffer.writeDouble(objectToSend.x);
outBuffer.writeDouble(objectToSend.y);
outBuffer.writeDouble(objectToSend.z);
}
}

@Override
public Vec3 decode(FriendlyByteBuf inBuffer) {
if(inBuffer.readBoolean()) {
return null;
}
return new Vec3(inBuffer.readDouble(), inBuffer.readDouble(), inBuffer.readDouble());
}
}

0 comments on commit b2d0cd2

Please sign in to comment.