bevy_yoetz/lib.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
//! Yoetz - A Rule Based AI Plugin for the Bevy Game Engine
//!
//! Yoetz ("advisor" in Hebrew) is a rule based AI plugin for Bevy, structured around the following
//! tenets:
//!
//! 1. There is no need to build special data structures for calculating the transitions and the
//! scores when representing these mechanisms as code inside user systems is both more flexible
//! and simpler to use.
//! 2. The systems that check the rules need to be able to pass data to the systems act upon the
//! decisions.
//! 3. Enacting the decision should be done with the ECS. If the action that the rules mechanism
//! decided to do is reflected by components, it becomes easy to write different systems that
//! perform the various possible actions.
//!
//! # Quick Start
//!
//! Define the various actions the AI can do with an enum that derives [`YoetzSuggestion`], and add
//! a [`YoetzPlugin`] for it:
//!
//! ```no_run
//! # use bevy::prelude::*;
//! # use bevy_yoetz::prelude::*;
//! # let mut app = App::new();
//! app.add_plugins(YoetzPlugin::<AiBehavior>::new(FixedUpdate));
//!
//! #[derive(YoetzSuggestion)]
//! enum AiBehavior {
//! DoNothing,
//! Attack {
//! #[yoetz(key)]
//! target_to_attack: Entity,
//! },
//! }
//! ```
//!
//! Give [`YoetzAdvisor`](crate::advisor::YoetzAdvisor) to the AI controlled entities:
//!
//! ```no_run
//! # use bevy::prelude::*;
//! # use bevy_yoetz::prelude::*;
//! # let mut commands: Commands = panic!();
//! # #[derive(YoetzSuggestion)] enum AiBehavior { VariantSoThatItWontBeEmpty }
//! # #[derive(Component)] struct OtherComponentsForThisEntity;
//! commands.spawn((
//! // The argument to `new` is a bonus for maintaining the current action.
//! YoetzAdvisor::<AiBehavior>::new(2.0),
//! OtherComponentsForThisEntity,
//! ));
//! ```
//!
//! Add under [`YoetzSystemSet::Suggest`] systems that check for the various rules and generate
//! suggestions with scores:
//!
//! ```no_run
//! # use bevy::prelude::*;
//! # use bevy_yoetz::prelude::*;
//! # #[derive(YoetzSuggestion)]
//! # enum AiBehavior {
//! # DoNothing,
//! # Attack {
//! # #[yoetz(key)]
//! # target_to_attack: Entity,
//! # },
//! # }
//! # let mut app = App::new();
//! app.add_systems(
//! FixedUpdate,
//! (
//! make_ai_entities_do_nothing,
//! give_targets_to_ai_entities,
//! )
//! .in_set(YoetzSystemSet::Suggest),
//! );
//!
//! fn make_ai_entities_do_nothing(mut query: Query<&mut YoetzAdvisor<AiBehavior>>) {
//! for mut advisor in query.iter_mut() {
//! // A constant suggestion, so that if nothing else beats this score the entity will
//! // still have a behavior to execute.
//! advisor.suggest(0.0, AiBehavior::DoNothing);
//! }
//! }
//!
//! # #[derive(Component)] struct Attackable;
//! fn give_targets_to_ai_entities(
//! mut query: Query<(&mut YoetzAdvisor<AiBehavior>, &GlobalTransform)>,
//! targets_query: Query<(Entity, &GlobalTransform), With<Attackable>>,
//! ) {
//! for (mut advisor, ai_transform) in query.iter_mut() {
//! for (target_entity, target_transorm) in targets_query.iter() {
//! let distance = ai_transform.translation().distance(target_transorm.translation());
//! advisor.suggest(
//! // The closer the target, the more desirable it is to attack it. If the
//! // distance is more than 10, the score will get below 0 and the DoNothing
//! // suggestion will be used instead.
//! 10.0 - distance,
//! AiBehavior::Attack {
//! target_to_attack: target_entity,
//! },
//! );
//! }
//! }
//! }
//! ```
//!
//! Add under [`YoetzSystemSet::Act`] systems that performs these actions. These systems use
//! components that are generated by the [`YoetzSuggestion`](bevy_yoetz_macros::YoetzSuggestion)
//! macro and are added and removed automatically by [`YoetzPlugin`]:
//!
//! ```no_run
//! # use bevy::prelude::*;
//! # use bevy_yoetz::prelude::*;
//! # #[derive(YoetzSuggestion)]
//! # enum AiBehavior {
//! # DoNothing,
//! # Attack {
//! # #[yoetz(key)]
//! # target_to_attack: Entity,
//! # },
//! # }
//! # let mut app = App::new();
//! app.add_systems(
//! FixedUpdate,
//! (
//! perform_do_nothing,
//! perform_attack,
//! )
//! .in_set(YoetzSystemSet::Act),
//! );
//!
//! fn perform_do_nothing(query: Query<&AiBehaviorDoNothing>) {
//! for _do_nothing in query.iter() {
//! // Do... nothing. This whole function is kind of pointless.
//! }
//! }
//!
//! # #[derive(Component)] struct Attacker;
//! # impl Attacker { fn attack(&mut self, _target: Entity) {} }
//! fn perform_attack(mut query: Query<(&mut Attacker, &AiBehaviorAttack)>) {
//! for (mut attacker, attack_behavior) in query.iter_mut() {
//! attacker.attack(attack_behavior.target_to_attack);
//! }
//! }
mod advisor;
use std::marker::PhantomData;
use bevy::ecs::schedule::{InternedScheduleLabel, ScheduleLabel};
use bevy::prelude::*;
use self::advisor::update_advisor;
use self::prelude::YoetzSuggestion;
pub use bevy;
pub mod prelude {
#[doc(inline)]
pub use crate::advisor::{YoetzAdvisor, YoetzSuggestion};
#[doc(inline)]
pub use crate::{YoetzPlugin, YoetzSystemSet};
}
/// Add systems for processing a [`YoetzSuggestion`].
pub struct YoetzPlugin<S: YoetzSuggestion> {
schedule: InternedScheduleLabel,
_phantom: PhantomData<fn(S)>,
}
impl<S: YoetzSuggestion> YoetzPlugin<S> {
/// Create a `YoetzPlugin` that cranks the [`YoetzAdvisor`](crate::advisor::YoetzAdvisor) in
/// the given schedule.
///
/// The update will be done between [`YoetzSystemSet::Suggest`] and [`YoetzSystemSet::Act`] in
/// that schedule.
pub fn new(schedule: impl ScheduleLabel) -> Self {
Self {
schedule: schedule.intern(),
_phantom: PhantomData,
}
}
}
impl<S: 'static + YoetzSuggestion> Plugin for YoetzPlugin<S> {
fn build(&self, app: &mut App) {
app.configure_sets(
self.schedule,
(
YoetzSystemSet::Suggest,
YoetzInternalSystemSet::Think,
YoetzSystemSet::Act,
)
.chain(),
);
app.add_systems(
self.schedule,
update_advisor::<S>.in_set(YoetzInternalSystemSet::Think),
);
}
}
/// System sets to put suggestion systems and action systems in.
#[derive(Debug, Clone, PartialEq, Eq, Hash, SystemSet)]
pub enum YoetzSystemSet {
/// Systems that suggest behaviors (by calling
/// [`YoetzAdvisor::suggest`](advisor::YoetzAdvisor::suggest)) should go in this set.
Suggest,
/// Systems that enact behaviors (by querying for the behavior structs generated by the
/// [`YoetzSuggestion`](bevy_yoetz_macros::YoetzSuggestion) macro) should go in this set.
Act,
}
#[doc(hidden)]
#[derive(Debug, Clone, PartialEq, Eq, Hash, SystemSet)]
pub enum YoetzInternalSystemSet {
Think,
}