wobblyhead hg > minor_monsters
comparison 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 |
comparison
equal
deleted
inserted
replaced
| 18:5dc69898a091 | 19:3a831478eda4 |
|---|---|
| 1 """ | 1 """ |
| 2 preview images of the build, for checking routes, placement and tower levels | 2 preview images of the build, for checking routes, placement and tower levels |
| 3 without starting the game. | 3 without starting the game. |
| 4 """ | 4 """ |
| 5 | |
| 6 import math | |
| 5 | 7 |
| 6 from PIL import Image, ImageDraw | 8 from PIL import Image, ImageDraw |
| 7 | 9 |
| 8 ROUTE_COLOURS = [(220, 0, 160), (0, 90, 255), (255, 120, 0), (0, 160, 60), | 10 ROUTE_COLOURS = [(220, 0, 160), (0, 90, 255), (255, 120, 0), (0, 160, 60), |
| 9 (140, 0, 255), (0, 180, 200), (200, 40, 40), (90, 90, 90)] | 11 (140, 0, 255), (0, 180, 200), (200, 40, 40), (90, 90, 90)] |
| 79 for k, p in enumerate(panels): | 81 for k, p in enumerate(panels): |
| 80 sheet.paste(p, ((k % cols) * pw, (k // cols) * ph)) | 82 sheet.paste(p, ((k % cols) * pw, (k // cols) * ph)) |
| 81 return sheet | 83 return sheet |
| 82 | 84 |
| 83 | 85 |
| 84 def tower_sheet(assets, pivots, footprint, scale): | 86 # screen angle the turning towers face on the tower sheet, the same as a new |
| 87 # tower in the game: up | |
| 88 FACE = -90 | |
| 89 | |
| 90 | |
| 91 def _turned(im, pivot, drawn): | |
| 92 """ | |
| 93 im on a transparent square with the pivot (shares of width and height) at | |
| 94 its centre. a sprite drawn pointing to angle drawn is turned around the | |
| 95 pivot to face FACE; drawn None leaves it as it is. | |
| 96 """ | |
| 97 px, py = round(im.width * pivot[0]), round(im.height * pivot[1]) | |
| 98 r = math.ceil(max(math.hypot(px - x, py - y) for x in (0, im.width) for y in (0, im.height))) | |
| 99 sq = Image.new("RGBA", (2 * r, 2 * r), (0, 0, 0, 0)) | |
| 100 sq.alpha_composite(im, (r - px, r - py)) | |
| 101 if drawn is None: | |
| 102 return sq | |
| 103 # pillow turns anticlockwise, screen angles grow clockwise | |
| 104 return sq.rotate(drawn - FACE, resample=Image.BICUBIC) | |
| 105 | |
| 106 | |
| 107 def tower_sheet(assets, angles, pivots, footprint, scale): | |
| 85 """ | 108 """ |
| 86 tower levels 1-5 per row: at full size on grey, so the white fill shows, | 109 tower levels 1-5 per row: at full size on grey, so the white fill shows, |
| 87 and at game scale next to each other. a cross marks the rotation point, | 110 and at game scale next to each other. turning towers are turned by their |
| 88 which is the tower position, and a circle the footprint around it. | 111 drawing direction in angles to face up, as in the game; a line from the |
| 89 footprint is the footprint radius in map pixels, scale the sprite scale. | 112 pivot shows that direction, and each tower is labelled with its angle. a |
| 113 cross marks the rotation point, which is the tower position, and a circle | |
| 114 the footprint around it. footprint is the footprint radius in map pixels, | |
| 115 scale the sprite scale. | |
| 90 """ | 116 """ |
| 91 rows = [] | 117 rows = [] |
| 92 for t in range(4): | 118 for t in range(4): |
| 93 names = [f"t{t}"] + [f"t{t}_{lv}" for lv in range(2, 6)] | 119 names = [f"t{t}"] + [f"t{t}_{lv}" for lv in range(2, 6)] |
| 94 rows.append([Image.open(assets / f"{n}.png") for n in names]) | 120 rows.append([(n, _turned(Image.open(assets / f"{n}.png"), pivots.get(n, (0.5, 0.5)), |
| 121 angles.get(n))) for n in names]) | |
| 95 pad = 16 | 122 pad = 16 |
| 96 cell_w = max(im.width for r in rows for im in r) + pad | 123 cell = max(im.width for r in rows for _, im in r) + pad |
| 97 cell_h = max(im.height for r in rows for im in r) + pad | 124 cell_h = cell + 12 |
| 98 small_w = round(cell_w * scale) + 4 | 125 small_w = round(cell * scale) + 4 |
| 99 full_r = footprint / scale | 126 full_r = footprint / scale |
| 100 sheet = Image.new("RGBA", (5 * cell_w + 5 * small_w + pad, 4 * cell_h + 20), | 127 sheet = Image.new("RGBA", (5 * cell + 5 * small_w + pad, 4 * cell_h + 20), |
| 101 (170, 170, 170, 255)) | 128 (170, 170, 170, 255)) |
| 102 draw = ImageDraw.Draw(sheet) | 129 draw = ImageDraw.Draw(sheet) |
| 103 for lv in range(5): | 130 for lv in range(5): |
| 104 draw.text((lv * cell_w + pad, 4), f"level {lv + 1}", fill=(0, 0, 0)) | 131 draw.text((lv * cell + pad, 4), f"level {lv + 1}", fill=(0, 0, 0)) |
| 105 draw.text((5 * cell_w + pad, 4), f"game scale {scale}", fill=(0, 0, 0)) | 132 draw.text((5 * cell + pad, 4), f"game scale {scale}, turning towers face {FACE}", |
| 133 fill=(0, 0, 0)) | |
| 134 dx, dy = math.cos(math.radians(FACE)), math.sin(math.radians(FACE)) | |
| 106 for t, r in enumerate(rows): | 135 for t, r in enumerate(rows): |
| 107 y = 20 + t * cell_h | 136 y = 20 + t * cell_h |
| 108 for lv, im in enumerate(r): | 137 cy = y + 12 + cell // 2 |
| 109 x = lv * cell_w + pad // 2 | 138 for lv, (name, im) in enumerate(r): |
| 110 sheet.alpha_composite(im, (x, y)) | 139 # label above, sprite centred on the pivot in the cell below it |
| 111 name = f"t{t}" if lv == 0 else f"t{t}_{lv + 1}" | 140 drawn = angles.get(name) |
| 112 px, py = pivots.get(name, (0.5, 0.5)) | 141 draw.text((lv * cell + pad // 2, y), f"{name}: " + ("does not turn" if drawn is None |
| 113 cx, cy = x + round(im.width * px), y + round(im.height * py) | 142 else f"angle {drawn}"), |
| 143 fill=(0, 0, 0)) | |
| 144 cx = lv * cell + cell // 2 | |
| 145 sheet.alpha_composite(im, (cx - im.width // 2, cy - im.height // 2)) | |
| 146 if drawn is not None: | |
| 147 arm = cell // 2 - pad // 2 | |
| 148 draw.line((cx, cy, cx + dx * arm, cy + dy * arm), fill=(255, 0, 0)) | |
| 114 draw.line((cx - 5, cy, cx + 5, cy), fill=(255, 0, 0)) | 149 draw.line((cx - 5, cy, cx + 5, cy), fill=(255, 0, 0)) |
| 115 draw.line((cx, cy - 5, cx, cy + 5), fill=(255, 0, 0)) | 150 draw.line((cx, cy - 5, cx, cy + 5), fill=(255, 0, 0)) |
| 116 draw.ellipse((cx - full_r, cy - full_r, cx + full_r, cy + full_r), | 151 draw.ellipse((cx - full_r, cy - full_r, cx + full_r, cy + full_r), |
| 117 outline=(0, 0, 255), width=2) | 152 outline=(0, 0, 255), width=2) |
| 118 small = im.resize((max(1, round(im.width * scale)), max(1, round(im.height * scale))), | 153 small = im.resize((max(1, round(im.width * scale)), max(1, round(im.height * scale))), |
| 119 Image.LANCZOS) | 154 Image.LANCZOS) |
| 120 sx, sy = 5 * cell_w + pad + lv * small_w, y + cell_h // 3 | 155 scx, scy = 5 * cell + pad + lv * small_w + small_w // 2, cy |
| 121 sheet.alpha_composite(small, (sx, sy)) | 156 sheet.alpha_composite(small, (scx - small.width // 2, scy - small.height // 2)) |
| 122 scx, scy = sx + small.width * px, sy + small.height * py | |
| 123 draw.ellipse((scx - footprint, scy - footprint, scx + footprint, scy + footprint), | 157 draw.ellipse((scx - footprint, scy - footprint, scx + footprint, scy + footprint), |
| 124 outline=(0, 0, 255)) | 158 outline=(0, 0, 255)) |
| 125 return sheet | 159 return sheet |
