Struct entity_data::entity_storage::EntityStorage
source · pub struct EntityStorage { /* private fields */ }
Expand description
A container of entities.
Implementations§
source§impl EntityStorage
impl EntityStorage
sourcepub fn new() -> EntityStorage
pub fn new() -> EntityStorage
Creates an empty EntityStorage
.
sourcepub fn add<S: ArchetypeState>(&mut self, state: S) -> EntityId
pub fn add<S: ArchetypeState>(&mut self, state: S) -> EntityId
Creates a new entity and returns its identifier.
sourcepub fn get_archetype<A: StaticArchetype>(&self) -> Option<&ArchetypeStorage>
pub fn get_archetype<A: StaticArchetype>(&self) -> Option<&ArchetypeStorage>
Returns a reference to the specified archetype.
sourcepub fn get_archetype_mut<A: StaticArchetype>(
&mut self
) -> Option<&mut ArchetypeStorage>
pub fn get_archetype_mut<A: StaticArchetype>( &mut self ) -> Option<&mut ArchetypeStorage>
Returns a mutable reference to the specified archetype.
sourcepub fn type_id_to_archetype_id(&self, type_id: &TypeId) -> Option<ArchetypeId>
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
.
sourcepub fn get_archetype_by_id(&self, id: ArchetypeId) -> Option<&ArchetypeStorage>
pub fn get_archetype_by_id(&self, id: ArchetypeId) -> Option<&ArchetypeStorage>
Returns a reference to the specified archetype.
sourcepub fn get_mut_archetype_by_id(
&mut self,
id: ArchetypeId
) -> Option<&mut ArchetypeStorage>
pub fn get_mut_archetype_by_id( &mut self, id: ArchetypeId ) -> Option<&mut ArchetypeStorage>
Returns a mutable reference to the specified archetype.
sourcepub fn contains(&self, entity: &EntityId) -> bool
pub fn contains(&self, entity: &EntityId) -> bool
Returns true
if the storage contains the specified entity.
sourcepub fn get<C: Component>(&self, entity: &EntityId) -> Option<&C>
pub fn get<C: Component>(&self, entity: &EntityId) -> Option<&C>
Returns a reference to the component C
of the specified entity.
sourcepub fn get_mut<C: Component>(&mut self, entity: &EntityId) -> Option<&mut C>
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.
sourcepub fn get_state<S: StaticArchetype>(&self, entity_id: &EntityId) -> Option<&S>
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.
sourcepub fn get_state_mut<S: StaticArchetype>(
&mut self,
entity_id: &EntityId
) -> Option<&mut S>
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.
sourcepub fn entry(&self, entity: &EntityId) -> Option<Entry<'_>>
pub fn entry(&self, entity: &EntityId) -> Option<Entry<'_>>
Returns an entry of entity
in the corresponding archetype.
sourcepub fn entry_mut(&mut self, entity: &EntityId) -> Option<EntryMut<'_>>
pub fn entry_mut(&mut self, entity: &EntityId) -> Option<EntryMut<'_>>
Returns a mutable entry of entity
in the corresponding archetype.
sourcepub fn remove(&mut self, entity: &EntityId) -> bool
pub fn remove(&mut self, entity: &EntityId) -> bool
Removes an entity from the storage. Returns true
if the entity was present in the storage.
pub fn entities(&self) -> AllEntities<'_>
sourcepub fn n_archetypes(&mut self) -> usize
pub fn n_archetypes(&mut self) -> usize
Returns the number of entities in the storage.
sourcepub fn count_entities(&self) -> usize
pub fn count_entities(&self) -> usize
Returns the number of entities in the storage.
source§impl EntityStorage
impl EntityStorage
sourcepub fn access(&mut self) -> SystemAccess<'_>
pub fn access(&mut self) -> SystemAccess<'_>
Provides access to all components. Allows simultaneous mutable access to multiple components.
sourcepub fn dispatch<'a>(&self, systems: impl AsMut<[System<'a>]>)
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>()]);