]> Untitled Git - axy/ft/pacman.git/commitdiff
main menu + fix
authorThomas Rimelen <trimelen@k1r1p12.42mulhouse.fr>
Mon, 12 Oct 2026 14:26:52 +0000 (16:26 +0200)
committerThomas Rimelen <trimelen@k1r1p12.42mulhouse.fr>
Mon, 12 Oct 2026 14:26:52 +0000 (16:26 +0200)
src/pacman/ecs/gui.py
src/pacman/ecs/world.py
src/pacman/game/__init__.py

index b5b38845d2ec3036af477a899e3e62a5522e915b..6fcac7afec48ff6e601ceb9d8029f8284df7fbe0 100644 (file)
@@ -21,7 +21,7 @@ from pacman.ecs.graphics import (
     dir_lookup,
 )
 from pacman.ecs.schedule import StartupSchedule, UpdateSchedule
-from pacman.ecs.update import PostUpdate
+from pacman.ecs.update import PostUpdate, PreUpdate
 from pacman.ecs.world import (
     CommandQueue,
     Component,
@@ -186,7 +186,5 @@ def gui_plugins(world: World) -> None:
             update_text_sprites,
         )
         .chain()
-        .in_set(PostUpdate)
-        .before(GraphicsSystems)
-        .after(PropagateTransform),
+        .in_set(PreUpdate)
     )
index ce049c7192dab7cf4e9b7d88bdd21982c6b7501f..ddf30ebe97e9c3bf0b7471b78bdcaf823c263384 100644 (file)
@@ -769,14 +769,19 @@ class World(FromWorld):
         if ty is None:
             ty = type(comp)
 
-        type Ty = ty  # type: ignore
-
-        def remove(query: Query[Entity, Ty], world: World) -> None:
-            world.remove_many(ty, list(query))
-
-        self.run_raw_system(remove)
+        self.despawn_resource(ty)
         return self.spawn(ty, comp)
 
+    def despawn_resource[T: Resource](
+            self, ty: type[T]
+        ) -> None:
+            type Ty = ty  # type: ignore
+    
+            def remove(query: Query[Entity, Ty], world: World) -> None:
+                world.remove_many(ty, list(query))
+    
+            self.run_raw_system(remove)
+
     def spawn_many[T: Components](
         self, ty: type[T], comps: list[T]
     ) -> list[Entity]:
index 1e364d75f7d8203aac72a41e651848cf068222d2..bc4e0bd0da1b73e03981445049f820ea5d65411a 100644 (file)
@@ -121,6 +121,18 @@ class GameState(Resource):
     lives: int = 3
 
 
+class MainMenu(Component):
+    pass
+
+
+class GameMenu(Component):
+    pass
+
+
+class GameGui(Component):
+    pass
+
+
 class PacGum(Component):
     pass
 
@@ -711,6 +723,10 @@ def setup_new_maze(world: World) -> None:
         world.run_raw_system(sys)
 
 
+def in_game(itr: Query[Maze]) -> bool:
+    return len(list(itr)) != 0
+
+
 def level_is_finished(
     pacgums: Query[SuperPacGum | PacGum], _: Not[GameIsOver]
 ) -> bool:
@@ -726,7 +742,7 @@ def victory(
     for maze in mazes:
         queue.queue(hierarchy.despawn_with_children(maze))
     if state.level == 2:
-        queue.queue(lambda world: world.add_raw_system(game_over)())
+        queue.queue(lambda world: world.run_raw_system(game_over))
         return
     state.level += 1
     state.lives += 1
@@ -784,9 +800,16 @@ def print_timer(state: GameState, config: Config) -> str:
     return f"{time // 60}"
 
 
-def start_game() -> None:
-    print("Game started")
-
+def start_game(
+    world: World,
+    hierarchy: Hierarchy[tuple[()]],
+    menus: Query[Entity, MainMenu],
+) -> None:
+    world.run_raw_system(setup_new_maze)
+    for menu in menus:
+        hierarchy.despawn_with_children(menu)(world)
+    world.despawn_resource(SelectedButton)
+    
 
 def quit_game(world: World) -> None:
     world.spawn_resource(WorldShouldExit())
@@ -868,20 +891,25 @@ def setup_text(world: World, fonts: Assets[Font]) -> None:
         start_game,
     )
 
-    world.run_system(
+    quit_button = world.run_system(
         spawn_button,
         Vector2((1600 - 300) / 2, (1200 - 200) / 2 + 120),
         "Quit",
         quit_game,
     )
 
+    world.spawn(
+        tuple[MainMenu, Children, CoordBundle],
+        (MainMenu(), Children((selected, quit_button)), CoordBundle()),
+    )
+
     world.spawn_resource(SelectedButton(selected))
 
 
 def run_game() -> None:
     World().with_plugins(graphics_plugins, minimal_plugins).with_systems(
         StartupSchedule,
-        Systems(setup_resources, setup_new_maze, setup_text).chain(),
+        Systems(setup_resources, setup_text).chain(),
     ).with_systems(
         UpdateSchedule,
         Systems(
@@ -895,7 +923,7 @@ def run_game() -> None:
             update_game_timer,
         )
         .chain()
-        .run_if(initial_pause_done)
+        .run_if(initial_pause_done, in_game)
         .in_set(Update),
         Systems(update_pacman_anim, update_ghost_anim)
         .before(GraphicsSystems)
@@ -908,6 +936,6 @@ def run_game() -> None:
         .chain()
         .after(PropagateTransform)
         .before(GraphicsSystems),
-        Systems(victory).run_if(level_is_finished),
+        Systems(victory).run_if(level_is_finished, in_game),
         Systems(lose).run_if(level_is_lost),
     ).run_main()