Core System¶
The core module provides the fundamental game engine components including the event system, game loop, state management, and save/load functionality.
Game Engine¶
Core game engine and state management.
This module provides the main Game class that manages the game loop, state, and coordinates all systems.
- class barebones_rpg.core.game.GameState(value)[source]¶
Bases:
EnumPossible game states.
- MENU = 1¶
- PLAYING = 2¶
- COMBAT = 3¶
- DIALOG = 4¶
- PAUSED = 5¶
- GAME_OVER = 6¶
- class barebones_rpg.core.game.GameConfig(title='Barebones RPG', screen_width=800, screen_height=600, fps=60, auto_save=True, debug_mode=False, save_directory='saves', extra=<factory>)[source]¶
Bases:
objectConfiguration for the game.
This can be extended by users to add their own config options.
- __init__(title='Barebones RPG', screen_width=800, screen_height=600, fps=60, auto_save=True, debug_mode=False, save_directory='saves', extra=<factory>)¶
- class barebones_rpg.core.game.Game(config=None)[source]¶
Bases:
objectMain game engine that manages the game loop and state.
The Game class is the central hub that coordinates all game systems. It manages the game loop, state transitions, and provides access to all major systems (entities, world, combat, etc.).
Example
>>> game = Game(GameConfig(title="My RPG")) >>> game.events.subscribe(EventType.GAME_START, lambda e: print("Game started!")) >>> game.start() >>> game.run() # Start the game loop
- __init__(config=None)[source]¶
Initialize the game.
- Parameters:
config (
Optional[GameConfig]) – Game configuration. Uses defaults if not provided.
- register_quest(quest)[source]¶
Register a quest for saving/loading.
Deprecated since version Use:
QuestManager().add_quest(quest)instead. QuestManager is the single source of truth for quests.
- property quests: QuestManager¶
Access the quest manager singleton.
- Returns:
The QuestManager singleton instance
Example
>>> game = Game() >>> quest = Quest(name="Save the Village") >>> game.quests.start_quest(quest.id)
- save_to_file(save_name='default')[source]¶
Save the game to a file.
- Parameters:
save_name (
str) – Name of the save file- Return type:
- Returns:
True if save was successful
- Raises:
SerializationError – If the save data cannot be serialized or written
Example
>>> game.save_to_file("quicksave")
- load_from_file(save_name)[source]¶
Load the game from a file.
- Parameters:
save_name (
str) – Name of the save file to load- Return type:
- Returns:
True if load was successful
- Raises:
SerializationError – If the save file is corrupted or unreadable
Example
>>> game.load_from_file("quicksave")
Event System¶
Event system for the RPG framework.
This module provides a flexible publish-subscribe event system that allows different parts of the game to communicate without tight coupling.
- class barebones_rpg.core.events.EventType(value)[source]¶
Bases:
EnumBuilt-in event types. Users can extend this or use custom strings.
- GAME_START = 1¶
- GAME_END = 2¶
- GAME_PAUSE = 3¶
- GAME_RESUME = 4¶
- COMBAT_START = 5¶
- COMBAT_END = 6¶
- COMBAT_TURN_START = 7¶
- COMBAT_TURN_END = 8¶
- ATTACK = 9¶
- DAMAGE_DEALT = 10¶
- DAMAGE_TAKEN = 11¶
- HEAL = 12¶
- DEATH = 13¶
- ENTITY_CREATED = 14¶
- ENTITY_DESTROYED = 15¶
- STAT_CHANGED = 16¶
- LEVEL_UP = 17¶
- ITEM_ACQUIRED = 18¶
- ITEM_USED = 19¶
- ITEM_DROPPED = 20¶
- ITEM_EQUIPPED = 21¶
- ITEM_UNEQUIPPED = 22¶
- QUEST_STARTED = 23¶
- QUEST_COMPLETED = 24¶
- QUEST_FAILED = 25¶
- OBJECTIVE_COMPLETED = 26¶
- LOCATION_ENTERED = 27¶
- LOCATION_EXITED = 28¶
- DOOR_OPENED = 29¶
- CHEST_OPENED = 30¶
- DIALOG_STARTED = 31¶
- DIALOG_ENDED = 32¶
- CHOICE_MADE = 33¶
- class barebones_rpg.core.events.Event(event_type, data=None)[source]¶
Bases:
objectBase event class that carries event data.
- __init__(event_type, data=None)¶
- class barebones_rpg.core.events.EventManager[source]¶
Bases:
objectCentral event manager for publish-subscribe pattern.
This allows different parts of the game to communicate without tight coupling. Components can subscribe to events and publish events without knowing about each other.
Example
>>> events = EventManager() >>> def on_damage(event): ... print(f"Damage dealt: {event.data['amount']}") >>> events.subscribe(EventType.DAMAGE_DEALT, on_damage) >>> events.publish(Event(EventType.DAMAGE_DEALT, {"amount": 10})) Damage dealt: 10
Save Manager¶
Save manager for handling game save/load operations.
This module provides the SaveManager class for managing game saves, including JSON serialization, file I/O, and directory management.
- class barebones_rpg.core.save_manager.SaveManager(save_directory)[source]¶
Bases:
objectManager for saving and loading game state.
Handles JSON serialization, file I/O, directory creation, and versioning.
Example
>>> manager = SaveManager("saves") >>> save_data = {"player": {"name": "Hero", "level": 5}} >>> manager.save("my_save", save_data) >>> loaded = manager.load("my_save") >>> print(loaded["player"]["name"]) Hero
- SAVE_VERSION = '1.0.0'¶
- __init__(save_directory)[source]¶
Initialize the save manager.
- Parameters:
save_directory (
str) – Directory path for save files (absolute or relative)
- save(save_name, save_data)[source]¶
Save game data to a file.
- Parameters:
- Return type:
- Returns:
True if save was successful
Example
>>> manager.save("quicksave", game_state)
- load(save_name)[source]¶
Load game data from a file.
- Parameters:
save_name (
str) – Name of the save to load- Return type:
- Returns:
Dictionary containing game state, or None if load failed
Example
>>> data = manager.load("quicksave")
- list_saves()[source]¶
List all available save files.
Example
>>> saves = manager.list_saves() >>> print(saves) ['quicksave', 'autosave', 'manual_save_1']
Serialization¶
Serialization utilities for save/load system.
This module provides utilities for serializing game state, including a callback registry for handling function references in saved data.
- class barebones_rpg.core.serialization.CallbackRegistry[source]¶
Bases:
objectRegistry for mapping callback functions to symbolic names.
This enables serialization of callbacks by storing symbolic names instead of function references. When loading, the registry is used to restore the actual function references.
Example
>>> def heal_player(entity, context): ... entity.heal(50) >>> >>> CallbackRegistry.register("heal_player", heal_player) >>> key = CallbackRegistry.encode(heal_player) >>> print(key) 'heal_player' >>> >>> restored = CallbackRegistry.decode(key) >>> restored is heal_player True
- classmethod register(name, callback)[source]¶
Register a callback with a symbolic name.
- Parameters:
- Return type:
Example
>>> CallbackRegistry.register("my_callback", my_function)
- classmethod encode(callback)[source]¶
Encode a callback to its symbolic name.
- Parameters:
callback (
Optional[Callable]) – The callback function to encode- Return type:
- Returns:
Symbolic name or None if callback is None or not registered
Example
>>> key = CallbackRegistry.encode(my_function)
- classmethod decode(name)[source]¶
Decode a symbolic name back to a callback.
- Parameters:
- Return type:
- Returns:
The callback function or None if not found
Example
>>> callback = CallbackRegistry.decode("my_callback")
- classmethod clear()[source]¶
Clear all registered callbacks.
This is primarily for testing purposes.
- Return type:
- classmethod auto_register_from_module(module_name, prefix='')[source]¶
Auto-register all functions from a module.
This is a convenience method for bulk registration.
- Parameters:
- Return type:
- Returns:
Number of functions registered
Example
>>> CallbackRegistry.auto_register_from_module("my_game.callbacks")
- barebones_rpg.core.serialization.serialize_callback(callback)[source]¶
Serialize a callback to a string key.
This is a convenience wrapper around CallbackRegistry.encode().
- barebones_rpg.core.serialization.deserialize_callback(key)[source]¶
Deserialize a callback from a string key.
This is a convenience wrapper around CallbackRegistry.decode().
- barebones_rpg.core.serialization.serialize_callbacks(callbacks)[source]¶
Serialize a list of callbacks.
- barebones_rpg.core.serialization.deserialize_callbacks(keys)[source]¶
Deserialize a list of callbacks.
- barebones_rpg.core.serialization.decode_enum(enum_class, value)[source]¶
Decode a string to an enum value.
- class barebones_rpg.core.serialization.SerializationContext[source]¶
Bases:
objectContext for serialization operations.
This can be used to pass additional context during serialization, such as entity lookups, custom serializers, etc.
Registry System¶
Generic registry base class for framework registries.
This module provides a reusable registry pattern used throughout the framework for managing collections of named objects (AI types, loot items, callbacks, etc.).
- class barebones_rpg.core.registry.Registry[source]¶
Bases:
Generic[T]Base class for type-safe registries.
Provides a common interface for registering and retrieving named objects. Subclasses should define their own _registry class variable to maintain separate storage.
Example
>>> class MyItemRegistry(Registry[Item]): ... _registry: Dict[str, Item] = {} >>> >>> item = Item(name="Sword") >>> MyItemRegistry.register("sword", item) >>> retrieved = MyItemRegistry.get("sword") >>> assert retrieved is item
- classmethod register(name, item)[source]¶
Register an item with a name.
- Parameters:
- Return type:
Example
>>> Registry.register("my_item", my_item)
- classmethod get(name)[source]¶
Get an item by name.
- Parameters:
name (
str) – Name of the item to retrieve- Return type:
- Returns:
The registered item or None if not found
Example
>>> item = Registry.get("my_item")
- classmethod has(name)[source]¶
Check if an item is registered.
Example
>>> if Registry.has("my_item"): ... print("Item exists")
- classmethod get_all_names()[source]¶
Get all registered names.
Example
>>> names = Registry.get_all_names() >>> print(names) ['item1', 'item2', 'item3']
Singleton Pattern¶
Singleton metaclass for framework systems.
This module provides a singleton pattern implementation for framework-managed systems like QuestManager, ensuring only one instance exists.
- class barebones_rpg.core.singleton.Singleton[source]¶
Bases:
typeMetaclass that implements the singleton pattern.
Ensures only one instance of a class exists. Subsequent calls to create an instance return the existing instance.
Example
>>> class MySystem(metaclass=Singleton): ... def __init__(self): ... self.data = [] >>> >>> instance1 = MySystem() >>> instance2 = MySystem() >>> instance1 is instance2 True