From 25648f08e9c456dda93f5f7e0d5b77852ca33e99 Mon Sep 17 00:00:00 2001 From: = <=> Date: Mon, 28 Sep 2026 16:06:57 +0200 Subject: [PATCH] More refactors --- src/pacman/ecs/__init__.py | 20 +++++++++++++------- src/pacman/ecs/coord.py | 3 +-- src/pacman/ecs/graphics.py | 20 ++++++++++++++++++-- src/pacman/ecs/hierarchy.py | 2 +- 4 files changed, 33 insertions(+), 12 deletions(-) diff --git a/src/pacman/ecs/__init__.py b/src/pacman/ecs/__init__.py index cce709c..56fc740 100644 --- a/src/pacman/ecs/__init__.py +++ b/src/pacman/ecs/__init__.py @@ -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() diff --git a/src/pacman/ecs/coord.py b/src/pacman/ecs/coord.py index 2b8108e..cc3547b 100644 --- a/src/pacman/ecs/coord.py +++ b/src/pacman/ecs/coord.py @@ -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) diff --git a/src/pacman/ecs/graphics.py b/src/pacman/ecs/graphics.py index 15e911d..2c4dc99 100644 --- a/src/pacman/ecs/graphics.py +++ b/src/pacman/ecs/graphics.py @@ -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([])) diff --git a/src/pacman/ecs/hierarchy.py b/src/pacman/ecs/hierarchy.py index f0698d2..a50e97c 100644 --- a/src/pacman/ecs/hierarchy.py +++ b/src/pacman/ecs/hierarchy.py @@ -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 -- 2.53.0