Files
DevKage faf2df4b8c feat: Add initial version of behavior_tree and flame_behavior_tree package (#3045)
First pass on adding behavior tree for flame. This PR adds 2 packages:

- behavior_tree: A pure dart implementation of behavior tree.
- flame_behavior_tree: A bridge package that integrates behavior_tree
with flame.

Demo: 


https://github.com/flame-engine/flame/assets/33748002/1d2b00ab-1b6e-406e-9052-a24370c8f1ab
2024-03-10 17:29:15 +01:00

2.1 KiB

flame

This package provides a simple and easy to use behavior tree API in pure dart.


Behavior tree is a very common way of implementing AI behavior in game and robotics. Using this, you can break-down a complex behavior of an in game AI, into multiple smaller nodes.

Features

  • Nodes
    • Composite
      • Sequence: Continues execution until one of the children fails.
      • Selector: Continues execution until one of the children succeeds.
    • Decorator
      • Inverter: Flips the status of the child node.
      • Limiter: Limits the number of ticks for child node.
    • Task
      • Task: Executes a given callback when ticked.
      • AsyncTask: Executes an async callback when ticked.
      • Condition: Checks a condition when ticked.

Getting started

Add this package to your dart project using,

dart pub add behavior_tree

Usage

  • Create a behavior tree.
final treeRoot = Sequence(
  children: [
    Condition(() => isHungry),
    Task(() => goToShop()),
    Task(() => buyFood()),
    Task(() => goToHome()),
    Task(() => eatFood()),
  ]
);
  • Tick the root node to update the tree.
final treeRoot = ...;
treeRoot.tick();