Struct rin::ecs::EntityStorages[][src]

pub struct EntityStorages<'a> { /* fields omitted */ }

Allows to access and modify the Send components of the entities in the world.

Implementations

impl<'a> EntityStorages<'a>[src]

pub fn storage_for<'r, S>(&'r self) -> Sto<'r, S> where
    S: UnorderedDataSend<'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;
    }
}

pub fn ordered_storage_for<'e, S>(
    &'e self
) -> Option<<S as OrderedData<'e>>::Storage> where
    S: OrderedDataSend<'e>, 
[src]

Storage for the operator passed as type parameter

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

pub fn changed_iter_for<'r, S>(
    &'r self
) -> EntitiesComponentIter<'r, <S as ChangedData<'r>>::ChangedIter, <S as UnorderedData<'r>>::Storage> where
    S: ChangedDataSend<'r> + EntitiesData + '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)]
#[autochanges]
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));

pub fn has_storage_changed<C>(&self) -> bool where
    C: ComponentSend,
    <C as Component>::Storage: for<'s> Storage<'s, C>,
    <C as Component>::Storage: ChangedStorageExt<'a>, 
[src]

Returns true if a Changed or AutoChanged storage changed during the last run

This will return true if any compoennt was modified (returns true on has_changed) but also if any was inserted or removed from the storage.

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.

pub fn changes_iter_with<'r, S, F>(&'r mut self, changed: F) where
    F: FnMut(<S as UnorderedData<'r>>::ComponentsRef) -> bool,
    S: ChangesDataSend<'r>,
    <S as UnorderedData<'r>>::Storage: StorageRef<'r>, 
[src]

Update Changed storages

This method has to be called once per run to update Changed storages. It receives a function that usually updates the components and returns a boolean indicating if the component changed (true) or not (false).

This is usually faster than AutoChanged storages where the update of the components can happen anywhere but it’s less flexible cause only changes done in this call will be taken into account as changed

pub fn changes_ordered_iter_with<'r, S, F>(&'r mut self, changed: F) where
    F: FnMut(<S as OrderedData<'r>>::ComponentsRef) -> bool,
    S: ChangesOrderedDataSend<'r>, 
[src]

Update Changed storages using an ordered iterator

This method has to be called once per run to update Changed storages. It receives a function that usually updates the components and returns a boolean indicating if the component changed (true) or not (false).

This is usually faster than AutoChanged storages where the update of the components can happen anywhere but it’s less flexible cause only changes done in this call will be taken into account as changed

pub fn update_changed<C>(&self) where
    C: ComponentSend,
    <C as Component>::Storage: AutoChangedStorageExt<'a>, 
[src]

Update AutoChanged storages

This method has to be called once per run to update AutoChanged storages. It should be run after all the systems that modify the component and before using changed_iter_for on it.

pub fn component_for<C>(&'r self, entity: &Entity) -> Option<Ptr<'r, C>> where
    C: ComponentSend,
    <C as Component>::Storage: for<'s> 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();

pub fn component_for_mut<C>(&'r self, entity: &Entity) -> Option<PtrMut<'r, C>> where
    C: ComponentSend,
    <C as Component>::Storage: for<'s> 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();

pub fn has_component<C>(&self, entity: &Entity) -> bool where
    C: 'static, 
[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));

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

Returns the node for a hierarchical component

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

Returns the node for a hierarchical component for writing

pub fn clone(&self) -> EntityStorages<'_>[src]

Trait Implementations

impl<'a> EntitiesStorage for EntityStorages<'a>[src]

impl<'a> EntityStoragesExt<'a> for EntityStorages<'a>[src]

impl<'a> Send for EntityStorages<'a>[src]

impl<'a> Sync for EntityStorages<'a>[src]

Auto Trait Implementations

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

impl<'a> Unpin for EntityStorages<'a>

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

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Any for T where
    T: Any
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Downcast for T where
    T: Any
[src]

impl<T> DowncastSync for T where
    T: Any + Send + Sync
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<V> IntoPnt<V> for V[src]

impl<V> IntoVec<V> for V[src]

impl<T> Pointable for T[src]

type Init = T

The type for initializers.

impl<T> Same<T> for T[src]

type Output = T

Should always be Self

impl<SS, SP> SupersetOf<SS> for SP where
    SS: SubsetOf<SP>, 
[src]

impl<SS, SP> SupersetOf<SS> for SP where
    SS: SubsetOf<SP>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

impl<V, T> VZip<V> for T where
    V: MultiLane<T>, 
[src]