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