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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
//local shortcuts
use crate::prelude::*;
//third-party shortcuts
use bevy::prelude::*;
use bevy::ecs::system::EntityCommands;
use bevy::ecs::world::Command;
//standard shortcuts
//-------------------------------------------------------------------------------------------------------------------
/// Extends `App` with reactivity helpers.
pub trait ReactAppExt
{
/// Adds a [`WorldReactor`] to the app with *only* starting triggers.
///
/// Equivalent to:
/*
```rust
app.react(|rc| rc.on_persistent(triggers, reactor));
```
*/
fn add_reactor<M>(
&mut self,
triggers: impl ReactionTriggerBundle,
reactor: impl IntoSystem<(), (), M> + Send + Sync + 'static
) -> &mut Self;
/// Adds a [`WorldReactor`] to the app.
///
/// The reactor can be accessed with the [`Reactor`] system param.
fn add_world_reactor<R>(&mut self, reactor: R) -> &mut Self
where
R: WorldReactor<StartingTriggers = ()>;
/// Adds a [`WorldReactor`] to the app with starting triggers.
///
/// The reactor be accessed with the [`Reactor`] system param.
fn add_world_reactor_with<R: WorldReactor>(&mut self, reactor: R, triggers: R::StartingTriggers) -> &mut Self;
/// Adds an [`EntityWorldReactor`] to the app.
///
/// The reactor can be accessed with the [`EntityReactor`] system param.
fn add_entity_reactor<R: EntityWorldReactor>(&mut self, reactor: R) -> &mut Self;
/// Provides access to [`ReactCommands`].
fn react<T>(&mut self, callback: impl FnOnce(&mut ReactCommands) -> T) -> &mut Self;
}
impl ReactAppExt for App
{
fn add_reactor<M>(
&mut self,
triggers: impl ReactionTriggerBundle,
reactor: impl IntoSystem<(), (), M> + Send + Sync + 'static
) -> &mut Self
{
// Make sure app is ready to use ReactCommands.
if !self.world().contains_resource::<ReactCache>()
{
self.init_resource::<ReactCache>();
}
self.setup_auto_despawn();
// Add reactor.
self.react(|rc| rc.on_persistent(triggers, reactor))
}
fn add_world_reactor<R>(&mut self, reactor: R) -> &mut Self
where
R: WorldReactor<StartingTriggers = ()>
{
if self.world().contains_resource::<WorldReactorRes<R>>()
{
panic!("duplicate world reactors of type {:?} are not allowed", std::any::type_name::<R>());
}
let sys_command = self.world_mut().spawn_system_command_from(reactor.reactor());
self.world_mut().insert_resource(WorldReactorRes::<R>::new(sys_command));
self
}
fn add_world_reactor_with<R: WorldReactor>(&mut self, reactor: R, triggers: R::StartingTriggers) -> &mut Self
{
if self.world().contains_resource::<WorldReactorRes<R>>()
{
panic!("duplicate world reactors of type {:?} are not allowed", std::any::type_name::<R>());
}
let sys_command = self.world_mut().spawn_system_command_from(reactor.reactor());
self.world_mut().insert_resource(WorldReactorRes::<R>::new(sys_command));
// Make sure app is ready to use ReactCommands.
if !self.world().contains_resource::<ReactCache>()
{
self.init_resource::<ReactCache>();
}
self.setup_auto_despawn();
// Add starting triggers.
self.world_mut().syscall_once((),
move |mut c: Commands, reactor: Reactor<R>|
{
reactor.add_starting_triggers(&mut c, triggers);
}
);
self
}
fn add_entity_reactor<R: EntityWorldReactor>(&mut self, reactor: R) -> &mut Self
{
if self.world().contains_resource::<EntityWorldReactorRes<R>>()
{
panic!("duplicate entity world reactors of type {:?} are not allowed", std::any::type_name::<R>());
}
let sys_command = self.world_mut().spawn_system_command_from(reactor.reactor());
self.world_mut().insert_resource(EntityWorldReactorRes::<R>::new(sys_command));
self
}
fn react<T>(&mut self, callback: impl FnOnce(&mut ReactCommands) -> T) -> &mut Self
{
// Ignore returned value.
let _ = self.world_mut().react(callback);
self
}
}
//-------------------------------------------------------------------------------------------------------------------
/// Extends `World` with reactivity helpers.
pub trait ReactWorldExt
{
/// Schedules a [`SystemCommand`] to be spawned.
///
/// Systems are not initialized until they are first run.
///
/// Returns the system command id that will eventually reference the spawned system.
/// To run the system, schedule it with `commands.queue(system_command)`.
fn spawn_system_command<S, M>(&mut self, system: S) -> SystemCommand
where
S: IntoSystem<(), (), M> + Send + Sync + 'static;
/// Schedules a [`SystemCommand`] to be spawned from a pre-defined callback.
///
/// Returns the system command id that will eventually reference the spawned system.
/// To run the system, schedule it with `commands.queue(system_command)`.
fn spawn_system_command_from(&mut self, callback: SystemCommandCallback) -> SystemCommand;
/// Provides access to [`ReactCommands`].
fn react<T>(&mut self, callback: impl FnOnce(&mut ReactCommands) -> T) -> T;
/// Schedules a system event targeting a given [`SystemCommand`].
///
/// The target system can consume the event with the [`SystemEvent`] system parameter.
///
/// If scheduled from user-land, this will cause a [`reaction_tree()`] to execute, otherwise it will be
/// processed within the already-running reaction tree.
fn send_system_event<T: Send + Sync + 'static>(&mut self, command: SystemCommand, event: T);
/// Sends a broadcasted event.
/// - Reactors can listen for the event with the [`broadcast()`] trigger.
/// - Reactors can read the event with the [`BroadcastEvent`] system parameter.
fn broadcast<E: Send + Sync + 'static>(&mut self, event: E);
/// Sends an entity-targeted event.
/// - Reactors can listen for the event with the [`entity_event()`] trigger.
/// - Reactors can read the event with the [`EntityEvent`] system parameter.
fn entity_event<E: Send + Sync + 'static>(&mut self, entity: Entity, event: E);
}
impl ReactWorldExt for World
{
fn spawn_system_command<S, M>(&mut self, system: S) -> SystemCommand
where
S: IntoSystem<(), (), M> + Send + Sync + 'static
{
self.spawn_system_command_from(SystemCommandCallback::new(system))
}
fn spawn_system_command_from(&mut self, callback: SystemCommandCallback) -> SystemCommand
{
SystemCommand(self.spawn(SystemCommandStorage::new(callback)).id())
}
fn react<T>(&mut self, callback: impl FnOnce(&mut ReactCommands) -> T) -> T
{
let mut c = self.commands();
let mut rc = c.react();
let result = (callback)(&mut rc);
self.flush();
result
}
fn send_system_event<T: Send + Sync + 'static>(&mut self, command: SystemCommand, event: T)
{
let data_entity = self.spawn(SystemEventData::new(event)).id();
EventCommand{ system: command, data_entity }.apply(self);
}
fn broadcast<E: Send + Sync + 'static>(&mut self, event: E)
{
self.syscall(event, ReactCache::schedule_broadcast_reaction::<E>);
}
fn entity_event<E: Send + Sync + 'static>(&mut self, entity: Entity, event: E)
{
self.syscall((entity, event), ReactCache::schedule_entity_event_reaction::<E>);
}
}
//-------------------------------------------------------------------------------------------------------------------
/// Extends `Commands` with reactivity helpers.
pub trait ReactCommandsExt
{
/// Obtains a [`ReactCommands`] instance.
fn react(&mut self) -> ReactCommands<'_, '_>;
/// Schedules a [`SystemCommand`] to be spawned.
///
/// Systems are not initialized until they are first run.
///
/// Returns the system command id that will eventually reference the spawned system.
/// To run the system, schedule it with `commands.queue(system_command)`.
fn spawn_system_command<S, M>(&mut self, system: S) -> SystemCommand
where
S: IntoSystem<(), (), M> + Send + Sync + 'static;
/// Schedules a [`SystemCommand`] to be spawned from a pre-defined callback.
///
/// Returns the system command id that will eventually reference the spawned system.
/// To run the system, schedule it with `commands.queue(system_command)`.
fn spawn_system_command_from(&mut self, callback: SystemCommandCallback) -> SystemCommand;
/// Schedules a system event targeting a given [`SystemCommand`].
///
/// The target system can consume the event with the [`SystemEvent`] system parameter.
///
/// If scheduled from user-land, this will cause a [`reaction_tree()`] to execute, otherwise it will be
/// processed within the already-running reaction tree.
fn send_system_event<T: Send + Sync + 'static>(&mut self, command: SystemCommand, event: T);
}
impl<'w, 's> ReactCommandsExt for Commands<'w, 's>
{
fn react(&mut self) -> ReactCommands<'_, '_>
{
ReactCommands{ commands: self.reborrow() }
}
fn spawn_system_command<S, M>(&mut self, system: S) -> SystemCommand
where
S: IntoSystem<(), (), M> + Send + Sync + 'static
{
self.spawn_system_command_from(SystemCommandCallback::new(system))
}
fn spawn_system_command_from(&mut self, callback: SystemCommandCallback) -> SystemCommand
{
SystemCommand(self.spawn(SystemCommandStorage::new(callback)).id())
}
fn send_system_event<T: Send + Sync + 'static>(&mut self, command: SystemCommand, event: T)
{
let data_entity = self.spawn(SystemEventData::new(event)).id();
self.queue(EventCommand{ system: command, data_entity });
}
}
//-------------------------------------------------------------------------------------------------------------------
/// Extends `EntityCommands` with reactivity helpers.
pub trait ReactEntityCommandsExt
{
/// Obtains a [`ReactCommands`] instance.
fn react(&mut self) -> ReactCommands<'_, '_>;
/// Registers the current entity with an [`EntityWorldReactor`].
fn add_world_reactor<T: EntityWorldReactor>(&mut self, data: T::Local);
}
impl<'a> ReactEntityCommandsExt for EntityCommands<'a>
{
fn react(&mut self) -> ReactCommands<'_, '_>
{
ReactCommands{ commands: self.commands() }
}
fn add_world_reactor<T: EntityWorldReactor>(&mut self, data: T::Local)
{
let id = self.id();
self.commands().syscall((id, data),
|In((id, data)): In<(Entity, T::Local)>, mut c: Commands, reactor: EntityReactor<T>|
{
reactor.add(&mut c, id, data);
}
);
}
}
//-------------------------------------------------------------------------------------------------------------------