Goal-oriented Action Planning (GOAP) with Bevy integration
MIT License
AKA DOGOAP - GOAP implemented in data-oriented way to facilitate dynamically setting up states/actions/goals rather than only at compile-time
Includes bevy_dogoap which provides a neat Bevy integration of the dogoap library
dogoap
docs - Standalone library for creation actions, states and goals to be used with the provided plannerbevy_dogoap
docs - Integration of the dogoap
library into BevyIn this example, we create a IsHungry DatumComponent
that signals if the entity is hungry or not, and a EatAction ActionComponent
.
#[derive(Component, Reflect, Clone, DatumComponent)]
struct IsHungry(bool);
#[derive(Component, Reflect, Clone, Default, ActionComponent)]
struct EatAction;
fn setup(mut commands: Commands) {
// We want our final outcome to be that IsHungry is set to false
let goal = Goal::from_reqs(&[IsHungry::is(false)]);
// Here we declare that the result of perfoming EatAction is that IsHungry gets set to false
let eat_action = EatAction::new().add_mutator(IsHungry::set(false));
// Creating the planner with everything together gives us Components we can use with Bevy
let (planner, components) = create_planner!({
actions: [(EatAction, eat_action)],
state: [IsHungry(true)],
goals: [goal],
});
// Add `planner` + `components` to your Entity, or spawn a new one
commands.spawn((Name::new("Planner"), planner, components));
}
// System for handling EatAction
fn handle_eat_action(
mut commands: Commands,
mut query: Query<(Entity, &EatAction, &mut IsHungry)>,
) {
for (entity, _eat_action, mut is_hungry) in query.iter_mut() {
is_hungry.0 = false;
commands.entity(entity).remove::<EatAction>();
}
}
Once you run this code, the planner will automatically figure out that the entity needs to execute the EatAction in order to set IsHungry to false, and your defined System handles the actual logic of the action.
More involved examples can be found here: bevy_dogoap examples
MIT 2024 - Victor Bjelkholm