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
|
use std::borrow::Cow;
use crate::component::Sequence as ComponentSequence;
use crate::entity::Declaration as EntityDeclaration;
use crate::sole::Sole;
use crate::system::observer::Observer;
use crate::system::System;
use crate::uid::Uid;
use crate::{SoleAlreadyExistsError, World};
/// A collection of systems, entities & soles that can be added to a [`World`].
pub trait Extension
{
fn collect(self, collector: Collector<'_>);
}
/// Passed to a [`Extension`] to collects it's systems, entities & soles.
pub struct Collector<'world>
{
world: &'world mut World,
}
impl<'world> Collector<'world>
{
/// Returns a new `Collector` for the given [`World`].
pub fn new(world: &'world mut World) -> Self
{
Self { world }
}
/// Adds a system to the [`World`].
pub fn add_system<'this, SystemImpl>(
&'this mut self,
phase_euid: Uid,
system: impl System<'this, SystemImpl>,
)
{
self.world.register_system(phase_euid, system);
}
/// Adds a observer system to the [`World`].
pub fn add_observer<'this, SystemImpl>(
&'this mut self,
observer: impl Observer<'this, SystemImpl>,
)
{
self.world.register_observer(observer);
}
/// Adds a entity to the [`World`].
pub fn spawn<Comps>(&mut self, components: Comps)
where
Comps: ComponentSequence,
{
self.world.spawn(components);
}
/// Adds a entity to the [`World`].
pub fn spawn_named<Comps>(
&mut self,
name: impl Into<Cow<'static, str>>,
components: Comps,
) where
Comps: ComponentSequence,
{
self.world.spawn_named(name, components);
}
/// Adds a declared entity to the [`World`].
pub fn spawn_declared_entity(&mut self, entity_decl: &EntityDeclaration)
{
self.world.spawn_declared_entity(entity_decl);
}
/// Adds a singleton component to the [`World`].
///
/// # Errors
/// Returns `Err` if the [`Sole`] already exists.
pub fn add_sole<SoleT>(&mut self, sole: SoleT) -> Result<(), SoleAlreadyExistsError>
where
SoleT: Sole,
{
self.world.add_sole(sole)
}
}
|