make WorldBuilder non-consuming

This commit is contained in:
Carter Anderson 2020-04-07 13:25:01 -07:00
parent 45e28ef32b
commit 2565a69230
11 changed files with 29 additions and 44 deletions

View file

@ -26,18 +26,16 @@ pub struct WorldBuilder<'a> {
parent_entity: Option<Entity>,
}
// TODO: make this a non-consuming builder
impl<'a> WorldBuilder<'a> {
pub fn build_entity(mut self) -> Self {
pub fn build_entity(&mut self) -> &mut Self {
let entity = *self.world.insert((), vec![()]).first().unwrap();
self.current_entity = Some(entity);
self.add_parent_to_current_entity();
self
}
pub fn build(self) {}
// note: this is slow and does a full entity copy
pub fn add<T>(self, component: T) -> Self
/// note: this is slow and does a full entity copy
pub fn add<T>(&mut self, component: T) -> &mut Self
where
T: legion::storage::Component,
{
@ -47,7 +45,7 @@ impl<'a> WorldBuilder<'a> {
self
}
pub fn tag<T>(self, tag: T) -> Self
pub fn tag<T>(&mut self, tag: T) -> &mut Self
where
T: legion::storage::Tag,
{
@ -57,7 +55,7 @@ impl<'a> WorldBuilder<'a> {
self
}
pub fn add_entities<T, C>(self, tags: T, components: C) -> Self
pub fn add_entities<T, C>(&mut self, tags: T, components: C) -> &mut Self
where
T: TagSet + TagLayout + for<'b> Filter<ChunksetFilterData<'b>>,
C: IntoComponentSource,
@ -66,18 +64,18 @@ impl<'a> WorldBuilder<'a> {
self
}
pub fn add_entity(mut self, entity_archetype: impl EntityArchetype) -> Self {
pub fn add_entity(&mut self, entity_archetype: impl EntityArchetype) -> &mut Self {
let current_entity = entity_archetype.insert(self.world);
self.current_entity = Some(current_entity);
self.add_parent_to_current_entity();
self
}
pub fn add_children(mut self, build_children: impl Fn(WorldBuilder) -> WorldBuilder) -> Self {
pub fn add_children(&mut self, build_children: impl Fn(&mut WorldBuilder)) -> &mut Self {
self.parent_entity = self.current_entity;
self.current_entity = None;
self = build_children(self);
build_children(self);
self.current_entity = self.parent_entity;
self.parent_entity = None;

View file

@ -126,6 +126,5 @@ fn setup(world: &mut World, resources: &mut Resources) {
Vec3::new(0.0, 0.0, 1.0),
)),
..Default::default()
})
.build();
});
}

View file

@ -104,8 +104,7 @@ fn create_entities_builder_add_component(
Vec3::new(3.0, 8.0, 5.0),
Vec3::new(0.0, 0.0, 0.0),
Vec3::new(0.0, 0.0, 1.0),
)))
.build();
)));
}
fn create_entities_builder_archetype(
@ -143,8 +142,7 @@ fn create_entities_builder_archetype(
Vec3::new(0.0, 0.0, 1.0),
)),
..Default::default()
})
.build();
});
}
fn setup(world: &mut World, resources: &mut Resources) {

View file

@ -89,6 +89,5 @@ fn setup(world: &mut World, resources: &mut Resources) {
Vec3::new(0.0, 0.0, 1.0),
)),
..Default::default()
})
.build();
});
}

View file

@ -44,8 +44,8 @@ fn setup(world: &mut World, resources: &mut Resources) {
..Default::default()
});
let mut builder = world
.build()
let mut builder = world.build();
builder
// plane
.add_entity(MeshEntity {
mesh: plane_handle,
@ -84,7 +84,7 @@ fn setup(world: &mut World, resources: &mut Resources) {
),
..Default::default()
});
builder = builder.add_entity(MeshEntity {
builder.add_entity(MeshEntity {
mesh: cube_handle,
material: spawned_material_handle,
translation: Translation::new(
@ -97,8 +97,6 @@ fn setup(world: &mut World, resources: &mut Resources) {
..Default::default()
},
..Default::default()
})
});
}
builder.build();
}

View file

@ -52,7 +52,7 @@ fn setup(world: &mut World, resources: &mut Resources) {
material: cube_material_handle,
translation: Translation::new(0.0, 0.0, 3.0),
..Default::default()
})
});
})
// light
.add_entity(LightEntity {
@ -67,6 +67,5 @@ fn setup(world: &mut World, resources: &mut Resources) {
Vec3::new(0.0, 0.0, 1.0),
)),
..Default::default()
})
.build();
});
}

View file

@ -53,6 +53,5 @@ fn setup(world: &mut World, resources: &mut Resources) {
Vec3::new(0.0, 0.0, 1.0),
)),
..Default::default()
})
.build();
});
}

View file

@ -44,8 +44,8 @@ fn setup(world: &mut World, resources: &mut Resources) {
..Default::default()
});
let mut builder = world
.build()
let mut builder = world.build();
builder
// plane
.add_entity(MeshEntity {
mesh: plane_handle,
@ -84,7 +84,7 @@ fn setup(world: &mut World, resources: &mut Resources) {
),
..Default::default()
});
builder = builder.add_entity(MeshEntity {
builder.add_entity(MeshEntity {
mesh: cube_handle,
material: spawned_material_handle,
translation: Translation::new(
@ -93,8 +93,6 @@ fn setup(world: &mut World, resources: &mut Resources) {
0.0,
),
..Default::default()
})
});
}
builder.build();
}

View file

@ -71,6 +71,5 @@ fn setup(world: &mut World, resources: &mut Resources) {
Vec3::new(0.0, 0.0, 1.0),
)),
..Default::default()
})
.build();
});
}

View file

@ -110,7 +110,7 @@ fn setup(world: &mut World, resources: &mut Resources) {
Margins::new(20.0, 20.0, 20.0, 20.0),
Color::rgb(0.6, 0.6, 1.0),
),
})
});
})
// alpha test
.add_entity(UiEntity {
@ -120,6 +120,5 @@ fn setup(world: &mut World, resources: &mut Resources) {
Margins::new(0.0, 100.0, 0.0, 100.0),
Color::rgba(1.0, 0.9, 0.9, 0.4),
),
})
.build();
});
}

View file

@ -27,7 +27,8 @@ fn build_move_system() -> Box<dyn Schedulable> {
}
fn setup(world: &mut World, _resources: &mut Resources) {
let mut builder = world.build().add_entity(Camera2dEntity {
let mut builder = world.build();
builder.add_entity(Camera2dEntity {
camera: Camera::new(CameraType::default_orthographic()),
..Default::default()
});
@ -37,7 +38,7 @@ fn setup(world: &mut World, _resources: &mut Resources) {
for i in 0..count {
// 2d camera
let cur = Vec2::new(1.0, 1.0) * 1.0 + prev;
builder = builder.add_entity(UiEntity {
builder.add_entity(UiEntity {
node: Node::new(
math::vec2(75.0, 75.0) + cur,
Anchors::new(0.5, 0.5, 0.5, 0.5),
@ -48,6 +49,4 @@ fn setup(world: &mut World, _resources: &mut Resources) {
prev = cur;
}
builder.build();
}