pub struct ECS { /* private fields */ }
Expand description
Implementations§
source§impl ECS
impl ECS
sourcepub fn insert_entity(&mut self) -> u32
pub fn insert_entity(&mut self) -> u32
Insert a new entity and return the corresponding entity key.
§Examples
let mut ecs = ecs_tiny::ECS::new();
let entity_key = ecs.insert_entity();
sourcepub fn remove_entity(&mut self, entity_key: u32) -> Option<()>
pub fn remove_entity(&mut self, entity_key: u32) -> Option<()>
Remove an entity with the corresponding entity key. If the entity corresponding to the entity key is not found, return an Err(ECSError::NotFound). Otherwise, return an Ok(()).
§Examples
let mut ecs = ecs_tiny::ECS::new();
let entity_key = ecs.insert_entity();
ecs.remove_entity(entity_key).unwrap();
sourcepub fn get_entity(&self, entity_key: u32) -> Option<()>
pub fn get_entity(&self, entity_key: u32) -> Option<()>
Return entity with the corresponding entity key. If the entity corresponding to the entity key is not found, return an Err(ECSError::NotFound). Otherwise, return an Ok(()).
§Examples
let mut ecs = ecs_tiny::ECS::new();
let entity_key = ecs.insert_entity();
ecs.get_entity(entity_key).unwrap();
sourcepub fn iter_entity(&self) -> impl Iterator<Item = u32> + '_
pub fn iter_entity(&self) -> impl Iterator<Item = u32> + '_
Return an iterator over all entity keys.
§Examples
let mut ecs = ecs_tiny::ECS::new();
let entity_key0 = ecs.insert_entity();
let entity_key1 = ecs.insert_entity();
let entity_key2 = ecs.insert_entity();
let mut iter = ecs.iter_entity();
assert_eq!(iter.next(), Some(entity_key0));
assert_eq!(iter.next(), Some(entity_key1));
assert_eq!(iter.next(), Some(entity_key2));
assert_eq!(iter.next(), None);
sourcepub fn insert_comp<T: 'static>(
&mut self,
entity_key: u32,
comp: T
) -> Option<(TypeId, u32)>
pub fn insert_comp<T: 'static>( &mut self, entity_key: u32, comp: T ) -> Option<(TypeId, u32)>
Insert a new component with the corresponding entity key and return the corresponding component key. If the entity corresponding to the entity key is not found, return an Err(ECSError::NotFound). Otherwise, return an Ok(CompKey).
§Examples
let mut ecs = ecs_tiny::ECS::new();
let entity_key = ecs.insert_entity();
let comp_key = ecs.insert_comp(entity_key, 42).unwrap();
sourcepub fn remove_comp<T: 'static>(&mut self, comp_key: (TypeId, u32)) -> Option<T>
pub fn remove_comp<T: 'static>(&mut self, comp_key: (TypeId, u32)) -> Option<T>
Remove a component with the corresponding component key and type, and return the component. If the component corresponding to the component key and type is not found, return an Err(ECSError::NotFound). Otherwise, return an Ok(T).
§Examples
let mut ecs = ecs_tiny::ECS::new();
let entity_key = ecs.insert_entity();
let comp_key = ecs.insert_comp(entity_key, 42).unwrap();
let comp = ecs.remove_comp::<i32>(comp_key).unwrap();
assert_eq!(comp, 42);
sourcepub fn get_comp<T: 'static>(&self, comp_key: (TypeId, u32)) -> Option<&T>
pub fn get_comp<T: 'static>(&self, comp_key: (TypeId, u32)) -> Option<&T>
Return a component with the corresponding component key and type. If the component corresponding to the component key and type is not found, return an Err(ECSError::NotFound). Otherwise, return an Ok(&T).
§Examples
let mut ecs = ecs_tiny::ECS::new();
let entity_key = ecs.insert_entity();
let comp_key = ecs.insert_comp(entity_key, 42).unwrap();
let comp = ecs.get_comp::<i32>(comp_key).unwrap();
assert_eq!(comp, &42);
sourcepub fn get_comp_mut<T: 'static>(
&mut self,
comp_key: (TypeId, u32)
) -> Option<&mut T>
pub fn get_comp_mut<T: 'static>( &mut self, comp_key: (TypeId, u32) ) -> Option<&mut T>
Return a mutable component with the corresponding component key and type. If the component corresponding to the component key and type is not found, return an Err(ECSError::NotFound). Otherwise, return an Ok(&mut T).
§Examples
let mut ecs = ecs_tiny::ECS::new();
let entity_key = ecs.insert_entity();
let comp_key = ecs.insert_comp(entity_key, 42).unwrap();
let comp = ecs.get_comp_mut::<i32>(comp_key).unwrap();
assert_eq!(comp, &mut 42);
sourcepub fn iter_comp<T: 'static>(&self) -> Option<impl Iterator<Item = &T>>
pub fn iter_comp<T: 'static>(&self) -> Option<impl Iterator<Item = &T>>
Return an iterator over all components of the corresponding type. If the component type is not found, return an Err(ECSError::NotFound). Otherwise, return an Ok(impl Iterator<Item = &T>).
§Examples
let mut ecs = ecs_tiny::ECS::new();
let entity_key0 = ecs.insert_entity();
let entity_key1 = ecs.insert_entity();
ecs.insert_comp(entity_key0, 42).unwrap();
ecs.insert_comp(entity_key0, 63).unwrap();
ecs.insert_comp(entity_key1, 42).unwrap();
let mut iter = ecs.iter_comp::<i32>().unwrap();
assert_eq!(iter.next(), Some(&42));
assert_eq!(iter.next(), Some(&63));
assert_eq!(iter.next(), Some(&42));
assert_eq!(iter.next(), None);
sourcepub fn iter_comp_mut<T: 'static>(
&mut self
) -> Option<impl Iterator<Item = &mut T>>
pub fn iter_comp_mut<T: 'static>( &mut self ) -> Option<impl Iterator<Item = &mut T>>
Return a mutable iterator over all components of the corresponding type. If the component type is not found, return an Err(ECSError::NotFound). Otherwise, return an Ok(impl Iterator<Item = &mut T>).
§Examples
let mut ecs = ecs_tiny::ECS::new();
let entity_key0 = ecs.insert_entity();
let entity_key1 = ecs.insert_entity();
ecs.insert_comp(entity_key0, 42).unwrap();
ecs.insert_comp(entity_key0, 63).unwrap();
ecs.insert_comp(entity_key1, 42).unwrap();
let mut iter = ecs.iter_comp_mut::<i32>().unwrap();
assert_eq!(iter.next(), Some(&mut 42));
assert_eq!(iter.next(), Some(&mut 63));
assert_eq!(iter.next(), Some(&mut 42));
assert_eq!(iter.next(), None);
sourcepub fn get_entity_by_comp(&self, comp_key: (TypeId, u32)) -> Option<u32>
pub fn get_entity_by_comp(&self, comp_key: (TypeId, u32)) -> Option<u32>
Return an entity key with the corresponding component key. If the component corresponding to the component key is not found, return an Err(ECSError::NotFound). Otherwise, return an Ok(EntityKey).
§Examples
let mut ecs = ecs_tiny::ECS::new();
let entity_key0 = ecs.insert_entity();
let entity_key1 = ecs.insert_entity();
let comp_key0 = ecs.insert_comp(entity_key0, 42).unwrap();
let comp_key1 = ecs.insert_comp(entity_key0, 63).unwrap();
let comp_key2 = ecs.insert_comp(entity_key1, 42).unwrap();
let entity_key = ecs.get_entity_by_comp(comp_key0).unwrap();
assert_eq!(entity_key, entity_key0);
sourcepub fn iter_comp_by_entity<T: 'static>(
&self,
entity_key: u32
) -> Option<impl Iterator<Item = &T>>
pub fn iter_comp_by_entity<T: 'static>( &self, entity_key: u32 ) -> Option<impl Iterator<Item = &T>>
Return an iterator over all components with the corresponding entity key and type. If the entity corresponding to the entity key and type is not found, return an Err(ECSError::NotFound). Otherwise, return an Ok(impl Iterator<Item = &T>).
§Examples
let mut ecs = ecs_tiny::ECS::new();
let entity_key0 = ecs.insert_entity();
let entity_key1 = ecs.insert_entity();
ecs.insert_comp(entity_key0, 42).unwrap();
ecs.insert_comp(entity_key0, 63).unwrap();
ecs.insert_comp(entity_key1, 42).unwrap();
let mut iter = ecs.iter_comp_by_entity::<i32>(entity_key0).unwrap();
assert_eq!(iter.next(), Some(&42));
assert_eq!(iter.next(), Some(&63));
assert_eq!(iter.next(), None);
sourcepub fn iter_comp_mut_by_entity<T: 'static>(
&mut self,
entity_key: u32
) -> Option<impl Iterator<Item = &mut T>>
pub fn iter_comp_mut_by_entity<T: 'static>( &mut self, entity_key: u32 ) -> Option<impl Iterator<Item = &mut T>>
Return a mutable iterator over all components with the corresponding entity key and type. If the entity corresponding to the entity key and type is not found, return an Err(ECSError::NotFound). Otherwise, return an Ok(impl Iterator<Item = &mut T>).
§Examples
let mut ecs = ecs_tiny::ECS::new();
let entity_key0 = ecs.insert_entity();
let entity_key1 = ecs.insert_entity();
ecs.insert_comp(entity_key0, 42).unwrap();
ecs.insert_comp(entity_key0, 63).unwrap();
ecs.insert_comp(entity_key1, 42).unwrap();
let mut iter = ecs.iter_comp_mut_by_entity::<i32>(entity_key0).unwrap();
assert_eq!(iter.next(), Some(&mut 42));
assert_eq!(iter.next(), Some(&mut 63));
assert_eq!(iter.next(), None);