pub struct ECS { /* private fields */ }Expand description
A minimal ECS supporting entity and component insertion/removal, association, and single-type iteration.
§Examples
let mut ecs = ecs_tiny::ECS::new();
let entity_key = ecs.insert_entity();
ecs.register::<i32>().unwrap();
let comp_key0 = ecs.insert_comp(entity_key, 42).unwrap();
let comp_key1 = ecs.insert_comp(entity_key, 63).unwrap();
for comp in ecs.iter_comp_mut::<i32>().unwrap() {
*comp += 1;
}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 None.
Otherwise, return an Some(()).
§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 None.
Otherwise, return an Some(()).
§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 register<T>(&mut self) -> Option<()>where
T: Any,
pub fn register<T>(&mut self) -> Option<()>where
T: Any,
Register component type.
§Examples
let mut ecs = ecs_tiny::ECS::new();
let entity_key = ecs.insert_entity();
ecs.register::<i32>().unwrap();
let comp_key = ecs.insert_comp(entity_key, 42).unwrap();Sourcepub fn unregister<T>(&mut self) -> Option<()>where
T: Any,
pub fn unregister<T>(&mut self) -> Option<()>where
T: Any,
Unregister component type.
§Examples
let mut ecs = ecs_tiny::ECS::new();
let entity_key = ecs.insert_entity();
ecs.register::<i32>().unwrap();
let comp_key = ecs.insert_comp(entity_key, 42).unwrap();
ecs.unregister::<i32>().unwrap();Sourcepub fn insert_comp<T>(
&mut self,
entity_key: u32,
comp: T,
) -> Option<(TypeId, u32)>where
T: Any,
pub fn insert_comp<T>(
&mut self,
entity_key: u32,
comp: T,
) -> Option<(TypeId, u32)>where
T: Any,
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 None.
Otherwise, return an Some(CompKey).
§Examples
let mut ecs = ecs_tiny::ECS::new();
let entity_key = ecs.insert_entity();
ecs.register::<i32>().unwrap();
let comp_key = ecs.insert_comp(entity_key, 42).unwrap();Sourcepub fn remove_comp<T>(&mut self, comp_key: (TypeId, u32)) -> Option<T>where
T: Any,
pub fn remove_comp<T>(&mut self, comp_key: (TypeId, u32)) -> Option<T>where
T: Any,
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 None.
Otherwise, return an Some(T).
§Examples
let mut ecs = ecs_tiny::ECS::new();
let entity_key = ecs.insert_entity();
ecs.register::<i32>().unwrap();
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>(&self, comp_key: (TypeId, u32)) -> Option<&T>where
T: Any,
pub fn get_comp<T>(&self, comp_key: (TypeId, u32)) -> Option<&T>where
T: Any,
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 None.
Otherwise, return an Some(&T).
§Examples
let mut ecs = ecs_tiny::ECS::new();
let entity_key = ecs.insert_entity();
ecs.register::<i32>().unwrap();
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>(&mut self, comp_key: (TypeId, u32)) -> Option<&mut T>where
T: Any,
pub fn get_comp_mut<T>(&mut self, comp_key: (TypeId, u32)) -> Option<&mut T>where
T: Any,
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 None.
Otherwise, return an Some(&mut T).
§Examples
let mut ecs = ecs_tiny::ECS::new();
let entity_key = ecs.insert_entity();
ecs.register::<i32>().unwrap();
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>(&self) -> Option<impl Iterator<Item = &T>>where
T: Any,
pub fn iter_comp<T>(&self) -> Option<impl Iterator<Item = &T>>where
T: Any,
Return an iterator over all components of the corresponding type.
If the component type is not found, return an None.
Otherwise, return an Some(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.register::<i32>().unwrap();
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>(&mut self) -> Option<impl Iterator<Item = &mut T>>where
T: Any,
pub fn iter_comp_mut<T>(&mut self) -> Option<impl Iterator<Item = &mut T>>where
T: Any,
Return a mutable iterator over all components of the corresponding type.
If the component type is not found, return an None.
Otherwise, return an Some(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.register::<i32>().unwrap();
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 None.
Otherwise, return an Some(EntityKey).
§Examples
let mut ecs = ecs_tiny::ECS::new();
let entity_key0 = ecs.insert_entity();
let entity_key1 = ecs.insert_entity();
ecs.register::<i32>().unwrap();
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>(
&self,
entity_key: u32,
) -> Option<impl Iterator<Item = &T>>where
T: Any,
pub fn iter_comp_by_entity<T>(
&self,
entity_key: u32,
) -> Option<impl Iterator<Item = &T>>where
T: Any,
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 None.
Otherwise, return an Some(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.register::<i32>().unwrap();
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>(
&mut self,
entity_key: u32,
) -> Option<impl Iterator<Item = &mut T>>where
T: Any,
pub fn iter_comp_mut_by_entity<T>(
&mut self,
entity_key: u32,
) -> Option<impl Iterator<Item = &mut T>>where
T: Any,
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 None.
Otherwise, return an None(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.register::<i32>().unwrap();
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);