Struct bones_lib::ecs::prelude::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 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 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_with_bitset<'a>( &'a self, bitset: &'a BitSetVec ) -> EntityIterator<'a>

Iterates over entities using the provided bitset.

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) -> &Twhere 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 Twhere 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 Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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.

§

impl<T> FromWorld for Twhere T: Default,

§

fn from_world(_world: &World) -> T

Creates Self using data from the given World.
source§

impl<T, U> Into<U> for Twhere 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 Twhere 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 Twhere 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 Twhere 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 Twhere 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 Twhere 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 Twhere V: MultiLane<T>,

§

fn vzip(self) -> V