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
use std::marker::PhantomData;

use specs::{System, Tracked};
use specs_bundler::{Bundle, Bundler};

use super::{Parent, SceneGraphSystem};

#[derive(Debug)]
pub struct SceneGraphBundle<'deps, P> {
    deps: Vec<&'deps str>,
    _marker: PhantomData<P>,
}

impl<'deps, P> Default for SceneGraphBundle<'deps, P> {
    #[inline(always)]
    fn default() -> Self {
        Self::new(&[])
    }
}

impl<'deps, P> SceneGraphBundle<'deps, P> {
    #[inline(always)]
    pub fn new(deps: &[&'deps str]) -> Self {
        SceneGraphBundle {
            deps: deps.to_vec(),
            _marker: PhantomData,
        }
    }
}

impl<'deps, 'world, 'a, 'b, P> Bundle<'world, 'a, 'b> for SceneGraphBundle<'deps, P>
where
    P: Parent,
    P::Storage: Default + Tracked,
{
    type Error = ();

    #[inline]
    fn bundle(
        self,
        mut bundler: Bundler<'world, 'a, 'b>,
    ) -> Result<Bundler<'world, 'a, 'b>, Self::Error> {
        bundler.world.register::<P>();

        let mut system = SceneGraphSystem::<P>::new();

        system.setup(&mut bundler.world.res);

        bundler.dispatcher_builder = bundler.dispatcher_builder.with(
            system,
            SceneGraphSystem::<P>::name(),
            self.deps.as_slice(),
        );

        Ok(bundler)
    }
}