Trait rinecs::entity::EntitiesExt[][src]

pub trait EntitiesExt<'a> {
Show methods fn iter_for<'e, S: UnorderedDataSend<'e> + ReadOnlyOp<'e>>(
        &'e self
    ) -> <S as UnorderedData<'e>>::Iter;
fn iter_for_mut<'e, S: UnorderedDataSend<'e>>(
        &'e mut self
    ) -> <S as UnorderedData<'e>>::IterMut;
fn ordered_iter_for<'e, S: OrderedDataSend<'e> + ReadOnlyOrderedOp<'e>>(
        &'e self
    ) -> <S as OrderedData<'e>>::Iter;
fn ordered_iter_for_mut<'e, S: OrderedDataSend<'e>>(
        &'e mut self
    ) -> <S as OrderedData<'e>>::Iter;
fn iter_for_entities<'e, U, E>(&'e self, entities: E) -> E::IntoEntitiesIter
    where
        E: IntoEntitiesIterator<'e, U::Storage>,
        U: UnorderedDataSend<'e>,
        U::Storage: ReadOnlyStorage
;
fn iter_for_entities_mut<'e, U, E>(
        &'e mut self,
        entities: E
    ) -> E::IntoEntitiesIterMut
    where
        E: IntoEntitiesIterator<'e, U::Storage>,
        U: UnorderedDataSend<'e>
;
fn iter_for_entities_opt<'e, U, E>(
        &'e self,
        entities: E
    ) -> E::IntoEntitiesOptIter
    where
        E: IntoEntitiesIterator<'e, U::Storage>,
        U: UnorderedDataSend<'e>,
        U::Storage: ReadOnlyStorage
;
fn iter_for_entities_opt_mut<'e, U, E>(
        &'e mut self,
        entities: E
    ) -> E::IntoEntitiesOptIterMut
    where
        E: IntoEntitiesIterator<'e, U::Storage>,
        U: UnorderedDataSend<'e>
;
fn entity_components<'r, S>(
        &'r self,
        entity: &Entity
    ) -> Option<<S as UnorderedData<'r>>::ComponentsRef>
    where
        S: UnorderedDataSend<'r> + ReadOnlyOp<'r> + 'r,
        S::Storage: StorageRef<'r, Component = S::ComponentsRef>
;
fn entity_components_mut<'r, S>(
        &'r mut self,
        entity: &Entity
    ) -> Option<<S as UnorderedData<'r>>::ComponentsRef>
    where
        S: UnorderedDataSend<'r> + 'r,
        S::Storage: StorageRef<'r, Component = S::ComponentsRef>
;
fn component_for<C: ComponentSend>(
        &self,
        entity: &Entity
    ) -> Option<Ptr<'_, C>>
    where
        <C as Component>::Storage: Storage<'s, C>
;
fn component_for_mut<C: ComponentSend>(
        &self,
        entity: &Entity
    ) -> Option<PtrMut<'_, C>>
    where
        <C as Component>::Storage: Storage<'s, C>
;
fn has_component<C: 'static>(&self, entity: &Entity) -> bool;
fn tree_node_for<C: ComponentSend>(
        &self,
        entity: &Entity
    ) -> Option<NodePtr<'_, C>>
    where
        <C as Component>::Storage: HierarchicalStorage<'b, C>
;
fn tree_node_for_mut<C: ComponentSend>(
        &self,
        entity: &Entity
    ) -> Option<NodePtrMut<'_, C>>
    where
        <C as Component>::Storage: HierarchicalStorage<'b, C>
;
fn changed_iter_for<'r, S>(
        &'r self
    ) -> EntitiesComponentIter<'r, S::ChangedIter, <S as UnorderedData<'r>>::Storage>
    where
        S: ChangedDataSend<'r> + EntitiesData + ReadOnlyOp<'r> + 'r,
        <S as UnorderedData<'r>>::Storage: StorageRef<'r>
;
fn changed_iter_for_mut<'r, S>(
        &'r mut self
    ) -> EntitiesComponentIter<'r, S::ChangedIter, <S as UnorderedData<'r>>::Storage>
    where
        S: ChangedDataSend<'r> + EntitiesData + 'r,
        <S as UnorderedData<'r>>::Storage: StorageRef<'r>
;
fn storage_for<'r, S>(&'r self) -> Sto<'r, S>
    where
        S: UnorderedDataSend<'r> + ReadOnlyOp<'r>
;
fn storage_for_mut<'r, S>(&'r mut self) -> Sto<'r, S>
    where
        S: UnorderedDataSend<'r>
;
}

Common trait for all Entities like objects.

Allows access and modification of Send components. Useful to create functions that can be used from different systems and need an Entities like parameter.

Required methods

fn iter_for<'e, S: UnorderedDataSend<'e> + ReadOnlyOp<'e>>(
    &'e self
) -> <S as UnorderedData<'e>>::Iter
[src]

Iterator over all the components that match the operator

for pos in entities.iter_for::<Read<Position>>(){

}

Will iterate over all the entities that have both position and velocity and update the position by adding the velocity to it. In this example position can be modified cause we use the Write operator but velocity can only be read.

fn iter_for_mut<'e, S: UnorderedDataSend<'e>>(
    &'e mut self
) -> <S as UnorderedData<'e>>::IterMut
[src]

Iterator over all the components that match the operator

for (pos, vel) in entities.iter_for_mut::<(Write<Position>, Read<Velocity>)>(){
    pos.x += vel.x;
    pos.y += vel.y;
}

Will iterate over all the entities that have both position and velocity and update the position by adding the velocity to it. In this example position can be modified cause we use the Write operator but velocity can only be read.

fn ordered_iter_for<'e, S: OrderedDataSend<'e> + ReadOnlyOrderedOp<'e>>(
    &'e self
) -> <S as OrderedData<'e>>::Iter
[src]

Iterator over ordered data

Similar to iter_for but to be used with operators that specify an order like ReadAndParent in which parents will be processed first and then their children

fn ordered_iter_for_mut<'e, S: OrderedDataSend<'e>>(
    &'e mut self
) -> <S as OrderedData<'e>>::Iter
[src]

Iterator over ordered data

Similar to iter_for but to be used with operators that specify an order like ReadAndParent in which parents will be processed first and then their children

fn iter_for_entities<'e, U, E>(&'e self, entities: E) -> E::IntoEntitiesIter where
    E: IntoEntitiesIterator<'e, U::Storage>,
    U: UnorderedDataSend<'e>,
    U::Storage: ReadOnlyStorage
[src]

Iterator for the specified operators over a set of entities instead of every entity in the world

It’s usage is similar to iter_for but instead of iterating over all the entities it only returns components that match the operators for the entities passed as parameter

let e1 = world.new_entity()
    .add(Position{x: 0., y: 0.})
    .build();
let e2 = world.new_entity()
    .add(Position{x: 0., y: 0.})
    .build();
let e3 = world.new_entity()
    .add(Position{x: 0., y: 0.})
    .build();
let entities = world.entities();
for pos in entities.iter_for_entities::<Read<Position>, _>(&vec![e1, e2]){
    //...
}

The operators parameter in this method needs a second type for the entities iterator type which can jsut be elided with a _ since it’ll be guessed from the passed parameter

fn iter_for_entities_mut<'e, U, E>(
    &'e mut self,
    entities: E
) -> E::IntoEntitiesIterMut where
    E: IntoEntitiesIterator<'e, U::Storage>,
    U: UnorderedDataSend<'e>, 
[src]

fn iter_for_entities_opt<'e, U, E>(
    &'e self,
    entities: E
) -> E::IntoEntitiesOptIter where
    E: IntoEntitiesIterator<'e, U::Storage>,
    U: UnorderedDataSend<'e>,
    U::Storage: ReadOnlyStorage
[src]

fn iter_for_entities_opt_mut<'e, U, E>(
    &'e mut self,
    entities: E
) -> E::IntoEntitiesOptIterMut where
    E: IntoEntitiesIterator<'e, U::Storage>,
    U: UnorderedDataSend<'e>, 
[src]

fn entity_components<'r, S>(
    &'r self,
    entity: &Entity
) -> Option<<S as UnorderedData<'r>>::ComponentsRef> where
    S: UnorderedDataSend<'r> + ReadOnlyOp<'r> + 'r,
    S::Storage: StorageRef<'r, Component = S::ComponentsRef>, 
[src]

Similar to iter_for_entities but for one entity instead of several

let e1 = world.new_entity()
    .add(Position{x: 0., y: 0.})
    .add(Velocity{x: 0., y: 0.})
    .build();
let entities = world.entities();
let (pos, vel) = entities
    .entity_components::<(Read<Position>, Read<Velocity>)>(&e1)
    .unwrap();

fn entity_components_mut<'r, S>(
    &'r mut self,
    entity: &Entity
) -> Option<<S as UnorderedData<'r>>::ComponentsRef> where
    S: UnorderedDataSend<'r> + 'r,
    S::Storage: StorageRef<'r, Component = S::ComponentsRef>, 
[src]

fn component_for<C: ComponentSend>(&self, entity: &Entity) -> Option<Ptr<'_, C>> where
    <C as Component>::Storage: Storage<'s, C>, 
[src]

Returns a component for reading for the passed entity

let e1 = world.new_entity()
    .add(Position{x: 0., y: 0.})
    .build();
let entities = world.entities();
let pos = entities.component_for::<Position>(&e1).unwrap();

fn component_for_mut<C: ComponentSend>(
    &self,
    entity: &Entity
) -> Option<PtrMut<'_, C>> where
    <C as Component>::Storage: Storage<'s, C>, 
[src]

Returns a component for writing for the passed entity

let e1 = world.new_entity()
    .add(Position{x: 0., y: 0.})
    .build();
let entities = world.entities();
let mut pos = entities.component_for_mut::<Position>(&e1).unwrap();

fn has_component<C: 'static>(&self, entity: &Entity) -> bool[src]

Returns true if the entity has the specified component

let e1 = world.new_entity()
    .add(Position{x: 0., y: 0.})
    .build();
let entities = world.entities();
assert!(entities.has_component::<Position>(&e1));

fn tree_node_for<C: ComponentSend>(
    &self,
    entity: &Entity
) -> Option<NodePtr<'_, C>> where
    <C as Component>::Storage: HierarchicalStorage<'b, C>, 
[src]

Returns the node for a hierarchical component

fn tree_node_for_mut<C: ComponentSend>(
    &self,
    entity: &Entity
) -> Option<NodePtrMut<'_, C>> where
    <C as Component>::Storage: HierarchicalStorage<'b, C>, 
[src]

Returns the node for a hierarchical component for writing

fn changed_iter_for<'r, S>(
    &'r self
) -> EntitiesComponentIter<'r, S::ChangedIter, <S as UnorderedData<'r>>::Storage> where
    S: ChangedDataSend<'r> + EntitiesData + ReadOnlyOp<'r> + 'r,
    <S as UnorderedData<'r>>::Storage: StorageRef<'r>, 
[src]

Iterator for the specified operators only over the entities whose component as specified in the first operator has changed

The first component specified has to implement the Changes trait and have a Changed or AutoChanged storage

Changed and Autochanged storages keep an index of which elements have changed. This method uses that index and will only ever iterate over elements that have changed. So it can greatly accelerate some operations when we only need to access elements that changed during the current run.

To get a correct result this method should only be used after running changes_iter_with for Changed storages or update_changed for AutoChanged storages.

With an AutoChanged storage, the component needs to implement the Changes trait which informs rinecs if the component did change and allows to reset it’s changed state at the end of the run:

#[derive(Component, Debug)]
#[storage(AutoChangedDenseVec)]
struct Position{x: i32, y: i32, changed: bool}

impl Position {
    pub fn new(x: i32, y: i32) -> Position {
        Position{ x, y, changed: false}
    }

    pub fn set(&mut self, x: i32, y: i32) {
        self.changed = self.x != x || self.y != y;
        self.x = x;
        self.y = y;
    }
}

impl Changes for Position{
    fn has_changed(&self) -> bool{
        self.changed
    }

    fn reset_changed(&mut self){
        self.changed = false;
    }
}

let e1 = world.new_entity()
    .add(Position::new(0, 0))
    .build();
let e2 = world.new_entity()
    .add(Position::new(0, 0))
    .build();
let e3 = world.new_entity()
    .add(Position::new(0, 0))
    .build();
let mut entities = world.entities();
let mut v = 0;
let unique_entities = UniqueEntities::try_new(vec![e1, e2]).unwrap();
for pos in entities.iter_for_entities_mut::<Write<Position>, _>(&unique_entities) {
    pos.set(v, v);
    v += 1;
}

entities.update_changed::<Position>();

// will only iterate over e2 since e1 was set to the same value and e3 was never touched
let mut iter = entities.changed_iter_for::<(Read<Position>, Entity)>();
assert_eq!(Some(e2), iter.next().map(|(_,e)| e));
assert_eq!(None, iter.next().map(|(_,e)| e));

With a Changed storage, the component doesn’t need any trait but we need to manually inform rinecs if the component did change by only updating it using the changes_iter_with method before being able to use update_changed. This method is faster and should be use when we can ensure that the changed state of a component will only change on this system:

#[derive(Component, Debug)]
#[changes]
struct Position{x: i32, y: i32}

impl Position {
    pub fn new(x: i32, y: i32) -> Position {
        Position{ x, y }
    }

    pub fn update(&mut self, x: i32, y: i32) -> bool {
        let changed = self.x != x || self.y != y;
        self.x = x;
        self.y = y;
        changed
    }
}

let e1 = world.new_entity()
    .add(Position::new(0, 0))
    .build();
let e2 = world.new_entity()
    .add(Position::new(0, 0))
    .build();
let e3 = world.new_entity()
    .add(Position::new(0, 0))
    .build();
let mut entities = world.entities();
let mut v = 0;
entities.changes_iter_with::<(Write<Position>, Entity), _>(|(pos, e)| {
    if e != e3 {
        let changed = pos.update(v, v);
        v += 1;
        changed
    }else{
        false
    }
});

// will only iterate over e2 since e1 was set to the same value and e3 was never touched
let mut iter = entities.changed_iter_for::<(Read<Position>, Entity)>();
assert_eq!(Some(e2), iter.next().map(|(_,e)| e));
assert_eq!(None, iter.next().map(|(_,e)| e));

fn changed_iter_for_mut<'r, S>(
    &'r mut self
) -> EntitiesComponentIter<'r, S::ChangedIter, <S as UnorderedData<'r>>::Storage> where
    S: ChangedDataSend<'r> + EntitiesData + 'r,
    <S as UnorderedData<'r>>::Storage: StorageRef<'r>, 
[src]

fn storage_for<'r, S>(&'r self) -> Sto<'r, S> where
    S: UnorderedDataSend<'r> + ReadOnlyOp<'r>, 
[src]

Storage for the operator passed as type parameter

Calling iter_for can be slow if it needs to be called lots of times. In those cases calling instead storage_for once and then iter() on it multiple times can make performance much better

let mut storage = entities.storage_for_mut::<(Write<Position>, Read<Velocity>)>().unwrap();
for i in 0..1000000 {
    for (pos, vel) in storage.iter_mut() {
        pos.x += vel.x;
        pos.y += vel.y;
    }
}

fn storage_for_mut<'r, S>(&'r mut self) -> Sto<'r, S> where
    S: UnorderedDataSend<'r>, 
[src]

Loading content...

Implementors

impl<'a> EntitiesExt<'a> for EntitiesDebug<'a>[src]

impl<'a> EntitiesExt<'a> for World[src]

impl<'a> EntitiesExt<'a> for Entities<'a>[src]

impl<'a> EntitiesExt<'a> for EntitiesCreation<'a>[src]

impl<'a> EntitiesExt<'a> for EntitiesThreadLocal<'a>[src]

Loading content...