mini-arcade-core 0.9.7__tar.gz → 0.9.8__tar.gz
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.
- {mini_arcade_core-0.9.7 → mini_arcade_core-0.9.8}/PKG-INFO +1 -1
- {mini_arcade_core-0.9.7 → mini_arcade_core-0.9.8}/pyproject.toml +1 -1
- {mini_arcade_core-0.9.7 → mini_arcade_core-0.9.8}/src/mini_arcade_core/__init__.py +3 -0
- mini_arcade_core-0.9.8/src/mini_arcade_core/cheats.py +235 -0
- {mini_arcade_core-0.9.7 → mini_arcade_core-0.9.8}/LICENSE +0 -0
- {mini_arcade_core-0.9.7 → mini_arcade_core-0.9.8}/README.md +0 -0
- {mini_arcade_core-0.9.7 → mini_arcade_core-0.9.8}/src/mini_arcade_core/backend/__init__.py +0 -0
- {mini_arcade_core-0.9.7 → mini_arcade_core-0.9.8}/src/mini_arcade_core/backend/backend.py +0 -0
- {mini_arcade_core-0.9.7 → mini_arcade_core-0.9.8}/src/mini_arcade_core/backend/events.py +0 -0
- {mini_arcade_core-0.9.7 → mini_arcade_core-0.9.8}/src/mini_arcade_core/backend/types.py +0 -0
- {mini_arcade_core-0.9.7 → mini_arcade_core-0.9.8}/src/mini_arcade_core/entity.py +0 -0
- {mini_arcade_core-0.9.7 → mini_arcade_core-0.9.8}/src/mini_arcade_core/game.py +0 -0
- {mini_arcade_core-0.9.7 → mini_arcade_core-0.9.8}/src/mini_arcade_core/keymaps/__init__.py +0 -0
- {mini_arcade_core-0.9.7 → mini_arcade_core-0.9.8}/src/mini_arcade_core/keymaps/keys.py +0 -0
- {mini_arcade_core-0.9.7 → mini_arcade_core-0.9.8}/src/mini_arcade_core/keymaps/sdl.py +0 -0
- {mini_arcade_core-0.9.7 → mini_arcade_core-0.9.8}/src/mini_arcade_core/scenes/__init__.py +0 -0
- {mini_arcade_core-0.9.7 → mini_arcade_core-0.9.8}/src/mini_arcade_core/scenes/autoreg.py +0 -0
- {mini_arcade_core-0.9.7 → mini_arcade_core-0.9.8}/src/mini_arcade_core/scenes/registry.py +0 -0
- {mini_arcade_core-0.9.7 → mini_arcade_core-0.9.8}/src/mini_arcade_core/scenes/scene.py +0 -0
- {mini_arcade_core-0.9.7 → mini_arcade_core-0.9.8}/src/mini_arcade_core/two_d/__init__.py +0 -0
- {mini_arcade_core-0.9.7 → mini_arcade_core-0.9.8}/src/mini_arcade_core/two_d/boundaries2d.py +0 -0
- {mini_arcade_core-0.9.7 → mini_arcade_core-0.9.8}/src/mini_arcade_core/two_d/collision2d.py +0 -0
- {mini_arcade_core-0.9.7 → mini_arcade_core-0.9.8}/src/mini_arcade_core/two_d/geometry2d.py +0 -0
- {mini_arcade_core-0.9.7 → mini_arcade_core-0.9.8}/src/mini_arcade_core/two_d/kinematics2d.py +0 -0
- {mini_arcade_core-0.9.7 → mini_arcade_core-0.9.8}/src/mini_arcade_core/two_d/physics2d.py +0 -0
- {mini_arcade_core-0.9.7 → mini_arcade_core-0.9.8}/src/mini_arcade_core/ui/__init__.py +0 -0
- {mini_arcade_core-0.9.7 → mini_arcade_core-0.9.8}/src/mini_arcade_core/ui/menu.py +0 -0
|
@@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "mini-arcade-core"
|
|
7
|
-
version = "0.9.
|
|
7
|
+
version = "0.9.8"
|
|
8
8
|
description = "Tiny scene-based game loop core for small arcade games."
|
|
9
9
|
authors = [
|
|
10
10
|
{ name = "Santiago Rincon", email = "rincores@gmail.com" },
|
|
@@ -10,6 +10,7 @@ from importlib.metadata import PackageNotFoundError, version
|
|
|
10
10
|
from typing import Callable, Type, Union
|
|
11
11
|
|
|
12
12
|
from mini_arcade_core.backend import Backend, Event, EventType
|
|
13
|
+
from mini_arcade_core.cheats import CheatCode, CheatManager
|
|
13
14
|
from mini_arcade_core.entity import Entity, KinematicEntity, SpriteEntity
|
|
14
15
|
from mini_arcade_core.game import Game, GameConfig
|
|
15
16
|
from mini_arcade_core.keymaps.keys import Key, keymap
|
|
@@ -105,6 +106,8 @@ __all__ = [
|
|
|
105
106
|
"keymap",
|
|
106
107
|
"SceneRegistry",
|
|
107
108
|
"register_scene",
|
|
109
|
+
"CheatManager",
|
|
110
|
+
"CheatCode",
|
|
108
111
|
]
|
|
109
112
|
|
|
110
113
|
PACKAGE_NAME = "mini-arcade-core" # or whatever is in your pyproject.toml
|
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Cheats module for Mini Arcade Core.
|
|
3
|
+
Provides cheat codes and related functionality.
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
from __future__ import annotations
|
|
7
|
+
|
|
8
|
+
from collections import deque
|
|
9
|
+
from dataclasses import dataclass
|
|
10
|
+
from enum import Enum
|
|
11
|
+
from typing import Callable, Deque, Dict, Optional, Sequence, TypeVar
|
|
12
|
+
|
|
13
|
+
# Justification: We want to keep the type variable name simple here.
|
|
14
|
+
# pylint: disable=invalid-name
|
|
15
|
+
TContext = TypeVar("TContext")
|
|
16
|
+
# pylint: enable=invalid-name
|
|
17
|
+
|
|
18
|
+
CheatCallback = Callable[[TContext], None]
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
@dataclass(frozen=True)
|
|
22
|
+
class CheatCode:
|
|
23
|
+
"""
|
|
24
|
+
Represents a registered cheat code.
|
|
25
|
+
|
|
26
|
+
:ivar name (str): Unique name of the cheat code.
|
|
27
|
+
:ivar sequence (tuple[str, ...]): Sequence of key strings that trigger the cheat.
|
|
28
|
+
:ivar callback (CheatCallback): Function to call when the cheat is activated.
|
|
29
|
+
:ivar clear_buffer_on_match (bool): Whether to clear the input buffer after a match.
|
|
30
|
+
:ivar enabled (bool): Whether the cheat code is enabled.
|
|
31
|
+
"""
|
|
32
|
+
|
|
33
|
+
name: str
|
|
34
|
+
sequence: tuple[str, ...]
|
|
35
|
+
callback: CheatCallback
|
|
36
|
+
clear_buffer_on_match: bool = False
|
|
37
|
+
enabled: bool = True
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
class CheatManager:
|
|
41
|
+
"""
|
|
42
|
+
Reusable cheat code matcher.
|
|
43
|
+
Keeps a rolling buffer of recent keys and triggers callbacks on sequence match.
|
|
44
|
+
"""
|
|
45
|
+
|
|
46
|
+
def __init__(
|
|
47
|
+
self,
|
|
48
|
+
buffer_size: int = 16,
|
|
49
|
+
*,
|
|
50
|
+
enabled: bool = True,
|
|
51
|
+
normalize: Optional[Callable[[str], str]] = None,
|
|
52
|
+
key_getter: Optional[Callable[[object], Optional[str]]] = None,
|
|
53
|
+
):
|
|
54
|
+
"""
|
|
55
|
+
:param buffer_size: Maximum size of the input buffer.
|
|
56
|
+
:type buffer_size: int
|
|
57
|
+
|
|
58
|
+
:param enabled: Whether the cheat manager is enabled.
|
|
59
|
+
:type enabled: bool
|
|
60
|
+
|
|
61
|
+
:param normalize: Optional function to normalize key strings.
|
|
62
|
+
:type normalize: Callable[[str], str] | None
|
|
63
|
+
|
|
64
|
+
:param key_getter: Optional function to extract key string from event object.
|
|
65
|
+
:type key_getter: Callable[[object], Optional[str]] | None
|
|
66
|
+
"""
|
|
67
|
+
self.enabled = enabled
|
|
68
|
+
self._buffer: Deque[str] = deque(maxlen=buffer_size)
|
|
69
|
+
self._codes: Dict[str, CheatCode] = {}
|
|
70
|
+
|
|
71
|
+
self._normalize = normalize or (lambda s: s.strip().upper())
|
|
72
|
+
# key_getter: how to extract a key from an engine/backend event object
|
|
73
|
+
self._key_getter = key_getter or self._default_key_getter
|
|
74
|
+
|
|
75
|
+
# Justification: We want to keep the number of arguments manageable here.
|
|
76
|
+
# pylint: disable=too-many-arguments
|
|
77
|
+
def register_code(
|
|
78
|
+
self,
|
|
79
|
+
name: str,
|
|
80
|
+
sequence: Sequence[str],
|
|
81
|
+
callback: CheatCallback,
|
|
82
|
+
*,
|
|
83
|
+
clear_buffer_on_match: bool = False,
|
|
84
|
+
enabled: bool = True,
|
|
85
|
+
):
|
|
86
|
+
"""
|
|
87
|
+
Register a new cheat code.
|
|
88
|
+
|
|
89
|
+
:param name: Unique name for the cheat code.
|
|
90
|
+
:type name: str
|
|
91
|
+
|
|
92
|
+
:param sequence: Sequence of key strings that trigger the cheat.
|
|
93
|
+
:type sequence: Sequence[str]
|
|
94
|
+
|
|
95
|
+
:param callback: Function to call when the cheat is activated.
|
|
96
|
+
:type callback: CheatCallback
|
|
97
|
+
|
|
98
|
+
:param clear_buffer_on_match: Whether to clear the input buffer after a match.
|
|
99
|
+
:type clear_buffer_on_match: bool
|
|
100
|
+
|
|
101
|
+
:param enabled: Whether the cheat code is enabled.
|
|
102
|
+
:type enabled: bool
|
|
103
|
+
"""
|
|
104
|
+
if not name:
|
|
105
|
+
raise ValueError("Cheat code name must be non-empty.")
|
|
106
|
+
if not sequence:
|
|
107
|
+
raise ValueError(
|
|
108
|
+
f"Cheat code '{name}' sequence must be non-empty."
|
|
109
|
+
)
|
|
110
|
+
|
|
111
|
+
norm_seq = tuple(self._normalize(k) for k in sequence)
|
|
112
|
+
self._codes[name] = CheatCode(
|
|
113
|
+
name=name,
|
|
114
|
+
sequence=norm_seq,
|
|
115
|
+
callback=callback,
|
|
116
|
+
clear_buffer_on_match=clear_buffer_on_match,
|
|
117
|
+
enabled=enabled,
|
|
118
|
+
)
|
|
119
|
+
|
|
120
|
+
# pylint: enable=too-many-arguments
|
|
121
|
+
|
|
122
|
+
def unregister_code(self, name: str):
|
|
123
|
+
"""
|
|
124
|
+
Unregister a cheat code by name.
|
|
125
|
+
|
|
126
|
+
:param name: Name of the cheat code to unregister.
|
|
127
|
+
:type name: str
|
|
128
|
+
"""
|
|
129
|
+
self._codes.pop(name, None)
|
|
130
|
+
|
|
131
|
+
def clear_buffer(self):
|
|
132
|
+
"""Clear the input buffer."""
|
|
133
|
+
self._buffer.clear()
|
|
134
|
+
|
|
135
|
+
def process_event(self, event: object, context: TContext) -> list[str]:
|
|
136
|
+
"""
|
|
137
|
+
Call from Scene when a key is pressed.
|
|
138
|
+
Returns list of cheat names matched (often 0 or 1).
|
|
139
|
+
|
|
140
|
+
:param event: The event object from the backend/engine.
|
|
141
|
+
:type event: object
|
|
142
|
+
|
|
143
|
+
:param context: Context object passed to cheat callbacks.
|
|
144
|
+
:type context: TContext
|
|
145
|
+
"""
|
|
146
|
+
if not self.enabled:
|
|
147
|
+
return []
|
|
148
|
+
key = self._key_getter(event)
|
|
149
|
+
if not key:
|
|
150
|
+
return []
|
|
151
|
+
return self.process_key(key, context)
|
|
152
|
+
|
|
153
|
+
def process_key(self, key: str, context: TContext) -> list[str]:
|
|
154
|
+
"""
|
|
155
|
+
Pure method for tests: feed a key string directly.
|
|
156
|
+
|
|
157
|
+
:param key: The key string to process.
|
|
158
|
+
:type key: str
|
|
159
|
+
|
|
160
|
+
:param context: Context object passed to cheat callbacks.
|
|
161
|
+
:type context: TContext
|
|
162
|
+
|
|
163
|
+
:return: List of cheat names matched.
|
|
164
|
+
:rtype: list[str]
|
|
165
|
+
"""
|
|
166
|
+
if not self.enabled:
|
|
167
|
+
return []
|
|
168
|
+
|
|
169
|
+
k = self._normalize(key)
|
|
170
|
+
if not k:
|
|
171
|
+
return []
|
|
172
|
+
|
|
173
|
+
self._buffer.append(k)
|
|
174
|
+
buf = tuple(self._buffer)
|
|
175
|
+
|
|
176
|
+
matched: list[str] = []
|
|
177
|
+
# Check if buffer ends with any cheat sequence
|
|
178
|
+
for code in self._codes.values():
|
|
179
|
+
if not code.enabled:
|
|
180
|
+
continue
|
|
181
|
+
seq = code.sequence
|
|
182
|
+
if len(seq) > len(buf):
|
|
183
|
+
continue
|
|
184
|
+
if buf[-len(seq) :] == seq:
|
|
185
|
+
code.callback(context)
|
|
186
|
+
matched.append(code.name)
|
|
187
|
+
if code.clear_buffer_on_match:
|
|
188
|
+
self.clear_buffer()
|
|
189
|
+
break # buffer changed; stop early
|
|
190
|
+
|
|
191
|
+
return matched
|
|
192
|
+
|
|
193
|
+
@staticmethod
|
|
194
|
+
def _default_key_getter(event: object) -> Optional[str]:
|
|
195
|
+
"""
|
|
196
|
+
Best-effort extraction:
|
|
197
|
+
- event.key
|
|
198
|
+
- event.key_code
|
|
199
|
+
- event.code
|
|
200
|
+
- event.scancode
|
|
201
|
+
- dict-like {"key": "..."}
|
|
202
|
+
Adjust/override with key_getter in your engine if needed.
|
|
203
|
+
|
|
204
|
+
:param event: The event object.
|
|
205
|
+
:type event: object
|
|
206
|
+
|
|
207
|
+
:return: Extracted key string, or None if not found.
|
|
208
|
+
:rtype: Optional[str]
|
|
209
|
+
"""
|
|
210
|
+
if event is None:
|
|
211
|
+
return None
|
|
212
|
+
|
|
213
|
+
# dict-like
|
|
214
|
+
if isinstance(event, dict):
|
|
215
|
+
v = (
|
|
216
|
+
event.get("key")
|
|
217
|
+
or event.get("key_code")
|
|
218
|
+
or event.get("code")
|
|
219
|
+
or event.get("scancode")
|
|
220
|
+
)
|
|
221
|
+
if isinstance(v, Enum):
|
|
222
|
+
return v.name
|
|
223
|
+
return str(v) if v is not None else None
|
|
224
|
+
|
|
225
|
+
# object-like
|
|
226
|
+
for attr in ("key", "key_code", "code", "scancode"):
|
|
227
|
+
if hasattr(event, attr):
|
|
228
|
+
v = getattr(event, attr)
|
|
229
|
+
if v is None:
|
|
230
|
+
continue
|
|
231
|
+
if isinstance(v, Enum):
|
|
232
|
+
return v.name # <-- THIS is the important bit
|
|
233
|
+
return str(v)
|
|
234
|
+
|
|
235
|
+
return None
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{mini_arcade_core-0.9.7 → mini_arcade_core-0.9.8}/src/mini_arcade_core/two_d/boundaries2d.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
{mini_arcade_core-0.9.7 → mini_arcade_core-0.9.8}/src/mini_arcade_core/two_d/kinematics2d.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|