Struct World

Source
pub struct World { /* private fields */ }
Expand description

The World struct holds all the data of a world.

Implementations§

Source§

impl World

Source

pub fn new() -> World

Source

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!"))?;
Source

pub fn remove_resource<T>(&mut self)
where T: SendSync,

Removes the requested resource from the World’s Resources if it exists, otherwise does nothing.

§Example
    world.add_resource(String::from("This is an example resource!"))?;
    world.remove_resource::<String>();
Source

pub fn get_resource<T>( &self, ) -> Result<MappedRwLockReadGuard<'_, RawRwLock, T>, ResourceError>
where T: SendSync,

Gets a resource as readable.

§Errors

If the resource doesn’t exist, an error variant is returned.

§Example
    world.add_resource(20_u32)?;

    // In this example we print our resource
    println!("{}", world.get_resource::<u32>()?);
Source

pub fn get_resource_mut<T>( &self, ) -> Result<MappedRwLockWriteGuard<'_, RawRwLock, T>, ResourceError>
where T: SendSync,

Gets a resource as writable.

§Errors

If the resource doesn’t exist, an error variant is returned.

§Example
    world.add_resource(20_u32);

    // resource plus one
    *world.get_resource_mut::<u32>().unwrap() += 1;
Source

pub fn register_component<T>(&mut self)
where T: SendSync,

Register a component.

Source

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

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

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

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

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

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

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§

Source§

impl Debug for World

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl Default for World

Source§

fn default() -> World

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

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> 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
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.