Struct bones_lib::ecs::entities::Entities

pub struct Entities { /* private fields */ }
Expand description

Holds a list of alive entities.

It also holds a list of entities that were recently killed, which allows to remove components of deleted entities at the end of a game frame.

Implementations§

§

impl Entities

pub fn single_with<Q>( &self, query: Q, ) -> (Entity, <<Q as QueryItem>::Iter as Iterator>::Item)
where Q: QueryItem,

Get a single entity and components in the given query if there is exactly one entity matching the query.

§Panics

This method panics if the number of matching entities is not exactly one.

pub fn get_single_with<Q>( &self, query: Q, ) -> Result<(Entity, <<Q as QueryItem>::Iter as Iterator>::Item), QuerySingleError>
where Q: QueryItem,

Get a single entity and components in the given query if there is exactly one entity matching the query.

pub fn first_with_bitset(&self, bitset: &BitSetVec) -> Entity

Get the first entity in the given bitset.

§Panics

This method panics if there are no entities in the bitset.

pub fn get_first_with_bitset(&self, bitset: &BitSetVec) -> Option<Entity>

Get the first entity in the given bitset.

pub fn first_with<Q>( &self, query: Q, ) -> (Entity, <<Q as QueryItem>::Iter as Iterator>::Item)
where Q: QueryItem,

Get the first entity and components in the given query.

§Panics

This method panics if there are no entities that match the query.

pub fn get_first_with<Q>( &self, query: Q, ) -> Option<(Entity, <<Q as QueryItem>::Iter as Iterator>::Item)>
where Q: QueryItem,

Get the first entity and components in the given query.

pub fn iter_with_bitset<'a>( &'a self, bitset: &'a BitSetVec, ) -> EntityIterator<'a>

Iterates over entities using the provided bitset.

pub fn iter_with<Q>( &self, query: Q, ) -> EntitiesIterWith<'_, <Q as QueryItem>::Iter>
where Q: QueryItem,

Iterate over the entities and components in the given query.

The QueryItem trait is automatically implemented for references to Comp and CompMut and for tuples of up to 26 items, so you can join over your mutable or immutable component borrows in your systems.

You can also pass a single component, to iterate only over the components that have alive entities.

§Example

fn my_system(entities: Res<Entities>, mut pos: CompMut<Pos>, vel: Comp<Vel>) {
    for (entity, (pos, vel)) in entities.iter_with((&mut pos, &vel)) {
        pos.x += vel.x;
        pos.y += vel.y;
    }
}

You may optionally iterate over components with &Optional(&comp) or mutably with &mut OptionalMut(&mut comp_mut). Entities are not filtered by component in OptionalQueryItem. None is returned for these. If done with single Optional query item, all entities are iterated over.

Syntax is &Optional(&comp), or &mut OptionalMut(&mut comp). Reference to comp and reference to Optional is required for now.

§Optional Example

fn my_system(entities: Res<Entities>, mut pos: CompMut<Pos>, vel: Comp<Vel>, pos_max: Comp<PosMax>) {
    for (entity, (pos, vel, pos_max)) in entities.iter_with((&mut pos, &vel, &Optional(&pos_max))) {
        // Update pos from vel on all entities that have pos and vel components
        pos.x += vel.x;
        pos.y += vel.y;

        // limit pos.x by pos_max.x if entity has PosMax component
        if let Some(pos_max) = pos_max {
            if pos.x > pos_max.x {
                pos.x = pos_max.x
            }
        }
    }
}

pub fn create(&mut self) -> Entity

Creates a new Entity and returns it.

This function will not reuse the index of an entity that is still in the killed entities.

pub fn is_alive(&self, entity: Entity) -> bool

Checks if the Entity is still alive.

Returns true if it is alive. Returns false if it has been killed.

pub fn kill(&mut self, entity: Entity)

Kill an entity.

pub fn all_cloned(&self) -> Vec<Entity>

Returns a list of all Entitys cloned into a new vec.

pub fn kill_all(&mut self)

Kills all entities.

pub fn killed(&self) -> &Vec<Entity>

Returns entities in the killed list.

pub fn clear_killed(&mut self)

Clears the killed entity list.

pub fn bitset(&self) -> &BitSetVec

Returns a bitset where each index where the bit is set to 1 indicates the index of an alive entity.

Useful for joining over Entity and ComponentStore<T> at the same time.

pub fn iter(&self) -> EntityIterator<'_>

Iterates over all alive entities.

Trait Implementations§

§

impl Clone for Entities

§

fn clone(&self) -> Entities

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
§

impl Debug for Entities

§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
§

impl Default for Entities

§

fn default() -> Entities

Returns the “default value” for a type. Read more
§

impl HasSchema for Entities

§

fn schema() -> &'static Schema

Get this type’s [Schema].
§

fn register_schema()

Register this schema with the global schema registry. Read more
§

fn cast<T>(this: &Self) -> &T
where T: HasSchema,

Cast a reference of this type to a reference of another type with the same memory layout. Read more
§

fn try_cast<T>(this: &Self) -> Result<&T, SchemaMismatchError>
where T: HasSchema,

Cast a reference of this type to a reference of another type with the same memory layout. Read more
§

fn cast_mut<T>(this: &mut Self) -> &mut T
where T: HasSchema,

Cast a mutable reference of this type to a reference of another type with the same memory layout. Read more
§

fn try_cast_mut<T>(this: &mut Self) -> Result<&mut T, SchemaMismatchError>
where T: HasSchema,

Cast a mutable reference of this type to a reference of another type with the same memory layout. Read more
§

fn as_schema_ref(&self) -> SchemaRef<'_>
where Self: Sized,

Converts a reference of T to a SchemaRef
§

fn as_schema_mut(&mut self) -> SchemaRefMut<'_>
where Self: Sized,

Converts a reference of T to a SchemaRefMut

Auto Trait Implementations§

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> 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> CloneToUninit for T
where T: Clone,

source§

default unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> FromWorld for T
where T: Default,

§

fn from_world(_world: &World) -> T

Creates Self using data from the given World.
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.

§

impl<T> RawClone for T
where T: Clone,

§

unsafe fn raw_clone(src: *const c_void, dst: *mut c_void)

Write the default value of the type to the pointer. Read more
§

fn raw_clone_cb() -> Unsafe<&'static (dyn Fn(*const c_void, *mut c_void) + Send + Sync)>

Get a callback suitable for [SchemaData].
§

impl<T> RawDefault for T
where T: Default,

§

unsafe fn raw_default(dst: *mut c_void)

Write the default value of the type to the pointer. Read more
§

fn raw_default_cb() -> Unsafe<&'static (dyn Fn(*mut c_void) + Send + Sync)>

Get a callback suitable for [SchemaData].
§

impl<T> RawDrop for T

§

unsafe fn raw_drop(ptr: *mut c_void)

Write the default value of the type to the pointer. Read more
§

fn raw_drop_cb() -> Unsafe<&'static (dyn Fn(*mut c_void) + Send + Sync)>

Get a callback suitable for [SchemaData].
source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

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

§

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>,

§

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.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V