优化载具HUD

This commit is contained in:
Atsuihsio 2024-12-17 23:31:06 +08:00
parent fed3dcca79
commit 527c38d751
10 changed files with 91 additions and 64 deletions

View file

@ -2,7 +2,6 @@ package com.atsuishio.superbwarfare.client.screens;
import com.atsuishio.superbwarfare.ModUtils; import com.atsuishio.superbwarfare.ModUtils;
import com.atsuishio.superbwarfare.client.RenderHelper; import com.atsuishio.superbwarfare.client.RenderHelper;
import com.atsuishio.superbwarfare.config.server.CannonConfig;
import com.atsuishio.superbwarfare.entity.AnnihilatorEntity; import com.atsuishio.superbwarfare.entity.AnnihilatorEntity;
import com.atsuishio.superbwarfare.entity.ICannonEntity; import com.atsuishio.superbwarfare.entity.ICannonEntity;
import com.atsuishio.superbwarfare.entity.Mk42Entity; import com.atsuishio.superbwarfare.entity.Mk42Entity;
@ -12,10 +11,8 @@ import com.atsuishio.superbwarfare.tools.TraceTool;
import com.mojang.blaze3d.platform.GlStateManager; import com.mojang.blaze3d.platform.GlStateManager;
import com.mojang.blaze3d.systems.RenderSystem; import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.client.Minecraft; import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.renderer.GameRenderer; import net.minecraft.client.renderer.GameRenderer;
import net.minecraft.network.chat.Component; import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.Mth; import net.minecraft.util.Mth;
import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.LivingEntity; import net.minecraft.world.entity.LivingEntity;
@ -32,19 +29,8 @@ import java.text.DecimalFormat;
@Mod.EventBusSubscriber(value = Dist.CLIENT) @Mod.EventBusSubscriber(value = Dist.CLIENT)
public class CannonHudOverlay { public class CannonHudOverlay {
public static float health = 0;
public static float maxHealth = 0;
public static float indicatorPosH = 0; public static float indicatorPosH = 0;
public static float energy = 0;
public static float maxEnergy = 0;
private static final ResourceLocation ARMOR = ModUtils.loc("textures/screens/armor.png");
private static final ResourceLocation ENERGY = ModUtils.loc("textures/screens/energy.png");
private static final ResourceLocation HEALTH = ModUtils.loc("textures/screens/armor_value.png");
private static final ResourceLocation HEALTH_FRAME = ModUtils.loc("textures/screens/armor_value_frame.png");
@SubscribeEvent(priority = EventPriority.NORMAL) @SubscribeEvent(priority = EventPriority.NORMAL)
public static void eventHandler(RenderGuiEvent.Pre event) { public static void eventHandler(RenderGuiEvent.Pre event) {
int w = event.getWindow().getGuiScaledWidth(); int w = event.getWindow().getGuiScaledWidth();
@ -62,40 +48,19 @@ public class CannonHudOverlay {
RenderSystem.setShader(GameRenderer::getPositionTexShader); RenderSystem.setShader(GameRenderer::getPositionTexShader);
RenderSystem.blendFuncSeparate(GlStateManager.SourceFactor.SRC_ALPHA, GlStateManager.DestFactor.ONE_MINUS_SRC_ALPHA, GlStateManager.SourceFactor.ONE, GlStateManager.DestFactor.ZERO); RenderSystem.blendFuncSeparate(GlStateManager.SourceFactor.SRC_ALPHA, GlStateManager.DestFactor.ONE_MINUS_SRC_ALPHA, GlStateManager.SourceFactor.ONE, GlStateManager.DestFactor.ZERO);
RenderSystem.setShaderColor(1, 1, 1, 1); RenderSystem.setShaderColor(1, 1, 1, 1);
GuiGraphics guiGraphics = event.getGuiGraphics();
if (cannon instanceof Mk42Entity) { if (cannon instanceof Mk42Entity) {
health = cannon.getEntityData().get(Mk42Entity.HEALTH);
maxHealth = CannonConfig.MK42_HP.get();
indicatorPosH = 1.3f; indicatorPosH = 1.3f;
} }
if (cannon instanceof Mle1934Entity) { if (cannon instanceof Mle1934Entity) {
health = cannon.getEntityData().get(Mle1934Entity.HEALTH);
maxHealth = CannonConfig.MLE1934_HP.get();
indicatorPosH = 1.2f; indicatorPosH = 1.2f;
} }
if (cannon instanceof AnnihilatorEntity) { if (cannon instanceof AnnihilatorEntity) {
health = cannon.getEntityData().get(AnnihilatorEntity.HEALTH);
maxHealth = CannonConfig.ANNIHILATOR_HP.get();
energy = cannon.getEntityData().get(AnnihilatorEntity.ENERGY);
maxEnergy = CannonConfig.ANNIHILATOR_MAX_ENERGY.get().floatValue();
indicatorPosH = cannon.getEntityData().get(AnnihilatorEntity.OFFSET_ANGLE); indicatorPosH = cannon.getEntityData().get(AnnihilatorEntity.OFFSET_ANGLE);
guiGraphics.pose().pushPose();
guiGraphics.blit(ENERGY, w - 96, h - 28, 0, 0, 12, 12, 12, 12);
guiGraphics.blit(HEALTH_FRAME, w - 83, h - 26, 0, 0, 80, 8, 80, 8);
guiGraphics.blit(HEALTH, w - 83, h - 26, 0, 0, (int) (80 * energy / maxEnergy), 8, 80, 8);
guiGraphics.pose().popPose();
} }
guiGraphics.pose().pushPose();
guiGraphics.blit(ARMOR, w - 96, h - 14, 0, 0, 12, 12, 12, 12);
guiGraphics.blit(HEALTH_FRAME, w - 83, h - 12, 0, 0, 80, 8, 80, 8);
guiGraphics.blit(HEALTH, w - 83, h - 12, 0, 0, (int) (80 * health / maxHealth), 8, 80, 8);
guiGraphics.pose().popPose();
float yRotOffset = Mth.lerp(event.getPartialTick(), player.yRotO, player.getYRot()); float yRotOffset = Mth.lerp(event.getPartialTick(), player.yRotO, player.getYRot());
float xRotOffset = Mth.lerp(event.getPartialTick(), player.xRotO, player.getXRot()); float xRotOffset = Mth.lerp(event.getPartialTick(), player.xRotO, player.getXRot());
float diffY = cannon.getViewYRot(event.getPartialTick()) - yRotOffset; float diffY = cannon.getViewYRot(event.getPartialTick()) - yRotOffset;

View file

@ -1,10 +1,8 @@
package com.atsuishio.superbwarfare.client.screens; package com.atsuishio.superbwarfare.client.screens;
import com.atsuishio.superbwarfare.ModUtils; import com.atsuishio.superbwarfare.ModUtils;
import com.atsuishio.superbwarfare.config.server.CannonConfig; import com.atsuishio.superbwarfare.entity.IChargeEntity;
import com.atsuishio.superbwarfare.entity.ICannonEntity;
import com.atsuishio.superbwarfare.entity.IVehicleEntity; import com.atsuishio.superbwarfare.entity.IVehicleEntity;
import com.atsuishio.superbwarfare.entity.SpeedboatEntity;
import com.mojang.blaze3d.platform.GlStateManager; import com.mojang.blaze3d.platform.GlStateManager;
import com.mojang.blaze3d.systems.RenderSystem; import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.client.Minecraft; import net.minecraft.client.Minecraft;
@ -52,20 +50,22 @@ public class VehicleHudOverlay {
RenderSystem.setShaderColor(1, 1, 1, 1); RenderSystem.setShaderColor(1, 1, 1, 1);
GuiGraphics guiGraphics = event.getGuiGraphics(); GuiGraphics guiGraphics = event.getGuiGraphics();
if (vehicle instanceof SpeedboatEntity) { guiGraphics.pose().pushPose();
health = vehicle.getEntityData().get(SpeedboatEntity.HEALTH); if (vehicle instanceof IChargeEntity iCharge) {
maxHealth = CannonConfig.SPEEDBOAT_HP.get(); energy = iCharge.getEnergy();
energy = vehicle.getEntityData().get(SpeedboatEntity.ENERGY); maxEnergy = iCharge.getMaxEnergy();
maxEnergy = CannonConfig.SPEEDBOAT_MAX_ENERGY.get().floatValue(); guiGraphics.blit(ENERGY, w - 96, h - 28, 0, 0, 12, 12, 12, 12);
guiGraphics.blit(HEALTH_FRAME, w - 83, h - 26, 0, 0, 80, 8, 80, 8);
guiGraphics.blit(HEALTH, w - 83, h - 26, 0, 0, (int) (80 * energy / maxEnergy), 8, 80, 8);
} }
guiGraphics.pose().pushPose(); if (vehicle instanceof IVehicleEntity iVehicle) {
guiGraphics.blit(ENERGY, w - 96, h - 28, 0, 0, 12, 12, 12, 12); health = iVehicle.getHealth();
guiGraphics.blit(HEALTH_FRAME, w - 83, h - 26, 0, 0, 80, 8, 80, 8); maxHealth = iVehicle.getMaxHealth();
guiGraphics.blit(HEALTH, w - 83, h - 26, 0, 0, (int) (80 * energy / maxEnergy), 8, 80, 8); guiGraphics.blit(ARMOR, w - 96, h - 14, 0, 0, 12, 12, 12, 12);
guiGraphics.blit(ARMOR, w - 96, h - 14, 0, 0, 12, 12, 12, 12); guiGraphics.blit(HEALTH_FRAME, w - 83, h - 12, 0, 0, 80, 8, 80, 8);
guiGraphics.blit(HEALTH_FRAME, w - 83, h - 12, 0, 0, 80, 8, 80, 8); guiGraphics.blit(HEALTH, w - 83, h - 12, 0, 0, (int) (80 * health / maxHealth), 8, 80, 8);
guiGraphics.blit(HEALTH, w - 83, h - 12, 0, 0, (int) (80 * health / maxHealth), 8, 80, 8); }
guiGraphics.pose().popPose(); guiGraphics.pose().popPose();
} }
@ -73,6 +73,6 @@ public class VehicleHudOverlay {
private static boolean shouldRenderCrossHair(Player player) { private static boolean shouldRenderCrossHair(Player player) {
if (player == null) return false; if (player == null) return false;
return !player.isSpectator() return !player.isSpectator()
&& (player.getVehicle() != null && player.getVehicle() instanceof IVehicleEntity && !(player.getVehicle() instanceof ICannonEntity)); && (player.getVehicle() != null && player.getVehicle() instanceof IVehicleEntity);
} }
} }

View file

@ -66,6 +66,7 @@ public class AnnihilatorEntity extends Entity implements GeoEntity, ICannonEntit
private final AnimatableInstanceCache cache = GeckoLibUtil.createInstanceCache(this); private final AnimatableInstanceCache cache = GeckoLibUtil.createInstanceCache(this);
public static final float MAX_HEALTH = CannonConfig.ANNIHILATOR_HP.get(); public static final float MAX_HEALTH = CannonConfig.ANNIHILATOR_HP.get();
public static final float MAX_ENERGY = CannonConfig.ANNIHILATOR_MAX_ENERGY.get().floatValue();
public static final float SHOOT_COST = CannonConfig.ANNIHILATOR_SHOOT_COST.get().floatValue(); public static final float SHOOT_COST = CannonConfig.ANNIHILATOR_SHOOT_COST.get().floatValue();
protected int interpolationSteps; protected int interpolationSteps;
@ -527,6 +528,26 @@ public class AnnihilatorEntity extends Entity implements GeoEntity, ICannonEntit
@Override @Override
public boolean canCharge() { public boolean canCharge() {
return this.entityData.get(ENERGY) < CannonConfig.ANNIHILATOR_MAX_ENERGY.get().floatValue(); return this.entityData.get(ENERGY) < MAX_ENERGY;
}
@Override
public int getEnergy() {
return this.entityData.get(ENERGY).intValue();
}
@Override
public int getMaxEnergy() {
return (int)MAX_ENERGY;
}
@Override
public float getHealth() {
return this.entityData.get(HEALTH).intValue();
}
@Override
public float getMaxHealth() {
return (int)MAX_HEALTH;
} }
} }

View file

@ -3,6 +3,7 @@ package com.atsuishio.superbwarfare.entity;
import net.minecraft.world.entity.player.Player; import net.minecraft.world.entity.player.Player;
public interface ICannonEntity extends IVehicleEntity { public interface ICannonEntity extends IVehicleEntity {
void cannonShoot(Player player); void cannonShoot(Player player);
float getHealth();
float getMaxHealth();
} }

View file

@ -5,4 +5,6 @@ public interface IChargeEntity {
void charge(int amount); void charge(int amount);
boolean canCharge(); boolean canCharge();
int getEnergy();
int getMaxEnergy();
} }

View file

@ -1,3 +1,5 @@
package com.atsuishio.superbwarfare.entity; package com.atsuishio.superbwarfare.entity;
public interface IVehicleEntity { public interface IVehicleEntity {
float getHealth();
float getMaxHealth();
} }

View file

@ -421,4 +421,14 @@ public class Mk42Entity extends Entity implements GeoEntity, ICannonEntity {
return this.cache; return this.cache;
} }
@Override
public float getHealth() {
return this.entityData.get(HEALTH).intValue();
}
@Override
public float getMaxHealth() {
return (int)MAX_HEALTH;
}
} }

View file

@ -516,4 +516,14 @@ public class Mle1934Entity extends Entity implements GeoEntity, ICannonEntity {
return this.cache; return this.cache;
} }
@Override
public float getHealth() {
return this.entityData.get(HEALTH).intValue();
}
@Override
public float getMaxHealth() {
return (int)MAX_HEALTH;
}
} }

View file

@ -300,6 +300,9 @@ public class SpeedboatEntity extends Entity implements GeoEntity, IChargeEntity,
cannotFire = false; cannotFire = false;
} }
turretYRotO = this.getTurretYRot();
turretXRotO = this.getTurretXRot();
Entity driver = this.getFirstPassenger(); Entity driver = this.getFirstPassenger();
if (driver instanceof Player player) { if (driver instanceof Player player) {
if (heat > 100) { if (heat > 100) {
@ -364,12 +367,11 @@ public class SpeedboatEntity extends Entity implements GeoEntity, IChargeEntity,
*/ */
private void gunnerFire() { private void gunnerFire() {
if (this.entityData.get(COOL_DOWN) != 0 || this.cannotFire) return; if (this.entityData.get(COOL_DOWN) != 0 || this.cannotFire) return;
if (this.getItemStacks().stream().noneMatch(stack -> stack.is(ModItems.HEAVY_AMMO.get()))) return;
Entity driver = this.getFirstPassenger(); Entity driver = this.getFirstPassenger();
if (driver == null) return; if (driver == null) return;
if (driver instanceof Player player && !(player.getMainHandItem().is(ModTags.Items.GUN))) { if (driver instanceof Player player && !(player.getMainHandItem().is(ModTags.Items.GUN))) {
if (this.getItemStacks().stream().noneMatch(stack -> stack.is(ModItems.HEAVY_AMMO.get())) && !player.getInventory().hasAnyMatching(s -> s.is(ModItems.CREATIVE_AMMO_BOX.get()))) return;
if (player.getCapability(ModVariables.PLAYER_VARIABLES_CAPABILITY, null).orElse(new ModVariables.PlayerVariables()).holdFire) { if (player.getCapability(ModVariables.PLAYER_VARIABLES_CAPABILITY, null).orElse(new ModVariables.PlayerVariables()).holdFire) {
ProjectileEntity projectile = new ProjectileEntity(driver.level()) ProjectileEntity projectile = new ProjectileEntity(driver.level())
.shooter(player) .shooter(player)
@ -393,7 +395,6 @@ public class SpeedboatEntity extends Entity implements GeoEntity, IChargeEntity,
} }
Level level = player.level(); Level level = player.level();
final Vec3 center = new Vec3(this.getX(), this.getEyeY(), this.getZ()); final Vec3 center = new Vec3(this.getX(), this.getEyeY(), this.getZ());
for (Entity target : level.getEntitiesOfClass(Entity.class, new AABB(center, center).inflate(4), e -> true).stream().sorted(Comparator.comparingDouble(e -> e.distanceToSqr(center))).toList()) { for (Entity target : level.getEntitiesOfClass(Entity.class, new AABB(center, center).inflate(4), e -> true).stream().sorted(Comparator.comparingDouble(e -> e.distanceToSqr(center))).toList()) {
@ -401,12 +402,10 @@ public class SpeedboatEntity extends Entity implements GeoEntity, IChargeEntity,
ModUtils.PACKET_HANDLER.send(PacketDistributor.PLAYER.with(() -> serverPlayer), new ShakeClientMessage(6, 5, 7, this.getX(), this.getEyeY(), this.getZ())); ModUtils.PACKET_HANDLER.send(PacketDistributor.PLAYER.with(() -> serverPlayer), new ShakeClientMessage(6, 5, 7, this.getX(), this.getEyeY(), this.getZ()));
} }
} }
if (level instanceof ServerLevel) { if (level instanceof ServerLevel) {
this.entityData.set(COOL_DOWN, 3); this.entityData.set(COOL_DOWN, 3);
heat += 4; heat += 4;
} }
this.getItemStacks().stream().filter(stack -> stack.is(ModItems.HEAVY_AMMO.get())).findFirst().ifPresent(stack -> stack.shrink(1)); this.getItemStacks().stream().filter(stack -> stack.is(ModItems.HEAVY_AMMO.get())).findFirst().ifPresent(stack -> stack.shrink(1));
} }
} }
@ -513,9 +512,6 @@ public class SpeedboatEntity extends Entity implements GeoEntity, IChargeEntity,
float gunAngle = -Math.clamp(-140f, 140f, Mth.wrapDegrees(driver.getYHeadRot() - this.getYRot())); float gunAngle = -Math.clamp(-140f, 140f, Mth.wrapDegrees(driver.getYHeadRot() - this.getYRot()));
turretYRotO = this.getTurretYRot();
turretXRotO = this.getTurretXRot();
this.setTurretYRot(gunAngle); this.setTurretYRot(gunAngle);
this.setTurretXRot(driver.getXRot() - this.getXRot()); this.setTurretXRot(driver.getXRot() - this.getXRot());
} }
@ -846,4 +842,24 @@ public class SpeedboatEntity extends Entity implements GeoEntity, IChargeEntity,
public void stopOpen(Player pPlayer) { public void stopOpen(Player pPlayer) {
this.level().gameEvent(GameEvent.CONTAINER_CLOSE, this.position(), GameEvent.Context.of(pPlayer)); this.level().gameEvent(GameEvent.CONTAINER_CLOSE, this.position(), GameEvent.Context.of(pPlayer));
} }
@Override
public int getEnergy() {
return this.entityData.get(ENERGY).intValue();
}
@Override
public int getMaxEnergy() {
return (int)MAX_ENERGY;
}
@Override
public float getHealth() {
return this.entityData.get(HEALTH).intValue();
}
@Override
public float getMaxHealth() {
return (int)MAX_HEALTH;
}
} }

View file

@ -1,7 +1,7 @@
package com.atsuishio.superbwarfare.network.message; package com.atsuishio.superbwarfare.network.message;
import com.atsuishio.superbwarfare.ModUtils; import com.atsuishio.superbwarfare.ModUtils;
import com.atsuishio.superbwarfare.entity.ICannonEntity; import com.atsuishio.superbwarfare.entity.IVehicleEntity;
import com.atsuishio.superbwarfare.init.ModItems; import com.atsuishio.superbwarfare.init.ModItems;
import com.atsuishio.superbwarfare.init.ModSounds; import com.atsuishio.superbwarfare.init.ModSounds;
import com.atsuishio.superbwarfare.network.ModVariables; import com.atsuishio.superbwarfare.network.ModVariables;
@ -52,7 +52,7 @@ public class ZoomMessage {
capability.syncPlayerVariables(player); capability.syncPlayerVariables(player);
}); });
if (player.isPassenger() && player.getVehicle() instanceof ICannonEntity) { if (player.isPassenger() && player.getVehicle() instanceof IVehicleEntity) {
SoundTool.playLocalSound(player, ModSounds.CANNON_ZOOM_IN.get(), 2, 1); SoundTool.playLocalSound(player, ModSounds.CANNON_ZOOM_IN.get(), 2, 1);
} }
} }
@ -64,7 +64,7 @@ public class ZoomMessage {
capability.syncPlayerVariables(player); capability.syncPlayerVariables(player);
}); });
if (player.isPassenger() && player.getVehicle() instanceof ICannonEntity) { if (player.isPassenger() && player.getVehicle() instanceof IVehicleEntity) {
SoundTool.playLocalSound(player, ModSounds.CANNON_ZOOM_OUT.get(), 2, 1); SoundTool.playLocalSound(player, ModSounds.CANNON_ZOOM_OUT.get(), 2, 1);
} }