Struct ECS

Source
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

Source

pub fn new() -> Self

Create a new ECS instance.

§Examples
let mut ecs = ecs_tiny::ECS::new();
Source

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();
Source

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();
Source

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();
Source

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);
Source

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();
Source

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();
Source

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();
Source

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);
Source

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);
Source

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);
Source

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);
Source

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);
Source

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);
Source

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);
Source

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);
Source

pub fn clear(&mut self)

Clear all entities and components.

§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.clear();

Trait Implementations§

Source§

impl Default for ECS

Source§

fn default() -> ECS

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

Auto Trait Implementations§

§

impl Freeze for ECS

§

impl RefUnwindSafe for ECS

§

impl Send for ECS

§

impl Sync for ECS

§

impl Unpin for ECS

§

impl UnwindSafe for ECS

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where 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 T
where 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 T
where U: Into<T>,

Source§

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 T
where U: TryFrom<T>,

Source§

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.