Struct recs::Ecs
[−]
[src]
pub struct Ecs { // some 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, provided
that they implement Clone
.
Tip: #[derive(Clone)]
will make your life a little easier :-)
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 with no components, and return its ID.
fn has_entity(&self, id: EntityId) -> bool
Return true
if the provided entity exists in this system.
fn destroy_entity(&mut self, id: EntityId) -> bool
Destroy the provided entity, automatically removing any of its components.
Return true
if the entity existed and was successfully deleted;
return false
if the provided entity ID was not found in the system.
fn set<C: Any + Clone>(&mut self, id: EntityId, comp: &C) -> Option<C>
For the specified entity, add a component of type C
to the system.
If the entity already has a component of type C
, it is returned
and overwritten.
To modify a component in place, see borrow_mut
.
Panics
Panics if the requested entity does not exist.
fn get<C: Any + Clone>(&self, id: EntityId) -> Option<C>
Return a clone of the requested entity's component of type C
, or
None
if the entity does not have that component.
To examine a component without copying, see borrow
.
Panics
Panics if the requested entity does not exist.
fn has<C: Any + Clone>(&self, id: EntityId) -> bool
Return true
Panics if the requested entity has a component of type C
.
fn borrow<C: Any + Clone>(&self, id: EntityId) -> Option<&C>
Return a reference to the requested entity's component of type C
, or
None
if the entity does not have that component.
Panics
Panics if the requested entity does not exist.
fn borrow_mut<C: Any + Clone>(&mut self, id: EntityId) -> Option<&mut C>
Return a mutable reference to the requested entity's component of type
C
, or None
if the entity does not have that component.
Panics
Panics if the requested entity does not exist.
fn iter_ids(&self) -> EntityIdIter
Return an iterator over every ID in the system.
fn collect_ids(&self) -> Vec<EntityId>
Return a vector containing copies of every ID in the system.
Useful for accessing entity IDs without borrowing the ECS.
fn iter_components(&self, id: EntityId) -> ComponentIter
Return an iterator yielding references to all components of the requested entity.
Panics
Panics if the requested entity does not exist.
fn iter_components_mut(&mut self, id: EntityId) -> ComponentIterMut
Return an iterator yielding mutable references to all components of the requested entity.
Panics
Panics if the requested entity does not exist.