Trait moonshine_behavior::Behavior
source · pub trait Behavior: Component + Debug {
// Provided methods
fn allows_next(&self, _next: &Self) -> bool { ... }
fn is_resumable(&self) -> bool { ... }
}Expand description
A Component which represents some state of its Entity.
§Usage
A Behavior is typically implemented as an enum type.
§Example
use bevy::prelude::*;
use moonshine_behavior::prelude::*;
#[derive(Component, Default, Debug, Reflect)]
#[reflect(Component)]
enum Bird {
#[default]
Idle,
Fly,
Sleep,
Chirp,
}
use Bird::*;
impl Behavior for Bird {
fn allows_next(&self, next: &Self) -> bool {
match self {
Idle => matches!(next, Sleep | Fly | Chirp),
Fly => matches!(next, Chirp),
Sleep | Chirp => false,
}
}
fn is_resumable(&self) -> bool {
matches!(self, Idle | Fly)
}
}
fn spawn_bird(mut commands: Commands) {
commands.spawn(BehaviorBundle::<Bird>::default());
}
fn chirp(mut bird: Query<BehaviorMut<Bird>>) {
bird.single_mut().try_start(Chirp);
}
fn is_chirping_while_flying(bird: Query<BehaviorRef<Bird>>) -> bool {
let behavior = bird.single();
matches!(*behavior, Chirp) && matches!(behavior.previous(), Some(Fly))
}Provided Methods§
sourcefn allows_next(&self, _next: &Self) -> bool
fn allows_next(&self, _next: &Self) -> bool
Returns true if some next Behavior is allowed to be started after this one.
By default, any behavior is allowed to start after any other behavior.
sourcefn is_resumable(&self) -> bool
fn is_resumable(&self) -> bool
Returns true if this Behavior may be resumed after it has been paused.
By default, all behaviors are resumable.
Object Safety§
This trait is not object safe.