package im.expensive.ui.display.impl;
import com.mojang.blaze3d.matrix.MatrixStack;
import com.mojang.blaze3d.platform.GlStateManager;
import im.expensive.Expensive;
import im.expensive.events.EventDisplay;
import im.expensive.functions.impl.render.HUD;
import im.expensive.ui.display.ElementRenderer;
import im.expensive.ui.styles.Style;
import im.expensive.utils.animations.Animation;
import im.expensive.utils.animations.Direction;
import im.expensive.utils.animations.impl.EaseBackIn;
import im.expensive.utils.client.Gradient;
import im.expensive.utils.drag.Dragging;
import im.expensive.utils.math.MathUtil;
import im.expensive.utils.math.StopWatch;
import im.expensive.utils.math.Vector4i;
import im.expensive.utils.render.ColorUtils;
import im.expensive.utils.render.DisplayUtils;
import im.expensive.utils.render.Scissor;
import im.expensive.utils.render.Stencil;
import im.expensive.utils.render.font.Fonts;
import lombok.AccessLevel;
import lombok.experimental.FieldDefaults;
import net.minecraft.client.gui.AbstractGui;
import net.minecraft.client.gui.screen.ChatScreen;
import net.minecraft.client.renderer.entity.EntityRenderer;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.scoreboard.Score;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.math.vector.Vector4f;
import net.minecraft.util.text.TextFormatting;
import org.lwjgl.opengl.GL11;
@FieldDefaults(level = AccessLevel.PRIVATE)
public class TargetInfoRenderer implements ElementRenderer {
final StopWatch stopWatch = new StopWatch();
Dragging drag;
LivingEntity entity = null;
boolean shouldToBack;
final Animation animation = new EaseBackIn(400, 1, 1);
float healthAnimation = 0.0f, absorptionAnimation = 0.0f;
float posX, posY;
static final float width = 102f;
static final float height = 32;
static final float spacing = 4;
static final float rounding = 4;
static final float headSize = 22;
public TargetInfoRenderer() {
this.drag = drag;
}
@Override
public void render(EventDisplay eventDisplay) {
posX = drag.getX();
posY = drag.getY();
entity = getTarget(entity);
boolean backAnimation = !shouldToBack || stopWatch.isReached(1000);
animation.setDuration(backAnimation ?300 : 400);
animation.setDirection(backAnimation ? Direction.BACKWARDS : Direction.FORWARDS);
if (animation.getOutput() == 0.0f) {
entity = null;
}
if (entity != null) {
drag.setWidth(width);
drag.setHeight(height);
float health = fix1000Health(entity.getHealth());
float maxHealth = MathHelper.clamp(entity.getMaxHealth(), 0, 24);
healthAnimation = MathUtil.fast(healthAnimation,
MathHelper.clamp(health / maxHealth, 0, 1), 10);
absorptionAnimation = MathUtil.fast(absorptionAnimation,
MathHelper.clamp(entity.getAbsorptionAmount() / maxHealth, 0, 1), 10);
GlStateManager.pushMatrix();
setSizeAnimation(animation.getOutput());
drawBackGround(rounding);
renderTextThatDisplaysTargetInfo(eventDisplay.getMatrixStack());
drawTargetHead(entity, posX + spacing, posY + spacing+2, headSize, headSize);
renderHealthBar();
GlStateManager.popMatrix();
}
}
private LivingEntity getTarget(LivingEntity nullTarget) {
LivingEntity auraTarget = Expensive.getInstance().getFunctionRegistry().getKillAura().getTarget();
LivingEntity target = nullTarget;
if (auraTarget != null) {
stopWatch.reset();
shouldToBack = true;
target = auraTarget;
} else if (mc.currentScreen instanceof ChatScreen) {
stopWatch.reset();
shouldToBack = true;
target = mc.player;
} else {
shouldToBack = false;
}
return target;
}
public void drawTargetHead(LivingEntity entity, float x, float y, float width, float height) {
if (entity != null) {
EntityRenderer<? super LivingEntity> rendererManager = mc.getRenderManager().getRenderer(entity);
drawFace(rendererManager.getEntityTexture(entity), x, y-3, 8F, 8F, 8F, 8F, width, height, 64F, 64F, entity);
}
}
private void renderHealthBar() {
Style style = Expensive.getInstance().getStyleManager().getCurrentStyle();
Vector4i vector4i = new Vector4i(HUD.getColor(0), HUD.getColor(90), HUD.getColor(180), HUD.getColor(170));
DisplayUtils.drawRoundedRect(posX + 6+ headSize + spacing -32 + spacing, posY +1f + height - spacing * 1 - 2, (width - 7), 1.5f, new Vector4f(1,2,1,2), ColorUtils.rgba(0,0,0,255));
DisplayUtils.drawRoundedRect(posX + 6+ headSize + spacing -32 + spacing, posY +1f + height - spacing * 1 - 2, (width - 7) * healthAnimation, 1.5f, new Vector4f(1,2,1,2), ColorUtils.getColor(1));
}
private void renderTextThatDisplaysTargetInfo(MatrixStack matrixStack) {
Style style = Expensive.getInstance().getStyleManager().getCurrentStyle();
float animationValue = (float) animation.getOutput();
float halfAnimationValueRest = (1 - animationValue) / 2f;
float testX = posX + (width * halfAnimationValueRest);
float testY = posY + (height * halfAnimationValueRest);
float testW = width * animationValue;
float testH = height * animationValue;
drawStyledRect(posX, posY, width, height, rounding, 195);
//рендерится голова
Stencil.initStencilToWrite();
DisplayUtils.drawRoundedRect(posX + spacing, posY + spacing + 1, headSize, headSize,2, style.getSecondColor().getRGB() );
Stencil.readStencilBuffer(1);
drawTargetHead(entity, posX + spacing, posY + spacing + 2, headSize, headSize);
Stencil.uninitStencilBuffer();
Scissor.push();
Scissor.setFromComponentCoordinates(testX, testY, testW - 1, testH);
String hpText = "";
if (entity.isInvisible() && userConnectedToFunTimeAndEntityIsPlayer()) {
hpText = "Неизвестно";
} else {
float health = (float) MathUtil.round(healthAnimation * 20.0f, 0.1f);
hpText = health + " Hp";
if (health > 10) {
hpText += " -> Lose";
} else if (health <= 10) {
hpText += " -> Win!";
}
}
Fonts.montserrat.drawText(matrixStack, TextFormatting.getTextWithoutFormattingCodes(entity.getName().getString()), posX - 1 + headSize + spacing + spacing, posY + 3 + spacing + 1, -1, 8);
Fonts.montserrat.drawText(matrixStack, hpText, posX - 1+ headSize + spacing + spacing,
posY + 4.5f + spacing + 1 + spacing + spacing, ColorUtils.rgba(255,255,255, 170), 6.5F);
Scissor.unset();
Scissor.pop();
}
public void setSizeAnimation(double scale) {
GlStateManager.translated(posX + (width / 2), posY + (height / 2), 0);
GlStateManager.scaled(scale, scale, scale);
GlStateManager.translated(-(posX + (width / 2)), -(posY + (height / 2)), 0);
}
public void drawFace(ResourceLocation res, float d,
float y,
float u,
float v,
float uWidth,
float vHeight,
float width,
float height,
float tileWidth,
float tileHeight,
LivingEntity target) {
GL11.glPushMatrix();
GL11.glEnable(GL11.GL_BLEND);
mc.getTextureManager().bindTexture(res);
float hurtPercent = (target.hurtTime - (target.hurtTime != 0 ? mc.timer.renderPartialTicks : 0.0f)) / 8.0f;
GL11.glColor4f(1, 1 - hurtPercent, 1 - hurtPercent, 1);
AbstractGui.drawScaledCustomSizeModalRect(d, y, u, v, uWidth, vHeight, width, height, tileWidth, tileHeight);
GL11.glColor4f(1, 1, 1, 1);
GL11.glPopMatrix();
}
private float fix1000Health(float original) {
Score score = mc.world.getScoreboard().getOrCreateScore(entity.getScoreboardName(),
mc.world.getScoreboard().getObjectiveInDisplaySlot(2));
return userConnectedToFunTimeAndEntityIsPlayer() ? score.getScorePoints() : original;
}
private boolean userConnectedToFunTimeAndEntityIsPlayer() {
String header = mc.ingameGUI.getTabList().header == null ? " " : mc.ingameGUI.getTabList().header.getString().toLowerCase();
return (mc.getCurrentServerData() != null && mc.getCurrentServerData().serverIP.contains("funtime")
&& (header.contains("анархия") || header.contains("гриферский")) && entity instanceof PlayerEntity);
}
private void drawBackGround(float radius) {
Vector4i vector4i = new Vector4i(HUD.getColor(50), HUD.getColor(160), HUD.getColor(100), HUD.getColor(80));
DisplayUtils.drawRoundedRect(posX + 1, posY + 1, width - 2, height - 2, radius, ColorUtils.rgba(0, 0, 0, 125));
DisplayUtils.drawShadow(posX - 3, posY - 3, width + 6, height + 6, 40, (ColorUtils.setAlpha(ColorUtils.getColor(10), 85)));
DisplayUtils.drawRoundedRect(posX - 1, posY - 1, width + 2, height + 2, radius, (ColorUtils.setAlpha(ColorUtils.getColor(10), 50)));
final ResourceLocation grab = new ResourceLocation("expensive/images/grab.png");
if (mc.currentScreen instanceof ChatScreen) {
DisplayUtils.drawImage(grab, posX + 65, posY + 10, -29, -29, -1);
}
}
private void drawStyledRect(float x,
float y,
float width,
float height,
float radius, int alpha) {
DisplayUtils.drawShadow(x - 1, y - 2, width + 3, height + 3, 20, ColorUtils.getColor(0));
DisplayUtils.drawRoundedRect(x - 2, y - 2, width + 5, height + 4, new Vector4f(4, 4, 4, 4), Gradient.ClientGradient());
DisplayUtils.drawRoundedRect(x - 1, y - 1, width + 3, height + 2, new Vector4f(4, 4, 4, 4), ColorUtils.rgba(0, 0, 0, 160));
}
}