]> Untitled Git - axy/ft/pacman.git/commitdiff
More refactors
author= <=>
Mon, 28 Sep 2026 14:06:57 +0000 (16:06 +0200)
committer= <=>
Mon, 28 Sep 2026 14:06:57 +0000 (16:06 +0200)
src/pacman/ecs/__init__.py
src/pacman/ecs/coord.py
src/pacman/ecs/graphics.py
src/pacman/ecs/hierarchy.py

index cce709ccef1a2588164f5a91dc0280f6f377de9d..56fc740dbe630481d0f4e98cc5b1916bbc0c224b 100644 (file)
@@ -8,7 +8,12 @@ from pacman.ecs.coord import (
     CoordAbs2D,
     coord_update_plugins,
 )
-from pacman.ecs.graphics import ShouldRender, Sprite2D, graphics_plugins
+from pacman.ecs.graphics import (
+    ShouldRender,
+    Sprite2D,
+    WindowEvents,
+    graphics_plugins,
+)
 from pacman.ecs.hierarchy import (
     ChildOf,
     hierachy_plugins,
@@ -56,6 +61,11 @@ def move_root(root: Query[Coord2D, WiggleRoot]) -> None:
         coord.x = 30.0 + math.cos(tick) * 30.0
 
 
+def print_clicks(events: WindowEvents) -> None:
+    for pos, click in events.clicks():
+        print(pos, click)
+
+
 def setup_world(world: World, assets: Assets[Sprite2D]) -> None:
     asset_paths = list(Path("./asset").rglob("*.png"))
     root = world.spawn(
@@ -88,7 +98,7 @@ def setup_world(world: World, assets: Assets[Sprite2D]) -> None:
                 WiggleSprite(),
                 ChildOf(root),
             )
-            for j in range(3)
+            for j in range(30)
             for i, asset_path in enumerate(asset_paths)
         ],
     )
@@ -99,10 +109,6 @@ if __name__ == "__main__":
         World()
         .with_plugins(minimal_plugins, graphics_plugins)
         .with_systems(StartupSchedule, setup_world)
-        .with_systems(
-            UpdateSchedule,
-            move_sprites,
-            move_root,
-        )
+        .with_systems(UpdateSchedule, move_sprites, move_root, print_clicks)
     )
     world.run_main()
index 2b8108e4c39fd239759f49d85b0e1e4bfb1f5d25..cc3547b45fff32944f3c1f3403a198e659eb3e8b 100644 (file)
@@ -38,5 +38,4 @@ def coord_update_plugins(world: World) -> None:
         Systems(update_coord, update_coord_hierarchy)
         .chain()
         .in_set(PostUpdate, PropagateTransform),
-    )
-    world.with_systems(UpdateSchedule, update_coord)
+    ).with_systems(UpdateSchedule, update_coord)
index 15e911decc51707a9d8c7e79c602225db97fa929..2c4dc99e46f2c9e667369fdd775a2ce2a40a7034 100644 (file)
@@ -1,5 +1,6 @@
+from collections.abc import Iterator
 from dataclasses import dataclass
-from typing import override
+from typing import cast, override
 
 import pygame
 
@@ -31,10 +32,25 @@ class Window(Resource):
 class WindowEvents(Resource):
     events: list[pygame.event.Event]
 
+    def clicks(self) -> Iterator[tuple[int, pygame.Vector2]]:
+        return (
+            cast(tuple[int, pygame.Vector2], (e.pos, e.button))
+            for e in self.events
+            if e.type == pygame.MOUSEBUTTONDOWN
+        )
+
+    def keys(self) -> Iterator[int]:
+        return (
+            cast(int, (e.key)) for e in self.events if e.type == pygame.KEYDOWN
+        )
+
 
 def init_window(world: World) -> None:
     pygame.init()
-    world.spawn(Window, Window(pygame.display.set_mode(), pygame.time.Clock()))
+    world.spawn(
+        Window,
+        Window(pygame.display.set_mode((1600, 1200)), pygame.time.Clock()),
+    )
     world.spawn(WindowEvents, WindowEvents([]))
 
 
index f0698d26dc63dc0cc942eb156ef5dccac3c8e023..a50e97ce282fd23dc0d0b6d55b8535260ff7784b 100644 (file)
@@ -51,7 +51,7 @@ def parent_insert_hook(
 def parent_remove_hook(queue: CommandQueue) -> RawComponentHook[Children]:
     def inner(itr: RawComponentArg[Children]) -> None:
         removes = [e for _, children in itr for e in children.children]
-        queue.queue(lambda world: world.remove_many(Children, removes))
+        queue.queue(lambda world: world.remove_many(ChildOf, removes))
 
     return inner