bevy_cobweb 0.13.0

Reactivity primitives for Bevy
Documentation
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
//local shortcuts
use crate::prelude::*;

//third-party shortcuts
//use bevy::ecs::component::ComponentId;
use bevy::ecs::system::SystemParam;
use bevy::prelude::*;

//standard shortcuts
use std::any::{type_name, TypeId};
use std::marker::PhantomData;

//-------------------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------------------

//todo: switch to ComponentId when observers are implemented
//(cannot do so yet because component ids are not available when reactions are triggered, only type ids)
struct ReactComponentId<T: ReactComponent>
{
    //id: ComponentId,
    id: TypeId,
    p: PhantomData<T>,
}

impl<T: ReactComponent> ReactComponentId<T>
{
    fn id(&self) -> TypeId
    {
        self.id
    }
}

impl<T: ReactComponent> FromWorld for ReactComponentId<T>
{
    fn from_world(_world: &mut World) -> Self
    {
        Self{
            //id: world.components().get_id(std::any::TypeId::of::<React<T>>()),
            id: TypeId::of::<T>(),
            p: PhantomData::default(),
        }
    }
}

//-------------------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------------------

/// Tracks metadata for accessing entity reactions (entity events use [`EventAccessTracker`] instead).
#[derive(Resource)]
pub(crate) struct EntityReactionAccessTracker
{
    /// True when in a system reacting to an entity reaction.
    currently_reacting: bool,
    /// The system command that is running the current entity reaction.
    system: SystemCommand,
    /// The source of the most recent entity reaction.
    reaction_source: Entity,
    /// The type of the most recent entity reaction trigger.
    reaction_type: EntityReactionType,

    /// Reaction information cached for when the reaction system actually runs.
    prepared: Vec<(SystemCommand, Entity, EntityReactionType)>,
}

impl EntityReactionAccessTracker
{
    /// Caches metadata for an entity reaction.
    pub(crate) fn prepare(&mut self, system: SystemCommand, source: Entity, reaction: EntityReactionType)
    {
        self.prepared.push((system, source, reaction));
    }

    /// Sets metadata for the current entity reaction.
    pub(crate) fn start(&mut self, reactor: SystemCommand)
    {
        let Some(pos) = self.prepared.iter().position(|(s, _, _)| *s == reactor) else {
            tracing::error!("prepared entity reaction is missing {:?}", reactor);
            debug_assert!(false);
            return;
        };
        let (system, source, reaction) = self.prepared.swap_remove(pos);

        debug_assert!(!self.currently_reacting);
        self.currently_reacting = true;
        self.system = system;
        self.reaction_source = source;
        self.reaction_type = reaction;
    }

    /// Unsets the 'is reacting' flag.
    pub(crate) fn end(&mut self)
    {
        self.currently_reacting = false;
    }

    /// Returns `true` if an entity reaction is currently being processed.
    fn is_reacting(&self) -> bool
    {
        self.currently_reacting
    }

    /// Returns the system running the entity reaction.
    fn system(&self) -> SystemCommand
    {
        self.system
    }

    /// Returns the source of the most recent entity reaction.
    fn source(&self) -> Entity
    {
        self.reaction_source
    }

    /// Returns the [`EntityReactionType`] of the most recent entity reaction.
    fn reaction_type(&self) -> EntityReactionType
    {
        self.reaction_type
    }
}

impl Default for EntityReactionAccessTracker
{
    fn default() -> Self
    {
        Self{
            currently_reacting: false,
            system: SystemCommand(Entity::PLACEHOLDER),
            reaction_source: Entity::PLACEHOLDER,
            reaction_type: EntityReactionType::Insertion(TypeId::of::<()>()),
            prepared: Vec::default(),
        }
    }
}

//-------------------------------------------------------------------------------------------------------------------

/// System parameter for reading entity component insertion events in systems that react to those events.
///
/// Can only be used within [`SystemCommands`](super::SystemCommand).
///
/// Use [`entity_insertion`] or [`insertion`] to make a trigger that will read these events.
///
/*
```rust
fn example(mut c: Commands)
{
    let entity = c.spawn_empty().id();
    c.react().on(
        insertion::<A>(),  // entity-specific: entity_insertion::<A>(target_entity)
        |event: InsertionEvent<A>|
        {
            if let Some(entity) = event.get()
            {
                println!("'A' was inserted to {:?}", entity);
            }
        }
    );

    rcommands.insert(entity, A);
}
```
*/
#[derive(SystemParam)]
pub struct InsertionEvent<'w, 's, T: ReactComponent>
{
    component_id: Local<'s, ReactComponentId<T>>,
    tracker: Res<'w, EntityReactionAccessTracker>,
}

impl<'w, 's, T: ReactComponent> InsertionEvent<'w, 's, T>
{
    /// Returns the entity that received a `React<T>` component insertion that the system is reacting to.
    ///
    /// This will return at most one unique entity each time a reactor runs.
    ///
    /// Panics if the system is not reacting to an insertion event for `T`.
    pub fn entity(&self) -> Entity
    {
        self.get()
            .unwrap_or_else(|| panic!("failed reading insertion event for {}, there is no event", type_name::<T>()))
    }

    /// See [`Self::entity`].
    pub fn get(&self) -> Option<Entity>
    {
        if !self.tracker.is_reacting() { return None; }
        let EntityReactionType::Insertion(component_id) = self.tracker.reaction_type() else { return None; };
        if component_id != self.component_id.id() { return None; }

        Some(self.tracker.source())
    }

    /// Returns `true` if there is nothing to read.
    ///
    /// Equivalent to `event.get().is_none()`.
    pub fn is_empty(&self) -> bool
    {
        self.get().is_none()
    }
}

//-------------------------------------------------------------------------------------------------------------------

/// System parameter for reading entity component mutation events in systems that react to those events.
///
/// Can only be used within [`SystemCommands`](super::SystemCommand).
///
/// Use [`entity_mutation`] or [`mutation`] to make a trigger that will read these events.
///
/*
```rust
fn example(mut c: Commands, query: Query<&mut React<A>>)
{
    c.react().on(
        mutation::<A>(),  // entity-specific: entity_mutation::<A>(target_entity)
        |event: MutationEvent<A>|
        {
            if let Some(entity) = event.get()
            {
                println!("'A' was mutated on {:?}", entity);
            }
        }
    );

    query.single_mut().get_mut(&mut rcommands);  //triggers mutation reactions
}
```
*/
#[derive(SystemParam)]
pub struct MutationEvent<'w, 's, T: ReactComponent>
{
    component_id: Local<'s, ReactComponentId<T>>,
    tracker: Res<'w, EntityReactionAccessTracker>,
}

impl<'w, 's, T: ReactComponent> MutationEvent<'w, 's, T>
{
    /// Returns the entity on which a `React<T>` component was mutated that the system is reacting to.
    ///
    /// This will return at most one unique entity each time a reactor runs.
    ///
    /// Panics if the system is not reacting to a mutation event for `T`.
    pub fn entity(&self) -> Entity
    {
        self.get()
            .unwrap_or_else(|| panic!("failed reading mutation event for {}, there is no event", type_name::<T>()))
    }

    /// See [`Self::entity`].
    pub fn get(&self) -> Option<Entity>
    {
        if !self.tracker.is_reacting() { return None; }
        let EntityReactionType::Mutation(component_id) = self.tracker.reaction_type() else { return None; };
        if component_id != self.component_id.id() { return None; }

        Some(self.tracker.source())
    }

    /// Returns `true` if there is nothing to read.
    ///
    /// Equivalent to `event.get().is_none()`.
    pub fn is_empty(&self) -> bool
    {
        self.get().is_none()
    }
}

//-------------------------------------------------------------------------------------------------------------------

/// System parameter for reading entity component removal events in systems that react to those events.
///
/// Note that removals are detected for entity despawns, so if the entity returned from `RemovalEvent` does not
/// exist that implies that it was removed due to a despawn (although not a guarantee, since it could have been removed
/// and the entity despawned later).
///
/// Use [`entity_removal`] or [`removal`] to make a trigger that will read these events.
///
/*
```rust
fn example(mut c: Commands, query: Query<Entity, With<React<A>>>)
{
    c.react().on(
        removal::<A>(),  // entity-specific: entity_removal::<A>(target_entity)
        |event: RemovalEvent<A>|
        {
            if let Some(entity) = event.get()
            {
                println!("'A' was removed from {:?}", entity);
            }
        }
    );

    c.entity(*query.single()).remove::<A>();
}
```
*/
#[derive(SystemParam)]
pub struct RemovalEvent<'w, 's, T: ReactComponent>
{
    component_id: Local<'s, ReactComponentId<T>>,
    tracker: Res<'w, EntityReactionAccessTracker>,
}

impl<'w, 's, T: ReactComponent> RemovalEvent<'w, 's, T>
{
    /// Returns the entity from which a `React<T>` component was removed that the system is reacting to.
    ///
    /// This will return at most one unique entity each time a reactor runs.
    ///
    /// Panics if the system is not reacting to a removal event for `T`.
    pub fn entity(&self) -> Entity
    {
        self.get()
            .unwrap_or_else(|| panic!("failed reading removal event for {}, there is no event", type_name::<T>()))
    }

    /// See [`Self::entity`].
    pub fn get(&self) -> Option<Entity>
    {
        if !self.tracker.is_reacting() { return None; }
        let EntityReactionType::Removal(component_id) = self.tracker.reaction_type() else { return None; };
        if component_id != self.component_id.id() { return None; }

        Some(self.tracker.source())
    }

    /// Returns `true` if there is nothing to read.
    ///
    /// Equivalent to `event.get().is_none()`.
    pub fn is_empty(&self) -> bool
    {
        self.get().is_none()
    }
}

//-------------------------------------------------------------------------------------------------------------------

/// System parameter for reading entity-specific data for [`EntityWorldReactor`] reactors.
///
/*
```rust
#[derive(ReactComponent)]
struct MyComponent(Duration);

struct MyReactor;

impl EntityWorldReactor for MyReactor
{
    type Triggers = EntityMutationTrigger::<MyComponent>;
    type Local = String;

    fn reactor() -> SystemCommandCallback
    {
        SystemCommandCallback::new(
            |data: EntityLocal<MyReactor>, components: Reactive<MyComponent>|
            {
                let (entity, data) = data.get();
                let Some(component) = components.get(entity) else { return };
                println!("Entity {:?} now has {:?}", data, component);
            }
        )
    }
}

fn prep_entity(mut c: Commands, reactor: EntityReactor<MyReactor>)
{
    let entity = c.spawn(MyComponent(Duration::default()));
    reactor.add(&mut c, entity, "ClockTracker");
}

fn update_entity(mut commands: Commands, time: Res<Time>, mut components: ReactiveMut<MyComponent>)
{
    let elapsed = time.elapsed();
    let component = components.single_mut(&mut c);
    component.0 = elapsed;
}

struct ExamplePlugin;
impl Plugin for ExamplePlugin
{
    fn build(&self, app: &mut App)
    {
        app.add_entity_reactor::<MyReactor>()
            .add_systems(Setup, prep_entity)
            .add_systems(Update, update_entity);
    }
}
```
*/
#[derive(SystemParam)]
pub struct EntityLocal<'w, 's, T: EntityWorldReactor>
{
    reactor: EntityReactor<'w, T>,
    tracker: Res<'w, EntityReactionAccessTracker>,
    data: Query<'w, 's, &'static mut EntityWorldLocal<T>>,
}

impl<'w, 's, T: EntityWorldReactor> EntityLocal<'w, 's, T>
{
    /// Gets the current entity.
    ///
    /// Panics if not called from within an [`EntityWorldReactor`] system.
    pub fn entity(&self) -> Entity
    {
        self.check();
        self.tracker.source()
    }

    /// Gets the current entity's local data.
    ///
    /// Panics if not called from within an [`EntityWorldReactor`] system.
    pub fn get(&self) -> (Entity, &T::Local)
    {
        self.check();
        (
            self.tracker.source(),
            self.data.get(self.tracker.source()).expect("entity missing local data in EntityLocal").inner()
        )
    }

    /// Gets the current entity's local data.
    ///
    /// Panics if not called from within an [`EntityWorldReactor`] system.
    pub fn get_mut(&mut self) -> (Entity, &mut T::Local)
    {
        self.check();
        (
            self.tracker.source(),
            self.data.get_mut(self.tracker.source())
                .expect("entity missing local data in EntityLocal")
                .into_inner()
                .inner_mut()
        )
    }

    fn check(&self)
    {
        if !self.tracker.is_reacting()
        {
            panic!("EntityLocal should only be used in an EntityWorldReactor");
        }
        if self.tracker.system() != self.reactor.system().expect("EntityLocal should only be used in an EntityWorldReactor")
        {
            panic!("EntityLocal should only be used in an EntityWorldReactor");
        }
    }
}

//-------------------------------------------------------------------------------------------------------------------