Working HUD.
authorSimon Cross <hodgestar@gmail.com>
Sat, 5 Mar 2016 23:25:25 +0000 (01:25 +0200)
committerSimon Cross <hodgestar@gmail.com>
Sat, 5 Mar 2016 23:25:25 +0000 (01:25 +0200)
koperkapel/hud.py
koperkapel/serums.py

index 4bce86c3c332fb6e948171fcd8b5ed0e00c70554..8668b6c67ff01f8f5acaf6f911eb19debdd2b544 100644 (file)
@@ -1,5 +1,7 @@
 """ Roach up display. """
 
+import math
+from pygame.surface import Surface
 from .actors.surf import SurfActor
 from .serums import default_serums
 
@@ -10,12 +12,35 @@ class HudActor(SurfActor):
         self.stats = stats
         super().__init__(surf=self._surf, **kw)
 
+    def _build_bar(self, value, *args, **kw):
+        icon = default_serums.assemble_icon(*args, **kw)
+        rect = icon.get_rect()
+        value = math.ceil(value)
+        surf = Surface((rect.w, rect.h * value)).convert_alpha()
+        surf.fill((255, 255, 255, 0))
+        x, y = 0, (surf.get_rect().h - rect.h)
+        for i in range(value):
+            surf.blit(icon, (x, y))
+            y -= rect.h / 4
+        return surf
+
     def _rebuild_hud_surf(self):
-        smart = default_serums.assemble_icon("smart")
-        fast = default_serums.assemble_icon("fast")
-        strong = default_serums.assemble_icon("strong")
-        health = default_serums.assemble_icon("strong")
-        return smart
+        stats = self._stats
+        bars = [
+            self._build_bar(stats.smart, "smart"),
+            self._build_bar(stats.fast, "fast"),
+            self._build_bar(stats.strong, "strong"),
+            self._build_bar(stats.health / 5, "strong", color="health"),
+        ]
+        rects = [b.get_rect() for b in bars]
+        h, w = max(r.h for r in rects), sum(r.w for r in rects)
+        x = (w - rects[0].w) / 2
+        surf = Surface((w, h)).convert_alpha()
+        surf.fill((255, 255, 255, 0))
+        for i, bar in enumerate(bars):
+            surf.blit(bar, (x, h - rects[i].h))
+            x += rects[i].w / 2
+        return surf
 
     @property
     def stats(self):
index 5162180502e36e8c2c126f1da5379f1e5b079049..f80163932538b22e83db90ac3875eedd0aed760f 100644 (file)
@@ -19,6 +19,7 @@ SERUM_OVERLAY_COLORS = {
     "smart": (0, 0, 255, 255),  # blue
     "fast": (0, 255, 0, 255),  # green
     "strong": (255, 0, 255, 255),  # purple
+    "health": (255, 0, 0, 255),  # red
     "none": (170, 68, 0, 255),  # brown
 }
 
@@ -41,12 +42,13 @@ class SerumFactory:
     def __init__(self, suffix):
         self.suffix = suffix
 
-    def assemble_icon(self, name):
+    def assemble_icon(self, name, color=None):
         assert name in SERUMS
+        color = color or name
         serum_icon = images.load(safepath("serum%s/%s") % (
             self.suffix, SERUM_TILENAME_MAP[name],))
         frame = serum_icon.copy()
-        frame.fill(SERUM_OVERLAY_COLORS[name], None, BLEND_RGBA_MULT)
+        frame.fill(SERUM_OVERLAY_COLORS[color], None, BLEND_RGBA_MULT)
         return frame
 
     def assemble(self, name):