Functional Expansion: Add object oriented entity concept
This commit is contained in:
parent
d25706163c
commit
1a8740ae07
7 changed files with 74 additions and 7 deletions
68
src/dnd_dm_toolkit/core/bus.py
Normal file
68
src/dnd_dm_toolkit/core/bus.py
Normal file
|
|
@ -0,0 +1,68 @@
|
||||||
|
from typing import Protocol, List, Callable, Dict
|
||||||
|
from dataclasses import dataclass
|
||||||
|
from ..core.stuctures.event import (
|
||||||
|
GameEvent
|
||||||
|
)
|
||||||
|
from ..core.enums.game import EventType
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class Hook:
|
||||||
|
"""
|
||||||
|
Represents a single event hook.
|
||||||
|
|
||||||
|
Attributes:
|
||||||
|
event_type: Type of event to listen for.
|
||||||
|
handler: Function to call when event fires.
|
||||||
|
priority: Execution order (lower number = earlier execution).
|
||||||
|
source: Name of what created this hook (for debugging).
|
||||||
|
"""
|
||||||
|
event_type: EventType
|
||||||
|
handler: Callable[[GameEvent], None]
|
||||||
|
priority: int = 100
|
||||||
|
source: object | None = None
|
||||||
|
|
||||||
|
class Hookable(Protocol):
|
||||||
|
hooks: List[Hook]
|
||||||
|
|
||||||
|
def _init_hooks(self, hooks: List[Hook]): ...
|
||||||
|
|
||||||
|
def add_hook(self, hook: Hook): ...
|
||||||
|
|
||||||
|
def del_hook(self, hook: Hook): ...
|
||||||
|
|
||||||
|
class EventBus:
|
||||||
|
"""
|
||||||
|
Global event bus for managing game events (Singleton).
|
||||||
|
|
||||||
|
This is the central communication hub where:
|
||||||
|
- Events are emitted (broadcasted)
|
||||||
|
- Hooks listen for specific event types
|
||||||
|
- Multiple hooks can react to the same event
|
||||||
|
"""
|
||||||
|
_instance = None
|
||||||
|
_initialized = False
|
||||||
|
|
||||||
|
def __new__(cls):
|
||||||
|
"""Ensure only one instance exists (Singleton pattern)."""
|
||||||
|
if cls._instance == None:
|
||||||
|
cls._instance = super().__new__(cls)
|
||||||
|
return cls._instance
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
"""Initialize the event bus (only runs once due to _initialized flag)."""
|
||||||
|
if self._initialized:
|
||||||
|
return
|
||||||
|
|
||||||
|
# Storage for hooks: EventType -> List of Hooks
|
||||||
|
self._hooks: Dict[EventType, List[Hook]] = {}
|
||||||
|
|
||||||
|
# Counter for generating unique hook IDs
|
||||||
|
self._next_hook_id = 1
|
||||||
|
|
||||||
|
# Mapping: hook_id -> (event_type, hook_index)
|
||||||
|
# This allows O(1) hook removal by ID
|
||||||
|
self._hook_id_map: Dict[int, tuple[EventType, int]] = dict()
|
||||||
|
|
||||||
|
self._initialized = True
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
import warnings
|
import warnings
|
||||||
from typing import List, Callable, Protocol, Any
|
from typing import List, Callable, Protocol
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from ..enums.game import (
|
from ..enums.game import (
|
||||||
Ability,
|
Ability,
|
||||||
|
|
@ -12,7 +12,7 @@ from ..enums.game import (
|
||||||
CreatureType
|
CreatureType
|
||||||
)
|
)
|
||||||
from ..sentinel import _AllStatsSentinel
|
from ..sentinel import _AllStatsSentinel
|
||||||
from ..constants import MAXIMUM_STAT_VALUE
|
from ..constants import MAXIMUM_STAT_VALUE, ALL_STATS
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class Attributes:
|
class Attributes:
|
||||||
|
|
@ -193,12 +193,11 @@ class Race:
|
||||||
alignment_tendency: Optional typical alignment for the race.
|
alignment_tendency: Optional typical alignment for the race.
|
||||||
"""
|
"""
|
||||||
name: str
|
name: str
|
||||||
ability_score_increases: dict[Ability, int]
|
ability_score_increases: dict[Ability | ALL_STATS, int]
|
||||||
size: CreatureSize
|
size: CreatureSize
|
||||||
speed: int
|
speed: int
|
||||||
type: CreatureType
|
type: CreatureType
|
||||||
languages: list[Language]
|
languages: list[Language]
|
||||||
features: list[Feature]
|
features: list[Feature]
|
||||||
age_description: str = ""
|
age_description: str = "they can live approximately 80-100 years"
|
||||||
alignment_tendency: str = ""
|
alignment_tendency: str = "None"
|
||||||
extra_language_choices: int = 0
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
from ..core.enums.game import LanguageRarity
|
from ..core.enums.game import LanguageRarity
|
||||||
from ..core.stuctures.character import Language, Race
|
from ..core.stuctures.creature import Language, Race
|
||||||
|
|
||||||
def get_available_languages(
|
def get_available_languages(
|
||||||
race: Race,
|
race: Race,
|
||||||
Loading…
Add table
Add a link
Reference in a new issue