Bonfire Game Engine
repository·master·Indexed 23 days ago
https://github.com/rafaelbarbosatec/bonfireA 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.
What's inside Bonfire
- Bonfire is a game engine designed for building RPGs and similar game types, powered by the Flame Engine. It is built on top of Flutter and provides specialized abstractions for game development perspectives.
Build recommendations for Android
masterTo ensure stability on Android, it is recommended to disable Impeller. Add the following
<meta-data>tag inside the<application>tag of yourAndroidManifest.xmlfile.<meta-data android:name="io.flutter.embedding.android.EnableImpeller" android:value="false" />Build recommendations for Web
masterWhen building your Bonfire game for the Web, use the
canvaskitrenderer to ensure optimal performance and compatibility.flutter build web --web-renderer=canvaskitExplore Bonfire game examples
masterTheawesomedirectory contains various game examples built with the Bonfire framework. You can use these projects to learn how to implement specific game mechanics, UI, and structures in your own Bonfire projects by exploring their source code.Configure MatrixLayer for axis inversion
masterThe
MatrixLayerclass (used withinMatrixMapGenerator.generate) includes anaxisInvertedproperty.- If
axisInvertedisfalse(default): The generator treats the matrix asmatrix[x][y]. - If
axisInvertedistrue: The generator treats the matrix asmatrix[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).
- If
Render a game using ListenerGameWidget
masterThe
ListenerGameWidgetis aStatefulWidgetresponsible for attaching a FlameGameinstance 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
gameparameter. You can also provide aloadingBuilderto show a widget while the game is loading and anerrorBuilderto handle errors during theonLoadphase.// Inside a State... late MyGameClass game; @override void initState() { super.initState(); game = MyGameClass(); } // ... @override Widget build(BuildContext context) { return ListenerGameWidget( game: game, ); }Configure loading and error builders in ListenerGameWidget
masterUse these builders to improve the user experience during the game's initialization phase:
loadingBuilder: AGameLoadingWidgetBuilderthat returns a widget to be displayed while the game'sonLoadandonMountfutures are resolving. Defaults to an emptyContainer().errorBuilder: AGameErrorWidgetBuilderthat returns a widget if an error occurs during theonLoadmethod. If not provided, errors are propagated up the Flutter tree.
Configure input and focus in ListenerGameWidget
masterControl how the game receives user input:
focusNode: AFocusNodeto control the game's focus. If omitted, an internal node is used.autofocus: Aboolthat determines if thefocusNodeshould request focus once the game is mounted. Defaults totrue.mouseCursor: AMouseCursorto be used when hovering over the game area.initialActiveOverlays: AList<String>of overlay keys that should be active immediately upon mounting.
Configure background and text direction in ListenerGameWidget
masterCustomize the visual environment surrounding the game:
backgroundBuilder: AWidgetBuilderthat provides a widget tree to be built behind the game elements but in front of theGame.backgroundColor.textDirection: Sets theTextDirection(e.g.,TextDirection.ltr) for text elements within the game context.
Useful Bonfire ecosystem packages
masterThe following packages extend Bonfire's functionality:
bonfire_bloc: Integration with the BLoC pattern for state management.bonfire_spine: Integration with Spine for 2D skeletal animation.
Configure overlays in ListenerGameWidget
masterYou can render Flutter widget layers over the game surface using the
overlayBuilderMap. To use overlays, your game subclass must be mixed withHasWidgetsOverlay.- Define the overlays in the
overlayBuilderMapusing aMap<String, OverlayWidgetBuilder<T>>where the key is the overlay name. - Control the visibility of these overlays using the
Game.overlaysproperty (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');- Define the overlays in the
Construct map tiles with TerrainBuilder
masterThe
TerrainBuilderclass is used to automatically generateTileobjects for a matrix-based map using a list ofMapTerraindefinitions. It handles the logic of selecting the correct sprite (center, edge, or corner) based on the surrounding terrain values provided inItemMatrixProperties.To use it, initialize the builder with the
tileSizeand a list of availableterrainList. Then, callbuild(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 standardMapTerrainsprite. - 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
Tilewithout a sprite or collision data.
- Center Tiles: If the tile is identified as a center tile via