moonshine_behavior/plugin.rs
1use std::marker::PhantomData;
2
3use bevy_app::prelude::*;
4use moonshine_util::reflect::Registerable;
5
6use crate::{Behavior, Memory, Transition, TransitionQueue};
7
8/// A [`Plugin`] for any [`Behavior`] type.
9///
10/// This plugin must be added to the [`App`] for behavior [`Transitions`](Transition) to work correctly.
11/// You must also add the [`transition`](crate::transition::transition) system separately somewhere in your schedule.
12///
13/// # Example
14/// ```rust
15/// use bevy::prelude::*;
16/// use moonshine_behavior::prelude::*;
17///
18/// #[derive(Component, Debug, Reflect)]
19/// #[reflect(Component)]
20/// struct B;
21///
22/// impl Behavior for B {}
23///
24/// App::new()
25/// .add_plugins(BehaviorPlugin::<B>::default())
26/// .add_systems(Update, transition::<B>);
27///
28/// // ...
29/// ```
30pub struct BehaviorPlugin<T: Behavior>(PhantomData<T>);
31
32impl<T: Behavior> Default for BehaviorPlugin<T> {
33 fn default() -> Self {
34 Self(PhantomData)
35 }
36}
37
38impl<T: Behavior + Registerable> Plugin for BehaviorPlugin<T> {
39 fn build(&self, app: &mut App) {
40 app.register_type::<Transition<T>>()
41 .register_type::<Memory<T>>()
42 .register_type::<TransitionQueue<T>>()
43 .register_required_components::<T, Transition<T>>();
44 }
45}