sonolus.py 0.1.5__py3-none-any.whl → 0.1.7__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.
sonolus/build/cli.py CHANGED
@@ -2,6 +2,7 @@ import argparse
2
2
  import contextlib
3
3
  import http.server
4
4
  import importlib
5
+ import json
5
6
  import shutil
6
7
  import socket
7
8
  import socketserver
@@ -11,7 +12,7 @@ from time import perf_counter
11
12
 
12
13
  from sonolus.build.engine import package_engine
13
14
  from sonolus.build.level import package_level_data
14
- from sonolus.build.project import build_project_to_collection
15
+ from sonolus.build.project import build_project_to_collection, get_project_schema
15
16
  from sonolus.script.project import Project
16
17
 
17
18
 
@@ -147,6 +148,14 @@ def main():
147
148
  dev_parser.add_argument("--build-dir", type=str, default="./build")
148
149
  dev_parser.add_argument("--port", type=int, default=8000)
149
150
 
151
+ schema_parser = subparsers.add_parser("schema")
152
+ schema_parser.add_argument(
153
+ "module",
154
+ type=str,
155
+ nargs="?",
156
+ help="Module path (e.g., 'module.name'). If omitted, will auto-detect if only one module exists.",
157
+ )
158
+
150
159
  args = parser.parse_args()
151
160
 
152
161
  if not args.module:
@@ -161,16 +170,18 @@ def main():
161
170
  if project is None:
162
171
  sys.exit(1)
163
172
 
164
- build_dir = Path(args.build_dir)
165
-
166
173
  if args.command == "build":
174
+ build_dir = Path(args.build_dir)
167
175
  start_time = perf_counter()
168
176
  build_project(project, build_dir)
169
177
  end_time = perf_counter()
170
178
  print(f"Project built successfully to '{build_dir.resolve()}' in {end_time - start_time:.2f}s")
171
179
  elif args.command == "dev":
180
+ build_dir = Path(args.build_dir)
172
181
  start_time = perf_counter()
173
182
  build_collection(project, build_dir)
174
183
  end_time = perf_counter()
175
184
  print(f"Build finished in {end_time - start_time:.2f}s")
176
185
  run_server(build_dir / "site", port=args.port)
186
+ elif args.command == "schema":
187
+ print(json.dumps(get_project_schema(project), indent=2))
sonolus/build/project.py CHANGED
@@ -5,7 +5,7 @@ from sonolus.build.engine import package_engine
5
5
  from sonolus.build.level import package_level_data
6
6
  from sonolus.script.engine import Engine
7
7
  from sonolus.script.level import Level
8
- from sonolus.script.project import Project
8
+ from sonolus.script.project import Project, ProjectSchema
9
9
 
10
10
  BLANK_PNG = (
11
11
  b"\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x01\x00\x00\x00\x01\x01\x00\x00\x00\x007n\xf9$"
@@ -92,3 +92,32 @@ def load_resources_files_to_collection(base_path: Path) -> Collection:
92
92
  collection.load_from_scp(path)
93
93
  collection.load_from_source(base_path)
94
94
  return collection
95
+
96
+
97
+ def get_project_schema(project: Project) -> ProjectSchema:
98
+ by_archetype: dict[str, dict[str, bool]] = {}
99
+ for archetype in project.engine.data.play.archetypes:
100
+ fields = by_archetype.setdefault(archetype.name, {})
101
+ # If a field is exported, we should exclude it if it's imported in watch mode
102
+ for field in archetype._exported_keys_:
103
+ fields[field] = False
104
+ for field in archetype._imported_keys_:
105
+ fields[field] = True
106
+ for archetype in project.engine.data.watch.archetypes:
107
+ fields = by_archetype.setdefault(archetype.name, {})
108
+ for field in archetype._imported_keys_:
109
+ if field not in fields:
110
+ fields[field] = True
111
+ for archetype in project.engine.data.preview.archetypes:
112
+ fields = by_archetype.setdefault(archetype.name, {})
113
+ for field in archetype._imported_keys_:
114
+ fields[field] = True
115
+ return {
116
+ "archetypes": [
117
+ {
118
+ "name": name,
119
+ "fields": [*fields],
120
+ }
121
+ for name, fields in by_archetype.items()
122
+ ]
123
+ }
@@ -5,7 +5,7 @@ from collections.abc import Callable
5
5
  from dataclasses import dataclass
6
6
  from enum import Enum, StrEnum
7
7
  from types import FunctionType
8
- from typing import Annotated, Any, ClassVar, Self, get_origin
8
+ from typing import Annotated, Any, ClassVar, Self, TypedDict, get_origin
9
9
 
10
10
  from sonolus.backend.ir import IRConst, IRInstr
11
11
  from sonolus.backend.mode import Mode
@@ -307,6 +307,11 @@ class _ArchetypeLevelData:
307
307
  type _ArchetypeData = _ArchetypeSelfData | _ArchetypeReferenceData | _ArchetypeLevelData
308
308
 
309
309
 
310
+ class ArchetypeSchema(TypedDict):
311
+ name: str
312
+ fields: list[str]
313
+
314
+
310
315
  class _BaseArchetype:
311
316
  _is_comptime_value_ = True
312
317
 
@@ -401,6 +406,10 @@ class _BaseArchetype:
401
406
  data.extend(field.type._accept_(bound.arguments[field.name] or zeros(field.type))._to_list_())
402
407
  native_call(Op.Spawn, archetype_id, *(Num(x) for x in data))
403
408
 
409
+ @classmethod
410
+ def schema(cls) -> ArchetypeSchema:
411
+ return {"name": cls.name or "unnamed", "fields": list(cls._imported_fields_)}
412
+
404
413
  def _level_data_entries(self, level_refs: dict[Any, int] | None = None):
405
414
  if not isinstance(self._data_, _ArchetypeLevelData):
406
415
  raise RuntimeError("Entity is not level data")
sonolus/script/array.py CHANGED
@@ -60,7 +60,7 @@ class Array[T, Size](GenericValue, ArrayLike[T]):
60
60
  raise ValueError(f"{cls.__name__} constructor should be used with {cls.size()} values, got {len(args)}")
61
61
  parameterized_cls = cls
62
62
  if ctx():
63
- place = ctx().alloc(size=parameterized_cls.size())
63
+ place = ctx().alloc(size=parameterized_cls._size_())
64
64
  result: parameterized_cls = parameterized_cls._from_place_(place)
65
65
  result._copy_from_(parameterized_cls._with_value(values))
66
66
  return result
@@ -182,10 +182,18 @@ class Context:
182
182
  return
183
183
  assert len(self.outgoing) == 0
184
184
  self.outgoing[None] = header
185
+ values = {}
186
+ # First do a pass through and copy every value
185
187
  for name, target_value in header.loop_variables.items():
186
188
  with using_ctx(self):
187
189
  if type(target_value)._is_value_type_():
188
190
  value = self.scope.get_value(name)
191
+ values[name] = value._get_() # _get_() will make a copy on value types
192
+ # Then actually set them
193
+ for name, target_value in header.loop_variables.items():
194
+ with using_ctx(self):
195
+ if type(target_value)._is_value_type_():
196
+ value = values[name]
189
197
  value = type(target_value)._accept_(value)
190
198
  target_value._set_(value)
191
199
  else:
sonolus/script/project.py CHANGED
@@ -1,6 +1,10 @@
1
+ from __future__ import annotations
2
+
1
3
  from os import PathLike
2
4
  from pathlib import Path
5
+ from typing import Self, TypedDict
3
6
 
7
+ from sonolus.script.archetype import ArchetypeSchema
4
8
  from sonolus.script.engine import Engine
5
9
  from sonolus.script.level import Level
6
10
 
@@ -23,3 +27,50 @@ class Project:
23
27
  self.engine = engine
24
28
  self.levels = levels or []
25
29
  self.resources = Path(resources or "resources")
30
+
31
+ def with_levels(self, levels: list[Level]) -> Self:
32
+ """Create a new project with the specified levels.
33
+
34
+ Args:
35
+ levels: The levels of the project.
36
+
37
+ Returns:
38
+ The new project.
39
+ """
40
+ return Project(self.engine, levels, self.resources)
41
+
42
+ def dev(self, build_dir: PathLike, port: int = 8080):
43
+ """Start a development server for the project.
44
+
45
+ Args:
46
+ build_dir: The path to the build directory.
47
+ port: The port of the development server.
48
+ """
49
+ from sonolus.build.cli import build_collection, run_server
50
+
51
+ build_collection(self, Path(build_dir))
52
+ run_server(Path(build_dir) / "site", port=port)
53
+
54
+ def build(self, build_dir: PathLike):
55
+ """Build the project.
56
+
57
+ Args:
58
+ build_dir: The path to the build directory.
59
+ """
60
+ from sonolus.build.cli import build_project
61
+
62
+ build_project(self, Path(build_dir))
63
+
64
+ def schema(self) -> ProjectSchema:
65
+ """Generate the schema of the project.
66
+
67
+ Returns:
68
+ The schema of the project.
69
+ """
70
+ from sonolus.build.project import get_project_schema
71
+
72
+ return get_project_schema(self)
73
+
74
+
75
+ class ProjectSchema(TypedDict):
76
+ archetypes: list[ArchetypeSchema]
sonolus/script/quad.py CHANGED
@@ -3,7 +3,7 @@ from __future__ import annotations
3
3
  from typing import Protocol, Self
4
4
 
5
5
  from sonolus.script.record import Record
6
- from sonolus.script.vec import Vec2
6
+ from sonolus.script.vec import Vec2, pnpoly
7
7
 
8
8
 
9
9
  class Quad(Record):
@@ -95,6 +95,17 @@ class Quad(Record):
95
95
  """Rotate the quad by the given angle about its center and return a new quad."""
96
96
  return self.rotate_about(angle, self.center)
97
97
 
98
+ def contains_point(self, point: Vec2, /) -> bool:
99
+ """Check if the quad contains the given point.
100
+
101
+ Args:
102
+ point: The point to check.
103
+
104
+ Returns:
105
+ True if the point is inside the quad, False otherwise.
106
+ """
107
+ return pnpoly((self.bl, self.tl, self.tr, self.br), point)
108
+
98
109
 
99
110
  class Rect(Record):
100
111
  """A rectangle defined by its top, right, bottom, and left edges.
@@ -225,6 +236,17 @@ class Rect(Record):
225
236
  l=self.l + shrinkage.x,
226
237
  )
227
238
 
239
+ def contains_point(self, point: Vec2, /) -> bool:
240
+ """Check if the rectangle contains the given point.
241
+
242
+ Args:
243
+ point: The point to check.
244
+
245
+ Returns:
246
+ True if the point is inside the rectangle, False otherwise.
247
+ """
248
+ return self.l <= point.x <= self.r and self.b <= point.y <= self.t
249
+
228
250
 
229
251
  class QuadLike(Protocol):
230
252
  """A protocol for types that can be used as quads."""
sonolus/script/sprite.py CHANGED
@@ -353,9 +353,7 @@ class StandardSprite:
353
353
  SIMULTANEOUS_CONNECTION_PURPLE = Annotated[Sprite, sprite("#SIMULTANEOUS_CONNECTION_PURPLE")]
354
354
  SIMULTANEOUS_CONNECTION_CYAN = Annotated[Sprite, sprite("#SIMULTANEOUS_CONNECTION_CYAN")]
355
355
 
356
- SIMULTANEOUS_CONNECTION_NEUTRAL_SEAMLESS = Annotated[
357
- Sprite, sprite("#SIMULTANEOUS_CONNECTION_NEUTRAL_SEAMLESS")
358
- ]
356
+ SIMULTANEOUS_CONNECTION_NEUTRAL_SEAMLESS = Annotated[Sprite, sprite("#SIMULTANEOUS_CONNECTION_NEUTRAL_SEAMLESS")]
359
357
  SIMULTANEOUS_CONNECTION_RED_SEAMLESS = Annotated[Sprite, sprite("#SIMULTANEOUS_CONNECTION_RED_SEAMLESS")]
360
358
  SIMULTANEOUS_CONNECTION_GREEN_SEAMLESS = Annotated[Sprite, sprite("#SIMULTANEOUS_CONNECTION_GREEN_SEAMLESS")]
361
359
  SIMULTANEOUS_CONNECTION_BLUE_SEAMLESS = Annotated[Sprite, sprite("#SIMULTANEOUS_CONNECTION_BLUE_SEAMLESS")]
sonolus/script/vec.py CHANGED
@@ -1,6 +1,8 @@
1
1
  from math import atan2, cos, sin
2
2
  from typing import Self
3
3
 
4
+ from sonolus.script.array import Array
5
+ from sonolus.script.array_like import ArrayLike
4
6
  from sonolus.script.num import Num
5
7
  from sonolus.script.record import Record
6
8
 
@@ -194,3 +196,30 @@ class Vec2(Record):
194
196
  A new vector with inverted direction.
195
197
  """
196
198
  return Vec2(x=-self.x, y=-self.y)
199
+
200
+
201
+ def pnpoly(vertices: ArrayLike[Vec2] | tuple[Vec2, ...], test: Vec2) -> bool:
202
+ """Check if a point is inside a polygon.
203
+
204
+ No guaranteed behavior for points on the edges or very close to the edges.
205
+
206
+ Args:
207
+ vertices: The vertices of the polygon.
208
+ test: The point to test.
209
+
210
+ Returns:
211
+ Whether the point is inside the polygon.
212
+ """
213
+ if isinstance(vertices, tuple):
214
+ vertices = Array(*vertices)
215
+ i = 0
216
+ j = len(vertices) - 1
217
+ c = False
218
+ while i < len(vertices):
219
+ if (vertices[i].y > test.y) != (vertices[j].y > test.y) and test.x < (vertices[j].x - vertices[i].x) * (
220
+ test.y - vertices[i].y
221
+ ) / (vertices[j].y - vertices[i].y) + vertices[i].x:
222
+ c = not c
223
+ j = i
224
+ i += 1
225
+ return c
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: sonolus.py
3
- Version: 0.1.5
3
+ Version: 0.1.7
4
4
  Summary: Sonolus engine development in Python
5
5
  Requires-Python: >=3.13
6
6
  Description-Content-Type: text/markdown
@@ -26,16 +26,16 @@ sonolus/backend/optimize/passes.py,sha256=OPzcpFUU7qbQFSrgr84UuzDg_vB1GYkZj2K2aq
26
26
  sonolus/backend/optimize/simplify.py,sha256=Tz4RftjH5TpIxoIOwiZuLchsrwzI9GaS_exdjAW6HKk,7927
27
27
  sonolus/backend/optimize/ssa.py,sha256=D3CQm3s3gcuU0_k_0pXVrwirm4xjAZsX6corrTDS1Co,9013
28
28
  sonolus/build/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
29
- sonolus/build/cli.py,sha256=YlWg3Jfe1gi2wnlIJDh8LCMLSPV3l9AiuDWk2Bbphmg,5812
29
+ sonolus/build/cli.py,sha256=12sFalCUJmn0Dg3f8ipNIkPmZ_djG15gldqWzDG7AYo,6241
30
30
  sonolus/build/collection.py,sha256=IsDgedsAJ-foHzQ4LnQ9zSVXSz5wVVt4sgqiUICBvCU,10573
31
31
  sonolus/build/compile.py,sha256=wBiRX6rnq8Op4_vVXJ-bPLtffgV4ppNBa9M9hIe9twI,3579
32
32
  sonolus/build/engine.py,sha256=TAJCdSWOsEcTq9o6jfR3A2JZ8gOoc9YQx6FC_qW-dFw,6766
33
33
  sonolus/build/level.py,sha256=3sdGPvIZ4a15u3-JdITnB6rznp6a2E3k6A0gB8A15JA,610
34
34
  sonolus/build/node.py,sha256=jwsVWt6Brh4M9MypUt3Nqnxfm7fXrCMRWYQGwBTAszI,1162
35
- sonolus/build/project.py,sha256=x350tPcwKCYakdbrP796b545hVUaBwrzY0yEcQR7ut4,4023
35
+ sonolus/build/project.py,sha256=QukkCVQBGxnp1F2jQjaAkTg3TutwhWqKGXAfQZDMk1c,5162
36
36
  sonolus/script/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
37
- sonolus/script/archetype.py,sha256=gYRkUD_hI_MhXA-diIObIp0xqpX6aNiPqWLhiw-t094,33887
38
- sonolus/script/array.py,sha256=KJxyL4I7A-CNFQW1RbZYQGVI527XZmoesDR-q6BurB8,9339
37
+ sonolus/script/archetype.py,sha256=cQDoNVO7dUHYQ7RlAHiyoEnUYt0seCnFPD_bPzo0BNE,34114
38
+ sonolus/script/array.py,sha256=rPAK3kdeL6A_a82DSnxDe6Cl99vjS2UVU97n1r8arsA,9341
39
39
  sonolus/script/array_like.py,sha256=OVOLMadS-Jj941S-EgoxlFPdaQLT6ZrHzaMnKtB6PfU,8346
40
40
  sonolus/script/bucket.py,sha256=ZNlNeBZ1FzRLQ49bTuuJDmnYZ-_sjQEvosySx3ESxQc,6944
41
41
  sonolus/script/containers.py,sha256=tf0-UUSq8NH6k9jIKq6qgXRxanFQ0f5olH6uHghTynk,13171
@@ -53,22 +53,22 @@ sonolus/script/options.py,sha256=nTOuoRC2p_qoMx4ngOVDY2WZDooTxwwGTGKyb994v2A,751
53
53
  sonolus/script/particle.py,sha256=K06ArT9tstRNdbuGIviDmDDWcK3-ieA53LHg0Xvizow,8304
54
54
  sonolus/script/pointer.py,sha256=pOh5vCSl6xR7mDm5PMOhBc_wf4O-b938DE0LtZWBmPc,1119
55
55
  sonolus/script/print.py,sha256=mNYu9QWiacBBGZrnePZQMVwbbguoelUps9GiOK_aVRU,2096
56
- sonolus/script/project.py,sha256=iGdMEpT6mUcyTmfGwmIW25ucHB51ETd1_SyQ6UC7AYg,612
57
- sonolus/script/quad.py,sha256=SVMcfRYx8Bv5t7A2vhus9QUiw-1V-qtRhldr3DnnNWw,7693
56
+ sonolus/script/project.py,sha256=DhmBuH0CkFKmF27o1V9XhIOsywXDi57i-Wpwyd-70LM,2040
57
+ sonolus/script/quad.py,sha256=e2kXKSp9K46Q9qstLsGLFPxaW7Z2kfVfignA8Ka2-Pw,8375
58
58
  sonolus/script/record.py,sha256=rOVlo-eUUvFG_Ee-wKrgX_ZwOqWKWtS7s8m-juM70yE,11216
59
59
  sonolus/script/runtime.py,sha256=xeiLJFeqkCev1x6pBiM5afyvi3GWdpLJ5OYRg9M9nwo,18562
60
- sonolus/script/sprite.py,sha256=-hURLrLrxtu73qSQ4x4_gJqnJ3XiORUmNojh5fdm2E8,15732
60
+ sonolus/script/sprite.py,sha256=FOozsPtaQnInnm-QshtYznnC66_8jb1D2sHmo7nFEsY,15718
61
61
  sonolus/script/text.py,sha256=IsoINZJXefjReYDjJFwVaFsUCdgeQvPBDeywljM2dWo,13025
62
62
  sonolus/script/timing.py,sha256=ZR0ypV2PIoDCMHHGOMfCeezStCsBQdzomdqaz5VKex0,2981
63
63
  sonolus/script/transform.py,sha256=hH6KSRQC8vV-Z10CRCrGewMYqQwUMH3mQIEmniuC2Zw,10760
64
64
  sonolus/script/ui.py,sha256=kyuP88sLRJPT-Yx-7fx8Xu9Wdegyw_CJNslcP4WnDUs,7268
65
65
  sonolus/script/values.py,sha256=JuvJknskuY6FPprUp9R-6Gj2TDJLu4ppbVcYedG5dG0,1049
66
- sonolus/script/vec.py,sha256=Uph6Z9xofwbKGZu0H7rl6fzlO4bj5i0cnWu4LQeIL4I,5270
66
+ sonolus/script/vec.py,sha256=looCsmYpt6jlasdBqddtkC0hA1exZ3lxPP3_BL8cPXw,6148
67
67
  sonolus/script/internal/__init__.py,sha256=T6rzLoiOUaiSQtaHMZ88SNO-ijSjSSv33TKtUwu-Ms8,136
68
68
  sonolus/script/internal/builtin_impls.py,sha256=w27aKMhLX4Ivd7uKz1ZgVy1lC5scSSJ23-VfsbCfIb0,7731
69
69
  sonolus/script/internal/callbacks.py,sha256=vWzJG8uiJoEtsNnbeZPqOHogCwoLpz2D1MnHY2wVV8s,2801
70
70
  sonolus/script/internal/constant.py,sha256=lIBBR84Rfw0rBSZU2reCrUnB3yEMEdOX6A7_LgptYz8,3776
71
- sonolus/script/internal/context.py,sha256=evLOXcOqskK5Gh5Je86ghKlMaon-Opi7dDoMtkCjEh8,13533
71
+ sonolus/script/internal/context.py,sha256=I9X89aTWgoY-_jgnJHEnzzVLAvILACtPfgdE4ipxjOc,13930
72
72
  sonolus/script/internal/descriptor.py,sha256=XRFey-EjiAm_--KsNl-8N0Mi_iyQwlPh68gDp0pKf3E,392
73
73
  sonolus/script/internal/dict_impl.py,sha256=alu_wKGSk1kZajNf64qbe7t71shEzD4N5xNIATH8Swo,1885
74
74
  sonolus/script/internal/error.py,sha256=ZNnsvQVQAnFKzcvsm6-sste2lo-tP5pPI8sD7XlAZWc,490
@@ -82,8 +82,8 @@ sonolus/script/internal/range.py,sha256=lrTanQFHU7RuQxSSPwDdoC30Y8FnHGxcP1Ahditu
82
82
  sonolus/script/internal/transient.py,sha256=pSDFGu0m26zVsp6owmNTeSPue-osDd1JahjJSijW8t0,1520
83
83
  sonolus/script/internal/tuple_impl.py,sha256=vjXmScLVdeTkDn3t9fgIRqtW31iwngnaP2rmA6nlsLw,3431
84
84
  sonolus/script/internal/value.py,sha256=ik9sMKl0TbsH_C6QNxD4WfpAnmBFISgmmlazWwh3kY0,4308
85
- sonolus_py-0.1.5.dist-info/METADATA,sha256=HHVoPymf7iwqOCBJqIEMETzGgzgPIwEA3Owrjudj_a4,216
86
- sonolus_py-0.1.5.dist-info/WHEEL,sha256=C2FUgwZgiLbznR-k0b_5k3Ai_1aASOXDss3lzCUsUug,87
87
- sonolus_py-0.1.5.dist-info/entry_points.txt,sha256=oTYspY_b7SA8TptEMTDxh4-Aj-ZVPnYC9f1lqH6s9G4,54
88
- sonolus_py-0.1.5.dist-info/licenses/LICENSE,sha256=JEKpqVhQYfEc7zg3Mj462sKbKYmO1K7WmvX1qvg9IJk,1067
89
- sonolus_py-0.1.5.dist-info/RECORD,,
85
+ sonolus_py-0.1.7.dist-info/METADATA,sha256=R9hAvB7RLZb6duTGJWpmquqnfLZ4gpWJDGOcsm175ls,216
86
+ sonolus_py-0.1.7.dist-info/WHEEL,sha256=C2FUgwZgiLbznR-k0b_5k3Ai_1aASOXDss3lzCUsUug,87
87
+ sonolus_py-0.1.7.dist-info/entry_points.txt,sha256=oTYspY_b7SA8TptEMTDxh4-Aj-ZVPnYC9f1lqH6s9G4,54
88
+ sonolus_py-0.1.7.dist-info/licenses/LICENSE,sha256=JEKpqVhQYfEc7zg3Mj462sKbKYmO1K7WmvX1qvg9IJk,1067
89
+ sonolus_py-0.1.7.dist-info/RECORD,,