diff tools/preview.py @ 19:3a831478eda4

adjusted tower pivot and angle definitions to better match scribbled assets
author ark
date 2026-10-20 00:36 +0200
parents 30bd10284fd8
children 2f555a54c332
line wrap: on
line diff
--- a/tools/preview.py	Tue Oct 20 00:14:24 2026 +0200
+++ b/tools/preview.py	Tue Oct 20 00:36:53 2026 +0200
@@ -3,6 +3,8 @@
 without starting the game.
 """
 
+import math
+
 from PIL import Image, ImageDraw
 
 ROUTE_COLOURS = [(220, 0, 160), (0, 90, 255), (255, 120, 0), (0, 160, 60),
@@ -81,45 +83,77 @@
     return sheet
 
 
-def tower_sheet(assets, pivots, footprint, scale):
+# screen angle the turning towers face on the tower sheet, the same as a new
+# tower in the game: up
+FACE = -90
+
+
+def _turned(im, pivot, drawn):
+    """
+    im on a transparent square with the pivot (shares of width and height) at
+    its centre. a sprite drawn pointing to angle drawn is turned around the
+    pivot to face FACE; drawn None leaves it as it is.
+    """
+    px, py = round(im.width * pivot[0]), round(im.height * pivot[1])
+    r = math.ceil(max(math.hypot(px - x, py - y) for x in (0, im.width) for y in (0, im.height)))
+    sq = Image.new("RGBA", (2 * r, 2 * r), (0, 0, 0, 0))
+    sq.alpha_composite(im, (r - px, r - py))
+    if drawn is None:
+        return sq
+    # pillow turns anticlockwise, screen angles grow clockwise
+    return sq.rotate(drawn - FACE, resample=Image.BICUBIC)
+
+
+def tower_sheet(assets, angles, pivots, footprint, scale):
     """
     tower levels 1-5 per row: at full size on grey, so the white fill shows,
-    and at game scale next to each other. a cross marks the rotation point,
-    which is the tower position, and a circle the footprint around it.
-    footprint is the footprint radius in map pixels, scale the sprite scale.
+    and at game scale next to each other. turning towers are turned by their
+    drawing direction in angles to face up, as in the game; a line from the
+    pivot shows that direction, and each tower is labelled with its angle. a
+    cross marks the rotation point, which is the tower position, and a circle
+    the footprint around it. footprint is the footprint radius in map pixels,
+    scale the sprite scale.
     """
     rows = []
     for t in range(4):
         names = [f"t{t}"] + [f"t{t}_{lv}" for lv in range(2, 6)]
-        rows.append([Image.open(assets / f"{n}.png") for n in names])
+        rows.append([(n, _turned(Image.open(assets / f"{n}.png"), pivots.get(n, (0.5, 0.5)),
+                                 angles.get(n))) for n in names])
     pad = 16
-    cell_w = max(im.width for r in rows for im in r) + pad
-    cell_h = max(im.height for r in rows for im in r) + pad
-    small_w = round(cell_w * scale) + 4
+    cell = max(im.width for r in rows for _, im in r) + pad
+    cell_h = cell + 12
+    small_w = round(cell * scale) + 4
     full_r = footprint / scale
-    sheet = Image.new("RGBA", (5 * cell_w + 5 * small_w + pad, 4 * cell_h + 20),
+    sheet = Image.new("RGBA", (5 * cell + 5 * small_w + pad, 4 * cell_h + 20),
                       (170, 170, 170, 255))
     draw = ImageDraw.Draw(sheet)
     for lv in range(5):
-        draw.text((lv * cell_w + pad, 4), f"level {lv + 1}", fill=(0, 0, 0))
-    draw.text((5 * cell_w + pad, 4), f"game scale {scale}", fill=(0, 0, 0))
+        draw.text((lv * cell + pad, 4), f"level {lv + 1}", fill=(0, 0, 0))
+    draw.text((5 * cell + pad, 4), f"game scale {scale}, turning towers face {FACE}",
+              fill=(0, 0, 0))
+    dx, dy = math.cos(math.radians(FACE)), math.sin(math.radians(FACE))
     for t, r in enumerate(rows):
         y = 20 + t * cell_h
-        for lv, im in enumerate(r):
-            x = lv * cell_w + pad // 2
-            sheet.alpha_composite(im, (x, y))
-            name = f"t{t}" if lv == 0 else f"t{t}_{lv + 1}"
-            px, py = pivots.get(name, (0.5, 0.5))
-            cx, cy = x + round(im.width * px), y + round(im.height * py)
+        cy = y + 12 + cell // 2
+        for lv, (name, im) in enumerate(r):
+            # label above, sprite centred on the pivot in the cell below it
+            drawn = angles.get(name)
+            draw.text((lv * cell + pad // 2, y), f"{name}: " + ("does not turn" if drawn is None
+                                                               else f"angle {drawn}"),
+                      fill=(0, 0, 0))
+            cx = lv * cell + cell // 2
+            sheet.alpha_composite(im, (cx - im.width // 2, cy - im.height // 2))
+            if drawn is not None:
+                arm = cell // 2 - pad // 2
+                draw.line((cx, cy, cx + dx * arm, cy + dy * arm), fill=(255, 0, 0))
             draw.line((cx - 5, cy, cx + 5, cy), fill=(255, 0, 0))
             draw.line((cx, cy - 5, cx, cy + 5), fill=(255, 0, 0))
             draw.ellipse((cx - full_r, cy - full_r, cx + full_r, cy + full_r),
                          outline=(0, 0, 255), width=2)
             small = im.resize((max(1, round(im.width * scale)), max(1, round(im.height * scale))),
                               Image.LANCZOS)
-            sx, sy = 5 * cell_w + pad + lv * small_w, y + cell_h // 3
-            sheet.alpha_composite(small, (sx, sy))
-            scx, scy = sx + small.width * px, sy + small.height * py
+            scx, scy = 5 * cell + pad + lv * small_w + small_w // 2, cy
+            sheet.alpha_composite(small, (scx - small.width // 2, scy - small.height // 2))
             draw.ellipse((scx - footprint, scy - footprint, scx + footprint, scy + footprint),
                          outline=(0, 0, 255))
     return sheet