pub struct World { /* private fields */ }Expand description
The game world containing all entities and their components.
§Example
use jugar_core::{World, Entity, Position, Velocity};
let mut world = World::new();
let entity = world.spawn();
world.add_component(entity, Position::new(0.0, 0.0));
world.add_component(entity, Velocity::new(1.0, 0.0));
// Query and update
if let Some(pos) = world.get_component_mut::<Position>(entity) {
pos.x += 10.0;
}Implementations§
Source§impl World
impl World
Sourcepub fn despawn(&mut self, entity: Entity) -> Result<()>
pub fn despawn(&mut self, entity: Entity) -> Result<()>
Despawns an entity and removes all its components
§Errors
Returns CoreError::EntityNotFound if the entity doesn’t exist.
Sourcepub fn add_component<T: Any + Send + Sync>(
&mut self,
entity: Entity,
component: T,
)
pub fn add_component<T: Any + Send + Sync>( &mut self, entity: Entity, component: T, )
Adds a component to an entity
If the entity already has this component type, it is replaced.
Sourcepub fn get_component<T: Any>(&self, entity: Entity) -> Option<&T>
pub fn get_component<T: Any>(&self, entity: Entity) -> Option<&T>
Gets a reference to a component on an entity
Sourcepub fn get_component_mut<T: Any>(&mut self, entity: Entity) -> Option<&mut T>
pub fn get_component_mut<T: Any>(&mut self, entity: Entity) -> Option<&mut T>
Gets a mutable reference to a component on an entity
Sourcepub fn has_component<T: Any>(&self, entity: Entity) -> bool
pub fn has_component<T: Any>(&self, entity: Entity) -> bool
Checks if an entity has a specific component
Sourcepub fn remove_component<T: Any>(&mut self, entity: Entity) -> bool
pub fn remove_component<T: Any>(&mut self, entity: Entity) -> bool
Removes a component from an entity
Returns true if the component was removed, false if it didn’t exist.
Sourcepub fn entity_count(&self) -> usize
pub fn entity_count(&self) -> usize
Returns the number of entities in the world
Sourcepub fn entities(&self) -> impl Iterator<Item = Entity> + '_
pub fn entities(&self) -> impl Iterator<Item = Entity> + '_
Returns an iterator over all entities
Sourcepub fn component_type_count(&self) -> usize
pub fn component_type_count(&self) -> usize
Returns the number of registered component types
This is used by the probar introspection module.
Sourcepub fn entity_component_count_internal(&self, entity: Entity) -> usize
pub fn entity_component_count_internal(&self, entity: Entity) -> usize
Returns the number of components attached to an entity
This is used by the probar introspection module.