Struct rs_ecs::World

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

The world storing entities and their components.

Implementations§

source§

impl World

source

pub fn new() -> Self

Create an empty world.

source§

impl World

source

pub fn alloc(&mut self) -> Entity

Create an Entity without any components. To add components, see Self::insert().

§Example
let mut world = World::new();

let entity = world.alloc();
world.insert(entity, (42_u32, true));
source

pub fn free(&mut self, ent: Entity)

Remove an Entity and all its components from the world. To remove components, see Self::remove().

§Example
let mut world = World::new();

let entity = world.alloc();
world.insert(entity, (42_u32, true));

world.free(entity);
source

pub fn clear(&mut self)

Remove all entites and their components from the world.

Note that this will re-use the memory allocations but it will drop the meta-data which implies that previously used Entity values will be repeated.

source§

impl World

source

pub fn insert<B>(&mut self, ent: Entity, comps: B)
where B: Bundle,

Insert components for a given Entity.

If a component is already present for the entity, its value will be overwritten.

§Example
let mut world = World::new();

let entity = world.alloc();
world.insert(entity, (42_u32, true));
world.insert(entity, (String::from("Hello"),));
source

pub fn remove<B>(&mut self, ent: Entity) -> Option<B>
where B: Bundle,

Remove components for a given Entity.

§Example
let mut world = World::new();

let entity = world.alloc();
world.insert(entity, (42_u32, true, String::from("Hello")));

world.remove::<(u32, bool)>(entity).unwrap();
world.remove::<(String,)>(entity).unwrap();
source

pub fn exchange<B1, B2>(&mut self, ent: Entity, new_comps: B2) -> Option<B1>
where B1: Bundle, B2: Bundle,

Exchange components for a given Entity

§Example
let mut world = World::new();

let entity = world.alloc();
world.insert(entity, (42_u32, true));
assert!(world.contains::<u32>(entity));
assert!(world.contains::<bool>(entity));
assert!(!world.contains::<String>(entity));

world.exchange::<(u32, bool), _>(entity, (String::from("Hello"),)).unwrap();
assert!(!world.contains::<u32>(entity));
assert!(!world.contains::<bool>(entity));
assert!(world.contains::<String>(entity));
source§

impl World

source

pub fn transfer(&mut self, ent: Entity, other: &mut World) -> Entity

Transfer an Entity and its components from this world to another.

§Example
let mut world = World::new();

let entity = world.alloc();
world.insert(entity, (23_i32, false, String::from("Goodbye")));

let mut another_world = World::new();
let entity = world.transfer(entity, &mut another_world);

let comp = another_world.query_one::<&String>(entity).unwrap();
assert_eq!(&*comp.get(), "Goodbye");
source§

impl World

source

pub fn exists(&self, ent: Entity) -> bool

Check if a given Entity exists.

§Example
let mut world = World::new();

let entity = world.alloc();
assert!(world.exists(entity));

world.free(entity);
assert!(!world.exists(entity));
source

pub fn contains<C>(&self, ent: Entity) -> bool
where C: 'static,

Check if a certain component type is present for an Entity.

§Example
let mut world = World::new();

let entity = world.alloc();
world.insert(entity, (42_u32, true));

assert!(world.contains::<u32>(entity));
source

pub fn query_one<S>(&self, ent: Entity) -> Option<QueryOne<'_, S>>
where S: QuerySpec,

Access the components matching given query for an Entity.

Note that for repeated calls, map can be used to amortize the set-up costs.

§Example
let mut world = World::new();

let entity = world.alloc();
world.insert(entity, (42_u32, true));

{
    let mut comp = world.query_one::<&mut u32>(entity).unwrap();
    *comp.get_mut() = 42;
}

let comp = world.query_one::<&u32>(entity).unwrap();
assert_eq!(*comp.get(), 42);
source§

impl World

source

pub fn clone(&mut self, cloner: &Cloner) -> Self

Creates a copy of the World

This requires that all component types are available in the given cloner.

§Example
let mut world = World::new();

let ent = world.alloc();
world.insert(ent, (42, "foo".to_owned(),));

let mut cloner = Cloner::new();

cloner.add_copyable::<i32>();
cloner.add_cloneable::<String>();

let snapshot = world.clone(&cloner);

let mut comp = world.query_one::<&mut String>(ent).unwrap();
*comp.get_mut() = "bar".to_owned();

let comp = snapshot.query_one::<&String>(ent).unwrap();
assert_eq!(*comp.get(), "foo");

Trait Implementations§

source§

impl Default for World

source§

fn default() -> Self

Create an empty world.

Auto Trait Implementations§

§

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.

§

impl<T> Pointable for T

§

const ALIGN: usize = _

The alignment of pointer.
§

type Init = T

The type for initializers.
§

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

Initializes a with the given initializer. Read more
§

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

Dereferences the given pointer. Read more
§

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

Mutably dereferences the given pointer. Read more
§

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

§

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.