wobblyhead hg > minor_monsters
comparison tools/build.py @ 8:2c4fed8baed9
art update, persistent sound setting, tower selling as double action
| author | ark |
|---|---|
| date | 2026-10-19 13:37 +0200 |
| parents | 910b26feca76 |
| children | 53f015966118 |
comparison
equal
deleted
inserted
replaced
| 7:4fd2da999ba4 | 8:2c4fed8baed9 |
|---|---|
| 6 | 6 |
| 7 steps: sprites, tower levels, maps, masks, sounds, config, checks, previews. | 7 steps: sprites, tower levels, maps, masks, sounds, config, checks, previews. |
| 8 a step | 8 a step |
| 9 is skipped when all its outputs are newer than its inputs and the build | 9 is skipped when all its outputs are newer than its inputs and the build |
| 10 scripts; --force runs every step. --watch reruns the build whenever a file | 10 scripts; --force runs every step. --watch reruns the build whenever a file |
| 11 in art/, art/sounds/ or config/ changes. | 11 in art/, art/sounds/, art/digits/ or config/ changes. |
| 12 | 12 |
| 13 requires python 3.11 or later and pillow. | 13 requires python 3.11 or later and pillow. |
| 14 """ | 14 """ |
| 15 | 15 |
| 16 import base64 | 16 import base64 |
| 32 import preview | 32 import preview |
| 33 | 33 |
| 34 ROOT = Path(__file__).resolve().parent.parent | 34 ROOT = Path(__file__).resolve().parent.parent |
| 35 ART = ROOT / "art" | 35 ART = ROOT / "art" |
| 36 SOUNDS = ART / "sounds" | 36 SOUNDS = ART / "sounds" |
| 37 DIGITS = ART / "digits" | |
| 37 CONFIG = ROOT / "config" | 38 CONFIG = ROOT / "config" |
| 38 ASSETS = ROOT / "game" / "assets" | 39 ASSETS = ROOT / "game" / "assets" |
| 39 DATA = ROOT / "game" / "data" | 40 DATA = ROOT / "game" / "data" |
| 40 TEMP = ROOT / "temp" | 41 TEMP = ROOT / "temp" |
| 41 PREVIEW = TEMP / "preview" | 42 PREVIEW = TEMP / "preview" |
| 45 TOOLS = sorted((ROOT / "tools").glob("*.py")) | 46 TOOLS = sorted((ROOT / "tools").glob("*.py")) |
| 46 HEADER = "// generated by tools/build.py from {src}, do not edit\n" | 47 HEADER = "// generated by tools/build.py from {src}, do not edit\n" |
| 47 | 48 |
| 48 SPRITES = (["m%d" % i for i in range(5)] + ["b%d" % i for i in range(4)] | 49 SPRITES = (["m%d" % i for i in range(5)] + ["b%d" % i for i in range(4)] |
| 49 + ["t%d" % i for i in range(4)] + ["p%d" % i for i in range(4)] | 50 + ["t%d" % i for i in range(4)] + ["p%d" % i for i in range(4)] |
| 50 + ["c%d" % i for i in range(4)] + ["hpm", "hpb"]) | 51 + ["c%d" % i for i in range(4)] + ["hpm", "hpb"] |
| 52 # icons, buttons and screens (art.md) | |
| 53 + ["coin_icon", "heart", "monster_icon", "play_button", "pause_button", | |
| 54 "speed_x1", "speed_x2", "speed_x4", "sound_on", "sound_off", "next_wave", | |
| 55 "shop_bubble", "shop_bubble_dark", "shop_bubble_dark2", "tower_popup", | |
| 56 "upgrade_button", "sell_button", "monster_death", "boss_death", | |
| 57 "splash", "splash_play", "map_won", "map_won_button", | |
| 58 "game_lost", "game_lost_button", "game_won", "game_won_button"]) | |
| 51 # hp bar variants (art.md): the empty part is a darkened copy, the bar of a | 59 # hp bar variants (art.md): the empty part is a darkened copy, the bar of a |
| 52 # slowed monster has its colour set to this hue in degrees | 60 # slowed monster has its colour set to this hue in degrees |
| 53 BAR_DARKEN = 0.35 | 61 BAR_DARKEN = 0.35 |
| 54 BAR_SLOW_HUE = 220 | 62 BAR_SLOW_HUE = 220 |
| 55 BARS = ["hpm", "hpb"] | 63 BARS = ["hpm", "hpb"] |
| 88 | 96 |
| 89 | 97 |
| 90 # 1. sprites | 98 # 1. sprites |
| 91 | 99 |
| 92 def step_sprites(force): | 100 def step_sprites(force): |
| 93 inputs = [ART / f"{n}.png" for n in SPRITES] | 101 # digits cut by tools/cut_digits.py, written as digit_NAME_N |
| 94 outputs = [ASSETS / f"{n}.png" for n in SPRITES + BAR_VARIANTS] | 102 digits = sorted(DIGITS.glob("*.png")) |
| 103 inputs = [ART / f"{n}.png" for n in SPRITES] + digits + [DIGITS] | |
| 104 outputs = ([ASSETS / f"{n}.png" for n in SPRITES + BAR_VARIANTS] | |
| 105 + [ASSETS / f"digit_{p.name}" for p in digits]) | |
| 95 if not stale(inputs, outputs, force): | 106 if not stale(inputs, outputs, force): |
| 96 print("sprites: up to date") | 107 print("sprites: up to date") |
| 97 return False | 108 return False |
| 98 ASSETS.mkdir(parents=True, exist_ok=True) | 109 ASSETS.mkdir(parents=True, exist_ok=True) |
| 110 for p in ASSETS.glob("digit_*.png"): | |
| 111 p.unlink() | |
| 112 for p in digits: | |
| 113 images.trim(Image.open(p).convert("RGBA")).save(ASSETS / f"digit_{p.name}", optimize=True) | |
| 99 for n in SPRITES: | 114 for n in SPRITES: |
| 100 im = images.trim(Image.open(ART / f"{n}.png").convert("RGBA")) | 115 im = images.trim(Image.open(ART / f"{n}.png").convert("RGBA")) |
| 101 im.save(ASSETS / f"{n}.png", optimize=True) | 116 im.save(ASSETS / f"{n}.png", optimize=True) |
| 102 if n in BARS: | 117 if n in BARS: |
| 103 slow = images.recolour(im, BAR_SLOW_HUE) | 118 slow = images.recolour(im, BAR_SLOW_HUE) |
| 104 images.darken(im, BAR_DARKEN).save(ASSETS / f"{n}_empty.png", optimize=True) | 119 images.darken(im, BAR_DARKEN).save(ASSETS / f"{n}_empty.png", optimize=True) |
| 105 slow.save(ASSETS / f"{n}_slow.png", optimize=True) | 120 slow.save(ASSETS / f"{n}_slow.png", optimize=True) |
| 106 images.darken(slow, BAR_DARKEN).save(ASSETS / f"{n}_slow_empty.png", optimize=True) | 121 images.darken(slow, BAR_DARKEN).save(ASSETS / f"{n}_slow_empty.png", optimize=True) |
| 107 print(f"sprites: wrote {len(SPRITES)} sprites and {len(BAR_VARIANTS)} bar variants") | 122 print(f"sprites: wrote {len(SPRITES)} sprites, {len(BAR_VARIANTS)} bar variants" |
| 123 f" and {len(digits)} digits") | |
| 108 return True | 124 return True |
| 109 | 125 |
| 110 | 126 |
| 111 # 2. tower levels | 127 # 2. tower levels |
| 112 | 128 |
| 490 return True | 506 return True |
| 491 | 507 |
| 492 | 508 |
| 493 def watch(): | 509 def watch(): |
| 494 def snapshot(): | 510 def snapshot(): |
| 495 files = list(ART.iterdir()) + list(SOUNDS.glob("*.mp3")) + list(CONFIG.iterdir()) + TOOLS | 511 files = (list(ART.iterdir()) + list(SOUNDS.glob("*.mp3")) + list(DIGITS.glob("*.png")) |
| 512 + list(CONFIG.iterdir()) + TOOLS) | |
| 496 return {p: p.stat().st_mtime for p in files if p.is_file()} | 513 return {p: p.stat().st_mtime for p in files if p.is_file()} |
| 497 | 514 |
| 498 seen = None | 515 seen = None |
| 499 while True: | 516 while True: |
| 500 now = snapshot() | 517 now = snapshot() |
