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
use bevy_hecs::{Bundle, Component, DynamicBundle, Entity, World};
pub trait WorldBuilderSource {
fn build(&mut self) -> WorldBuilder;
}
impl WorldBuilderSource for World {
fn build(&mut self) -> WorldBuilder {
WorldBuilder {
world: self,
current_entity: None,
}
}
}
pub struct WorldBuilder<'a> {
pub world: &'a mut World,
pub current_entity: Option<Entity>,
}
impl<'a> WorldBuilder<'a> {
pub fn entity(&mut self) -> &mut Self {
self.current_entity = Some(self.world.reserve_entity());
self
}
pub fn set_entity(&mut self, entity: Entity) -> &mut Self {
self.current_entity = Some(entity);
self
}
pub fn with<T>(&mut self, component: T) -> &mut Self
where
T: Component,
{
self.world
.insert_one(self.current_entity.expect("Cannot add component because the 'current entity' is not set. You should spawn an entity first."), component)
.unwrap();
self
}
pub fn with_bundle(&mut self, components: impl DynamicBundle) -> &mut Self {
self.world
.insert(self.current_entity.expect("Cannot add component because the 'current entity' is not set. You should spawn an entity first."), components)
.unwrap();
self
}
pub fn spawn_batch<I>(&mut self, components_iter: I) -> &mut Self
where
I: IntoIterator,
I::Item: Bundle,
{
self.world.spawn_batch(components_iter);
self
}
pub fn spawn(&mut self, components: impl DynamicBundle) -> &mut Self {
self.current_entity = Some(self.world.spawn(components));
self
}
}