|
1
|
1 """
|
|
|
2 preview images of the build, for checking routes, placement and tower levels
|
|
|
3 without starting the game.
|
|
|
4 """
|
|
|
5
|
|
|
6 from PIL import Image, ImageDraw
|
|
|
7
|
|
|
8 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)]
|
|
|
10
|
|
|
11
|
|
|
12 def _base(map_path, mp):
|
|
|
13 """map image faded towards white, with the placement grid drawn on it."""
|
|
|
14 im = Image.open(map_path).convert("RGBA")
|
|
|
15 im = Image.blend(im, Image.new("RGBA", im.size, (255, 255, 255, 255)), 0.55)
|
|
|
16 draw = ImageDraw.Draw(im)
|
|
|
17 cell, cols = mp["cell"], mp["cols"]
|
|
|
18 for i, c in enumerate(mp["grid"]):
|
|
|
19 if c == "1":
|
|
|
20 x, y = (i % cols) * cell, (i // cols) * cell
|
|
|
21 draw.rectangle((x, y, x + cell - 2, y + cell - 2), fill=(60, 170, 60, 255))
|
|
|
22 return im, draw
|
|
|
23
|
|
|
24
|
|
|
25 def _route(draw, pts, colour, width):
|
|
|
26 pts = [tuple(p) for p in pts]
|
|
|
27 draw.line(pts, fill=colour, width=width, joint="curve")
|
|
|
28 for x, y in pts:
|
|
|
29 draw.ellipse((x - 2, y - 2, x + 2, y + 2), fill=(0, 0, 0))
|
|
|
30 # arrow heads at a few points, showing the walking direction
|
|
|
31 for k in range(1, 6):
|
|
|
32 i = k * (len(pts) - 1) // 6
|
|
|
33 (x0, y0), (x1, y1) = pts[i], pts[i + 1]
|
|
|
34 dx, dy = x1 - x0, y1 - y0
|
|
|
35 n = (dx * dx + dy * dy) ** 0.5 or 1
|
|
|
36 dx, dy = dx / n, dy / n
|
|
|
37 tip = (x0 + dx * 6, y0 + dy * 6)
|
|
|
38 draw.polygon([tip, (x0 - dx * 6 - dy * 6, y0 - dy * 6 + dx * 6),
|
|
|
39 (x0 - dx * 6 + dy * 6, y0 - dy * 6 - dx * 6)], fill=colour)
|
|
|
40
|
|
|
41
|
|
|
42 def _ends(draw, mp):
|
|
|
43 for x, y, r in mp["entrances"]:
|
|
|
44 draw.ellipse((x - r, y - r, x + r, y + r), outline=(0, 0, 255), width=3)
|
|
|
45 for x, y, r in mp["exits"]:
|
|
|
46 draw.ellipse((x - r, y - r, x + r, y + r), outline=(0, 160, 0), width=3)
|
|
|
47
|
|
|
48
|
|
|
49 def map_preview(map_path, mp):
|
|
|
50 """the map with grid, centre line graph, routes, entrances and exits."""
|
|
|
51 im, draw = _base(map_path, mp)
|
|
|
52 for pts in mp["graph"]["edges"]:
|
|
|
53 draw.line([tuple(p) for p in pts], fill=(120, 120, 120), width=1)
|
|
|
54 for x, y in mp["graph"]["nodes"]:
|
|
|
55 draw.rectangle((x - 3, y - 3, x + 3, y + 3), outline=(0, 0, 0))
|
|
|
56 for k, r in enumerate(mp["routes"]):
|
|
|
57 _route(draw, r["points"], ROUTE_COLOURS[k % len(ROUTE_COLOURS)], 3)
|
|
|
58 _ends(draw, mp)
|
|
|
59 for k, r in enumerate(mp["routes"]):
|
|
|
60 x, y = r["points"][len(r["points"]) // 3]
|
|
|
61 draw.text((x + 6, y + 6 + 10 * k), f"route {r['number']}", fill=(0, 0, 0))
|
|
|
62 return im
|
|
|
63
|
|
|
64
|
|
|
65 def route_sheet(map_path, mp):
|
|
|
66 """one panel per route, half size, for maps with several routes."""
|
|
|
67 panels = []
|
|
|
68 for k, r in enumerate(mp["routes"]):
|
|
|
69 im, draw = _base(map_path, mp)
|
|
|
70 _route(draw, r["points"], ROUTE_COLOURS[k % len(ROUTE_COLOURS)], 5)
|
|
|
71 _ends(draw, mp)
|
|
|
72 draw.text((10, 10), f"route {r['number']}: length {r['length']:.0f},"
|
|
|
73 f" turning {r['turning']:.0f}, value {r['value']:.2f}", fill=(0, 0, 0))
|
|
|
74 panels.append(im.resize((im.width // 2, im.height // 2)))
|
|
|
75 cols = min(3, len(panels))
|
|
|
76 rows = (len(panels) + cols - 1) // cols
|
|
|
77 pw, ph = panels[0].size
|
|
|
78 sheet = Image.new("RGBA", (cols * pw, rows * ph), (255, 255, 255, 255))
|
|
|
79 for k, p in enumerate(panels):
|
|
|
80 sheet.paste(p, ((k % cols) * pw, (k // cols) * ph))
|
|
|
81 return sheet
|
|
|
82
|
|
|
83
|
|
16
|
84 def tower_sheet(assets, pivots, footprint, scale):
|
|
1
|
85 """
|
|
|
86 tower levels 1-5 per row: at full size on grey, so the white fill shows,
|
|
16
|
87 and at game scale next to each other. a cross marks the rotation point,
|
|
|
88 which is the tower position, and a circle the footprint around it.
|
|
|
89 footprint is the footprint radius in map pixels, scale the sprite scale.
|
|
1
|
90 """
|
|
|
91 rows = []
|
|
|
92 for t in range(4):
|
|
|
93 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])
|
|
|
95 pad = 16
|
|
|
96 cell_w = 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
|
|
16
|
98 small_w = round(cell_w * scale) + 4
|
|
|
99 full_r = footprint / scale
|
|
1
|
100 sheet = Image.new("RGBA", (5 * cell_w + 5 * small_w + pad, 4 * cell_h + 20),
|
|
|
101 (170, 170, 170, 255))
|
|
|
102 draw = ImageDraw.Draw(sheet)
|
|
|
103 for lv in range(5):
|
|
|
104 draw.text((lv * cell_w + pad, 4), f"level {lv + 1}", fill=(0, 0, 0))
|
|
16
|
105 draw.text((5 * cell_w + pad, 4), f"game scale {scale}", fill=(0, 0, 0))
|
|
1
|
106 for t, r in enumerate(rows):
|
|
|
107 y = 20 + t * cell_h
|
|
|
108 for lv, im in enumerate(r):
|
|
|
109 x = lv * cell_w + pad // 2
|
|
|
110 sheet.alpha_composite(im, (x, y))
|
|
2
|
111 name = f"t{t}" if lv == 0 else f"t{t}_{lv + 1}"
|
|
|
112 px, py = pivots.get(name, (0.5, 0.5))
|
|
|
113 cx, cy = x + round(im.width * px), y + round(im.height * py)
|
|
1
|
114 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))
|
|
16
|
116 draw.ellipse((cx - full_r, cy - full_r, cx + full_r, cy + full_r),
|
|
|
117 outline=(0, 0, 255), width=2)
|
|
|
118 small = im.resize((max(1, round(im.width * scale)), max(1, round(im.height * scale))),
|
|
|
119 Image.LANCZOS)
|
|
|
120 sx, sy = 5 * cell_w + pad + lv * small_w, y + cell_h // 3
|
|
|
121 sheet.alpha_composite(small, (sx, sy))
|
|
|
122 scx, scy = sx + small.width * px, sy + small.height * py
|
|
|
123 draw.ellipse((scx - footprint, scy - footprint, scx + footprint, scy + footprint),
|
|
|
124 outline=(0, 0, 255))
|
|
1
|
125 return sheet
|