scene.py

Scene Management System - Game State and UI Container

LOCATION: lunaengine/core/scene.py

DESCRIPTION:
Provides the foundation for organizing game content into manageable states.
Each scene represents a distinct game state (menu, gameplay, pause screen)
with its own logic, rendering, and UI elements. Supports seamless transitions
between scenes with proper lifecycle management.

KEY FEATURES:
- Scene lifecycle hooks (on_enter/on_exit)
- UI element management with unique identifiers
- Type-based element filtering and retrieval
- Scene transition state tracking

LIBRARIES USED:
- abc: Abstract base class for scene interface
- typing: Type hints for collections and optional values
- TYPE_CHECKING: For circular import resolution

USAGE PATTERN:
1. Inherit from Scene class
2. Implement abstract methods (on_enter, on_exit, update, render)
3. Add UI elements in on_enter method
4. Manage scene-specific logic in update method

class Scene

Description

Base class for all game scenes.

Provides lifecycle methods and UI element management.
All custom scenes should inherit from this class.

Attributes:
   ui_elements (List[UIElement]): List of UI elements in this scene
   _initialized (bool): Whether the scene has been initialized
   engine (LunaEngine): Reference to the game engine

Attributes
name: str = ''
WIDTH: int = 0
HEIGHT: int = 0
Methods
def __init__(self: Any, engine: 'LunaEngine', *args: tuple, **kwargs: dict) -> Any
Initialize a new scene with empty UI elements list.

Args:
   engine (LunaEngine): Reference to the game engine
def update_window_size(self: Any) -> Any
No documentation
def _add_event_to_handler(self: Any, element: UIElement) -> Any
No documentation
def _update_on_change_child(self: Any, element: UIElement) -> Any
No documentation
def _ui_element_list(self: Any, event_type: ElementsListEvents, element: UIElement, index: Optional[int] = None) -> Any
No documentation
def on_enter(self: Any, previous_scene: Optional[str] = None) -> None
Called when the scene becomes active.

Use this to initialize resources, create UI elements, or reset game state.

Args:
   previous_scene (str, optional): Name of the previous scene
def on_exit(self: Any, next_scene: Optional[str] = None) -> None
Called when the scene is being replaced.

Use this to clean up resources, save game state, or perform transitions.

Args:
   next_scene (str, optional): Name of the next scene
def update(self: Any, dt: float) -> None
Update scene logic.
def _update(self: Any, dt: float) -> Any
Update scene logic with profiling.
def render(self: Any, renderer: Renderer | OpenGLRenderer) -> None
Render the scene.

Called every frame to draw the scene content.

Args:
   renderer: The renderer to use for drawing operations
def get_scene_performance_stats(self: Any) -> Dict[str, float]
Get performance statistics for this scene
def add_ui_element(self: Any, ui_element: UIElement) -> None
Add a UI element to the scene.

Args:
   ui_element (UIElement): The UI element to add to the scene
def remove_ui_element(self: Any, ui_element: UIElement) -> bool
Remove a UI element from the scene.

Args:
   ui_element (UIElement): The UI element to remove
   
Returns:
   bool: True if element was found and removed, False otherwise
def get_ui_element_by_id(self: Any, element_id: str) -> Optional[UIElement]
Get UI element by its unique ID.

Args:
   element_id (str): The unique ID of the element to find
   
Returns:
   UIElement: The found UI element or None if not found
def get_ui_elements_by_type(self: Any, element_type: type) -> List[UIElement]
Get all UI elements of a specific type.

Args:
   element_type (type): The class type to filter by (e.g., Button, Label)
   
Returns:
   List[UIElement]: List of UI elements matching the specified type
def get_ui_elements_by_group(self: Any, group: str) -> List[UIElement]
Get all UI elements in the scene.

Returns:
   List[UIElement]: All UI elements in the scene
def toggle_element_group(self: Any, group: str, visible: bool) -> None
Toggle the visibility of UI elements in a specific group.

Args:
   group (str): The group to toggle
def clear_element_group(self: Any, group: str) -> None
Remove all UI elements in a specific group from the scene.

Args:
   group (str): The group to clear
def clear_element_type(self: Any, element_type: type) -> None
Remove all UI elements of a specific type from the scene.

Args:
   element_type (type): The class type to filter by (e.g., Button, Label)
def get_all_ui_elements(self: Any) -> List[UIElement]
Get all UI elements in the scene.

Returns:
   List[UIElement]: All UI elements in the scene
def has_element_by_id(self: Any, element_id: str) -> bool
Check if a UI element with the given ID exists in the scene.

Args:
   element_id (str): The unique ID of the element to check
Returns:
   bool: True if element exists, False otherwise
def has_element(self: Any, element: UIElement) -> bool
Check if a specific UI element exists in the scene.

Args:
   element (UIElement): The UI element to check
Returns:
   bool: True if element exists, False otherwise
def clear_ui_elements(self: Any) -> None
Remove all UI elements from the scene.
Back to Core Module