spawnpoint-studio 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (118) hide show
  1. spawnpoint/__init__.py +3 -0
  2. spawnpoint/_data/examples/coin_hopper.json +228 -0
  3. spawnpoint/_data/examples/neon_arena.json +251 -0
  4. spawnpoint/_data/examples/pixel_arena.json +242 -0
  5. spawnpoint/_data/examples/pixel_hopper.json +221 -0
  6. spawnpoint/_data/godot/addons/spawn_point/components/aim_component.gd +63 -0
  7. spawnpoint/_data/godot/addons/spawn_point/components/animator_component.gd +66 -0
  8. spawnpoint/_data/godot/addons/spawn_point/components/chase_ai_component.gd +107 -0
  9. spawnpoint/_data/godot/addons/spawn_point/components/collectible_component.gd +58 -0
  10. spawnpoint/_data/godot/addons/spawn_point/components/gate_component.gd +64 -0
  11. spawnpoint/_data/godot/addons/spawn_point/components/health_component.gd +83 -0
  12. spawnpoint/_data/godot/addons/spawn_point/components/hitbox_component.gd +50 -0
  13. spawnpoint/_data/godot/addons/spawn_point/components/hurtbox_component.gd +48 -0
  14. spawnpoint/_data/godot/addons/spawn_point/components/juice_component.gd +219 -0
  15. spawnpoint/_data/godot/addons/spawn_point/components/patrol_ai_component.gd +63 -0
  16. spawnpoint/_data/godot/addons/spawn_point/components/platformer_movement_component.gd +204 -0
  17. spawnpoint/_data/godot/addons/spawn_point/components/player_input_component.gd +77 -0
  18. spawnpoint/_data/godot/addons/spawn_point/components/projectile_component.gd +64 -0
  19. spawnpoint/_data/godot/addons/spawn_point/components/spawner_component.gd +58 -0
  20. spawnpoint/_data/godot/addons/spawn_point/components/stomp_component.gd +56 -0
  21. spawnpoint/_data/godot/addons/spawn_point/components/top_down_movement_component.gd +101 -0
  22. spawnpoint/_data/godot/addons/spawn_point/components/trigger_zone_component.gd +32 -0
  23. spawnpoint/_data/godot/addons/spawn_point/components/weapon_component.gd +83 -0
  24. spawnpoint/_data/godot/addons/spawn_point/manifest.json +1509 -0
  25. spawnpoint/_data/godot/addons/spawn_point/runtime/camera_rig.gd +71 -0
  26. spawnpoint/_data/godot/addons/spawn_point/runtime/cockpit.gd +62 -0
  27. spawnpoint/_data/godot/addons/spawn_point/runtime/entity_core.gd +98 -0
  28. spawnpoint/_data/godot/addons/spawn_point/runtime/fx.gd +112 -0
  29. spawnpoint/_data/godot/addons/spawn_point/runtime/game_state.gd +111 -0
  30. spawnpoint/_data/godot/addons/spawn_point/runtime/hud.gd +155 -0
  31. spawnpoint/_data/godot/addons/spawn_point/runtime/level.gd +19 -0
  32. spawnpoint/_data/godot/addons/spawn_point/runtime/sfx.gd +58 -0
  33. spawnpoint/_data/godot/addons/spawn_point/runtime/sp.gd +121 -0
  34. spawnpoint/_data/godot/addons/spawn_point/runtime/world.gd +130 -0
  35. spawnpoint/_data/godot/addons/spawn_point/shaders/flash.gdshader +9 -0
  36. spawnpoint/_data/godot/addons/spawn_point/shaders/floor_grid.gdshader +13 -0
  37. spawnpoint/_data/godot/addons/spawn_point/tools/build.gd +612 -0
  38. spawnpoint/_data/godot/addons/spawn_point/tools/build.tscn +6 -0
  39. spawnpoint/_data/godot/addons/spawn_point/tools/capture.gd +42 -0
  40. spawnpoint/_data/godot/addons/spawn_point/tools/capture.tscn +6 -0
  41. spawnpoint/_data/godot/addons/spawn_point/tools/dump_manifest.gd +91 -0
  42. spawnpoint/_data/godot/addons/spawn_point/tools/dump_manifest.tscn +6 -0
  43. spawnpoint/_data/godot/addons/spawn_point/tools/playtest.gd +428 -0
  44. spawnpoint/_data/godot/addons/spawn_point/tools/playtest.tscn +6 -0
  45. spawnpoint/_data/godot/addons/spawn_point/tools/playtest_nav.gd +152 -0
  46. spawnpoint/_data/godot/addons/spawn_point/tools/record.gd +89 -0
  47. spawnpoint/_data/godot/addons/spawn_point/tools/record.tscn +6 -0
  48. spawnpoint/_data/godot/addons/spawn_point/tools/smoke.gd +72 -0
  49. spawnpoint/_data/godot/addons/spawn_point/tools/smoke.tscn +6 -0
  50. spawnpoint/analyze.py +387 -0
  51. spawnpoint/build.py +146 -0
  52. spawnpoint/cli.py +290 -0
  53. spawnpoint/cockpit/__init__.py +0 -0
  54. spawnpoint/cockpit/server.py +473 -0
  55. spawnpoint/cockpit/static/app.css +111 -0
  56. spawnpoint/cockpit/static/app.js +327 -0
  57. spawnpoint/cockpit/static/index.html +79 -0
  58. spawnpoint/diagnostics.py +91 -0
  59. spawnpoint/expr.py +219 -0
  60. spawnpoint/gif.py +58 -0
  61. spawnpoint/godot.py +107 -0
  62. spawnpoint/graph.py +175 -0
  63. spawnpoint/level.py +157 -0
  64. spawnpoint/llm_server.py +77 -0
  65. spawnpoint/manifest.py +222 -0
  66. spawnpoint/playtest/__init__.py +104 -0
  67. spawnpoint/playtest/report.py +148 -0
  68. spawnpoint/playtest/runner.py +51 -0
  69. spawnpoint/playtest/tuner.py +151 -0
  70. spawnpoint/project.py +94 -0
  71. spawnpoint/py.typed +0 -0
  72. spawnpoint/rules.py +310 -0
  73. spawnpoint/sfx.py +352 -0
  74. spawnpoint/sprites/__init__.py +0 -0
  75. spawnpoint/sprites/ai.py +222 -0
  76. spawnpoint/sprites/contour.py +147 -0
  77. spawnpoint/sprites/generate.py +507 -0
  78. spawnpoint/sprites/importer.py +212 -0
  79. spawnpoint/sprites/resolve.py +139 -0
  80. spawnpoint/sprites/spriteset.py +159 -0
  81. spawnpoint/sprites/tiles.py +102 -0
  82. spawnpoint/studio/__init__.py +0 -0
  83. spawnpoint/studio/assemble.py +528 -0
  84. spawnpoint/studio/brief.py +197 -0
  85. spawnpoint/studio/creative.py +124 -0
  86. spawnpoint/studio/dataset.py +136 -0
  87. spawnpoint/studio/edits.py +206 -0
  88. spawnpoint/studio/levelgen.py +352 -0
  89. spawnpoint/studio/llm.py +145 -0
  90. spawnpoint/studio/orchestrator.py +341 -0
  91. spawnpoint/studio/prompts.py +122 -0
  92. spawnpoint/studio/web.py +123 -0
  93. spawnpoint/studio/webcheck.py +210 -0
  94. spawnpoint/studio_cli.py +232 -0
  95. spawnpoint_studio-0.1.0.dist-info/METADATA +360 -0
  96. spawnpoint_studio-0.1.0.dist-info/RECORD +118 -0
  97. spawnpoint_studio-0.1.0.dist-info/WHEEL +5 -0
  98. spawnpoint_studio-0.1.0.dist-info/entry_points.txt +2 -0
  99. spawnpoint_studio-0.1.0.dist-info/licenses/LICENSE +21 -0
  100. spawnpoint_studio-0.1.0.dist-info/top_level.txt +2 -0
  101. tests/conftest.py +49 -0
  102. tests/test_ai_sprites.py +122 -0
  103. tests/test_analyze.py +116 -0
  104. tests/test_cockpit.py +148 -0
  105. tests/test_e2e.py +33 -0
  106. tests/test_expr.py +59 -0
  107. tests/test_gif.py +19 -0
  108. tests/test_godot_library.py +17 -0
  109. tests/test_graph.py +44 -0
  110. tests/test_level.py +64 -0
  111. tests/test_live_llm.py +38 -0
  112. tests/test_manifest.py +54 -0
  113. tests/test_playtest.py +129 -0
  114. tests/test_rules.py +85 -0
  115. tests/test_sfx.py +30 -0
  116. tests/test_sprites.py +261 -0
  117. tests/test_studio.py +335 -0
  118. tests/test_web.py +41 -0
@@ -0,0 +1,507 @@
1
+ """Procedural pixel-art sprites with animations. Deterministic, GPU-free.
2
+
3
+ Every kind is a painter on a small canvas (drawn at 2x). Colors come from a 4-tone ramp derived from
4
+ one base color; extra features (ears, mask, wings, horns...) come from the entity name and a seed.
5
+ """
6
+
7
+ from __future__ import annotations
8
+
9
+ import colorsys
10
+ import math
11
+ import random
12
+ import re
13
+ import zlib
14
+ from dataclasses import dataclass, field
15
+ from typing import Callable
16
+
17
+ import numpy as np
18
+
19
+ from .spriteset import SpriteSet, build_sheet, finalize
20
+
21
+ RGBA = tuple[int, int, int, int]
22
+
23
+
24
+ # --- color ------------------------------------------------------------------------------------
25
+
26
+ @dataclass(frozen=True)
27
+ class Ramp:
28
+ outline: RGBA
29
+ shadow: RGBA
30
+ base: RGBA
31
+ light: RGBA
32
+ eye: RGBA = (29, 27, 42, 255)
33
+ white: RGBA = (255, 250, 240, 255)
34
+
35
+
36
+ def _hex(c: str) -> tuple[float, float, float]:
37
+ c = c.lstrip("#")
38
+ return tuple(int(c[i:i + 2], 16) / 255 for i in (0, 2, 4)) # type: ignore[return-value]
39
+
40
+
41
+ def _rgba(r: float, g: float, b: float) -> RGBA:
42
+ return (round(max(0, min(1, r)) * 255), round(max(0, min(1, g)) * 255), round(max(0, min(1, b)) * 255), 255)
43
+
44
+
45
+ def ramp(color: str) -> Ramp:
46
+ h, s, v = colorsys.rgb_to_hsv(*_hex(color))
47
+ # Shadows drift toward blue/purple, highlights toward warm: classic pixel-art hue shifting.
48
+ shadow = colorsys.hsv_to_rgb((h + 0.04) % 1, min(1, s * 1.1 + 0.05), v * 0.68)
49
+ light = colorsys.hsv_to_rgb((h - 0.03) % 1, s * 0.75, min(1, v * 1.18 + 0.08))
50
+ outline = colorsys.hsv_to_rgb((h + 0.06) % 1, min(1, s * 0.9 + 0.2), v * 0.28)
51
+ return Ramp(_rgba(*outline), _rgba(*shadow), _rgba(*_hex(color)), _rgba(*light))
52
+
53
+
54
+ # --- canvas -----------------------------------------------------------------------------------
55
+
56
+ class Canvas:
57
+ def __init__(self, w: int, h: int):
58
+ self.w, self.h = w, h
59
+ self.px = np.zeros((h, w, 4), dtype=np.uint8)
60
+
61
+ def set(self, x: float, y: float, c: RGBA) -> None:
62
+ x, y = int(round(x)), int(round(y))
63
+ if 0 <= x < self.w and 0 <= y < self.h:
64
+ self.px[y, x] = c
65
+
66
+ def rect(self, x0: float, y0: float, x1: float, y1: float, c: RGBA) -> None:
67
+ for y in range(int(round(y0)), int(round(y1)) + 1):
68
+ for x in range(int(round(x0)), int(round(x1)) + 1):
69
+ self.set(x, y, c)
70
+
71
+ def ellipse(self, cx: float, cy: float, rx: float, ry: float, c: RGBA, shade: Ramp | None = None) -> None:
72
+ """Filled ellipse; with `shade`, top-left gets the light tone and bottom-right the shadow."""
73
+ for y in range(self.h):
74
+ for x in range(self.w):
75
+ dx, dy = (x + 0.5 - cx) / max(rx, 0.01), (y + 0.5 - cy) / max(ry, 0.01)
76
+ if dx * dx + dy * dy <= 1.0:
77
+ col = c
78
+ if shade:
79
+ t = dx * 0.55 + dy * 0.85
80
+ col = shade.light if t < -0.55 else shade.shadow if t > 0.5 else c
81
+ self.px[y, x] = col
82
+
83
+ def poly(self, pts: list[tuple[float, float]], c: RGBA) -> None:
84
+ for y in range(self.h):
85
+ for x in range(self.w):
86
+ if _inside(x + 0.5, y + 0.5, pts):
87
+ self.px[y, x] = c
88
+
89
+ def outline(self, c: RGBA) -> None:
90
+ """1px outline around everything opaque (4-neighbourhood)."""
91
+ a = self.px[..., 3] > 0
92
+ p = np.pad(a, 1)
93
+ ring = (p[:-2, 1:-1] | p[2:, 1:-1] | p[1:-1, :-2] | p[1:-1, 2:]) & ~a
94
+ self.px[ring] = c
95
+
96
+ def copy(self) -> np.ndarray:
97
+ return self.px.copy()
98
+
99
+
100
+ def _inside(x: float, y: float, pts: list[tuple[float, float]]) -> bool:
101
+ inside = False
102
+ for (x1, y1), (x2, y2) in zip(pts, pts[1:] + pts[:1]):
103
+ if (y1 > y) != (y2 > y) and x < (x2 - x1) * (y - y1) / (y2 - y1) + x1:
104
+ inside = not inside
105
+ return inside
106
+
107
+
108
+ # --- features from names ----------------------------------------------------------------------
109
+
110
+ FEATURE_WORDS: dict[str, tuple[str, ...]] = {
111
+ "ninja": ("mask",), "samurai": ("mask",), "thief": ("mask",),
112
+ "fox": ("ears", "tail"), "cat": ("ears", "tail"), "kitty": ("ears", "tail"), "wolf": ("ears", "tail"),
113
+ "dog": ("ears",), "bunny": ("long_ears",), "rabbit": ("long_ears",),
114
+ "spider": ("legs",), "ant": ("legs", "antenna"), "beetle": ("legs",), "bug": ("legs", "antenna"),
115
+ "bat": ("wings",), "bird": ("wings", "beak"), "crow": ("wings", "beak"), "dragon": ("wings", "horns"),
116
+ "robot": ("antenna",), "bot": ("antenna",), "droid": ("antenna",), "mech": ("antenna",),
117
+ "devil": ("horns",), "demon": ("horns",), "imp": ("horns",), "goblin": ("ears",), "orc": ("horns",),
118
+ "knight": ("helmet",), "soldier": ("helmet",), "wizard": ("hat",), "witch": ("hat",), "mage": ("hat",),
119
+ "ghost": ("ghost",), "spirit": ("ghost",),
120
+ }
121
+
122
+
123
+ def features_for(name: str) -> list[str]:
124
+ words = re.findall(r"[A-Z][a-z0-9]*|[a-z0-9]+", name)
125
+ out: list[str] = []
126
+ for w in words:
127
+ for f in FEATURE_WORDS.get(w.lower(), ()):
128
+ if f not in out:
129
+ out.append(f)
130
+ return out
131
+
132
+
133
+ # --- kinds ------------------------------------------------------------------------------------
134
+
135
+ @dataclass
136
+ class Kind:
137
+ size: tuple[int, int]
138
+ anims: dict[str, tuple[int, float, bool]] # name -> (frames, fps, loop)
139
+ paint: Callable[["Ctx"], None]
140
+ anchor: str = "center"
141
+
142
+
143
+ @dataclass
144
+ class Ctx:
145
+ cv: Canvas
146
+ r: Ramp
147
+ feats: list[str]
148
+ anim: str
149
+ i: int
150
+ rng: random.Random
151
+ extra: dict = field(default_factory=dict)
152
+
153
+
154
+ def _eyes(c: Ctx, positions: list[tuple[int, int]], big: bool = False) -> None:
155
+ hurt = c.anim == "hurt"
156
+ for x, y in positions:
157
+ if hurt:
158
+ c.cv.set(x, y, c.r.eye)
159
+ c.cv.set(x - 1, y - 1, c.r.eye)
160
+ c.cv.set(x + 1, y + 1, c.r.eye)
161
+ elif big:
162
+ c.cv.rect(x - 1, y - 1, x, y, c.r.white)
163
+ c.cv.set(x, y, c.r.eye)
164
+ else:
165
+ c.cv.rect(x, y - 1, x, y, c.r.eye)
166
+
167
+
168
+ def paint_hero(c: Ctx) -> None:
169
+ cv, r, f = c.cv, c.r, c.feats
170
+ bob = {"idle": c.i % 2, "run": (0, 1, 0, 1)[c.i % 4]}.get(c.anim, 0)
171
+ # Legs (in shadow tone), positions per animation.
172
+ legs = {"run": ((-1, 1), (0, 0), (1, -1), (0, 0))[c.i % 4], "jump": (0, 0), "fall": (-1, 1)}.get(c.anim, (0, 0))
173
+ leg_h = {"jump": 1}.get(c.anim, 2)
174
+ cv.rect(4 + legs[0], 13, 5 + legs[0], 13 + leg_h, r.shadow)
175
+ cv.rect(7 + legs[1], 13, 8 + legs[1], 13 + leg_h, r.shadow)
176
+ # Tail behind the body.
177
+ if "tail" in f:
178
+ wag = (0, -1)[c.i % 2] if c.anim in ("idle", "run") else 0
179
+ cv.rect(1, 10 + bob + wag, 2, 11 + bob + wag, r.base)
180
+ cv.set(0, 9 + bob + wag, r.light)
181
+ # Body and arms.
182
+ cv.rect(3, 8 + bob, 8, 12 + bob, r.base)
183
+ cv.rect(3, 11 + bob, 8, 12 + bob, r.shadow)
184
+ arm_y = 7 + bob if c.anim in ("jump", "fall") else 9 + bob
185
+ cv.rect(2, arm_y, 2, arm_y + 2, r.shadow)
186
+ cv.rect(9, arm_y, 9, arm_y + 2, r.shadow)
187
+ # Head.
188
+ cv.ellipse(6, 5 + bob, 4, 4, r.base, shade=r)
189
+ if "ears" in f:
190
+ cv.poly([(2.2, 3 + bob), (3.6, -0.2 + bob), (5, 2.5 + bob)], r.base)
191
+ cv.poly([(7, 2.5 + bob), (8.4, -0.2 + bob), (9.8, 3 + bob)], r.base)
192
+ if "long_ears" in f:
193
+ cv.rect(4, -1 + bob, 4, 2 + bob, r.light)
194
+ cv.rect(7, -1 + bob, 7, 2 + bob, r.light)
195
+ if "horns" in f:
196
+ cv.set(3, 1 + bob, r.white)
197
+ cv.set(8, 1 + bob, r.white)
198
+ if "helmet" in f:
199
+ cv.rect(2, 1 + bob, 9, 3 + bob, (190, 196, 206, 255))
200
+ if "hat" in f:
201
+ cv.poly([(1.5, 3.5 + bob), (6, -4 + bob), (10.5, 3.5 + bob)], r.shadow)
202
+ if "mask" in f:
203
+ cv.rect(2, 4 + bob, 10, 6 + bob, r.outline)
204
+ ribbon = (0, 1)[c.i % 2] if c.anim == "run" else 0
205
+ cv.set(1, 5 + bob, r.outline)
206
+ cv.set(0, 6 + bob + ribbon, r.outline)
207
+ if c.anim == "hurt":
208
+ _eyes(c, [(7, 5 + bob), (9, 5 + bob)])
209
+ else: # eyes glint through the mask slit
210
+ cv.set(7, 5 + bob, r.white)
211
+ cv.set(9, 5 + bob, r.white)
212
+ else:
213
+ _eyes(c, [(7, 5 + bob), (9, 5 + bob)])
214
+ cv.outline(r.outline)
215
+
216
+
217
+ def paint_slime(c: Ctx) -> None:
218
+ cv, r, f = c.cv, c.r, c.feats
219
+ w, h = cv.w, cv.h
220
+ sq = {"idle": ((0, 0), (1, -1))[c.i % 2], "run": ((0, 0), (1, -1), (0, 0), (-1, 1))[c.i % 4],
221
+ "jump": (-2, 2), "fall": (-1, 1), "hurt": (2, -2)}[c.anim]
222
+ rx, ry = w / 2 - 1 + sq[0] * 0.6, h * 0.55 - 0.5 + sq[1] * 0.7
223
+ cy = h - ry
224
+ cv.ellipse(w / 2, cy, rx, ry, r.base, shade=r)
225
+ cv.rect(w / 2 - rx + 1, h - 2, w / 2 + rx - 2, h - 2, r.shadow)
226
+ if "spots" in f:
227
+ top = int(h - 2 * ry) + 2
228
+ for _ in range(3): # same rng sequence every frame, so spots stay put
229
+ x, dy = c.rng.randint(3, w - 5), c.rng.random()
230
+ cv.set(x, top + dy * max(0, h - 3 - top), r.shadow)
231
+ if "horns" in f:
232
+ cv.set(w / 2 - 2, h - 2 * ry - 1, r.white)
233
+ cv.set(w / 2 + 2, h - 2 * ry - 1, r.white)
234
+ if "antenna" in f:
235
+ cv.rect(w / 2, h - 2 * ry - 2, w / 2, h - 2 * ry, r.shadow)
236
+ cv.set(w / 2, h - 2 * ry - 3, r.light)
237
+ ey = int(round(h - ry - 0.5))
238
+ eyes = [(int(w / 2) + 3, ey)] if c.extra.get("one_eye") else [(int(w / 2) + 1, ey), (int(w / 2) + 4, ey)]
239
+ _eyes(c, eyes, big=True)
240
+ cv.outline(r.outline)
241
+
242
+
243
+ def paint_hopper(c: Ctx) -> None:
244
+ cv, r = c.cv, c.r
245
+ w, h = cv.w, cv.h
246
+ up = {"jump": 2, "fall": 1}.get(c.anim, (0, 1)[c.i % 2] if c.anim == "idle" else 0)
247
+ # Back legs
248
+ if c.anim in ("jump", "fall"):
249
+ cv.rect(1, h - 4, 3, h - 1, r.shadow)
250
+ cv.rect(w - 4, h - 4, w - 2, h - 1, r.shadow)
251
+ else:
252
+ cv.rect(0, h - 3, 3, h - 1, r.shadow)
253
+ cv.rect(w - 4, h - 3, w - 1, h - 1, r.shadow)
254
+ cv.ellipse(w / 2, h - 4.5 - up, w / 2 - 1.5, 3.8, r.base, shade=r)
255
+ # Eye bumps
256
+ cv.ellipse(w / 2 - 2.5, h - 8.5 - up, 2, 2, r.base)
257
+ cv.ellipse(w / 2 + 2.5, h - 8.5 - up, 2, 2, r.base)
258
+ _eyes(c, [(int(w / 2) - 2, int(h - 8.5 - up)), (int(w / 2) + 3, int(h - 8.5 - up))], big=True)
259
+ cv.rect(w / 2 - 2, h - 3 - up, w / 2 + 3, h - 3 - up, r.shadow) # mouth
260
+ cv.outline(r.outline)
261
+
262
+
263
+ def paint_flyer(c: Ctx) -> None:
264
+ cv, r, f = c.cv, c.r, c.feats
265
+ w, h = cv.w, cv.h
266
+ flap = (0, 1, 2, 1)[c.i % 4] if c.anim in ("idle", "run") else 1
267
+ bob = (0, 1)[c.i % 2]
268
+ if "ghost" in f:
269
+ cv.ellipse(w / 2, h / 2 + bob - 1, w / 2 - 2, h / 2 - 1, r.light, shade=r)
270
+ for x in range(3, w - 3, 2):
271
+ cv.set(x, h - 2 + bob, r.light)
272
+ else:
273
+ wing_y = [h / 2 - 3, h / 2 - 1, h / 2 + 1][flap]
274
+ cv.poly([(w / 2 - 2, h / 2 + bob), (0.5, wing_y + bob), (2.5, h / 2 + 2 + bob)], r.shadow)
275
+ cv.poly([(w / 2 + 2, h / 2 + bob), (w - 0.5, wing_y + bob), (w - 2.5, h / 2 + 2 + bob)], r.shadow)
276
+ cv.ellipse(w / 2, h / 2 + bob, 3.5, 3.2, r.base, shade=r)
277
+ if "beak" in f:
278
+ cv.set(w / 2 + 4, h / 2 + bob, (255, 196, 64, 255))
279
+ _eyes(c, [(int(w / 2) + 1, int(h / 2 + bob) - 1), (int(w / 2) + 3, int(h / 2 + bob) - 1)])
280
+ cv.outline(r.outline)
281
+
282
+
283
+ def paint_robot(c: Ctx) -> None:
284
+ cv, r, f = c.cv, c.r, c.feats
285
+ w, h = cv.w, cv.h
286
+ bob = (0, 1)[c.i % 2] if c.anim in ("idle", "run") else 0
287
+ # Treads
288
+ cv.rect(1, h - 3, w - 2, h - 1, r.outline)
289
+ for x in range(1 + (c.i % 2 if c.anim == "run" else 0), w - 1, 2):
290
+ cv.set(x, h - 2, r.shadow)
291
+ cv.rect(1, 3 + bob, w - 2, h - 4 + bob, r.base)
292
+ cv.rect(1, h - 6 + bob, w - 2, h - 4 + bob, r.shadow)
293
+ cv.rect(2, 4 + bob, w - 3, 4 + bob, r.light)
294
+ # Visor
295
+ cv.rect(w / 2 - 1, 6 + bob, w - 2, 7 + bob, r.outline)
296
+ if c.anim == "hurt":
297
+ cv.rect(w / 2, 6 + bob, w - 3, 7 + bob, (255, 80, 80, 255))
298
+ else:
299
+ blink = c.anim == "idle" and c.i % 2 == 1
300
+ cv.rect(w - 4, 6 + bob, w - 3, 7 + bob, r.white if not blink else r.light)
301
+ cv.rect(4, bob, 4, 2 + bob, r.shadow) # robots always get an antenna
302
+ cv.set(4, bob - 1, (255, 90, 90, 255) if c.i % 2 else r.light)
303
+ cv.outline(r.outline)
304
+
305
+
306
+ def paint_turret(c: Ctx) -> None:
307
+ cv, r = c.cv, c.r
308
+ w, h = cv.w, cv.h
309
+ metal, metal_dark = (170, 172, 190, 255), (96, 98, 118, 255)
310
+ recoil = 1 if c.anim == "hurt" else 0
311
+ # Barrel first (pointing right), then the head over its base.
312
+ cv.rect(6 - recoil, 5, w - 2 - recoil, 6, metal)
313
+ cv.rect(6 - recoil, 7, w - 2 - recoil, 7, metal_dark)
314
+ cv.rect(w - 2 - recoil, 4, w - 2 - recoil, 7, metal_dark)
315
+ cv.poly([(1, h - 0.5), (w - 3, h - 0.5), (w - 5, h - 4), (3, h - 4)], r.shadow)
316
+ cv.ellipse(5.5, 7.5, 4.2, 4.2, r.base, shade=r)
317
+ light = (255, 80, 80, 255) if c.i % 2 else r.light
318
+ cv.set(4, 6, light)
319
+ cv.outline(r.outline)
320
+
321
+
322
+ def paint_bug(c: Ctx) -> None:
323
+ cv, r, f = c.cv, c.r, c.feats
324
+ w, h = cv.w, cv.h
325
+ step = c.i % 2 if c.anim == "run" else 0
326
+ for k, x in enumerate((3, 6, 9)):
327
+ off = 1 if (k + step) % 2 else 0
328
+ cv.rect(x - off, 1, x - off, 3, r.outline)
329
+ cv.rect(x + off - 1, h - 4, x + off - 1, h - 2, r.outline)
330
+ cv.ellipse(w / 2 - 1, h / 2, 4.5, 3.2, r.base, shade=r)
331
+ cv.ellipse(w - 3.5, h / 2, 2.3, 2.2, r.shadow)
332
+ cv.rect(w / 2 - 1, h / 2 - 3, w / 2 - 1, h / 2 + 2, r.shadow)
333
+ if "antenna" in f:
334
+ cv.set(w - 1, h / 2 - 3, r.outline)
335
+ cv.set(w - 1, h / 2 + 2, r.outline)
336
+ _eyes(c, [(w - 2, int(h / 2) - 1)])
337
+ cv.outline(r.outline)
338
+
339
+
340
+ def paint_coin(c: Ctx) -> None:
341
+ cv, r = c.cv, c.r
342
+ w, h = cv.w, cv.h
343
+ widths = (3.5, 2.8, 1.6, 0.8, 1.6, 2.8)
344
+ rx = widths[c.i % len(widths)]
345
+ cv.ellipse(w / 2, h / 2, rx, h / 2 - 0.5, r.base, shade=r)
346
+ if rx > 1.5:
347
+ cv.rect(w / 2, h / 2 - 1, w / 2, h / 2 + 1, r.shadow)
348
+ cv.outline(r.outline)
349
+
350
+
351
+ def _star_points(cx: float, cy: float, ro: float, ri: float) -> list[tuple[float, float]]:
352
+ pts = []
353
+ for k in range(10):
354
+ a = -math.pi / 2 + k * math.pi / 5
355
+ rad = ro if k % 2 == 0 else ri
356
+ pts.append((cx + math.cos(a) * rad, cy + math.sin(a) * rad))
357
+ return pts
358
+
359
+
360
+ def paint_star(c: Ctx) -> None:
361
+ cv, r = c.cv, c.r
362
+ w, h = cv.w, cv.h
363
+ s = (1.0, 0.92, 1.0, 1.06)[c.i % 4]
364
+ cv.poly(_star_points(w / 2, h / 2 + 0.3, (w / 2 - 0.5) * s, (w / 4.2) * s), r.base)
365
+ cv.set(w / 2 - 1, h / 2 - 1, r.light)
366
+ cv.set(w / 2, h / 2 + 2, r.shadow)
367
+ cv.outline(r.outline)
368
+ if c.i % 4 == 3:
369
+ cv.set(w - 1, 0, r.white)
370
+
371
+
372
+ def paint_gem(c: Ctx) -> None:
373
+ cv, r = c.cv, c.r
374
+ w, h = cv.w, cv.h
375
+ cv.poly([(w / 2, 0.5), (w - 0.5, h / 3), (w / 2, h - 0.5), (0.5, h / 3)], r.base)
376
+ cv.poly([(w / 2, 0.5), (w / 2, h - 0.5), (0.5, h / 3)], r.light)
377
+ cv.rect(1, h / 3, w - 2, h / 3, r.shadow)
378
+ cv.outline(r.outline)
379
+ sparkle = c.i % 4
380
+ if sparkle == 1:
381
+ cv.set(1, 1, r.white)
382
+ elif sparkle == 3:
383
+ cv.set(w - 2, h - 3, r.white)
384
+
385
+
386
+ def paint_heart(c: Ctx) -> None:
387
+ cv, r = c.cv, c.r
388
+ w, h = cv.w, cv.h
389
+ s = (0.0, 0.4)[c.i % 2]
390
+ cv.ellipse(w / 2 - 1.8, 2.8, 2.2 + s, 2.2 + s, r.base, shade=r)
391
+ cv.ellipse(w / 2 + 1.8, 2.8, 2.2 + s, 2.2 + s, r.base, shade=r)
392
+ cv.poly([(0.5 - s, 3.5), (w - 0.5 + s, 3.5), (w / 2, h - 0.2 + s)], r.base)
393
+ cv.set(2, 2, r.white)
394
+ cv.outline(r.outline)
395
+
396
+
397
+ def paint_orb(c: Ctx) -> None:
398
+ cv, r = c.cv, c.r
399
+ w, h = cv.w, cv.h
400
+ cv.ellipse(w / 2, h / 2, w / 2 - 1, h / 2 - 1, r.base, shade=r)
401
+ cv.set(w / 2 - 1 + (c.i % 2), h / 2 - 2, r.white)
402
+ cv.outline(r.outline)
403
+
404
+
405
+ def paint_door(c: Ctx) -> None:
406
+ cv, r = c.cv, c.r
407
+ w, h = cv.w, cv.h
408
+ cv.rect(0, 0, w - 1, h - 1, r.base)
409
+ for x in range(2, w - 1, 3):
410
+ cv.rect(x, 0, x, h - 1, r.shadow)
411
+ for y in range(4, h, 14):
412
+ cv.rect(0, y, w - 1, y + 1, (120, 120, 132, 255))
413
+ cv.set(1, y, (200, 200, 210, 255))
414
+ cv.rect(w - 3, h // 2, w - 2, h // 2 + 1, (240, 200, 80, 255))
415
+ cv.outline(r.outline)
416
+
417
+
418
+ def paint_exit(c: Ctx) -> None:
419
+ cv, r = c.cv, c.r
420
+ w, h = cv.w, cv.h
421
+ cv.ellipse(w / 2, w / 2, w / 2 - 0.5, w / 2 - 0.5, r.base)
422
+ cv.rect(0.5, w / 2, w - 1.5, h - 1, r.base)
423
+ cv.ellipse(w / 2, w / 2 + 1, w / 2 - 2.5, w / 2 - 2.5, (20, 16, 36, 255))
424
+ cv.rect(2.5, w / 2 + 1, w - 3.5, h - 1, (20, 16, 36, 255))
425
+ rng = random.Random(c.i)
426
+ for _ in range(4):
427
+ cv.set(rng.randint(3, w - 4), rng.randint(4, h - 2), r.light)
428
+ cv.outline(r.outline)
429
+
430
+
431
+ def paint_portal(c: Ctx) -> None:
432
+ cv, r = c.cv, c.r
433
+ w, h = cv.w, cv.h
434
+ cv.ellipse(w / 2, h / 2, w / 2 - 0.5, h / 2 - 0.5, r.shadow)
435
+ cv.ellipse(w / 2, h / 2, w / 2 - 3, h / 2 - 3, (20, 16, 36, 255))
436
+ for k in range(4):
437
+ a = c.i * math.pi / 8 + k * math.pi / 2
438
+ for t in range(3):
439
+ rad = w / 2 - 2 - t * 2.2
440
+ cv.set(w / 2 + math.cos(a + t * 0.6) * rad, h / 2 + math.sin(a + t * 0.6) * rad, r.light)
441
+ cv.outline(r.outline)
442
+
443
+
444
+ def paint_spikes(c: Ctx) -> None:
445
+ cv, r = c.cv, c.r
446
+ w, h = cv.w, cv.h
447
+ teeth = 3
448
+ tw = w / teeth
449
+ for k in range(teeth):
450
+ x0 = k * tw
451
+ cv.poly([(x0 + 0.2, h), (x0 + tw / 2, 0.3), (x0 + tw - 0.2, h)], r.base)
452
+ cv.rect(x0 + tw / 2, 2, x0 + tw / 2, h - 1, r.light)
453
+ cv.outline(r.outline)
454
+
455
+
456
+ def paint_bullet(c: Ctx) -> None:
457
+ cv, r = c.cv, c.r
458
+ w, h = cv.w, cv.h
459
+ cv.ellipse(w / 2, h / 2, w / 2, h / 2, r.light)
460
+ cv.set(w / 2 - 1 + (c.i % 2), h / 2 - 1, r.white)
461
+
462
+
463
+ KINDS: dict[str, Kind] = {
464
+ "hero": Kind((12, 16), {"idle": (2, 3, True), "run": (4, 10, True), "jump": (1, 1, False),
465
+ "fall": (1, 1, False), "hurt": (1, 1, False)}, paint_hero, "bottom"),
466
+ "slime": Kind((14, 10), {"idle": (2, 3, True), "run": (4, 8, True), "jump": (1, 1, False),
467
+ "fall": (1, 1, False), "hurt": (1, 1, False)}, paint_slime, "bottom"),
468
+ "hopper": Kind((13, 11), {"idle": (2, 2, True), "run": (2, 6, True), "jump": (1, 1, False),
469
+ "fall": (1, 1, False), "hurt": (1, 1, False)}, paint_hopper, "bottom"),
470
+ "flyer": Kind((14, 10), {"idle": (4, 10, True), "run": (4, 14, True), "hurt": (1, 1, False)}, paint_flyer),
471
+ "robot": Kind((13, 13), {"idle": (2, 2, True), "run": (2, 8, True), "hurt": (1, 1, False)}, paint_robot, "bottom"),
472
+ "turret": Kind((14, 14), {"idle": (2, 2, True), "hurt": (1, 1, False)}, paint_turret, "bottom"),
473
+ "bug": Kind((12, 12), {"idle": (2, 3, True), "run": (2, 10, True), "hurt": (1, 1, False)}, paint_bug),
474
+ "coin": Kind((7, 7), {"idle": (6, 10, True)}, paint_coin),
475
+ "star": Kind((9, 9), {"idle": (4, 6, True)}, paint_star),
476
+ "gem": Kind((7, 9), {"idle": (4, 4, True)}, paint_gem),
477
+ "heart": Kind((8, 7), {"idle": (2, 3, True)}, paint_heart),
478
+ "orb": Kind((8, 8), {"idle": (2, 3, True)}, paint_orb),
479
+ "door": Kind((9, 63), {"idle": (1, 1, False)}, paint_door, "bottom"),
480
+ "exit": Kind((14, 24), {"idle": (3, 6, True)}, paint_exit, "bottom"),
481
+ "portal": Kind((20, 20), {"idle": (4, 8, True)}, paint_portal),
482
+ "spikes": Kind((15, 7), {"idle": (1, 1, False)}, paint_spikes, "bottom"),
483
+ "bullet": Kind((4, 4), {"idle": (2, 12, True)}, paint_bullet),
484
+ }
485
+
486
+
487
+ def generate(name: str, kind: str, color: str, features: list[str] | None = None, seed: int = 0,
488
+ scale: int = 2) -> SpriteSet:
489
+ if kind not in KINDS:
490
+ raise ValueError(f"unknown sprite kind '{kind}'")
491
+ spec = KINDS[kind]
492
+ feats = list(features) if features is not None else features_for(name)
493
+ rng_seed = zlib.crc32(f"{name}:{kind}:{seed}".encode())
494
+ extra = {"one_eye": random.Random(rng_seed).random() < 0.3}
495
+ if kind == "slime" and random.Random(rng_seed + 1).random() < 0.5 and "spots" not in feats:
496
+ feats.append("spots")
497
+ r = ramp(color)
498
+ frames: dict[str, list[np.ndarray]] = {}
499
+ for anim, (n, _fps, _loop) in spec.anims.items():
500
+ frames[anim] = []
501
+ for i in range(n):
502
+ cv = Canvas(*spec.size)
503
+ spec.paint(Ctx(cv, r, feats, anim, i, random.Random(rng_seed), extra))
504
+ frames[anim].append(cv.copy())
505
+ sheet, anims = build_sheet(frames, {a: v[1] for a, v in spec.anims.items()},
506
+ {a: v[2] for a, v in spec.anims.items()})
507
+ return finalize(name, sheet, spec.size, anims, scale, spec.anchor)