Struct ecs_tiny::ECS

source ·
pub struct ECS { /* private fields */ }
Expand description

Entity-Component-System.

§Examples

let mut ecs = ecs_tiny::ECS::new();

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

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

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

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

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

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

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

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

§

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>,

§

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.