Struct recs::Ecs
[−]
[src]
pub struct Ecs { /* fields omitted */ }
Primary data structure containing entity and component data.
Notice that Ecs
itself has no type parameters. Its methods to interact
with components do, but runtime reflection (via std::any::TypeId
) is
used to retrieve components from an internal HashMap
. Therefore, you
can create and use any data structure you want for components.
Tip: using #[derive(Clone)]
on your component types will make your life a little easier by
enabling the get
method, which avoids locking down the Ecs
with a mutable or immutable
borrow.
Methods
impl Ecs
[src]
fn new() -> Self
Create a new and empty entity-component system (ECS).
fn create_entity(&mut self) -> EntityId
Create a new entity in the ECS without components and return its ID.
fn exists(&self, id: EntityId) -> bool
Return true
if the provided entity exists in the system.
fn destroy_entity(&mut self, id: EntityId) -> EcsResult<()>
Destroy the provided entity, automatically removing any of its components.
Return NotFound::Entity
if the entity does not exist or was already deleted.
fn set<C: Component>(&mut self, id: EntityId, comp: C) -> EcsResult<Option<C>>
For the specified entity, add a component of type C
to the system.
If the entity already has a component prev
of type C
, return Some(prev)
. If not,
return None
. If the entity does not exist, return NotFound::Entity
.
To modify an existing component in place, see borrow_mut
.
fn get<C: Component + Clone>(&self, id: EntityId) -> EcsResult<C>
Return a clone of the requested entity's component of type C
, or a NotFound
variant
if the entity does not exist or does not have that component.
To examine or modify a component without making a clone, see borrow
and borrow_mut
.
fn has<C: Component>(&self, id: EntityId) -> EcsResult<bool>
Return true
if the specified entity has a component of type C
in the system, or
NotFound::Entity
if the entity does not exist.
fn has_all(&self, id: EntityId, set: &ComponentFilter) -> EcsResult<bool>
Return true
if each component type in the filter is present on the entity id
.
fn borrow<C: Component>(&self, id: EntityId) -> EcsResult<&C>
Return a shared reference to the requested entity's component of type C
, or a
NotFound
variant if the entity does not exist or does not have that component.
fn borrow_mut<C: Component>(&mut self, id: EntityId) -> EcsResult<&mut C>
Return a mutable reference to the requested entity's component of type C
, or a
NotFound
variant if the entity does not exist or does not have that component.
fn iter<'a>(&'a self) -> Box<Iterator<Item = EntityId> + 'a>
Return an iterator over every ID in the system.
fn collect(&self, dest: &mut Vec<EntityId>)
Collect all entity IDs into a vector (after emptying the vector).
Useful for accessing entity IDs without borrowing the ECS.
fn collect_with<'a>(
&'a self,
components: &'a ComponentFilter,
dest: &mut Vec<EntityId>
)
&'a self,
components: &'a ComponentFilter,
dest: &mut Vec<EntityId>
)
Collect the IDs of all entities containing a certain set of component types into a vector.
After calling this method, the vector dest
will contain only those entities who have
at least each type of component specified in the filter.