nodes/ui.pyΒΆ
Part of Clear Code Zelda.
1"""Player HUD: health bar, energy bar, EXP counter, weapon/magic boxes.
2
3Drawn from the root via ``on_draw`` on a CanvasLayer so it stays in screen
4space regardless of the camera offset.
5"""
6
7from __future__ import annotations
8
9from settings import (
10 BAR_HEIGHT,
11 ENERGY_BAR_WIDTH,
12 ENERGY_COLOUR,
13 HEALTH_BAR_WIDTH,
14 HEALTH_COLOUR,
15 ITEM_BOX_SIZE,
16 TEXT_COLOUR,
17 UI_BG_COLOUR,
18 UI_BORDER_COLOUR,
19 UI_BORDER_COLOUR_ACTIVE,
20)
21
22from simvx.core import Node2D
23
24#: Height of the equipped-weapon / equipped-spell chips. Their width follows the
25#: label, with ``ITEM_BOX_SIZE`` as the minimum.
26ITEM_BOX_HEIGHT = 34
27
28
29class HUD(Node2D):
30 """Static-position HUD drawn directly via on_draw.
31
32 Lives outside the world camera offset (added under a CanvasLayer-style
33 parent) so positions are screen-space. Everything it draws comes from the
34 player rather than from its own Properties, hence ``dynamic``.
35 """
36
37 dynamic = True
38
39 def __init__(self, player, **kwargs):
40 super().__init__(name="HUD", **kwargs)
41 self.player = player
42 self.z_index = 1000
43
44 def on_draw(self, renderer):
45 p = self.player
46 if p is None:
47 return
48 sw, sh = self.tree.screen_size if self.tree else (1280.0, 720.0)
49
50 # Status column, top-left: bars, kills, then the weapon and spell boxes.
51 # The bottom half of the screen belongs to the on-screen controls.
52 self._bar(renderer, 10, 10, HEALTH_BAR_WIDTH, BAR_HEIGHT, p.hp, p.max_hp, HEALTH_COLOUR)
53 self._bar(renderer, 10, 34, ENERGY_BAR_WIDTH, BAR_HEIGHT, p.energy, p.max_energy, ENERGY_COLOUR)
54 renderer.draw_text(f"Kills {p.kills}", (10, 60), colour=TEXT_COLOUR, scale=1.6)
55
56 self._item_box(renderer, 10, 88, p.weapon, p.weapon_switching)
57 self._item_box(renderer, 10 + ITEM_BOX_SIZE + 8, 88, p.magic, p.magic_switching)
58
59 # EXP, top-right.
60 exp_text = f"XP {int(p.exp)}"
61 renderer.draw_rect((sw - 140, 10), (130, 36), colour=UI_BG_COLOUR, filled=True)
62 renderer.draw_rect((sw - 140, 10), (130, 36), colour=UI_BORDER_COLOUR, filled=False)
63 renderer.draw_text(exp_text, (sw - 130, 18), colour=TEXT_COLOUR, scale=2)
64
65 # Help strip along the very bottom. Strip height + scale chosen so glyph
66 # bounding boxes (incl. descenders) stay inside the strip.
67 help_text = "WASD move SPACE attack CTRL magic Q/E swap M upgrade"
68 strip_h = 28
69 text_scale = 1.4
70 text_y = sh - strip_h + (strip_h - 14 * text_scale) / 2
71 renderer.draw_rect((0, sh - strip_h), (sw, strip_h), colour=(0.86, 0.86, 0.86, 1.0), filled=True)
72 renderer.draw_text(help_text, (10, text_y), colour=(0.10, 0.10, 0.12, 1.0), scale=text_scale)
73
74 def _bar(self, renderer, x, y, w, h, current, mx, colour):
75 # background
76 renderer.draw_rect((x, y), (w, h), colour=UI_BG_COLOUR, filled=True)
77 # filled
78 ratio = max(0.0, min(1.0, current / max(1.0, mx)))
79 renderer.draw_rect((x, y), (w * ratio, h), colour=colour, filled=True)
80 # border
81 renderer.draw_rect((x, y), (w, h), colour=UI_BORDER_COLOUR, filled=False)
82
83 def _item_box(self, renderer, x, y, label: str, active: bool):
84 """Labelled chip for the equipped weapon / spell, sized to fit its text."""
85 scale = 1.4
86 width = max(ITEM_BOX_SIZE, renderer.text_width(label, scale) + 20)
87 renderer.draw_rect((x, y), (width, ITEM_BOX_HEIGHT), colour=UI_BG_COLOUR, filled=True)
88 border = UI_BORDER_COLOUR_ACTIVE if active else UI_BORDER_COLOUR
89 renderer.draw_rect((x, y), (width, ITEM_BOX_HEIGHT), colour=border, filled=False)
90 renderer.draw_text(label, (x + 10, y + 8), colour=TEXT_COLOUR, scale=scale)