Bonfire Game Engine

repository·master·Indexed 23 days ago

https://github.com/rafaelbarbosatec/bonfire

A Flutter-based game engine optimized for building RPGs, leveraging the Flame Engine for core game loop and rendering. It provides specialized abstractions for RPG development, including the ListenerGameWidget for integrating Flame games into the Flutter widget tree, matrix-based map generation via MatrixMapGenerator and TerrainBuilder, and lighting effects like CircleLightingType and ArcLightingType. The ecosystem includes packages such as bonfire_bloc for state management and bonfire_spine for 2D skeletal animation.

Tokens
3.3K
Snippets
5
Records
26
Agent score
81%

What's inside Bonfire

  1. Build recommendations for Android

    master

    To ensure stability on Android, it is recommended to disable Impeller. Add the following <meta-data> tag inside the <application> tag of your AndroidManifest.xml file.

    <meta-data
        android:name="io.flutter.embedding.android.EnableImpeller"
        android:value="false" />
  2. Configure MatrixLayer for axis inversion

    master

    The MatrixLayer class (used within MatrixMapGenerator.generate) includes an axisInverted property.

    • If axisInverted is false (default): The generator treats the matrix as matrix[x][y].
    • If axisInverted is true: The generator treats the matrix as matrix[y][x]. This is useful for mapping standard 2D arrays where the first index represents the row (Y) and the second represents the column (X).
  3. Render a game using ListenerGameWidget

    master

    The ListenerGameWidget is a StatefulWidget responsible for attaching a Flame Game instance into the Flutter widget tree. It handles the game lifecycle (loading, mounting, resizing), input detection (pointer, keyboard, mouse), and provides mechanisms for loading states, error handling, and UI overlays.

    To use it, provide an instance of your game class to the game parameter. You can also provide a loadingBuilder to show a widget while the game is loading and an errorBuilder to handle errors during the onLoad phase.

    // Inside a State...
    late MyGameClass game;
    
    @override
    void initState() {
      super.initState();
      game = MyGameClass();
    }
    
    // ...
    
    @override
    Widget build(BuildContext context) {
      return ListenerGameWidget(
        game: game,
      );
    }
  4. Configure loading and error builders in ListenerGameWidget

    master

    Use these builders to improve the user experience during the game's initialization phase:

    • loadingBuilder: A GameLoadingWidgetBuilder that returns a widget to be displayed while the game's onLoad and onMount futures are resolving. Defaults to an empty Container().
    • errorBuilder: A GameErrorWidgetBuilder that returns a widget if an error occurs during the onLoad method. If not provided, errors are propagated up the Flutter tree.
  5. Configure input and focus in ListenerGameWidget

    master

    Control how the game receives user input:

    • focusNode: A FocusNode to control the game's focus. If omitted, an internal node is used.
    • autofocus: A bool that determines if the focusNode should request focus once the game is mounted. Defaults to true.
    • mouseCursor: A MouseCursor to be used when hovering over the game area.
    • initialActiveOverlays: A List<String> of overlay keys that should be active immediately upon mounting.
  6. Configure background and text direction in ListenerGameWidget

    master

    Customize the visual environment surrounding the game:

    • backgroundBuilder: A WidgetBuilder that provides a widget tree to be built behind the game elements but in front of the Game.backgroundColor.
    • textDirection: Sets the TextDirection (e.g., TextDirection.ltr) for text elements within the game context.
  7. Configure overlays in ListenerGameWidget

    master

    You can render Flutter widget layers over the game surface using the overlayBuilderMap. To use overlays, your game subclass must be mixed with HasWidgetsOverlay.

    1. Define the overlays in the overlayBuilderMap using a Map<String, OverlayWidgetBuilder<T>> where the key is the overlay name.
    2. Control the visibility of these overlays using the Game.overlays property (e.g., game.overlays.add('PauseMenu')).

    Example of defining a 'PauseMenu' overlay:

    final game = MyGame();
    
    Widget build(BuildContext context) {
      return ListenerGameWidget(
        game: game,
        overlayBuilderMap: {
          'PauseMenu': (ctx, game) {
            return Text('A pause menu');
          },
        },
      );
    }
    
    // To show the menu:
    // game.overlays.add('PauseMenu');
  8. Construct map tiles with TerrainBuilder

    master

    The TerrainBuilder class is used to automatically generate Tile objects for a matrix-based map using a list of MapTerrain definitions. It handles the logic of selecting the correct sprite (center, edge, or corner) based on the surrounding terrain values provided in ItemMatrixProperties.

    To use it, initialize the builder with the tileSize and a list of available terrainList. Then, call build(prop) for each position in your map matrix.

    Key behaviors:

    • Center Tiles: If the tile is identified as a center tile via prop.isCenterTile, it uses the standard MapTerrain sprite.
    • Corner/Edge Tiles: If not a center tile, the builder evaluates the surrounding values (valueTop, valueBottom, valueLeft, valueRight, etc.) to select appropriate corner sprites (e.g., topLeft, bottomRight) or edge sprites (e.g., left, top).
    • Fallback: If no matching terrain is found for the given properties, it returns a default Tile without a sprite or collision data.