freecs 4.9.2

A high-performance, archetype-based Entity Component System (ECS) written in Rust.
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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
//! An optional state machine over the dynamic layer, behind the `state`
//! feature: a current-and-next value per state type, transitions that emit an
//! event, run-condition gating of systems, and enter and exit combinators.
//! The state type is yours; the machinery combines the run conditions and
//! system parameters of the ECS layer with a small state model, all in this
//! one optional feature.
//!
//! A state type is any `Copy + PartialEq` value, usually an enum. Insert one
//! with [`insert_state`], drive it with [`apply_state_transition`] pushed onto
//! an early stage (or [`add_state_transitions`](StateScheduleExt::add_state_transitions)),
//! and request transitions with [`next_state`]. Each applied transition sends
//! a [`StateTransition`] event, so [`on_enter`] and [`on_exit`] read it for
//! exactly-once enter and exit logic, and [`while_in`], [`while_in_any`], and
//! [`run_if`] gate systems on the current state. The state value itself is
//! plain data in the host's resource map, so nothing here reaches past the
//! host it is given.
//!
//! ```rust
//! use freecs::Schedule;
//! use freecs::dynamic::DynWorld;
//! use freecs::system_param::{ResMut, ScheduleExt};
//! use freecs::state::{StateScheduleExt, insert_state, next_state, while_in};
//!
//! #[derive(Clone, Copy, PartialEq, Eq, Debug)]
//! enum Screen {
//!     Title,
//!     Playing,
//! }
//!
//! struct Ticks(u32);
//!
//! fn tick(mut ticks: ResMut<Ticks>) {
//!     ticks.0 += 1;
//! }
//!
//! let mut world = DynWorld::new();
//! insert_state(&mut world, Screen::Title);
//! world.insert_resource(Ticks(0));
//!
//! let mut schedule = Schedule::new();
//! schedule.add_state_transitions::<Screen>("screen_transitions");
//! schedule.push("tick", while_in(Screen::Playing, tick));
//!
//! schedule.run(&mut world); // enters Title; the gated tick is skipped
//! assert_eq!(world.resource::<Ticks>().unwrap().0, 0);
//!
//! next_state(&mut world, Screen::Playing);
//! schedule.run(&mut world); // transitions to Playing, then tick runs
//! assert_eq!(world.resource::<Ticks>().unwrap().0, 1);
//! ```

use crate::Schedule;
use crate::dynamic::ResourceHost;
use crate::system_param::{EventHost, IntoSystem};
use std::marker::PhantomData;

/// The current value of state type `S`, a resource inserted by
/// [`insert_state`]. Read it with [`current_state`] or [`in_state`].
/// `initialized` is false until the first [`apply_state_transition`] runs the
/// initial entry.
pub struct State<S> {
    pub current: S,
    pub initialized: bool,
}

/// The pending transition for state type `S`, a resource inserted by
/// [`insert_state`]. Request a transition with [`next_state`]; it lands on the
/// next [`apply_state_transition`].
pub struct NextState<S> {
    pub pending: Option<S>,
}

/// The event [`apply_state_transition`] sends each time state type `S`
/// changes. `before` is `None` on the initial entry, otherwise the state that
/// was left. Read it with an
/// [`EventReader`](crate::system_param::EventReader), or through [`on_enter`]
/// and [`on_exit`].
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct StateTransition<S> {
    pub before: Option<S>,
    pub after: S,
}

/// Inserts the [`State`] and [`NextState`] resources for `S`, starting at
/// `initial`. The initial state's entry runs on the first
/// [`apply_state_transition`], which also emits its [`StateTransition`].
pub fn insert_state<S, W>(host: &mut W, initial: S)
where
    S: Copy + PartialEq + Send + Sync + 'static,
    W: ResourceHost,
{
    let map = host.resource_map_mut();
    map.insert(State {
        current: initial,
        initialized: false,
    });
    map.insert(NextState::<S> { pending: None });
}

/// The current state of type `S`. Panics if [`insert_state`] was never called
/// for `S`.
pub fn current_state<S, W>(host: &W) -> S
where
    S: Copy + PartialEq + Send + Sync + 'static,
    W: ResourceHost,
{
    host.resource_map()
        .get::<State<S>>()
        .unwrap_or_else(|| {
            panic!(
                "no state of type {} was inserted; call insert_state first",
                std::any::type_name::<S>()
            )
        })
        .current
}

/// Whether the current state of type `S` is `state`.
pub fn in_state<S, W>(host: &W, state: S) -> bool
where
    S: Copy + PartialEq + Send + Sync + 'static,
    W: ResourceHost,
{
    current_state::<S, W>(host) == state
}

/// Requests a transition to `state`, applied on the next
/// [`apply_state_transition`]. A request for the current state is dropped
/// without emitting a transition. Panics if [`insert_state`] was never called
/// for `S`.
pub fn next_state<S, W>(host: &mut W, state: S)
where
    S: Copy + PartialEq + Send + Sync + 'static,
    W: ResourceHost,
{
    host.resource_map_mut()
        .get_mut::<NextState<S>>()
        .unwrap_or_else(|| {
            panic!(
                "no state of type {} was inserted; call insert_state first",
                std::any::type_name::<S>()
            )
        })
        .pending = Some(state);
}

/// Applies a pending transition for `S`: the initial entry on the first run,
/// then any requested change on later runs. On a change the current state
/// flips and a [`StateTransition`] is sent to the host's event bus, so enter
/// and exit systems reading it see each transition once. A request equal to
/// the current state is dropped. Push this onto an early stage, once per state
/// type, ahead of the systems that read the state.
pub fn apply_state_transition<S, W>(host: &mut W)
where
    S: Copy + PartialEq + Send + Sync + 'static,
    W: ResourceHost + EventHost,
{
    let transition = {
        let map = host.resource_map_mut();
        let Some(state) = map.get::<State<S>>() else {
            return;
        };
        let current = state.current;
        let initialized = state.initialized;

        if !initialized {
            map.get_mut::<State<S>>()
                .expect("state present above")
                .initialized = true;
            if let Some(next) = map.get_mut::<NextState<S>>()
                && next.pending == Some(current)
            {
                next.pending = None;
            }
            Some(StateTransition {
                before: None,
                after: current,
            })
        } else {
            let pending = map.get::<NextState<S>>().and_then(|next| next.pending);
            match pending {
                Some(next) => {
                    if let Some(next_state) = map.get_mut::<NextState<S>>() {
                        next_state.pending = None;
                    }
                    if next != current {
                        map.get_mut::<State<S>>()
                            .expect("state present above")
                            .current = next;
                        Some(StateTransition {
                            before: Some(current),
                            after: next,
                        })
                    } else {
                        None
                    }
                }
                None => None,
            }
        }
    };
    if let Some(transition) = transition {
        host.event_bus_mut().send::<StateTransition<S>>(transition);
    }
}

/// The marker for a plain world system, `FnMut(&mut W)`, in a gate.
pub struct PlainMarker;

/// The marker for a system-parameter function in a gate.
pub struct ParamMarker<Marker>(PhantomData<fn() -> Marker>);

/// One gateable system: a plain world system `FnMut(&mut W)` or a
/// system-parameter function ([`IntoSystem`](crate::system_param::IntoSystem)).
/// A single gated system and each member of a gated tuple resolve through
/// this, so a tuple may freely mix the two shapes.
pub trait GatedSystem<W, Marker> {
    /// Lowers the system to a runner the gate calls when its condition holds.
    fn into_gated_runner(self) -> impl FnMut(&mut W) + Send + 'static;
}

impl<W, F> GatedSystem<W, PlainMarker> for F
where
    F: FnMut(&mut W) + Send + 'static,
{
    fn into_gated_runner(self) -> impl FnMut(&mut W) + Send + 'static {
        self
    }
}

impl<W, Marker, F> GatedSystem<W, ParamMarker<Marker>> for F
where
    F: IntoSystem<W, Marker>,
{
    fn into_gated_runner(self) -> impl FnMut(&mut W) + Send + 'static {
        self.into_runner()
    }
}

/// The marker for a single system passed where a group is accepted.
pub struct SingleMarker<Marker>(PhantomData<fn() -> Marker>);

/// The marker for a tuple of systems passed as one gated group.
pub struct GroupMarker<Marker>(PhantomData<fn() -> Marker>);

/// One [`GatedSystem`] or a tuple of them, lowered to a single runner so a
/// gate checks its condition once and then runs the whole group in order. A
/// single system resolves through [`SingleMarker`]; a tuple through
/// [`GroupMarker`]. A tuple may mix plain world systems and system-parameter
/// functions. Implemented for tuples of up to sixteen systems.
pub trait IntoGroupRunner<W, Marker> {
    /// Builds the group's runner, initializing each member's system once.
    fn into_group_runner(self) -> impl FnMut(&mut W) + Send + 'static;
}

impl<W, Marker, S> IntoGroupRunner<W, SingleMarker<Marker>> for S
where
    S: GatedSystem<W, Marker>,
{
    fn into_group_runner(self) -> impl FnMut(&mut W) + Send + 'static {
        self.into_gated_runner()
    }
}

macro_rules! impl_group_runner_tuple {
    ($($system:ident $marker:ident $runner:ident),+) => {
        impl<W, $($system, $marker,)+> IntoGroupRunner<W, GroupMarker<($($marker,)+)>>
            for ($($system,)+)
        where
            $($system: GatedSystem<W, $marker>,)+
        {
            fn into_group_runner(self) -> impl FnMut(&mut W) + Send + 'static {
                let ($($runner,)+) = self;
                $(let mut $runner = $runner.into_gated_runner();)+
                move |world: &mut W| {
                    $($runner(world);)+
                }
            }
        }
    };
}

impl_group_runner_tuple!(S0 M0 r0);
impl_group_runner_tuple!(S0 M0 r0, S1 M1 r1);
impl_group_runner_tuple!(S0 M0 r0, S1 M1 r1, S2 M2 r2);
impl_group_runner_tuple!(S0 M0 r0, S1 M1 r1, S2 M2 r2, S3 M3 r3);
impl_group_runner_tuple!(S0 M0 r0, S1 M1 r1, S2 M2 r2, S3 M3 r3, S4 M4 r4);
impl_group_runner_tuple!(S0 M0 r0, S1 M1 r1, S2 M2 r2, S3 M3 r3, S4 M4 r4, S5 M5 r5);
impl_group_runner_tuple!(S0 M0 r0, S1 M1 r1, S2 M2 r2, S3 M3 r3, S4 M4 r4, S5 M5 r5, S6 M6 r6);
impl_group_runner_tuple!(
    S0 M0 r0, S1 M1 r1, S2 M2 r2, S3 M3 r3, S4 M4 r4, S5 M5 r5, S6 M6 r6, S7 M7 r7
);
impl_group_runner_tuple!(
    S0 M0 r0, S1 M1 r1, S2 M2 r2, S3 M3 r3, S4 M4 r4, S5 M5 r5, S6 M6 r6, S7 M7 r7, S8 M8 r8
);
impl_group_runner_tuple!(
    S0 M0 r0, S1 M1 r1, S2 M2 r2, S3 M3 r3, S4 M4 r4, S5 M5 r5, S6 M6 r6, S7 M7 r7, S8 M8 r8,
    S9 M9 r9
);
impl_group_runner_tuple!(
    S0 M0 r0, S1 M1 r1, S2 M2 r2, S3 M3 r3, S4 M4 r4, S5 M5 r5, S6 M6 r6, S7 M7 r7, S8 M8 r8,
    S9 M9 r9, S10 M10 r10
);
impl_group_runner_tuple!(
    S0 M0 r0, S1 M1 r1, S2 M2 r2, S3 M3 r3, S4 M4 r4, S5 M5 r5, S6 M6 r6, S7 M7 r7, S8 M8 r8,
    S9 M9 r9, S10 M10 r10, S11 M11 r11
);
impl_group_runner_tuple!(
    S0 M0 r0, S1 M1 r1, S2 M2 r2, S3 M3 r3, S4 M4 r4, S5 M5 r5, S6 M6 r6, S7 M7 r7, S8 M8 r8,
    S9 M9 r9, S10 M10 r10, S11 M11 r11, S12 M12 r12
);
impl_group_runner_tuple!(
    S0 M0 r0, S1 M1 r1, S2 M2 r2, S3 M3 r3, S4 M4 r4, S5 M5 r5, S6 M6 r6, S7 M7 r7, S8 M8 r8,
    S9 M9 r9, S10 M10 r10, S11 M11 r11, S12 M12 r12, S13 M13 r13
);
impl_group_runner_tuple!(
    S0 M0 r0, S1 M1 r1, S2 M2 r2, S3 M3 r3, S4 M4 r4, S5 M5 r5, S6 M6 r6, S7 M7 r7, S8 M8 r8,
    S9 M9 r9, S10 M10 r10, S11 M11 r11, S12 M12 r12, S13 M13 r13, S14 M14 r14
);
impl_group_runner_tuple!(
    S0 M0 r0, S1 M1 r1, S2 M2 r2, S3 M3 r3, S4 M4 r4, S5 M5 r5, S6 M6 r6, S7 M7 r7, S8 M8 r8,
    S9 M9 r9, S10 M10 r10, S11 M11 r11, S12 M12 r12, S13 M13 r13, S14 M14 r14, S15 M15 r15
);

/// Gates a system, or a tuple of systems, on an arbitrary world condition,
/// returning one runner that checks `condition` each pass and runs the group
/// only when it holds. Push the result onto a schedule.
pub fn run_if<W, Marker>(
    condition: impl Fn(&W) -> bool + Send + 'static,
    systems: impl IntoGroupRunner<W, Marker>,
) -> impl FnMut(&mut W) + Send + 'static {
    let mut runner = systems.into_group_runner();
    move |world: &mut W| {
        if condition(world) {
            runner(world);
        }
    }
}

/// Gates a system, or a tuple of systems, on the current state being `state`,
/// so one registration covers a whole group on one screen.
pub fn while_in<S, W, Marker>(
    state: S,
    systems: impl IntoGroupRunner<W, Marker>,
) -> impl FnMut(&mut W) + Send + 'static
where
    S: Copy + PartialEq + Send + Sync + 'static,
    W: ResourceHost,
{
    run_if(move |world: &W| in_state::<S, W>(world, state), systems)
}

/// Gates a system, or a tuple of systems, on the current state being any of
/// `states`, so one registration covers several screens (for example both
/// `Playing` and `Paused`).
pub fn while_in_any<S, W, Marker>(
    states: impl IntoIterator<Item = S>,
    systems: impl IntoGroupRunner<W, Marker>,
) -> impl FnMut(&mut W) + Send + 'static
where
    S: Copy + PartialEq + Send + Sync + 'static,
    W: ResourceHost,
{
    let states: Vec<S> = states.into_iter().collect();
    run_if(
        move |world: &W| states.contains(&current_state::<S, W>(world)),
        systems,
    )
}

/// Runs a system, or a tuple of systems, once each time state `S` enters
/// `state`, reading the [`StateTransition`] event through its own cursor so
/// each entry fires exactly once. Push it after [`apply_state_transition`].
pub fn on_enter<S, W, Marker>(
    state: S,
    systems: impl IntoGroupRunner<W, Marker>,
) -> impl FnMut(&mut W) + Send + 'static
where
    S: Copy + PartialEq + Send + Sync + 'static,
    W: EventHost + 'static,
{
    let mut cursor = 0u64;
    let mut runner = systems.into_group_runner();
    move |host: &mut W| {
        let entered = host
            .event_bus_mut()
            .consume::<StateTransition<S>>(&mut cursor)
            .iter()
            .any(|transition| transition.after == state);
        if entered {
            runner(host);
        }
    }
}

/// Runs a system, or a tuple of systems, once each time state `S` leaves
/// `state`, reading the [`StateTransition`] event through its own cursor so
/// each exit fires exactly once. Push it after [`apply_state_transition`].
pub fn on_exit<S, W, Marker>(
    state: S,
    systems: impl IntoGroupRunner<W, Marker>,
) -> impl FnMut(&mut W) + Send + 'static
where
    S: Copy + PartialEq + Send + Sync + 'static,
    W: EventHost + 'static,
{
    let mut cursor = 0u64;
    let mut runner = systems.into_group_runner();
    move |host: &mut W| {
        let exited = host
            .event_bus_mut()
            .consume::<StateTransition<S>>(&mut cursor)
            .iter()
            .any(|transition| transition.before == Some(state));
        if exited {
            runner(host);
        }
    }
}

/// Registers [`apply_state_transition`] for a state type onto a [`Schedule`],
/// so the transition step lands with the rest of a stage's systems.
pub trait StateScheduleExt<W> {
    /// Pushes [`apply_state_transition`] for `S` under `name`.
    fn add_state_transitions<S>(&mut self, name: &'static str) -> &mut Self
    where
        S: Copy + PartialEq + Send + Sync + 'static;
}

impl<W: ResourceHost + EventHost + 'static> StateScheduleExt<W> for Schedule<W> {
    fn add_state_transitions<S>(&mut self, name: &'static str) -> &mut Self
    where
        S: Copy + PartialEq + Send + Sync + 'static,
    {
        self.push(name, apply_state_transition::<S, W>)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::Schedule;
    use crate::dynamic::DynWorld;
    use crate::system_param::{EventReader, ResMut, ScheduleExt};

    #[derive(Clone, Copy, PartialEq, Eq, Debug)]
    enum Screen {
        Title,
        Playing,
        Paused,
    }

    struct Ticks(u32);
    struct Entered(u32);
    struct Exited(u32);

    fn state_schedule() -> Schedule<DynWorld> {
        let mut schedule = Schedule::new();
        schedule.add_state_transitions::<Screen>("screen_transitions");
        schedule
    }

    #[test]
    fn insert_and_read_state() {
        let mut world = DynWorld::new();
        insert_state(&mut world, Screen::Title);
        assert_eq!(current_state::<Screen, _>(&world), Screen::Title);
        assert!(in_state(&world, Screen::Title));
        assert!(!in_state(&world, Screen::Playing));
    }

    #[test]
    fn transition_applies_on_next_run() {
        let mut world = DynWorld::new();
        insert_state(&mut world, Screen::Title);
        let mut schedule = state_schedule();

        schedule.run(&mut world);
        assert_eq!(current_state::<Screen, _>(&world), Screen::Title);

        next_state(&mut world, Screen::Playing);
        assert_eq!(
            current_state::<Screen, _>(&world),
            Screen::Title,
            "the request is pending until the next transition step"
        );

        schedule.run(&mut world);
        assert_eq!(current_state::<Screen, _>(&world), Screen::Playing);
    }

    #[test]
    fn request_for_current_state_is_dropped() {
        let mut world = DynWorld::new();
        insert_state(&mut world, Screen::Title);
        world.insert_resource(Entered(0));
        let mut schedule = state_schedule();
        schedule.push("on_enter_title", on_enter(Screen::Title, count_enter));
        schedule.run(&mut world);
        assert_eq!(world.resource::<Entered>().unwrap().0, 1);

        next_state(&mut world, Screen::Title);
        schedule.run(&mut world);
        assert_eq!(
            world.resource::<Entered>().unwrap().0,
            1,
            "a no-op request emits no transition, so enter does not fire again"
        );
    }

    fn tick(mut ticks: ResMut<Ticks>) {
        ticks.0 += 1;
    }
    fn count_enter(mut entered: ResMut<Entered>) {
        entered.0 += 1;
    }
    fn count_exit(mut exited: ResMut<Exited>) {
        exited.0 += 1;
    }

    #[test]
    fn while_in_gates_a_single_system() {
        let mut world = DynWorld::new();
        insert_state(&mut world, Screen::Title);
        world.insert_resource(Ticks(0));
        let mut schedule = state_schedule();
        schedule.push("tick", while_in(Screen::Playing, tick));

        schedule.run(&mut world);
        assert_eq!(world.resource::<Ticks>().unwrap().0, 0);

        next_state(&mut world, Screen::Playing);
        schedule.run(&mut world);
        assert_eq!(world.resource::<Ticks>().unwrap().0, 1);
    }

    #[test]
    fn while_in_gates_a_tuple_as_one_entry() {
        let mut world = DynWorld::new();
        insert_state(&mut world, Screen::Playing);
        world.insert_resource(Ticks(0));
        world.insert_resource(Entered(0));
        let mut schedule = state_schedule();
        schedule.push(
            "group",
            while_in(Screen::Playing, (tick, count_enter, tick)),
        );

        schedule.run(&mut world);
        assert_eq!(world.resource::<Ticks>().unwrap().0, 2);
        assert_eq!(world.resource::<Entered>().unwrap().0, 1);
    }

    fn plain_tick(world: &mut DynWorld) {
        world.resource_mut::<Ticks>().unwrap().0 += 1;
    }

    #[test]
    fn while_in_gates_a_plain_world_system() {
        let mut world = DynWorld::new();
        insert_state(&mut world, Screen::Playing);
        world.insert_resource(Ticks(0));
        let mut schedule = state_schedule();
        schedule.push("plain", while_in(Screen::Playing, plain_tick));

        schedule.run(&mut world);
        assert_eq!(world.resource::<Ticks>().unwrap().0, 1);

        next_state(&mut world, Screen::Title);
        schedule.run(&mut world);
        assert_eq!(
            world.resource::<Ticks>().unwrap().0,
            1,
            "the plain world system is skipped off its state"
        );
    }

    #[test]
    fn while_in_gates_a_tuple_mixing_plain_and_param_systems() {
        let mut world = DynWorld::new();
        insert_state(&mut world, Screen::Playing);
        world.insert_resource(Ticks(0));
        world.insert_resource(Entered(0));
        let mut schedule = state_schedule();
        schedule.push(
            "mixed",
            while_in(Screen::Playing, (plain_tick, count_enter, tick)),
        );

        schedule.run(&mut world);
        assert_eq!(world.resource::<Ticks>().unwrap().0, 2);
        assert_eq!(world.resource::<Entered>().unwrap().0, 1);
    }

    #[test]
    fn while_in_any_covers_several_states() {
        let mut world = DynWorld::new();
        insert_state(&mut world, Screen::Paused);
        world.insert_resource(Ticks(0));
        let mut schedule = state_schedule();
        schedule.push("hud", while_in_any([Screen::Playing, Screen::Paused], tick));

        schedule.run(&mut world);
        assert_eq!(world.resource::<Ticks>().unwrap().0, 1);

        next_state(&mut world, Screen::Title);
        schedule.run(&mut world);
        assert_eq!(
            world.resource::<Ticks>().unwrap().0,
            1,
            "Title is not in the gate set"
        );
    }

    #[test]
    fn run_if_gates_on_a_plain_condition() {
        let mut world = DynWorld::new();
        world.insert_resource(Ticks(0));
        world.insert_resource(Entered(0));
        let mut schedule = Schedule::new();
        schedule.push(
            "tick",
            run_if(
                |world: &DynWorld| world.resource::<Entered>().unwrap().0 == 0,
                tick,
            ),
        );

        schedule.run(&mut world);
        assert_eq!(world.resource::<Ticks>().unwrap().0, 1);

        world.resource_mut::<Entered>().unwrap().0 = 1;
        schedule.run(&mut world);
        assert_eq!(world.resource::<Ticks>().unwrap().0, 1);
    }

    #[test]
    fn on_enter_and_on_exit_fire_once_per_transition() {
        let mut world = DynWorld::new();
        insert_state(&mut world, Screen::Title);
        world.insert_resource(Entered(0));
        world.insert_resource(Exited(0));
        let mut schedule = state_schedule();
        schedule.push("enter_playing", on_enter(Screen::Playing, count_enter));
        schedule.push("exit_title", on_exit(Screen::Title, count_exit));

        schedule.run(&mut world);
        assert_eq!(world.resource::<Entered>().unwrap().0, 0);
        assert_eq!(world.resource::<Exited>().unwrap().0, 0);

        next_state(&mut world, Screen::Playing);
        schedule.run(&mut world);
        assert_eq!(world.resource::<Entered>().unwrap().0, 1);
        assert_eq!(world.resource::<Exited>().unwrap().0, 1);

        schedule.run(&mut world);
        assert_eq!(world.resource::<Entered>().unwrap().0, 1);
        assert_eq!(world.resource::<Exited>().unwrap().0, 1);
    }

    #[test]
    fn initial_entry_emits_a_transition_event() {
        let mut world = DynWorld::new();
        insert_state(&mut world, Screen::Title);
        world.insert_resource(Seen(Vec::new()));
        let mut schedule = state_schedule();
        schedule.add_system("record", record_transitions);

        schedule.run(&mut world);
        assert_eq!(
            world.resource::<Seen>().unwrap().0,
            vec![(None, Screen::Title)]
        );
    }

    struct Seen(Vec<(Option<Screen>, Screen)>);

    fn record_transitions(reader: EventReader<StateTransition<Screen>>, mut seen: ResMut<Seen>) {
        for transition in &reader {
            seen.0.push((transition.before, transition.after));
        }
    }
}