From 070d4fa0f8f5b6caa5cb6d502f541745e9969ab7 Mon Sep 17 00:00:00 2001 From: jzitnik-dev Date: Sat, 3 Jan 2026 00:33:48 +0100 Subject: [PATCH] feat: Include health bar --- src/main/java/cz/jzitnik/game/Player.java | 13 ++++++++----- src/main/java/cz/jzitnik/ui/Stats.java | 18 ++++++++++++++++-- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/src/main/java/cz/jzitnik/game/Player.java b/src/main/java/cz/jzitnik/game/Player.java index d5c8d4a..24b992f 100644 --- a/src/main/java/cz/jzitnik/game/Player.java +++ b/src/main/java/cz/jzitnik/game/Player.java @@ -18,18 +18,24 @@ import java.util.concurrent.TimeUnit; @Slf4j public class Player { public static final int MAX_STAMINA = 20; + public static final int MAX_HEALTH = 30; + private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); private final RoomCords playerCords; - private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); private final GameItem[] inventory = new GameItem[Inventory.ITEMS_X * Inventory.ITEMS_Y]; - private int stamina = MAX_STAMINA; + @Setter + private PlayerRotation playerRotation = PlayerRotation.FRONT; @Setter private GameItem selectedItem; + @Setter + private int health = MAX_HEALTH; + private int stamina = MAX_STAMINA; private boolean swinging = false; public void increaseStamina() { stamina++; } + public void decreaseStamina() { stamina--; } @@ -59,9 +65,6 @@ public class Player { return added; } - @Setter - private PlayerRotation playerRotation = PlayerRotation.FRONT; - public void swing(int delayMs) { if (swinging) { return; diff --git a/src/main/java/cz/jzitnik/ui/Stats.java b/src/main/java/cz/jzitnik/ui/Stats.java index 5a9a8e8..45a5588 100644 --- a/src/main/java/cz/jzitnik/ui/Stats.java +++ b/src/main/java/cz/jzitnik/ui/Stats.java @@ -14,7 +14,7 @@ import lombok.Getter; @Getter @Dependency public class Stats { - public static final int BARS_COUNT = 1; + public static final int BARS_COUNT = 2; public static final int BAR_WIDTH = 72; public static final int BAR_HEIGHT = 8; public static final int BAR_PADDING = 2; @@ -24,6 +24,7 @@ public class Stats { public static final int OFFSET_X = 5; public static final int OFFSET_Y = 5; + private static final Pixel HEALTH_COLOR = new ColoredPixel(new TextColor.RGB(205, 0, 0)); private static final Pixel STAMINA_COLOR = new ColoredPixel(new TextColor.RGB(207, 175, 89)); @InjectState @@ -38,9 +39,22 @@ public class Stats { float staminaDelta = (float) gameState.getPlayer().getStamina() / Player.MAX_STAMINA; float staminaAmount = (BAR_WIDTH - 2) * staminaDelta; + float healthDelts = (float) gameState.getPlayer().getHealth() / Player.MAX_HEALTH; + float healthAmount = (BAR_WIDTH - 2) * healthDelts; + for (int x = 0; x < BAR_WIDTH; x++) { for (int y = 0; y < BAR_HEIGHT; y++) { - if (x == 0 || y == 0 || x == BAR_WIDTH - 1 || y == BAR_HEIGHT - 1 || x - 1 < staminaAmount) { + if (x == 0 || y == 0 || x == BAR_WIDTH - 1 || y == BAR_HEIGHT - 1 || x - 1 < healthAmount) { + buffer[y + OFFSET_Y][x + OFFSET_X] = HEALTH_COLOR; + } else { + buffer[y + OFFSET_Y][x + OFFSET_X] = new Empty(); + } + } + } + + for (int x = 0; x < BAR_WIDTH; x++) { + for (int y = BAR_HEIGHT + BAR_PADDING; y < BAR_PADDING + BAR_HEIGHT * 2; y++) { + if (x == 0 || y == BAR_HEIGHT + BAR_PADDING || x == BAR_WIDTH - 1 || y == BAR_PADDING + BAR_HEIGHT * 2 - 1 || x - 1 < staminaAmount) { buffer[y + OFFSET_Y][x + OFFSET_X] = STAMINA_COLOR; } else { buffer[y + OFFSET_Y][x + OFFSET_X] = new Empty();