pub struct World { /* private fields */ }
Expand description
The world storing entities and their components.
Implementations§
Source§impl World
impl World
Sourcepub fn alloc(&mut self) -> Entity
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§impl World
impl World
Sourcepub fn exchange<B1, B2>(&mut self, ent: Entity, new_comps: B2) -> Option<B1>where
B1: Bundle,
B2: Bundle,
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
impl World
Sourcepub fn transfer(&mut self, ent: Entity, other: &mut World) -> Entity
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
impl World
Sourcepub fn query_one<S>(&self, ent: Entity) -> Option<QueryOne<'_, S>>where
S: QuerySpec,
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
impl World
Sourcepub fn clone(&mut self, cloner: &Cloner) -> Self
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§
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
Mutably borrows from an owned value. Read more
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>
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 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>
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