Skip to content

Commit

Permalink
add altitude indicator
Browse files Browse the repository at this point in the history
  • Loading branch information
OutsiderH committed Feb 15, 2024
1 parent 068cd97 commit ded0634
Show file tree
Hide file tree
Showing 7 changed files with 183 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.github.outsiderh.doabarrelrollwithhud.renderer.AltitudeIndicator;
import com.github.outsiderh.doabarrelrollwithhud.renderer.Renderer;
import com.github.outsiderh.doabarrelrollwithhud.renderer.SpeedIndicator;
import com.github.outsiderh.doabarrelrollwithhud.utils.FlightComputer;
Expand All @@ -14,7 +15,7 @@
public class DoABarrelRollWithHud implements ClientModInitializer {
public static final String modId = "doabarrelrollwithhud";
public static final Logger loggerSource = LoggerFactory.getLogger(modId);
public static final List<Renderer> renderers = List.of(new SpeedIndicator());
public static final List<Renderer> renderers = List.of(new SpeedIndicator(), new AltitudeIndicator());
public static final FlightComputer fc = new FlightComputer();
@Override
public void onInitializeClient() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public void afterRender(MatrixStack matrices, float tickDelta, CallbackInfo ci)
return;
}
DoABarrelRollWithHud.fc.eval(client.player);
Renderer.updateSize(client);
for (final Renderer item : DoABarrelRollWithHud.renderers) {
item.render(client, matrices);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.github.outsiderh.doabarrelrollwithhud.renderer;

import com.github.outsiderh.doabarrelrollwithhud.DoABarrelRollWithHud;
import com.github.outsiderh.doabarrelrollwithhud.utils.Vector2Int;

import net.minecraft.client.MinecraftClient;
import net.minecraft.client.util.math.MatrixStack;

public class AltitudeIndicator extends Renderer {
@Override
public void render(MinecraftClient client, MatrixStack mat) {
Vector2Int altitudePos = new Vector2Int(hudEnd.x, (hudBegin.y + hudEnd.y) / 2);
drawTextWithFixedBox(client.textRenderer, mat, String.format("%.1f", DoABarrelRollWithHud.fc.altitudeInUnit()), color, altitudePos, TextAlign.Left, "xxxx.x");
drawVerticalLine(mat, hudEnd.x, hudBegin.y, hudEnd.y, color);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,163 @@
import net.minecraft.client.util.math.MatrixStack;

public abstract class Renderer extends DrawableHelper {
protected Vector2Int hudStart = new Vector2Int();
protected Vector2Int hudEnd = new Vector2Int();
protected void updateSize(MinecraftClient client) {
protected static final int color = 0xFF00FF00;
protected static final Vector2Int hudBegin = new Vector2Int();
protected static final Vector2Int hudEnd = new Vector2Int();
public static void updateSize(MinecraftClient client) {
Vector2Int windowSize = new Vector2Int(client.getWindow().getWidth(), client.getWindow().getHeight());
hudStart.x = windowSize.x / 4;
hudStart.y = windowSize.y / 4;
hudEnd.x = hudStart.x * 3;
hudEnd.y = hudStart.y * 3;
hudBegin.x = windowSize.x / 8;
hudBegin.y = windowSize.y / 8;
hudEnd.x = hudBegin.x * 3;
hudEnd.y = hudBegin.y * 3;
}
protected void drawText(TextRenderer tr, MatrixStack mat, String text, int color, Vector2Int pos, TextAlign align) {
switch (align) {
case Left:
case UpLeft: {
break;
case Middle:
}
case Up: {
pos.x -= tr.getWidth(text) / 2;
break;
case Right:
}
case UpRight: {
pos.x -= tr.getWidth(text);
break;
default:
}
case Left: {
pos.y -= tr.fontHeight / 2;
break;
}
case Central: {
pos.x -= tr.getWidth(text) / 2;
pos.y -= tr.fontHeight / 2;
break;
}
case Right: {
pos.x -= tr.getWidth(text);
pos.y -= tr.fontHeight / 2;
break;
}
case DownLeft: {
pos.y -= tr.fontHeight;
break;
}
case Down: {
pos.x -= tr.getWidth(text) / 2;
pos.y -= tr.fontHeight;
break;
}
case DownRight: {
pos.x -= tr.getWidth(text);
pos.y -= tr.fontHeight;
break;
}
default: {
break;
}
}
tr.draw(mat, text, pos.x, pos.y, color);
}
protected void drawTextCenterY(TextRenderer tr, MatrixStack mat, String text, int color, Vector2Int pos, TextAlign align) {
pos.y -= tr.fontHeight / 2;
protected void drawTextWithFixedBox(TextRenderer tr, MatrixStack mat, String text, int color, Vector2Int pos, TextAlign align, String size) {
Vector2Int begin = new Vector2Int();
Vector2Int end = new Vector2Int();
switch (align) {
case UpLeft: {
begin.x = pos.x;
begin.y = pos.y;
end.x = begin.x + tr.getWidth(size);
end.y = begin.y + tr.fontHeight;
pos.x += 2;
end.x += 2;
begin.y -= 2;
break;
}
case Up: {
int width = tr.getWidth(size);
begin.x = pos.x - width / 2;
begin.y = pos.y;
end.x = begin.x + width;
end.y = begin.y + tr.fontHeight;
begin.x -= 2;
begin.y -= 2;
break;
}
case UpRight: {
begin.x = pos.x - tr.getWidth(size);
begin.y = pos.y;
end.x = pos.x;
end.y = begin.y + tr.fontHeight;
begin.x -= 2;
begin.y -= 2;
break;
}
case Left: {
begin.x = pos.x;
begin.y = pos.y - tr.fontHeight / 2;
end.x = begin.x + tr.getWidth(size);
end.y = begin.y + tr.fontHeight;
pos.x += 2;
end.x += 2;
begin.y -= 2;
break;
}
case Central: {
int width = tr.getWidth(size);
begin.x = pos.x - width / 2;
begin.y = pos.y - tr.fontHeight / 2;
end.x = begin.x + width;
end.y = begin.y + tr.fontHeight;
begin.x -= 2;
begin.y -= 2;
break;
}
case Right: {
begin.x = pos.x - tr.getWidth(size);
begin.y = pos.y - tr.fontHeight / 2;
end.x = pos.x;
end.y = begin.y + tr.fontHeight;
begin.x -= 2;
begin.y -= 2;
break;
}
case DownLeft: {
begin.x = pos.x;
begin.y = pos.y - tr.fontHeight;
end.x = begin.x + tr.getWidth(size);
end.y = pos.y;
pos.x += 2;
end.x += 2;
begin.y -= 2;
break;
}
case Down: {
int width = tr.getWidth(size);
begin.x = pos.x - width / 2;
begin.y = pos.y - tr.fontHeight;
end.x = begin.x + width;
end.y = pos.y;
begin.x -= 2;
begin.y -= 2;
break;
}
case DownRight: {
begin.x = pos.x - tr.getWidth(size);
begin.y = pos.y - tr.fontHeight;
end.x = pos.x;
end.y = pos.y;
begin.x -= 2;
begin.y -= 2;
break;
}
default: {
break;
}
}
drawText(tr, mat, text, color, pos, align);
drawHorizontalLine(mat, begin.x, end.x, begin.y, color);
drawHorizontalLine(mat, begin.x, end.x, end.y, color);
drawVerticalLine(mat, begin.x, begin.y, end.y, color);
drawVerticalLine(mat, end.x, begin.y, end.y, color);
}
public abstract void render(MinecraftClient client, MatrixStack mat);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
public class SpeedIndicator extends Renderer {
@Override
public void render(MinecraftClient client, MatrixStack mat) {
updateSize(client);
Vector2Int airSpeedPos = new Vector2Int((hudStart.x + hudEnd.x) / 4, (hudStart.y + hudEnd.y) / 4);
drawTextCenterY(client.textRenderer, mat, String.format("%.1f", DoABarrelRollWithHud.fc.airSpeed), 0x0000FF00, airSpeedPos, TextAlign.Right);
Vector2Int airSpeedPos = new Vector2Int(hudBegin.x, (hudBegin.y + hudEnd.y) / 2);
drawTextWithFixedBox(client.textRenderer, mat, String.format("%.1f", DoABarrelRollWithHud.fc.airSpeedInUnit()), color, airSpeedPos, TextAlign.Right, "xxx.x");
drawVerticalLine(mat, hudBegin.x, hudBegin.y, hudEnd.y, color);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
package com.github.outsiderh.doabarrelrollwithhud.renderer;

public enum TextAlign {
UpLeft,
Up,
UpRight,
Left,
Middle,
Right
Central,
Right,
DownLeft,
Down,
DownRight
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,16 @@
import net.minecraft.util.math.Vec3d;

public class FlightComputer {
public double airSpeed;
public float airSpeed;
public float altitude;
public float airSpeedInUnit() {
return airSpeed / 1.7f * 100f;
}
public float altitudeInUnit() {
return altitude - 63f;
}
public void eval(PlayerEntity player) {
airSpeed = player.getVelocity().distanceTo(Vec3d.ZERO);
airSpeed = (float)player.getVelocity().distanceTo(Vec3d.ZERO);
altitude = (float)player.getPos().y;
}
}

0 comments on commit ded0634

Please sign in to comment.