]> Untitled Git - axy/ft/pacman.git/commitdiff
Cleanup and Maybe systemparam
author= <=>
Tue, 22 Sep 2026 13:22:28 +0000 (15:22 +0200)
committer= <=>
Tue, 22 Sep 2026 13:22:28 +0000 (15:22 +0200)
src/pacman/ecs/__init__.py
src/pacman/ecs/world.py

index 777dc1298fad5bfe0ac37f6f1416f6d6e73a5bcb..d963f97b07ec0d9a5a17ec0b89c2ad3df1457fb8 100644 (file)
@@ -190,27 +190,31 @@ if __name__ == "__main__":
             WiggleRoot(),
         ),
     )
-    for j in range(10):
-        for i, asset in enumerate(assets):
-            i += j
-            i = i % len(assets)
-            world.spawn(
-                tuple[
-                    Coord2D,
-                    CoordAbs2D,
-                    Sprite2D,
-                    ShouldRender,
-                    WiggleSprite,
-                    Parent,
-                ],
-                (
-                    Coord2D(30.0 * i, 500.0 + 10.0, 1.0 * i + j),
-                    CoordAbs2D(),
-                    Sprite2D(pygame.image.load(asset)),
-                    ShouldRender(),
-                    WiggleSprite(),
-                    Parent(root),
+    world.spawn_many(
+        tuple[
+            Coord2D,
+            CoordAbs2D,
+            Sprite2D,
+            ShouldRender,
+            WiggleSprite,
+            Parent,
+        ],
+        [
+            (
+                Coord2D(
+                    30.0 * (n := (i + j) % len(assets)),
+                    500.0 + 10.0,
+                    1.0 * n + j,
                 ),
+                CoordAbs2D(),
+                Sprite2D(pygame.image.load(asset)),
+                ShouldRender(),
+                WiggleSprite(),
+                Parent(root),
             )
+            for j in range(15)
+            for i, asset in enumerate(assets)
+        ],
+    )
 
     world.run_main()
index aa1dc2bffad521f93d3960b51c6b9dee13f969c1..f6a6a9c724f27929ab464a6369a12ff5af08f8e9 100644 (file)
@@ -311,6 +311,24 @@ class Not[T: FromWorld | Any](FromWorld, QueryFilter):
         return inner
 
 
+class Maybe[T: FromWorld](FromWorld):
+    def __init__(self, v: T | None) -> None:
+        self.v = v
+
+    @override
+    @classmethod
+    def extract_from_world(
+        cls, ty: type, world: "World"
+    ) -> Callable[[], "Maybe[T]"]:
+        inner = cast(type[T], get_args(ty)[0])
+        extractor = inner.extract_from_world(inner, world)
+
+        def inner() -> Maybe[T]:
+            return Maybe(extractor())
+
+        return inner
+
+
 class Query[Items, Filters = tuple[()]](
     FromWorld, Iterable[Items], Container[Entity]
 ):
@@ -732,24 +750,3 @@ class WorldShouldExit(Resource):
 
 def should_exit(_: WorldShouldExit) -> bool:
     return True
-
-
-class Marker(Component):
-    pass
-
-
-def test_system(q: Query[Marker], queue: CommandQueue) -> None:
-    queue.queue(lambda world: world.spawn(Marker, Marker()))
-    queue.queue(lambda world: world.spawn_empty())
-    print("ran")
-    if len(list(q)) == 10:
-        queue.queue(
-            lambda world: world.spawn(WorldShouldExit, WorldShouldExit())
-        )
-
-
-if __name__ == "__main__":
-    world = World()
-    # world.spawn(WorldShouldExit, WorldShouldExit())
-    world.with_systems(MainSchedule, test_system)
-    world.run_main()