wobblyhead hg > minor_monsters
annotate tools/preview.py @ 32:85d23328a215 0.10.0
added intro music, logic for browser audio triggering on splash screen
| author | ark |
|---|---|
| date | 2026-10-20 18:39 +0200 |
| parents | 2f555a54c332 |
| children |
| rev | line source |
|---|---|
| 1 | 1 """ |
| 2 preview images of the build, for checking routes, placement and tower levels | |
| 3 without starting the game. | |
| 4 """ | |
| 5 | |
|
19
3a831478eda4
adjusted tower pivot and angle definitions to better match scribbled assets
ark
parents:
16
diff
changeset
|
6 import math |
|
3a831478eda4
adjusted tower pivot and angle definitions to better match scribbled assets
ark
parents:
16
diff
changeset
|
7 |
| 1 | 8 from PIL import Image, ImageDraw |
| 9 | |
| 10 ROUTE_COLOURS = [(220, 0, 160), (0, 90, 255), (255, 120, 0), (0, 160, 60), | |
| 11 (140, 0, 255), (0, 180, 200), (200, 40, 40), (90, 90, 90)] | |
| 12 | |
| 13 | |
| 14 def _base(map_path, mp): | |
| 15 """map image faded towards white, with the placement grid drawn on it.""" | |
| 16 im = Image.open(map_path).convert("RGBA") | |
| 17 im = Image.blend(im, Image.new("RGBA", im.size, (255, 255, 255, 255)), 0.55) | |
| 18 draw = ImageDraw.Draw(im) | |
| 19 cell, cols = mp["cell"], mp["cols"] | |
| 20 for i, c in enumerate(mp["grid"]): | |
| 21 if c == "1": | |
| 22 x, y = (i % cols) * cell, (i // cols) * cell | |
| 23 draw.rectangle((x, y, x + cell - 2, y + cell - 2), fill=(60, 170, 60, 255)) | |
| 24 return im, draw | |
| 25 | |
| 26 | |
| 27 def _route(draw, pts, colour, width): | |
| 28 pts = [tuple(p) for p in pts] | |
| 29 draw.line(pts, fill=colour, width=width, joint="curve") | |
| 30 for x, y in pts: | |
| 31 draw.ellipse((x - 2, y - 2, x + 2, y + 2), fill=(0, 0, 0)) | |
| 32 # arrow heads at a few points, showing the walking direction | |
| 33 for k in range(1, 6): | |
| 34 i = k * (len(pts) - 1) // 6 | |
| 35 (x0, y0), (x1, y1) = pts[i], pts[i + 1] | |
| 36 dx, dy = x1 - x0, y1 - y0 | |
| 37 n = (dx * dx + dy * dy) ** 0.5 or 1 | |
| 38 dx, dy = dx / n, dy / n | |
| 39 tip = (x0 + dx * 6, y0 + dy * 6) | |
| 40 draw.polygon([tip, (x0 - dx * 6 - dy * 6, y0 - dy * 6 + dx * 6), | |
| 41 (x0 - dx * 6 + dy * 6, y0 - dy * 6 - dx * 6)], fill=colour) | |
| 42 | |
| 43 | |
| 44 def _ends(draw, mp): | |
| 45 for x, y, r in mp["entrances"]: | |
| 46 draw.ellipse((x - r, y - r, x + r, y + r), outline=(0, 0, 255), width=3) | |
| 47 for x, y, r in mp["exits"]: | |
| 48 draw.ellipse((x - r, y - r, x + r, y + r), outline=(0, 160, 0), width=3) | |
| 49 | |
| 50 | |
| 51 def map_preview(map_path, mp): | |
| 52 """the map with grid, centre line graph, routes, entrances and exits.""" | |
| 53 im, draw = _base(map_path, mp) | |
| 54 for pts in mp["graph"]["edges"]: | |
| 55 draw.line([tuple(p) for p in pts], fill=(120, 120, 120), width=1) | |
| 56 for x, y in mp["graph"]["nodes"]: | |
| 57 draw.rectangle((x - 3, y - 3, x + 3, y + 3), outline=(0, 0, 0)) | |
| 58 for k, r in enumerate(mp["routes"]): | |
| 59 _route(draw, r["points"], ROUTE_COLOURS[k % len(ROUTE_COLOURS)], 3) | |
| 60 _ends(draw, mp) | |
| 61 for k, r in enumerate(mp["routes"]): | |
| 62 x, y = r["points"][len(r["points"]) // 3] | |
| 23 | 63 draw.text((x + 6, y + 6 + 10 * k), f"route {k}", fill=(0, 0, 0)) |
| 1 | 64 return im |
| 65 | |
| 66 | |
| 67 def route_sheet(map_path, mp): | |
| 68 """one panel per route, half size, for maps with several routes.""" | |
| 69 panels = [] | |
| 70 for k, r in enumerate(mp["routes"]): | |
| 71 im, draw = _base(map_path, mp) | |
| 72 _route(draw, r["points"], ROUTE_COLOURS[k % len(ROUTE_COLOURS)], 5) | |
| 73 _ends(draw, mp) | |
| 23 | 74 draw.text((10, 10), f"route {k}: length {r['length']:.0f}," |
| 1 | 75 f" turning {r['turning']:.0f}, value {r['value']:.2f}", fill=(0, 0, 0)) |
| 76 panels.append(im.resize((im.width // 2, im.height // 2))) | |
| 77 cols = min(3, len(panels)) | |
| 78 rows = (len(panels) + cols - 1) // cols | |
| 79 pw, ph = panels[0].size | |
| 80 sheet = Image.new("RGBA", (cols * pw, rows * ph), (255, 255, 255, 255)) | |
| 81 for k, p in enumerate(panels): | |
| 82 sheet.paste(p, ((k % cols) * pw, (k // cols) * ph)) | |
| 83 return sheet | |
| 84 | |
| 85 | |
|
19
3a831478eda4
adjusted tower pivot and angle definitions to better match scribbled assets
ark
parents:
16
diff
changeset
|
86 # screen angle the turning towers face on the tower sheet, the same as a new |
|
3a831478eda4
adjusted tower pivot and angle definitions to better match scribbled assets
ark
parents:
16
diff
changeset
|
87 # tower in the game: up |
|
3a831478eda4
adjusted tower pivot and angle definitions to better match scribbled assets
ark
parents:
16
diff
changeset
|
88 FACE = -90 |
|
3a831478eda4
adjusted tower pivot and angle definitions to better match scribbled assets
ark
parents:
16
diff
changeset
|
89 |
|
3a831478eda4
adjusted tower pivot and angle definitions to better match scribbled assets
ark
parents:
16
diff
changeset
|
90 |
|
3a831478eda4
adjusted tower pivot and angle definitions to better match scribbled assets
ark
parents:
16
diff
changeset
|
91 def _turned(im, pivot, drawn): |
|
3a831478eda4
adjusted tower pivot and angle definitions to better match scribbled assets
ark
parents:
16
diff
changeset
|
92 """ |
|
3a831478eda4
adjusted tower pivot and angle definitions to better match scribbled assets
ark
parents:
16
diff
changeset
|
93 im on a transparent square with the pivot (shares of width and height) at |
|
3a831478eda4
adjusted tower pivot and angle definitions to better match scribbled assets
ark
parents:
16
diff
changeset
|
94 its centre. a sprite drawn pointing to angle drawn is turned around the |
|
3a831478eda4
adjusted tower pivot and angle definitions to better match scribbled assets
ark
parents:
16
diff
changeset
|
95 pivot to face FACE; drawn None leaves it as it is. |
|
3a831478eda4
adjusted tower pivot and angle definitions to better match scribbled assets
ark
parents:
16
diff
changeset
|
96 """ |
|
3a831478eda4
adjusted tower pivot and angle definitions to better match scribbled assets
ark
parents:
16
diff
changeset
|
97 px, py = round(im.width * pivot[0]), round(im.height * pivot[1]) |
|
3a831478eda4
adjusted tower pivot and angle definitions to better match scribbled assets
ark
parents:
16
diff
changeset
|
98 r = math.ceil(max(math.hypot(px - x, py - y) for x in (0, im.width) for y in (0, im.height))) |
|
3a831478eda4
adjusted tower pivot and angle definitions to better match scribbled assets
ark
parents:
16
diff
changeset
|
99 sq = Image.new("RGBA", (2 * r, 2 * r), (0, 0, 0, 0)) |
|
3a831478eda4
adjusted tower pivot and angle definitions to better match scribbled assets
ark
parents:
16
diff
changeset
|
100 sq.alpha_composite(im, (r - px, r - py)) |
|
3a831478eda4
adjusted tower pivot and angle definitions to better match scribbled assets
ark
parents:
16
diff
changeset
|
101 if drawn is None: |
|
3a831478eda4
adjusted tower pivot and angle definitions to better match scribbled assets
ark
parents:
16
diff
changeset
|
102 return sq |
|
3a831478eda4
adjusted tower pivot and angle definitions to better match scribbled assets
ark
parents:
16
diff
changeset
|
103 # pillow turns anticlockwise, screen angles grow clockwise |
|
3a831478eda4
adjusted tower pivot and angle definitions to better match scribbled assets
ark
parents:
16
diff
changeset
|
104 return sq.rotate(drawn - FACE, resample=Image.BICUBIC) |
|
3a831478eda4
adjusted tower pivot and angle definitions to better match scribbled assets
ark
parents:
16
diff
changeset
|
105 |
|
3a831478eda4
adjusted tower pivot and angle definitions to better match scribbled assets
ark
parents:
16
diff
changeset
|
106 |
|
3a831478eda4
adjusted tower pivot and angle definitions to better match scribbled assets
ark
parents:
16
diff
changeset
|
107 def tower_sheet(assets, angles, pivots, footprint, scale): |
| 1 | 108 """ |
| 109 tower levels 1-5 per row: at full size on grey, so the white fill shows, | |
|
19
3a831478eda4
adjusted tower pivot and angle definitions to better match scribbled assets
ark
parents:
16
diff
changeset
|
110 and at game scale next to each other. turning towers are turned by their |
|
3a831478eda4
adjusted tower pivot and angle definitions to better match scribbled assets
ark
parents:
16
diff
changeset
|
111 drawing direction in angles to face up, as in the game; a line from the |
|
3a831478eda4
adjusted tower pivot and angle definitions to better match scribbled assets
ark
parents:
16
diff
changeset
|
112 pivot shows that direction, and each tower is labelled with its angle. a |
|
3a831478eda4
adjusted tower pivot and angle definitions to better match scribbled assets
ark
parents:
16
diff
changeset
|
113 cross marks the rotation point, which is the tower position, and a circle |
|
3a831478eda4
adjusted tower pivot and angle definitions to better match scribbled assets
ark
parents:
16
diff
changeset
|
114 the footprint around it. footprint is the footprint radius in map pixels, |
|
3a831478eda4
adjusted tower pivot and angle definitions to better match scribbled assets
ark
parents:
16
diff
changeset
|
115 scale the sprite scale. |
| 1 | 116 """ |
| 117 rows = [] | |
| 118 for t in range(4): | |
| 119 names = [f"t{t}"] + [f"t{t}_{lv}" for lv in range(2, 6)] | |
|
19
3a831478eda4
adjusted tower pivot and angle definitions to better match scribbled assets
ark
parents:
16
diff
changeset
|
120 rows.append([(n, _turned(Image.open(assets / f"{n}.png"), pivots.get(n, (0.5, 0.5)), |
|
3a831478eda4
adjusted tower pivot and angle definitions to better match scribbled assets
ark
parents:
16
diff
changeset
|
121 angles.get(n))) for n in names]) |
| 1 | 122 pad = 16 |
|
19
3a831478eda4
adjusted tower pivot and angle definitions to better match scribbled assets
ark
parents:
16
diff
changeset
|
123 cell = max(im.width for r in rows for _, im in r) + pad |
|
3a831478eda4
adjusted tower pivot and angle definitions to better match scribbled assets
ark
parents:
16
diff
changeset
|
124 cell_h = cell + 12 |
|
3a831478eda4
adjusted tower pivot and angle definitions to better match scribbled assets
ark
parents:
16
diff
changeset
|
125 small_w = round(cell * scale) + 4 |
| 16 | 126 full_r = footprint / scale |
|
19
3a831478eda4
adjusted tower pivot and angle definitions to better match scribbled assets
ark
parents:
16
diff
changeset
|
127 sheet = Image.new("RGBA", (5 * cell + 5 * small_w + pad, 4 * cell_h + 20), |
| 1 | 128 (170, 170, 170, 255)) |
| 129 draw = ImageDraw.Draw(sheet) | |
| 130 for lv in range(5): | |
|
19
3a831478eda4
adjusted tower pivot and angle definitions to better match scribbled assets
ark
parents:
16
diff
changeset
|
131 draw.text((lv * cell + pad, 4), f"level {lv + 1}", fill=(0, 0, 0)) |
|
3a831478eda4
adjusted tower pivot and angle definitions to better match scribbled assets
ark
parents:
16
diff
changeset
|
132 draw.text((5 * cell + pad, 4), f"game scale {scale}, turning towers face {FACE}", |
|
3a831478eda4
adjusted tower pivot and angle definitions to better match scribbled assets
ark
parents:
16
diff
changeset
|
133 fill=(0, 0, 0)) |
|
3a831478eda4
adjusted tower pivot and angle definitions to better match scribbled assets
ark
parents:
16
diff
changeset
|
134 dx, dy = math.cos(math.radians(FACE)), math.sin(math.radians(FACE)) |
| 1 | 135 for t, r in enumerate(rows): |
| 136 y = 20 + t * cell_h | |
|
19
3a831478eda4
adjusted tower pivot and angle definitions to better match scribbled assets
ark
parents:
16
diff
changeset
|
137 cy = y + 12 + cell // 2 |
|
3a831478eda4
adjusted tower pivot and angle definitions to better match scribbled assets
ark
parents:
16
diff
changeset
|
138 for lv, (name, im) in enumerate(r): |
|
3a831478eda4
adjusted tower pivot and angle definitions to better match scribbled assets
ark
parents:
16
diff
changeset
|
139 # label above, sprite centred on the pivot in the cell below it |
|
3a831478eda4
adjusted tower pivot and angle definitions to better match scribbled assets
ark
parents:
16
diff
changeset
|
140 drawn = angles.get(name) |
|
3a831478eda4
adjusted tower pivot and angle definitions to better match scribbled assets
ark
parents:
16
diff
changeset
|
141 draw.text((lv * cell + pad // 2, y), f"{name}: " + ("does not turn" if drawn is None |
|
3a831478eda4
adjusted tower pivot and angle definitions to better match scribbled assets
ark
parents:
16
diff
changeset
|
142 else f"angle {drawn}"), |
|
3a831478eda4
adjusted tower pivot and angle definitions to better match scribbled assets
ark
parents:
16
diff
changeset
|
143 fill=(0, 0, 0)) |
|
3a831478eda4
adjusted tower pivot and angle definitions to better match scribbled assets
ark
parents:
16
diff
changeset
|
144 cx = lv * cell + cell // 2 |
|
3a831478eda4
adjusted tower pivot and angle definitions to better match scribbled assets
ark
parents:
16
diff
changeset
|
145 sheet.alpha_composite(im, (cx - im.width // 2, cy - im.height // 2)) |
|
3a831478eda4
adjusted tower pivot and angle definitions to better match scribbled assets
ark
parents:
16
diff
changeset
|
146 if drawn is not None: |
|
3a831478eda4
adjusted tower pivot and angle definitions to better match scribbled assets
ark
parents:
16
diff
changeset
|
147 arm = cell // 2 - pad // 2 |
|
3a831478eda4
adjusted tower pivot and angle definitions to better match scribbled assets
ark
parents:
16
diff
changeset
|
148 draw.line((cx, cy, cx + dx * arm, cy + dy * arm), fill=(255, 0, 0)) |
| 1 | 149 draw.line((cx - 5, cy, cx + 5, cy), fill=(255, 0, 0)) |
| 150 draw.line((cx, cy - 5, cx, cy + 5), fill=(255, 0, 0)) | |
| 16 | 151 draw.ellipse((cx - full_r, cy - full_r, cx + full_r, cy + full_r), |
| 152 outline=(0, 0, 255), width=2) | |
| 153 small = im.resize((max(1, round(im.width * scale)), max(1, round(im.height * scale))), | |
| 154 Image.LANCZOS) | |
|
19
3a831478eda4
adjusted tower pivot and angle definitions to better match scribbled assets
ark
parents:
16
diff
changeset
|
155 scx, scy = 5 * cell + pad + lv * small_w + small_w // 2, cy |
|
3a831478eda4
adjusted tower pivot and angle definitions to better match scribbled assets
ark
parents:
16
diff
changeset
|
156 sheet.alpha_composite(small, (scx - small.width // 2, scy - small.height // 2)) |
| 16 | 157 draw.ellipse((scx - footprint, scy - footprint, scx + footprint, scy + footprint), |
| 158 outline=(0, 0, 255)) | |
| 1 | 159 return sheet |
