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
/// Internal event used to stop the `Simulation`. The `halt` event queue will always be created.
pub struct Halt;

/// The core wiring for entity component systems built on `trex`. This macro takes a set of
/// `Component`s, events, and `System`s, and creates a `Simulation` type that manages them.
/// See the library documentation for an example of how this macro is used.
#[macro_export]
macro_rules! simulation {
    {
        components: {
            $( $C:ident : $F:ident ),*
        },

        events: {
            $( $queue:ident : $E:ident ),*
        },

        systems: {
            $( $system:ident : $S:ident ),*
        }
    } => {
        $(
            component! {
                $C : $F
            }
        )*

        pub struct Events {
            pub halt: $crate::EventQueue<$crate::Halt>,

            $(
                pub $queue: $crate::EventQueue<$E>,
            )*
        }

        impl Events {
            fn new() -> Events {
                Events {
                    halt: $crate::EventQueue::new(),

                    $(
                        $queue: $crate::EventQueue::new(),
                    )*
                }
            }
        }

        pub struct Simulation {
            pub world: $crate::World,
            pub events: Events,
            received_halt: bool,

            $(
                pub $system : $S
            ),*
        }

        impl Simulation {
            pub fn new() -> Simulation {
                Simulation {
                    world: {
                        let mut world = $crate::World::new();
                        $(
                            world.register_component::<$C>();
                        )+
                        world
                    },
                    events: Events::new(),
                    received_halt: false,
                    $(
                        $system: $S::new()
                    ),*
                }
            }

            pub fn setup<F>(&mut self, setup_fn: F) where F: FnOnce(&mut $crate::World, &mut Events) {
                setup_fn(&mut self.world, &mut self.events);
            }

            pub fn update(&mut self, dt: f32) {
                $(
                    self.$system.update(&mut self.world, &mut self.events, dt);
                )*

                $(
                    self.events.$queue.flush();
                )*

                if let Some(_) = self.events.halt.receive().next() {
                    self.received_halt = true;
                }
                self.events.halt.flush();
            }

            pub fn received_halt(&self) -> bool {
                self.received_halt
            }
        }
    }
}