pub struct World { /* private fields */ }
Expand description
The World
struct holds all the data of a world.
Implementations§
Source§impl World
impl World
pub fn new() -> World
Sourcepub fn add_resource(
&mut self,
resource_data: impl SendSync,
) -> Result<(), ResourceError>
pub fn add_resource( &mut self, resource_data: impl SendSync, ) -> Result<(), ResourceError>
This adds a resource to the World
’s Resources
. Resources are like global components and can be used for things like frametime and other things systems should have access to.
§Errors
If there is already a resource of the provided resource’s type present, an error variant is returned.
§Example
world.add_resource(String::from("This is an example resource!"))?;
Sourcepub fn remove_resource<T>(&mut self)where
T: SendSync,
pub fn remove_resource<T>(&mut self)where
T: SendSync,
Sourcepub fn get_resource<T>(
&self,
) -> Result<MappedRwLockReadGuard<'_, RawRwLock, T>, ResourceError>where
T: SendSync,
pub fn get_resource<T>(
&self,
) -> Result<MappedRwLockReadGuard<'_, RawRwLock, T>, ResourceError>where
T: SendSync,
Sourcepub fn get_resource_mut<T>(
&self,
) -> Result<MappedRwLockWriteGuard<'_, RawRwLock, T>, ResourceError>where
T: SendSync,
pub fn get_resource_mut<T>(
&self,
) -> Result<MappedRwLockWriteGuard<'_, RawRwLock, T>, ResourceError>where
T: SendSync,
Sourcepub fn register_component<T>(&mut self)where
T: SendSync,
pub fn register_component<T>(&mut self)where
T: SendSync,
Register a component.
Sourcepub fn create_entity(
&self,
components: impl ComponentSet,
) -> Result<Entity, EntityError>
pub fn create_entity( &self, components: impl ComponentSet, ) -> Result<Entity, EntityError>
Spawn an entity with components. The maximum size for tuples provided to this method is 10.
§Errors
Returns an error variant if a provided component is not registered in the world.
§Example
// spawn entity
world.create_entity((30_u32, 60.0_f32))?;
// You can also bind the entity to access it later on.
let entity = world.create_entity((20_u32, 30.0_f32))?;
// When only adding one component, put a comma after it for rust to recognise it as a tuple.
world.create_entity((20_u32,))?;
Sourcepub fn delete_entity(&self, entity: Entity) -> Result<(), EntityError>
pub fn delete_entity(&self, entity: Entity) -> Result<(), EntityError>
Delete an Entity
from the world. See QueryEntity::delete
if you are using a query, which is the recommended way to do this.
§Errors
Can only delete entities if they exist. Otherwise an error variant is returned.
§Example
let entity = world.create_entity((10_u32,))?;
// delete the entity
world.delete_entity(entity)?;
Sourcepub fn assign_components(
&self,
components: impl ComponentSet,
entity: Entity,
) -> Result<(), EntityError>
pub fn assign_components( &self, components: impl ComponentSet, entity: Entity, ) -> Result<(), EntityError>
Assign components to an Entity
. See QueryEntity::assign_components
if you are using a query, which is the recommended way to do this.
§Errors
This can fail when either the entity doesn’t exist, or one of the components is not registered. Then aan error variant will be returned.
§Example
let entity = world.create_entity((5_u32,))?;
// assign more components
world.assign_components((30.0_f32, String::from("hello world")), entity)?;
Sourcepub fn delete_component<C>(&self, entity: Entity) -> Result<(), EntityError>where
C: SendSync,
pub fn delete_component<C>(&self, entity: Entity) -> Result<(), EntityError>where
C: SendSync,
Delete components from an Entity
if it exists. See QueryEntity::delete_component
if you are using a query, which is the recommended way to do this.
§Errors
This fails if the component is not registered in the world, in which case an error variant is returned.
§Example
fn main() -> Result<(), magma_ecs::error::EntityError> {
let entity = world.create_entity((5_u32, 10.0_f32))?;
// delete a component
world.delete_component::<u32>(entity)?;
Sourcepub fn get_component<C>(
&self,
entity: Entity,
) -> Result<MappedRwLockReadGuard<'_, RawRwLock, C>, EntityError>where
C: SendSync,
pub fn get_component<C>(
&self,
entity: Entity,
) -> Result<MappedRwLockReadGuard<'_, RawRwLock, C>, EntityError>where
C: SendSync,
Get a component as readable from an Entity
.
§Errors
If the requested component is either not registered on the World
or the entity doesn’t have that component,
an error variant is returned.
§Example
// create an entity with u32
let entity = world.create_entity((30_u32,))?;
// get the component as readable
world.get_component::<u32>(entity)?;
Sourcepub fn get_component_mut<C>(
&self,
entity: Entity,
) -> Result<MappedRwLockWriteGuard<'_, RawRwLock, C>, EntityError>where
C: SendSync,
pub fn get_component_mut<C>(
&self,
entity: Entity,
) -> Result<MappedRwLockWriteGuard<'_, RawRwLock, C>, EntityError>where
C: SendSync,
Get a component as writable from an Entity
.
§Errors
If the requested component is either not registered on the World
or the entity doesn’t have that component,
an error variant is returned.
§Example
// create an entity with u32
let entity = world.create_entity((30_u32,))?;
// get the component as writable and add 1
*world.get_component_mut::<u32>(entity)? += 1;
Sourcepub fn query<Q>(&self) -> Result<Vec<QueryEntity<'_>>, EntityError>where
Q: ComponentSet,
pub fn query<Q>(&self) -> Result<Vec<QueryEntity<'_>>, EntityError>where
Q: ComponentSet,
Query for entities with specified components. Also see the methods of QueryEntity
.
§Errors
If any provided component is not registered in the world, an error variant is returned.
§Example
world.create_entity((30_u32, 20.0_f32))?;
world.create_entity((20_u32,))?;
// Only the first entity will be in our query, because we searched for (u32, f32).
let query = world.query::<(u32, f32)>()?;
Trait Implementations§
Auto Trait Implementations§
impl !Freeze for World
impl !RefUnwindSafe for World
impl Send for World
impl Sync for World
impl Unpin for World
impl !UnwindSafe for World
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more