pub struct World { /* private fields */ }Expand description
Central storage for all simulation entities and their components.
Uses separate SecondaryMap per component type (struct-of-arrays pattern)
to enable independent mutable borrows of different component storages
within the same system function.
Built-in components are accessed via typed methods. Games can attach
custom data via the extension storage (insert_ext / get_ext).
The query builder (world.query::<...>()) provides ECS-style iteration.
Implementations§
Source§impl World
impl World
Sourcepub fn spawn(&mut self) -> EntityId
pub fn spawn(&mut self) -> EntityId
Allocate a new entity. Returns its id. No components attached yet.
Sourcepub fn despawn(&mut self, id: EntityId)
pub fn despawn(&mut self, id: EntityId)
Remove an entity and all its components (built-in and extensions).
Cross-references are cleaned up automatically:
- If the entity is a rider aboard an elevator, it is removed from the
elevator’s rider list and
current_loadis adjusted. - If the entity is an elevator, its riders’ phases are reset to
Waiting.
Sourcepub fn entity_count(&self) -> usize
pub fn entity_count(&self) -> usize
Number of live entities.
Sourcepub fn position_mut(&mut self, id: EntityId) -> Option<&mut Position>
pub fn position_mut(&mut self, id: EntityId) -> Option<&mut Position>
Get an entity’s position mutably.
Sourcepub fn set_position(&mut self, id: EntityId, pos: Position)
pub fn set_position(&mut self, id: EntityId, pos: Position)
Set an entity’s position.
Sourcepub fn prev_position(&self, id: EntityId) -> Option<&Position>
pub fn prev_position(&self, id: EntityId) -> Option<&Position>
Snapshot of an entity’s position at the start of the current tick.
Pairs with position to support sub-tick interpolation
(see Simulation::position_at).
Sourcepub fn velocity_mut(&mut self, id: EntityId) -> Option<&mut Velocity>
pub fn velocity_mut(&mut self, id: EntityId) -> Option<&mut Velocity>
Get an entity’s velocity mutably.
Sourcepub fn set_velocity(&mut self, id: EntityId, vel: Velocity)
pub fn set_velocity(&mut self, id: EntityId, vel: Velocity)
Set an entity’s velocity.
Sourcepub fn elevator_mut(&mut self, id: EntityId) -> Option<&mut Elevator>
pub fn elevator_mut(&mut self, id: EntityId) -> Option<&mut Elevator>
Get an entity’s elevator component mutably.
Sourcepub fn set_elevator(&mut self, id: EntityId, elev: Elevator)
pub fn set_elevator(&mut self, id: EntityId, elev: Elevator)
Set an entity’s elevator component.
Sourcepub fn rider_mut(&mut self, id: EntityId) -> Option<&mut Rider>
pub fn rider_mut(&mut self, id: EntityId) -> Option<&mut Rider>
Get an entity’s rider component mutably.
Sourcepub fn stop_mut(&mut self, id: EntityId) -> Option<&mut Stop>
pub fn stop_mut(&mut self, id: EntityId) -> Option<&mut Stop>
Get an entity’s stop component mutably.
Sourcepub fn line_mut(&mut self, id: EntityId) -> Option<&mut Line>
pub fn line_mut(&mut self, id: EntityId) -> Option<&mut Line>
Get an entity’s line component mutably.
Sourcepub fn remove_line(&mut self, id: EntityId) -> Option<Line>
pub fn remove_line(&mut self, id: EntityId) -> Option<Line>
Remove an entity’s line component.
Sourcepub fn iter_lines(&self) -> impl Iterator<Item = (EntityId, &Line)>
pub fn iter_lines(&self) -> impl Iterator<Item = (EntityId, &Line)>
Iterate all line entities.
Sourcepub fn patience_mut(&mut self, id: EntityId) -> Option<&mut Patience>
pub fn patience_mut(&mut self, id: EntityId) -> Option<&mut Patience>
Get an entity’s patience mutably.
Sourcepub fn set_patience(&mut self, id: EntityId, patience: Patience)
pub fn set_patience(&mut self, id: EntityId, patience: Patience)
Set an entity’s patience.
Sourcepub fn preferences(&self, id: EntityId) -> Option<&Preferences>
pub fn preferences(&self, id: EntityId) -> Option<&Preferences>
Get an entity’s preferences.
Sourcepub fn set_preferences(&mut self, id: EntityId, prefs: Preferences)
pub fn set_preferences(&mut self, id: EntityId, prefs: Preferences)
Set an entity’s preferences.
Sourcepub fn access_control(&self, id: EntityId) -> Option<&AccessControl>
pub fn access_control(&self, id: EntityId) -> Option<&AccessControl>
Get an entity’s access control.
Sourcepub fn access_control_mut(&mut self, id: EntityId) -> Option<&mut AccessControl>
pub fn access_control_mut(&mut self, id: EntityId) -> Option<&mut AccessControl>
Get an entity’s access control mutably.
Sourcepub fn set_access_control(&mut self, id: EntityId, ac: AccessControl)
pub fn set_access_control(&mut self, id: EntityId, ac: AccessControl)
Set an entity’s access control.
Sourcepub fn energy_profile(&self, id: EntityId) -> Option<&EnergyProfile>
pub fn energy_profile(&self, id: EntityId) -> Option<&EnergyProfile>
Get an entity’s energy profile.
Sourcepub fn energy_metrics(&self, id: EntityId) -> Option<&EnergyMetrics>
pub fn energy_metrics(&self, id: EntityId) -> Option<&EnergyMetrics>
Get an entity’s energy metrics.
Sourcepub fn energy_metrics_mut(&mut self, id: EntityId) -> Option<&mut EnergyMetrics>
pub fn energy_metrics_mut(&mut self, id: EntityId) -> Option<&mut EnergyMetrics>
Get an entity’s energy metrics mutably.
Sourcepub fn set_energy_profile(&mut self, id: EntityId, profile: EnergyProfile)
pub fn set_energy_profile(&mut self, id: EntityId, profile: EnergyProfile)
Set an entity’s energy profile.
Sourcepub fn set_energy_metrics(&mut self, id: EntityId, metrics: EnergyMetrics)
pub fn set_energy_metrics(&mut self, id: EntityId, metrics: EnergyMetrics)
Set an entity’s energy metrics.
Sourcepub fn service_mode(&self, id: EntityId) -> Option<&ServiceMode>
pub fn service_mode(&self, id: EntityId) -> Option<&ServiceMode>
Get an entity’s service mode.
Sourcepub fn set_service_mode(&mut self, id: EntityId, mode: ServiceMode)
pub fn set_service_mode(&mut self, id: EntityId, mode: ServiceMode)
Set an entity’s service mode.
Sourcepub fn destination_queue(&self, id: EntityId) -> Option<&DestinationQueue>
pub fn destination_queue(&self, id: EntityId) -> Option<&DestinationQueue>
Get an entity’s destination queue.
Sourcepub fn set_destination_queue(&mut self, id: EntityId, queue: DestinationQueue)
pub fn set_destination_queue(&mut self, id: EntityId, queue: DestinationQueue)
Set an entity’s destination queue.
Sourcepub fn iter_elevators(
&self,
) -> impl Iterator<Item = (EntityId, &Position, &Elevator)>
pub fn iter_elevators( &self, ) -> impl Iterator<Item = (EntityId, &Position, &Elevator)>
Iterate all elevator entities (have Elevator + Position).
Sourcepub fn elevator_ids(&self) -> Vec<EntityId>
pub fn elevator_ids(&self) -> Vec<EntityId>
Iterate all elevator entity IDs (allocates).
Sourcepub fn elevator_ids_into(&self, buf: &mut Vec<EntityId>)
pub fn elevator_ids_into(&self, buf: &mut Vec<EntityId>)
Fill the buffer with all elevator entity IDs, clearing it first.
Sourcepub fn iter_riders(&self) -> impl Iterator<Item = (EntityId, &Rider)>
pub fn iter_riders(&self) -> impl Iterator<Item = (EntityId, &Rider)>
Iterate all rider entities.
Sourcepub fn iter_riders_mut(
&mut self,
) -> impl Iterator<Item = (EntityId, &mut Rider)>
pub fn iter_riders_mut( &mut self, ) -> impl Iterator<Item = (EntityId, &mut Rider)>
Iterate all rider entities mutably.
Sourcepub fn iter_stops(&self) -> impl Iterator<Item = (EntityId, &Stop)>
pub fn iter_stops(&self) -> impl Iterator<Item = (EntityId, &Stop)>
Iterate all stop entities.
Sourcepub fn iter_idle_elevators(
&self,
) -> impl Iterator<Item = (EntityId, &Position, &Elevator)>
pub fn iter_idle_elevators( &self, ) -> impl Iterator<Item = (EntityId, &Position, &Elevator)>
Iterate elevators in Idle phase (not disabled).
Sourcepub fn iter_moving_elevators(
&self,
) -> impl Iterator<Item = (EntityId, &Position, &Elevator)>
pub fn iter_moving_elevators( &self, ) -> impl Iterator<Item = (EntityId, &Position, &Elevator)>
Iterate elevators that are currently moving — either on a dispatched
trip (MovingToStop) or a repositioning trip (Repositioning).
Excludes disabled elevators.
Sourcepub fn iter_waiting_riders(&self) -> impl Iterator<Item = (EntityId, &Rider)>
pub fn iter_waiting_riders(&self) -> impl Iterator<Item = (EntityId, &Rider)>
Iterate riders in Waiting phase (not disabled).
Sourcepub fn find_stop_at_position(&self, position: f64) -> Option<EntityId>
pub fn find_stop_at_position(&self, position: f64) -> Option<EntityId>
Find the stop entity at a given position (within epsilon).
Sourcepub fn find_nearest_stop(&self, position: f64) -> Option<EntityId>
pub fn find_nearest_stop(&self, position: f64) -> Option<EntityId>
Find the stop entity nearest to a given position.
Unlike find_stop_at_position, this finds
the closest stop by minimum distance rather than requiring an exact match.
Used when ejecting riders from a disabled/despawned elevator mid-transit.
Sourcepub fn stop_position(&self, id: EntityId) -> Option<f64>
pub fn stop_position(&self, id: EntityId) -> Option<f64>
Get a stop’s position by entity id.
Sourcepub fn insert_ext<T: 'static + Send + Sync + Serialize + DeserializeOwned>(
&mut self,
id: EntityId,
value: T,
name: &str,
)
pub fn insert_ext<T: 'static + Send + Sync + Serialize + DeserializeOwned>( &mut self, id: EntityId, value: T, name: &str, )
Insert a custom component for an entity.
Games use this to attach their own typed data to simulation entities.
Extension components must be Serialize + DeserializeOwned to support
snapshot save/load. A name string is required for serialization roundtrips.
Extension components are automatically cleaned up on despawn().
use elevator_core::world::World;
use serde::{Serialize, Deserialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
struct VipTag { level: u32 }
let mut world = World::new();
let entity = world.spawn();
world.insert_ext(entity, VipTag { level: 3 }, "vip_tag");Sourcepub fn get_ext<T: 'static + Send + Sync + Clone>(
&self,
id: EntityId,
) -> Option<T>
pub fn get_ext<T: 'static + Send + Sync + Clone>( &self, id: EntityId, ) -> Option<T>
Get a clone of a custom component for an entity.
Sourcepub fn get_ext_mut<T: 'static + Send + Sync>(
&mut self,
id: EntityId,
) -> Option<&mut T>
pub fn get_ext_mut<T: 'static + Send + Sync>( &mut self, id: EntityId, ) -> Option<&mut T>
Get a mutable reference to a custom component for an entity.
Sourcepub fn remove_ext<T: 'static + Send + Sync>(
&mut self,
id: EntityId,
) -> Option<T>
pub fn remove_ext<T: 'static + Send + Sync>( &mut self, id: EntityId, ) -> Option<T>
Remove a custom component for an entity.
Sourcepub fn register_ext<T: 'static + Send + Sync + Serialize + DeserializeOwned>(
&mut self,
name: &str,
)
pub fn register_ext<T: 'static + Send + Sync + Serialize + DeserializeOwned>( &mut self, name: &str, )
Register an extension type for deserialization (creates empty storage).
Must be called before restore() for each extension type that was
present in the original simulation.
Sourcepub fn disable(&mut self, id: EntityId)
pub fn disable(&mut self, id: EntityId)
Mark an entity as disabled. Disabled entities are skipped by all systems.
Sourcepub fn is_disabled(&self, id: EntityId) -> bool
pub fn is_disabled(&self, id: EntityId) -> bool
Check if an entity is disabled.
Sourcepub fn insert_resource<T: 'static + Send + Sync>(&mut self, value: T)
pub fn insert_resource<T: 'static + Send + Sync>(&mut self, value: T)
Insert a global resource. Replaces any existing resource of the same type.
Resources are singletons not attached to any entity. Games use them for event channels, score trackers, or any global state.
use elevator_core::world::World;
use elevator_core::events::EventChannel;
#[derive(Debug)]
enum MyEvent { Score(u32) }
let mut world = World::new();
world.insert_resource(EventChannel::<MyEvent>::new());Sourcepub fn resource<T: 'static + Send + Sync>(&self) -> Option<&T>
pub fn resource<T: 'static + Send + Sync>(&self) -> Option<&T>
Get a shared reference to a global resource.
Sourcepub fn resource_mut<T: 'static + Send + Sync>(&mut self) -> Option<&mut T>
pub fn resource_mut<T: 'static + Send + Sync>(&mut self) -> Option<&mut T>
Get a mutable reference to a global resource.
Sourcepub fn remove_resource<T: 'static + Send + Sync>(&mut self) -> Option<T>
pub fn remove_resource<T: 'static + Send + Sync>(&mut self) -> Option<T>
Remove a global resource, returning it if it existed.
Sourcepub const fn query<Q: WorldQuery>(&self) -> QueryBuilder<'_, Q>
pub const fn query<Q: WorldQuery>(&self) -> QueryBuilder<'_, Q>
Create a query builder for iterating entities by component composition.
use elevator_core::prelude::*;
let mut sim = SimulationBuilder::demo().build().unwrap();
sim.spawn_rider_by_stop_id(StopId(0), StopId(1), 75.0).unwrap();
let world = sim.world();
for (id, rider, pos) in world.query::<(EntityId, &Rider, &Position)>().iter() {
println!("{id:?}: {:?} at {}", rider.phase(), pos.value());
}Sourcepub fn query_ext_mut<T: 'static + Send + Sync>(&mut self) -> ExtQueryMut<'_, T>
pub fn query_ext_mut<T: 'static + Send + Sync>(&mut self) -> ExtQueryMut<'_, T>
Create a mutable extension query builder.
Uses the keys-snapshot pattern: collects matching entity IDs upfront
into an owned Vec, then iterates with mutable access via
for_each_mut.
§Example
world.query_ext_mut::<VipTag>().for_each_mut(|id, tag| {
tag.level += 1;
});