Struct despero::ecs::EntityBuilder
source · pub struct EntityBuilder { /* private fields */ }
Expand description
Helper for incrementally constructing a bundle of components with dynamic component types
Prefer reusing the same builder over creating new ones repeatedly.
let mut world = World::new();
let mut builder = EntityBuilder::new();
builder.add(123).add("abc");
let e = world.spawn(builder.build()); // builder can now be reused
assert_eq!(*world.get::<&i32>(e).unwrap(), 123);
assert_eq!(*world.get::<&&str>(e).unwrap(), "abc");
Implementations§
source§impl EntityBuilder
impl EntityBuilder
sourcepub fn new() -> EntityBuilder
pub fn new() -> EntityBuilder
Create a builder representing an entity with no components
sourcepub fn add<T>(&mut self, component: T) -> &mut EntityBuilderwhere
T: Component,
pub fn add<T>(&mut self, component: T) -> &mut EntityBuilderwhere T: Component,
Add component
to the entity.
If the bundle already contains a component of type T
, it will
be dropped and replaced with the most recently added one.
sourcepub fn add_bundle(&mut self, bundle: impl DynamicBundle) -> &mut EntityBuilder
pub fn add_bundle(&mut self, bundle: impl DynamicBundle) -> &mut EntityBuilder
Add all components in bundle
to the entity.
If the bundle contains any component which matches the type of a component
already in the EntityBuilder
, the newly added component from the bundle
will replace the old component and the old component will be dropped.
sourcepub fn build(&mut self) -> BuiltEntity<'_>
pub fn build(&mut self) -> BuiltEntity<'_>
Construct a Bundle
suitable for spawning
sourcepub fn has<T>(&self) -> boolwhere
T: Component,
pub fn has<T>(&self) -> boolwhere T: Component,
Checks to see if the component of type T
exists
sourcepub fn get<'a, T>(&'a self) -> Option<T>where
T: ComponentRefShared<'a>,
pub fn get<'a, T>(&'a self) -> Option<T>where T: ComponentRefShared<'a>,
Borrow a shared reference T
to some component type, if it exists
Takes a reference as its type parameter for consistency with
EntityRef::get
, even though it cannot be a unique reference
because EntityBuilder
does not perform dynamic borrow checking.
sourcepub fn get_mut<'a, T>(&'a mut self) -> Option<T>where
T: ComponentRef<'a>,
pub fn get_mut<'a, T>(&'a mut self) -> Option<T>where T: ComponentRef<'a>,
Borrow a shared or unique reference T
to some component type, if it exists
sourcepub fn component_types(&self) -> impl Iterator<Item = TypeId>
pub fn component_types(&self) -> impl Iterator<Item = TypeId>
Enumerate the types of the entity builder’s components
Trait Implementations§
source§impl Default for EntityBuilder
impl Default for EntityBuilder
source§fn default() -> EntityBuilder
fn default() -> EntityBuilder
Auto Trait Implementations§
impl RefUnwindSafe for EntityBuilder
impl Send for EntityBuilder
impl Sync for EntityBuilder
impl Unpin for EntityBuilder
impl UnwindSafe for EntityBuilder
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere T: Any,
§fn into_any(self: Box<T, Global>) -> Box<dyn Any + 'static, Global>
fn into_any(self: Box<T, Global>) -> Box<dyn Any + 'static, Global>
Box<dyn Trait>
(where Trait: Downcast
) to Box<dyn Any>
. Box<dyn Any>
can
then be further downcast
into Box<ConcreteType>
where ConcreteType
implements Trait
.§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any + 'static>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any + 'static>
Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
. Rc<Any>
can then be
further downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
.§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &Any
’s vtable from &Trait
’s.§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s.source§impl<T> Instrument for T
impl<T> Instrument for T
source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
§impl<T> Pointable for T
impl<T> Pointable for T
source§impl<R, P> ReadPrimitive<R> for Pwhere
R: Read + ReadEndian<P>,
P: Default,
impl<R, P> ReadPrimitive<R> for Pwhere R: Read + ReadEndian<P>, P: Default,
source§fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
ReadEndian::read_from_little_endian()
.§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere SS: SubsetOf<SP>,
§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self
from the equivalent element of its
superset. Read more§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self
is actually part of its subset T
(and can be converted to it).§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset
but without any property checks. Always succeeds.§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self
to the equivalent element of its superset.