mini-arcade-core 0.9.2__py3-none-any.whl → 0.9.3__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -22,12 +22,17 @@ from .geometry2d import Bounds2D, Position2D, Size2D
22
22
  from .keys import Key, keymap
23
23
  from .kinematics2d import KinematicData
24
24
  from .physics2d import Velocity2D
25
+ from .registry import SceneRegistry
25
26
  from .scene import Scene
26
27
 
27
28
  logger = logging.getLogger(__name__)
28
29
 
29
30
 
30
- def run_game(initial_scene_cls: type[Scene], config: GameConfig | None = None):
31
+ def run_game(
32
+ initial_scene_cls: type[Scene],
33
+ config: GameConfig | None = None,
34
+ registry: SceneRegistry | None = None,
35
+ ):
31
36
  """
32
37
  Convenience helper to bootstrap and run a game with a single scene.
33
38
 
@@ -44,7 +49,7 @@ def run_game(initial_scene_cls: type[Scene], config: GameConfig | None = None):
44
49
  raise ValueError(
45
50
  "GameConfig.backend must be set to a Backend instance"
46
51
  )
47
- game = Game(cfg)
52
+ game = Game(cfg, registry=registry)
48
53
  scene = initial_scene_cls(game)
49
54
  game.run(scene)
50
55
 
@@ -72,6 +77,7 @@ __all__ = [
72
77
  "RectKinematic",
73
78
  "Key",
74
79
  "keymap",
80
+ "SceneRegistry",
75
81
  ]
76
82
 
77
83
  PACKAGE_NAME = "mini-arcade-core" # or whatever is in your pyproject.toml
mini_arcade_core/game.py CHANGED
@@ -9,15 +9,18 @@ from dataclasses import dataclass
9
9
  from datetime import datetime
10
10
  from pathlib import Path
11
11
  from time import perf_counter, sleep
12
- from typing import TYPE_CHECKING
12
+ from typing import TYPE_CHECKING, Union
13
13
 
14
14
  from PIL import Image # type: ignore[import]
15
15
 
16
16
  from .backend import Backend
17
+ from .registry import SceneRegistry
17
18
 
18
19
  if TYPE_CHECKING: # avoid runtime circular import
19
20
  from .scene import Scene
20
21
 
22
+ SceneOrId = Union["Scene", str]
23
+
21
24
 
22
25
  @dataclass
23
26
  class GameConfig:
@@ -49,7 +52,9 @@ class _StackEntry:
49
52
  class Game:
50
53
  """Core game object responsible for managing the main loop and active scene."""
51
54
 
52
- def __init__(self, config: GameConfig):
55
+ def __init__(
56
+ self, config: GameConfig, registry: SceneRegistry | None = None
57
+ ):
53
58
  """
54
59
  :param config: Game configuration options.
55
60
  :type config: GameConfig
@@ -65,6 +70,7 @@ class Game:
65
70
  "GameConfig.backend must be set to a Backend instance"
66
71
  )
67
72
  self.backend: Backend = config.backend
73
+ self.registry = registry or SceneRegistry(_factories={})
68
74
  self._scene_stack: list[_StackEntry] = []
69
75
 
70
76
  def current_scene(self) -> "Scene | None":
@@ -76,7 +82,7 @@ class Game:
76
82
  """
77
83
  return self._scene_stack[-1].scene if self._scene_stack else None
78
84
 
79
- def change_scene(self, scene: Scene):
85
+ def change_scene(self, scene: SceneOrId):
80
86
  """
81
87
  Swap the active scene. Concrete implementations should call
82
88
  ``on_exit``/``on_enter`` appropriately.
@@ -84,6 +90,8 @@ class Game:
84
90
  :param scene: The new scene to activate.
85
91
  :type scene: Scene
86
92
  """
93
+ scene = self._resolve_scene(scene)
94
+
87
95
  while self._scene_stack:
88
96
  entry = self._scene_stack.pop()
89
97
  entry.scene.on_exit()
@@ -91,11 +99,13 @@ class Game:
91
99
  self._scene_stack.append(_StackEntry(scene=scene, as_overlay=False))
92
100
  scene.on_enter()
93
101
 
94
- def push_scene(self, scene: "Scene", as_overlay: bool = False):
102
+ def push_scene(self, scene: SceneOrId, as_overlay: bool = False):
95
103
  """
96
104
  Push a scene on top of the current one.
97
105
  If as_overlay=True, underlying scene(s) may still be drawn but never updated.
98
106
  """
107
+ scene = self._resolve_scene(scene)
108
+
99
109
  top = self.current_scene()
100
110
  if top is not None:
101
111
  top.on_pause()
@@ -142,7 +152,7 @@ class Game:
142
152
  """Request that the main loop stops."""
143
153
  self._running = False
144
154
 
145
- def run(self, initial_scene: Scene):
155
+ def run(self, initial_scene: SceneOrId):
146
156
  """
147
157
  Run the main loop starting with the given scene.
148
158
 
@@ -241,3 +251,8 @@ class Game:
241
251
  self._convert_bmp_to_image(bmp_path, str(out_path))
242
252
  return str(out_path)
243
253
  return None
254
+
255
+ def _resolve_scene(self, scene: SceneOrId) -> "Scene":
256
+ if isinstance(scene, str):
257
+ return self.registry.create(scene, self)
258
+ return scene
@@ -0,0 +1,62 @@
1
+ """
2
+ Scene registry for mini arcade core.
3
+ Allows registering and creating scenes by string IDs.
4
+ """
5
+
6
+ from __future__ import annotations
7
+
8
+ from dataclasses import dataclass
9
+ from typing import TYPE_CHECKING, Dict, Protocol
10
+
11
+ if TYPE_CHECKING:
12
+ from mini_arcade_core.game import Game
13
+ from mini_arcade_core.scene import Scene
14
+
15
+
16
+ class SceneFactory(Protocol):
17
+ """
18
+ Protocol for scene factory callables.
19
+ """
20
+
21
+ def __call__(self, game: "Game") -> "Scene": ...
22
+
23
+
24
+ @dataclass
25
+ class SceneRegistry:
26
+ """
27
+ Registry for scene factories, allowing registration and creation of scenes by string IDs.
28
+ """
29
+
30
+ _factories: Dict[str, SceneFactory]
31
+
32
+ def register(self, scene_id: str, factory: SceneFactory) -> None:
33
+ """
34
+ Register a scene factory under a given scene ID.
35
+
36
+ :param scene_id: The string ID for the scene.
37
+ :type scene_id: str
38
+
39
+ :param factory: A callable that creates a Scene instance.
40
+ :type factory: SceneFactory
41
+ """
42
+ self._factories[scene_id] = factory
43
+
44
+ def create(self, scene_id: str, game: "Game") -> "Scene":
45
+ """
46
+ Create a scene instance using the registered factory for the given scene ID.
47
+
48
+ :param scene_id: The string ID of the scene to create.
49
+ :type scene_id: str
50
+
51
+ :param game: The Game instance to pass to the scene factory.
52
+ :type game: Game
53
+
54
+ :return: A new Scene instance.
55
+ :rtype: Scene
56
+
57
+ :raises KeyError: If no factory is registered for the given scene ID.
58
+ """
59
+ try:
60
+ return self._factories[scene_id](game)
61
+ except KeyError as e:
62
+ raise KeyError(f"Unknown scene_id={scene_id!r}") from e
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: mini-arcade-core
3
- Version: 0.9.2
3
+ Version: 0.9.3
4
4
  Summary: Tiny scene-based game loop core for small arcade games.
5
5
  License: Copyright (c) 2025 Santiago Rincón
6
6
 
@@ -1,19 +1,20 @@
1
- mini_arcade_core/__init__.py,sha256=7Ioji-MpfSQq4Ok3agVzP4mvcTNiThrY-DTDmVYFKo0,2609
1
+ mini_arcade_core/__init__.py,sha256=l1POPqWz4432MvXlftzB9K0jGWmq9jzDTDe-9bPWvs4,2739
2
2
  mini_arcade_core/backend.py,sha256=kB666RiMSpFaxVEi0KIOPGjh4cdCPWDs5vz8MdY37G4,6854
3
3
  mini_arcade_core/boundaries2d.py,sha256=H1HkCR1422MkQIEve2DFKvnav4RpvtLx-qTMxzmdDMQ,2610
4
4
  mini_arcade_core/collision2d.py,sha256=iq800wsoYQNft3SA-W4jeY1wXhbpz2E7IkIorZBI238,1718
5
5
  mini_arcade_core/entity.py,sha256=1AuE-_iMJYHxSyG783fsyByKK8Gx4vWWKMCEPjtgHXw,1776
6
- mini_arcade_core/game.py,sha256=2DaFRdu7ogrwukh2MRnTh0Hy2r1XcaGSHEpLBNxfqIU,7273
6
+ mini_arcade_core/game.py,sha256=YJD10IePBcZ8qyt1FCIWX9hnm8HhK85QK7J6hDEH1H4,7734
7
7
  mini_arcade_core/geometry2d.py,sha256=js791mMpsE_YbEoqtTOsOxNSflvKgSk6VeVKhj9N318,1282
8
8
  mini_arcade_core/keymaps/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
9
9
  mini_arcade_core/keymaps/sdl.py,sha256=lRBvgTfdT7enN0qV7S2ZoLsPAWpaiSHzsyVGDlyTzQA,1924
10
10
  mini_arcade_core/keys.py,sha256=LTg20SwLBI3kpPIiTNpq2yBft_QUGj-iNFSNm9M-Fus,3010
11
11
  mini_arcade_core/kinematics2d.py,sha256=twBx-1jEwdknIJEyYBUg4VZyAvw1d7pGHaGFd36TPbo,2085
12
12
  mini_arcade_core/physics2d.py,sha256=qIq86qWVuvcnLIMEPH6xx7XQO4tQhfiMZY6FseuVOR8,1636
13
+ mini_arcade_core/registry.py,sha256=F6zw7-FthcZeApLSpPe9sjwAm7ed9ji8pfw1CG24ac4,1699
13
14
  mini_arcade_core/scene.py,sha256=e0E81aHt6saYiGfA-1V2II1yWwcZfvqGTRAv8pZnQtY,3865
14
15
  mini_arcade_core/ui/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
15
16
  mini_arcade_core/ui/menu.py,sha256=79krwtWkGXKLSxG37PZsIIeFLayx9OTDN6esJepthlE,11343
16
- mini_arcade_core-0.9.2.dist-info/METADATA,sha256=6NW5an292zZXvaDVoLUKayukctCRzizdbHqM0a9vZ5g,8188
17
- mini_arcade_core-0.9.2.dist-info/WHEEL,sha256=zp0Cn7JsFoX2ATtOhtaFYIiE2rmFAD4OcMhtUki8W3U,88
18
- mini_arcade_core-0.9.2.dist-info/licenses/LICENSE,sha256=3lHAuV0584cVS5vAqi2uC6GcsVgxUijvwvtZckyvaZ4,1096
19
- mini_arcade_core-0.9.2.dist-info/RECORD,,
17
+ mini_arcade_core-0.9.3.dist-info/METADATA,sha256=MzprnzdjBtSbBmvJwXi4EsnDbgOZsu253zBmO7TF0hA,8188
18
+ mini_arcade_core-0.9.3.dist-info/WHEEL,sha256=zp0Cn7JsFoX2ATtOhtaFYIiE2rmFAD4OcMhtUki8W3U,88
19
+ mini_arcade_core-0.9.3.dist-info/licenses/LICENSE,sha256=3lHAuV0584cVS5vAqi2uC6GcsVgxUijvwvtZckyvaZ4,1096
20
+ mini_arcade_core-0.9.3.dist-info/RECORD,,