This repository has been archived by the owner on Jul 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathplugin.rs
165 lines (157 loc) · 4.98 KB
/
plugin.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
use crate::events::{delay_spawn_events, ReadySpawnEvent, SpawnEvent};
use crate::spawner::{Spawner, Spawners};
use bevy::prelude::*;
#[allow(clippy::needless_doctest_main)]
/// A plugin that enables spawning objects of type `T` while providing data of type `D`.
/// Using multiple combinations of `T` and `D` requires adding multiple instances of this plugin to an [`App`].
/// If your spawn systems don't require any data, simply pass `()` as the `D` type.
///
/// # Example
/// ```rust,ignore
/// use spew::prelude::*;
/// use bevy::prelude::*;
///
/// #[derive(Debug, Eq, PartialEq)]
/// enum Object {
/// Cube
/// }
///
/// fn main() {
/// App::new()
/// .add_plugins(DefaultPlugins)
/// .add_plugins(SpewPlugin::<Object, Transform>::default())
/// .run();
/// }
/// ```
pub struct SpewPlugin<T, D = ()>
where
T: Eq + Send + Sync + 'static,
D: Send + Sync + 'static,
{
_spawner_enum_type: std::marker::PhantomData<T>,
_data_type: std::marker::PhantomData<D>,
}
impl<T, D> Default for SpewPlugin<T, D>
where
T: Eq + Send + Sync + 'static,
D: Send + Sync + 'static,
{
fn default() -> Self {
Self {
_spawner_enum_type: std::marker::PhantomData,
_data_type: std::marker::PhantomData,
}
}
}
impl<T, D> Plugin for SpewPlugin<T, D>
where
T: Eq + Send + Sync + 'static,
D: Send + Sync + 'static,
{
fn build(&self, app: &mut App) {
app.add_event::<SpawnEvent<T, D>>()
.add_event::<ReadySpawnEvent<T, D>>()
.add_systems(Update, delay_spawn_events::<T, D>.in_set(SpewSystemSet));
}
fn is_unique(&self) -> bool {
false
}
}
/// The SystemSet that contains all spew systems.
#[derive(Debug, Hash, PartialEq, Eq, Clone, SystemSet)]
pub struct SpewSystemSet;
/// A trait that allows adding spawners to an [`App`].
/// Spawners are tuples of an object and a spawning function, e.g. `(Object::Cube, spawn_cube)`.
/// A spawning function has the same signature as a bevy system function, where user provided data is passed as an `In<D>` parameter in the first position.
///
/// The spawner's combination of object enum and user data must have been registered with an own [`SpewPlugin`] beforehand.
pub trait SpewApp {
/// Add a single spawner to the app.
///
/// # Example
/// ```rust,ignore
/// use spew::prelude::*;
/// use bevy::prelude::*;
///
/// #[derive(Debug, Eq, PartialEq)]
/// enum Object {
/// Cube
/// }
///
/// fn main() {
/// App::new()
/// .add_plugins(DefaultPlugins)
/// .add_plugins(SpewPlugin::<Object, Transform>::default())
/// .add_spawner((Object::Cube, spawn_cube))
/// .run();
/// }
///
/// fn spawn_cube(In(transform): In<Transform>, mut commands: Commands) {
/// info!("Spawning cube at {}", transform.translation);
/// commands.spawn((Name::new("Cube"), transform));
/// }
/// ```
fn add_spawner<T, D>(&mut self, spawner: T) -> &mut App
where
T: Spawner<D>;
/// Add multiple spawners to the app by providing them in a tuple.
///
/// # Example
/// ```rust,ignore
/// use spew::prelude::*;
/// use bevy::prelude::*;
///
/// #[derive(Debug, Eq, PartialEq)]
/// enum Object {
/// Cube,
/// Triangle,
/// Sphere,
/// }
///
/// fn main() {
/// App::new()
/// .add_plugins(DefaultPlugins)
/// .add_plugins(SpewPlugin::<Object, Transform>::default())
/// .add_spawners((
/// (Object::Cube, spawn_cube),
/// (Object::Triangle, spawn_triangle),
/// (Object::Sphere, spawn_sphere),
/// ))
/// .run();
/// }
///
/// fn spawn_cube(In(transform): In<Transform>, mut commands: Commands) {
/// info!("Spawning cube at {}", transform.translation);
/// commands.spawn((Name::new("Cube"), transform));
/// }
///
/// fn spawn_triangle(In(transform): In<Transform>, mut commands: Commands) {
/// info!("Spawning triangle at {}", transform.translation);
/// commands.spawn((Name::new("Triangle"), transform));
/// }
///
/// fn spawn_sphere(In(transform): In<Transform>, mut commands: Commands) {
/// info!("Spawning sphere at {}", transform.translation);
/// commands.spawn((Name::new("Sphere"), transform));
/// }
/// ```
fn add_spawners<T, D>(&mut self, spawners: T) -> &mut App
where
T: Spawners<D>;
}
impl SpewApp for App {
fn add_spawner<T, D>(&mut self, spawner: T) -> &mut App
where
T: Spawner<D>,
{
spawner.add_to_app(self);
self
}
fn add_spawners<T, D>(&mut self, spawners: T) -> &mut App
where
T: Spawners<D>,
{
spawners.add_to_app(self);
self
}
}