flame_behavior_tree 0.2.0-dev.1
flame_behavior_tree: ^0.2.0-dev.1 copied to clipboard
A bridge package that integrates behavior_tree package with flame.
This is a bridge package that integrates the behavior_tree dart package with Flame engine.
Features #
This package provides a HasBehaviorTree mixin for Flame Components. It can be added to any
Component and it takes care of ticking the behavior tree along with the component's update.
The behavior tree itself comes from the behavior_tree package, which is re-exported from here. Read its README to learn about the available nodes, how running nodes work and how to share data using the blackboard.
Getting started #
Add this package to your Flutter project using:
flutter pub add flame_behavior_tree
Usage #
Add the HasBehaviorTree mixin to the component that wants to follow a certain AI behavior, and
assign a BehaviorTree to behaviorTree:
class Enemy extends PositionComponent with HasBehaviorTree {
@override
Future<void> onLoad() async {
behaviorTree = BehaviorTree(
Selector([
Sequence([
Condition((context) => canSeePlayer()),
Task((context) => chasePlayer(context.dt)),
]),
Task((context) => patrol(context.dt)),
]),
owner: this,
);
}
}
- The tree is ticked on every update of the component.
context.dtin the nodes is the time since the previous tick. - Passing
owner: thislets nodes get the component withcontext.owner<Enemy>(). - The blackboard of the tree is available as
blackboardon the component, so the rest of your game can read and write the values that the AI works with. - The running nodes of the tree are aborted when the component is removed, or when you assign a new tree.
To tick the tree less often, increase tickInterval. This can be done at any time. The tree is
ticked on the first update, and then whenever tickInterval seconds have passed. The dt that
the nodes see is the time since the previous tick of the tree.
class Enemy extends PositionComponent with HasBehaviorTree {
Enemy() {
tickInterval = 0.5; // Think twice per second.
}
}
Flame nodes #
Besides the nodes of behavior_tree, this package has two nodes that work with the effects of
Flame:
PlayEffect(builder)adds an effect to the owner of the tree (or to atarget), is running while it plays and succeeds when it completes. The effect is removed if the node is aborted.MoveTo(destination, ...)moves the owner (or atarget) to a position, either in a givendurationor at a givenspeed.
Sequence([
MoveTo((context) => context.get(destination), speed: 100),
PlayEffect(
(context) => ColorEffect(
Colors.red,
EffectController(duration: 0.3, alternate: true),
),
),
])
Documentation and examples #
The documentation
explains how behavior trees work, with small interactive examples for the nodes and for
HasBehaviorTree. They can also be found in the
examples app.
See the example for an agent that walks in and out of a house.
Migrating from 0.1.x / 0.2.0-dev #
- Instead of
treeRoot = root, assignbehaviorTree = BehaviorTree(root, owner: this). HasBehaviorTreeis not generic anymore.- The blackboard now belongs to the
BehaviorTree. It is always available, uses typed keys and is not assigned on the component. Pass an existing one toBehaviorTree(blackboard: ...)if you need to. - Setting
tickIntervalafter the component was loaded now works, and the first tick happens immediately instead of after the first interval.
The nodes themselves changed too, see the migration guide in the behavior_tree README.