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,
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(
WiggleSprite(),
ChildOf(root),
)
- for j in range(3)
+ for j in range(30)
for i, asset_path in enumerate(asset_paths)
],
)
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()
Systems(update_coord, update_coord_hierarchy)
.chain()
.in_set(PostUpdate, PropagateTransform),
- )
- world.with_systems(UpdateSchedule, update_coord)
+ ).with_systems(UpdateSchedule, update_coord)
+from collections.abc import Iterator
from dataclasses import dataclass
-from typing import override
+from typing import cast, override
import pygame
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([]))
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