From 4e0e1a32fa972057ea0512dabf16f92562109ace Mon Sep 17 00:00:00 2001 From: andrewlalis Date: Mon, 25 Jul 2022 15:13:49 +0200 Subject: [PATCH] added shitty healthbar to the GUI. --- .../java/nl/andrewl/aos2_client/Client.java | 2 ++ .../aos2_client/model/ClientPlayer.java | 10 +++++++++ .../aos2_client/render/GameRenderer.java | 21 ++++++++++++++++++ .../src/main/resources/gui/health-green.png | Bin 0 -> 90 bytes client/src/main/resources/gui/health-red.png | Bin 0 -> 90 bytes .../nl/andrewl/aos2_server/PlayerManager.java | 4 ++++ .../aos2_server/ProjectileManager.java | 3 +++ 7 files changed, 40 insertions(+) create mode 100644 client/src/main/resources/gui/health-green.png create mode 100644 client/src/main/resources/gui/health-red.png diff --git a/client/src/main/java/nl/andrewl/aos2_client/Client.java b/client/src/main/java/nl/andrewl/aos2_client/Client.java index de35b1e..40ca682 100644 --- a/client/src/main/java/nl/andrewl/aos2_client/Client.java +++ b/client/src/main/java/nl/andrewl/aos2_client/Client.java @@ -169,6 +169,8 @@ public class Client implements Runnable { projectiles.remove(p.getId()); } } + } else if (msg instanceof ClientHealthMessage healthMessage) { + myPlayer.setHealth(healthMessage.health()); } } diff --git a/client/src/main/java/nl/andrewl/aos2_client/model/ClientPlayer.java b/client/src/main/java/nl/andrewl/aos2_client/model/ClientPlayer.java index a2c9dbf..40dd04e 100644 --- a/client/src/main/java/nl/andrewl/aos2_client/model/ClientPlayer.java +++ b/client/src/main/java/nl/andrewl/aos2_client/model/ClientPlayer.java @@ -9,12 +9,14 @@ import java.util.ArrayList; public class ClientPlayer extends Player { private final Inventory inventory; + private float health; private final Matrix4f heldItemTransform = new Matrix4f(); private final float[] heldItemTransformData = new float[16]; public ClientPlayer(int id, String username) { super(id, username); + this.health = 1; this.inventory = new Inventory(new ArrayList<>(), 0); } @@ -28,6 +30,14 @@ public class ClientPlayer extends Player { this.inventory.setSelectedIndex(inv.getSelectedIndex()); } + public float getHealth() { + return health; + } + + public void setHealth(float health) { + this.health = health; + } + public void updateHeldItemTransform(Camera cam) { heldItemTransform.identity() .translate(cam.getPosition()) diff --git a/client/src/main/java/nl/andrewl/aos2_client/render/GameRenderer.java b/client/src/main/java/nl/andrewl/aos2_client/render/GameRenderer.java index 2e5b3f9..1b1a5c4 100644 --- a/client/src/main/java/nl/andrewl/aos2_client/render/GameRenderer.java +++ b/client/src/main/java/nl/andrewl/aos2_client/render/GameRenderer.java @@ -51,6 +51,8 @@ public class GameRenderer { private GUITexture crosshairTexture; private GUITexture clipTexture; private GUITexture bulletTexture; + private GUITexture healthBarRedTexture; + private GUITexture healthBarGreenTexture; private long windowHandle; private int screenWidth = 800; @@ -123,9 +125,13 @@ public class GameRenderer { crosshairTexture = new GUITexture("gui/crosshair.png"); clipTexture = new GUITexture("gui/clip.png"); bulletTexture = new GUITexture("gui/bullet.png"); + healthBarRedTexture = new GUITexture("gui/health-red.png"); + healthBarGreenTexture = new GUITexture("gui/health-green.png"); guiRenderer.addTexture("crosshair", crosshairTexture); guiRenderer.addTexture("clip", clipTexture); guiRenderer.addTexture("bullet", bulletTexture); + guiRenderer.addTexture("health-red", healthBarRedTexture); + guiRenderer.addTexture("health-green", healthBarGreenTexture); log.debug("Initialized GUI renderer."); this.modelRenderer = new ModelRenderer(); @@ -255,6 +261,21 @@ public class GameRenderer { ); } } + // Render the player's health. + guiRenderer.draw( + healthBarRedTexture, + healthBarRedTexture.getIdealScaleX(64, screenWidth), + healthBarRedTexture.getIdealScaleY(16, screenHeight), + -0.90f, + -0.90f + ); + guiRenderer.draw( + healthBarGreenTexture, + healthBarGreenTexture.getIdealScaleX(64 * client.getMyPlayer().getHealth(), screenWidth), + healthBarGreenTexture.getIdealScaleY(16, screenHeight), + -0.90f, + -0.90f + ); guiRenderer.end(); glfwSwapBuffers(windowHandle); diff --git a/client/src/main/resources/gui/health-green.png b/client/src/main/resources/gui/health-green.png new file mode 100644 index 0000000000000000000000000000000000000000..155a74862676391487af52b436f9e3a0e6a5a3a5 GIT binary patch literal 90 zcmeAS@N?(olHy`uVBq!ia0vp^OhC-Y!3HG1DAjiXDVAa<&kznEsNqQI0P+PrT^vI= kW+tcnIR9aOV`C#D0}mS$!z?k0r$9*tPgg&ebxsLQ0Qb!l8~^|S literal 0 HcmV?d00001 diff --git a/client/src/main/resources/gui/health-red.png b/client/src/main/resources/gui/health-red.png new file mode 100644 index 0000000000000000000000000000000000000000..0c1d6074b64af4d04b667a457949ab7867a90f6a GIT binary patch literal 90 zcmeAS@N?(olHy`uVBq!ia0vp^OhC-Y!3HG1DAjiXDVAa<&kznEsNqQI0P+PrT^vI= kW+vCHU-@%>V`C#D1K%trr-NTYBY=_&p00i_>zopr018(XTL1t6 literal 0 HcmV?d00001 diff --git a/server/src/main/java/nl/andrewl/aos2_server/PlayerManager.java b/server/src/main/java/nl/andrewl/aos2_server/PlayerManager.java index 8b33d26..a9c3d15 100644 --- a/server/src/main/java/nl/andrewl/aos2_server/PlayerManager.java +++ b/server/src/main/java/nl/andrewl/aos2_server/PlayerManager.java @@ -76,6 +76,10 @@ public class PlayerManager { return clientHandlers.get(id); } + public ClientCommunicationHandler getHandler(ServerPlayer player) { + return clientHandlers.get(player.getId()); + } + public Collection getHandlers() { return Collections.unmodifiableCollection(clientHandlers.values()); } diff --git a/server/src/main/java/nl/andrewl/aos2_server/ProjectileManager.java b/server/src/main/java/nl/andrewl/aos2_server/ProjectileManager.java index a2cb643..0d492e6 100644 --- a/server/src/main/java/nl/andrewl/aos2_server/ProjectileManager.java +++ b/server/src/main/java/nl/andrewl/aos2_server/ProjectileManager.java @@ -6,6 +6,7 @@ import nl.andrewl.aos_core.Directions; import nl.andrewl.aos_core.model.Player; import nl.andrewl.aos_core.model.Projectile; import nl.andrewl.aos_core.model.world.Hit; +import nl.andrewl.aos_core.net.client.ClientHealthMessage; import nl.andrewl.aos_core.net.world.ChunkUpdateMessage; import org.joml.Matrix4f; import org.joml.Vector3f; @@ -122,6 +123,8 @@ public class ProjectileManager { if (hitPlayer.getHealth() == 0) { System.out.println("Player killed!!!"); server.getPlayerManager().playerKilled(hitPlayer); + } else { + server.getPlayerManager().getHandler(hitPlayer).sendDatagramPacket(new ClientHealthMessage(hitPlayer.getHealth())); } deleteProjectile(projectile); } else {