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 0000000..155a748 Binary files /dev/null and b/client/src/main/resources/gui/health-green.png differ 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 0000000..0c1d607 Binary files /dev/null and b/client/src/main/resources/gui/health-red.png differ 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 {