pub struct World { /* private fields */ }Expand description
Stores and exposes operations on entities, components, resources
§Entity and Components
Each Entity has a set of Components and each component can have up to one instance of each component type. Entity and components can be created, updated, removed and queried using a given World.
§Resources
A Resource is an unique instance of a given type that implement the Resource trait and don’t belong to a specific Entity. A UnsendableResource are a particular resource that cannot be shared between thread and can be accessed only by the main thread.
§Usage inside a system
To see how a system can interact with the data stored inside the World see the system module
Implementations§
Source§impl World
impl World
Sourcepub fn spawn_without_component(&mut self) -> Entity
pub fn spawn_without_component(&mut self) -> Entity
Creates an Entity without any components
Sourcepub fn spawn<T: ComponentBundle>(&mut self, component_bundle: T) -> Entity
pub fn spawn<T: ComponentBundle>(&mut self, component_bundle: T) -> Entity
Creates an Entity with a single component or with tuple of components
§Example
use zengine_macro::Component;
use zengine_ecs::{Entity, World, query::QueryIter};
#[derive(Component, Debug)]
struct ComponentA {
value: u32,
}
#[derive(Component, Debug)]
struct ComponentB {
value: f32,
}
let mut world = World::default();
world.spawn((ComponentA { value: 3 }, ComponentB { value: 1.0 }));Sourcepub fn add_component<T: ComponentBundle>(
&mut self,
entity: Entity,
component_bundle: T,
)
pub fn add_component<T: ComponentBundle>( &mut self, entity: Entity, component_bundle: T, )
Adds a component or a tuple of components to an Entity
Sourcepub fn remove_component<T: ComponentBundle>(&mut self, entity: Entity)
pub fn remove_component<T: ComponentBundle>(&mut self, entity: Entity)
Removes a component or a tuple of components from an Entity
Sourcepub fn query<T: QueryParameters>(&self) -> QueryRunner<T>
pub fn query<T: QueryParameters>(&self) -> QueryRunner<T>
Queries entity and components from the World
Returns a QueryRunner to fetch the data requested. A query runner has an internal cache to improve the performance of the query with subsequent calls
§Example
§Query all entities that have at least the ComponentA
use zengine_macro::Component;
use zengine_ecs::{World, query::QueryIter};
let world = World::default();
#[derive(Component, Debug)]
struct ComponentA {
value: u32,
}
#[derive(Component, Debug)]
struct ComponentB {
value: f32,
}
let mut query = world.query::<(&ComponentA,)>();
for c_a in query.run(&world).iter() {
println!("Component A: {:?}", c_a);
}§Query all entities that have at least the ComponentA and ComponentB
with a mutable access to ComponentB adding the Entity Key in the result
use zengine_macro::Component;
use zengine_ecs::{Entity, World, query::QueryIterMut};
let world = World::default();
#[derive(Component, Debug)]
struct ComponentA {
value: u32,
}
#[derive(Component, Debug)]
struct ComponentB {
value: f32,
}
let mut query = world.query::<(Entity, &ComponentA, &mut ComponentB)>();
for (e, c_a, c_b) in query.run(&world).iter_mut() {
c_b.value = 5.;
println!(
"Entity: {:?}, Component A: {:?}, Component B: {:?}",
e, c_a, c_b
);
}Sourcepub fn get_resource<T: Resource + 'static>(
&self,
) -> Option<RwLockReadGuard<'_, T>>
pub fn get_resource<T: Resource + 'static>( &self, ) -> Option<RwLockReadGuard<'_, T>>
Gets a reference to the resource of the given type if it exists
Sourcepub fn get_mut_resource<T: Resource + 'static>(
&self,
) -> Option<RwLockWriteGuard<'_, T>>
pub fn get_mut_resource<T: Resource + 'static>( &self, ) -> Option<RwLockWriteGuard<'_, T>>
Gets a mutable reference to the resource of the given type if it exists
Sourcepub fn create_resource<T: Resource + 'static>(&mut self, resource: T)
pub fn create_resource<T: Resource + 'static>(&mut self, resource: T)
Creates a new resource of the given value
Resource are unique data of a given type so if you create a resource of a type that already exists you will overwrite any existing data
Sourcepub fn remove_resource<T: Resource + 'static>(&mut self) -> Option<T>
pub fn remove_resource<T: Resource + 'static>(&mut self) -> Option<T>
Removes a resource of a given type and returns it if it exists
Sourcepub fn get_unsendable_resource<T: UnsendableResource + 'static>(
&self,
) -> Option<Ref<'_, T>>
pub fn get_unsendable_resource<T: UnsendableResource + 'static>( &self, ) -> Option<Ref<'_, T>>
Gets a reference to an unsendable resource of the given type if it exists
Sourcepub fn get_mut_unsendable_resource<T: UnsendableResource + 'static>(
&self,
) -> Option<RefMut<'_, T>>
pub fn get_mut_unsendable_resource<T: UnsendableResource + 'static>( &self, ) -> Option<RefMut<'_, T>>
Gets a mutable reference to an unsendable resource of the given type if it exists
Sourcepub fn create_unsendable_resource<T: UnsendableResource + 'static>(
&mut self,
resource: T,
)
pub fn create_unsendable_resource<T: UnsendableResource + 'static>( &mut self, resource: T, )
Creates a new unsendable resource of the given value
Unsendable resource are unique data of a given type so if you create an unsendable resource of a type that already exists you will overwrite any existing data
Sourcepub fn remove_unsendable_resource<T: UnsendableResource + 'static>(
&mut self,
) -> Option<T>
pub fn remove_unsendable_resource<T: UnsendableResource + 'static>( &mut self, ) -> Option<T>
Removes an unsendable resource of a given type and returns it if it exists
Sourcepub fn destroy_resource_with_type_id(&mut self, id: TypeId)
pub fn destroy_resource_with_type_id(&mut self, id: TypeId)
Destroy a resource using its type id
Sourcepub fn destroy_unsendable_resource_with_type_id(&mut self, id: TypeId)
pub fn destroy_unsendable_resource_with_type_id(&mut self, id: TypeId)
Destroy an unsendable resource using its type id
Sourcepub fn get_event_handler<T: Any + Debug>(
&self,
) -> Option<RwLockReadGuard<'_, EventHandler<T>>>
pub fn get_event_handler<T: Any + Debug>( &self, ) -> Option<RwLockReadGuard<'_, EventHandler<T>>>
Gets a reference to an EventHandler of a given type
Sourcepub fn get_mut_event_handler<T: Any + Debug>(
&self,
) -> Option<RwLockWriteGuard<'_, EventHandler<T>>>
pub fn get_mut_event_handler<T: Any + Debug>( &self, ) -> Option<RwLockWriteGuard<'_, EventHandler<T>>>
Gets a mutable reference to an EventHandler of a given type
Sourcepub fn create_event_handler<T: Any + Debug>(&mut self)
pub fn create_event_handler<T: Any + Debug>(&mut self)
Create a new event handler of a given type
EventHandlers are unique handler of a given type so if you create a EventHandler of a type that already exists you will overwrite any existing handler