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

A container of entities.

Implementations§

source§

impl EntityStorage

source

pub fn new() -> EntityStorage

Creates an empty EntityStorage.

source

pub fn add<S: ArchetypeState>(&mut self, state: S) -> EntityId

Creates a new entity and returns its identifier.

source

pub fn get_archetype<A: StaticArchetype>(&self) -> Option<&ArchetypeStorage>

Returns a reference to the specified archetype.

source

pub fn get_archetype_mut<A: StaticArchetype>( &mut self ) -> Option<&mut ArchetypeStorage>

Returns a mutable reference to the specified archetype.

source

pub fn type_id_to_archetype_id(&self, type_id: &TypeId) -> Option<ArchetypeId>

Maps the specified TypeId to respective ArchetypeId. If the storage doesn’t contain an archetype of type type_id, it returns None.

source

pub fn get_archetype_by_id(&self, id: ArchetypeId) -> Option<&ArchetypeStorage>

Returns a reference to the specified archetype.

source

pub fn get_mut_archetype_by_id( &mut self, id: ArchetypeId ) -> Option<&mut ArchetypeStorage>

Returns a mutable reference to the specified archetype.

source

pub fn contains(&self, entity: &EntityId) -> bool

Returns true if the storage contains the specified entity.

source

pub fn get<C: Component>(&self, entity: &EntityId) -> Option<&C>

Returns a reference to the component C of the specified entity.

source

pub fn get_mut<C: Component>(&mut self, entity: &EntityId) -> Option<&mut C>

Returns a mutable reference to the component C of the specified entity.

source

pub fn get_state<S: StaticArchetype>(&self, entity_id: &EntityId) -> Option<&S>

Returns a reference to the state at entity_id. Panics if TypeId of S is not equal to the type of the underlying archetype.

source

pub fn get_state_mut<S: StaticArchetype>( &mut self, entity_id: &EntityId ) -> Option<&mut S>

Returns a mutable reference to the state at entity_id. Panics if TypeId of S is not equal to the type of the underlying archetype.

source

pub fn entry(&self, entity: &EntityId) -> Option<Entry<'_>>

Returns an entry of entity in the corresponding archetype.

source

pub fn entry_mut(&mut self, entity: &EntityId) -> Option<EntryMut<'_>>

Returns a mutable entry of entity in the corresponding archetype.

source

pub fn remove(&mut self, entity: &EntityId) -> bool

Removes an entity from the storage. Returns true if the entity was present in the storage.

source

pub fn entities(&self) -> AllEntities<'_>

source

pub fn n_archetypes(&mut self) -> usize

Returns the number of entities in the storage.

source

pub fn count_entities(&self) -> usize

Returns the number of entities in the storage.

source§

impl EntityStorage

source

pub fn access(&mut self) -> SystemAccess<'_>

Provides access to all components. Allows simultaneous mutable access to multiple components.

source

pub fn dispatch<'a>(&self, systems: impl AsMut<[System<'a>]>)

Dispatches systems sequentially. For parallel execution, see dispatch_par (requires rayon feature).

Example
use entity_data::{EntityId, EntityStorage, System, SystemHandler};
use entity_data::system::SystemAccess;
use macros::Archetype;

#[derive(Default, Debug)]
struct Position {
    x: f32,
    y: f32,
}

#[derive(Archetype)]
struct Dog {
    pos: Position,
}

let mut storage = EntityStorage::new();
let dog0 = storage.add(Dog { pos: Default::default() });
let dog1 = storage.add(Dog { pos: Position { x: 3.0, y: 5.0 } });

struct PositionsPrintSystem {
    to_process: Vec<EntityId>,
}

impl SystemHandler for PositionsPrintSystem {
    fn run(&mut self, data: SystemAccess) {
        let positions = data.component::<Position>();
        for entity in &self.to_process {
            println!("{:?}", positions.get(entity));
        }
    }
}

let mut sys = PositionsPrintSystem {
    to_process: vec![dog0, dog1]
};
storage.dispatch(&mut [System::new(&mut sys).with::<Position>()]);

Trait Implementations§

source§

impl Default for EntityStorage

source§

fn default() -> EntityStorage

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

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.

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.

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

impl<T> Component for Twhere T: Send + Sync + 'static,