Struct EntityBuilder

Source
pub struct EntityBuilder<'a> { /* private fields */ }
Expand description

The entity builder, allowing to build an entity together with its components.

§Examples

use async_ecs::*;

struct Health(f32);

impl Component for Health {
    type Storage = HashMapStorage<Self>;
}

struct Pos {
    x: f32,
    y: f32,
}

impl Component for Pos {
    type Storage = DenseVecStorage<Self>;
}

let mut world = World::default();
world.register_component::<Health>();
world.register_component::<Pos>();

let entity = world
    .create_entity() // This call returns `EntityBuilder`
    .with(Health(4.0))
    .with(Pos { x: 1.0, y: 3.0 })
    .build(); // Returns the `Entity`

§Distinguishing Mandatory Components from Optional Components

use async_ecs::*;

struct MandatoryHealth(f32);

impl Component for MandatoryHealth {
    type Storage = HashMapStorage<Self>;
}

struct OptionalPos {
    x: f32,
    y: f32,
}

impl Component for OptionalPos {
    type Storage = DenseVecStorage<Self>;
}

let mut world = World::default();
world.register_component::<MandatoryHealth>();
world.register_component::<OptionalPos>();

let mut entitybuilder = world.create_entity().with(MandatoryHealth(4.0));

// something trivial to serve as our conditional
let include_optional = true;

if include_optional == true {
    entitybuilder = entitybuilder.with(OptionalPos { x: 1.0, y: 3.0 })
}

let entity = entitybuilder.build();

Implementations§

Source§

impl<'a> EntityBuilder<'a>

Source

pub fn new(world: &'a World) -> Self

Create new entity builder.

Trait Implementations§

Source§

impl<'a> Builder for EntityBuilder<'a>

Source§

fn with<T: Component>(self, c: T) -> Self

Inserts a component for this entity.

If a component was already associated with the entity, it will overwrite the previous component.

Source§

fn build(self) -> Entity

Finishes the building and returns the entity. As opposed to LazyBuilder, the components are available immediately.

Source§

impl Drop for EntityBuilder<'_>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<'a> Freeze for EntityBuilder<'a>

§

impl<'a> !RefUnwindSafe for EntityBuilder<'a>

§

impl<'a> Send for EntityBuilder<'a>

§

impl<'a> Sync for EntityBuilder<'a>

§

impl<'a> Unpin for EntityBuilder<'a>

§

impl<'a> !UnwindSafe for EntityBuilder<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Any for T
where T: Any,

Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> Resource for T
where T: Any + Send + Sync,