pub struct GameWorld { /* private fields */ }Expand description
A high-level facade over the internal ECS World and Assets registry.
GameWorld is the primary interface for game developers to create and
manage entities, components, and assets. It hides the raw types from
khora-data behind a clean, stable API surface.
§Examples
fn setup(&mut self, world: &mut GameWorld) {
// Spawn a camera
world.spawn_camera(Camera::new_perspective(
std::f32::consts::FRAC_PI_4, 16.0 / 9.0, 0.1, 1000.0,
));
// Spawn a custom entity
world.spawn((Transform::identity(), MyComponent { speed: 10.0 }));
}Implementations§
Source§impl GameWorld
impl GameWorld
Sourcepub fn spawn<B: ComponentBundle>(&mut self, bundle: B) -> EntityId
pub fn spawn<B: ComponentBundle>(&mut self, bundle: B) -> EntityId
Sourcepub fn despawn(&mut self, entity: EntityId) -> bool
pub fn despawn(&mut self, entity: EntityId) -> bool
Removes an entity and all its components from the world.
Returns true if the entity existed and was removed.
Sourcepub fn spawn_camera(&mut self, camera: Camera) -> EntityId
pub fn spawn_camera(&mut self, camera: Camera) -> EntityId
Spawns a camera entity with a Camera component and an identity
GlobalTransform.
This is the recommended way to add a camera to the scene. The
RenderAgent will automatically discover cameras during its
extraction phase.
Returns the EntityId of the camera entity.
Sourcepub fn add_mesh(&mut self, mesh: Mesh) -> HandleComponent<Mesh>
pub fn add_mesh(&mut self, mesh: Mesh) -> HandleComponent<Mesh>
Adds a mesh to the asset registry and returns a handle component.
The returned HandleComponent<Mesh> can be attached to entities
to give them a visible mesh. The RenderAgent will automatically
upload the mesh to GPU when the entity is rendered.
§Arguments
mesh- The CPU-side mesh data.
§Returns
A HandleComponent<Mesh> that references the stored mesh.
§Examples
let plane_mesh = create_plane(10.0, 0.0);
let handle = world.add_mesh(plane_mesh);
let entity = world.spawn((Transform::identity(), handle));Sourcepub fn add_component<C: Component>(&mut self, entity: EntityId, component: C)
pub fn add_component<C: Component>(&mut self, entity: EntityId, component: C)
Adds a component to an existing entity.
If the entity already has a component of this type, the old value is replaced.
Sourcepub fn remove_component<C: Component>(&mut self, entity: EntityId)
pub fn remove_component<C: Component>(&mut self, entity: EntityId)
Removes a component (and its semantic domain) from an entity.
This is an O(1) operation — the data is orphaned and cleaned up later by the garbage collector agent.
Sourcepub fn query<'a, Q: WorldQuery>(&'a self) -> Query<'a, Q>
pub fn query<'a, Q: WorldQuery>(&'a self) -> Query<'a, Q>
Sourcepub fn query_mut<'a, Q: WorldQuery>(&'a mut self) -> QueryMut<'a, Q>
pub fn query_mut<'a, Q: WorldQuery>(&'a mut self) -> QueryMut<'a, Q>
Sourcepub fn spawn_entity(&mut self, transform: &Transform) -> EntityId
pub fn spawn_entity(&mut self, transform: &Transform) -> EntityId
Spawns an entity with just a transform component.
Returns the EntityId of the newly created entity.
Sourcepub fn iter_entities(&self) -> impl Iterator<Item = EntityId> + '_
pub fn iter_entities(&self) -> impl Iterator<Item = EntityId> + '_
Returns an iterator over all entity IDs in the world.
Sourcepub fn get_transform_mut(&mut self, entity: EntityId) -> Option<&mut Transform>
pub fn get_transform_mut(&mut self, entity: EntityId) -> Option<&mut Transform>
Gets a mutable reference to a transform component.
Returns None if the entity doesn’t exist or has no transform.
Sourcepub fn get_transform(&self, entity: EntityId) -> Option<&Transform>
pub fn get_transform(&self, entity: EntityId) -> Option<&Transform>
Gets a reference to a transform component.
Returns None if the entity doesn’t exist or has no transform.
Sourcepub fn get_component_mut<C: Component>(
&mut self,
entity: EntityId,
) -> Option<&mut C>
pub fn get_component_mut<C: Component>( &mut self, entity: EntityId, ) -> Option<&mut C>
Gets a mutable reference to any component.
Returns None if the entity doesn’t exist or has no such component.
Sourcepub fn get_component<C: Component>(&self, entity: EntityId) -> Option<&C>
pub fn get_component<C: Component>(&self, entity: EntityId) -> Option<&C>
Gets a reference to any component.
Returns None if the entity doesn’t exist or has no such component.
Sourcepub fn sync_global_transform(&mut self, entity: EntityId)
pub fn sync_global_transform(&mut self, entity: EntityId)
Synchronizes the GlobalTransform component from the Transform component.
This should be called after modifying a Transform to ensure the changes are visible to the rendering system. This is a convenience method that copies the local transform to the global transform.
§Example
// Move entity
if let Some(transform) = world.get_transform_mut(entity) {
transform.translation += Vec3::Y * delta;
}
// Sync to rendering
world.sync_global_transform(entity);Sourcepub fn update_transform<F>(&mut self, entity: EntityId, f: F)
pub fn update_transform<F>(&mut self, entity: EntityId, f: F)
Sourcepub fn add_material<M: Material>(&mut self, material: M) -> MaterialComponent
pub fn add_material<M: Material>(&mut self, material: M) -> MaterialComponent
Adds a material to the asset registry and returns a handle component.
The returned MaterialComponent can be attached to entities
to give them a visible material.
§Arguments
material- The CPU-side material data (e.g., StandardMaterial).
§Returns
A MaterialComponent that references the stored material.
Auto Trait Implementations§
impl !Freeze for GameWorld
impl !RefUnwindSafe for GameWorld
impl Send for GameWorld
impl Sync for GameWorld
impl Unpin for GameWorld
impl !UnwindSafe for GameWorld
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
§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can
then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be
further downcast into Rc<ConcreteType> where ConcreteType implements Trait.§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>, which can then be
downcast into Box<dyn ConcreteType> where ConcreteType implements Trait.§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>, which can then be further
downcast into Rc<ConcreteType> where ConcreteType implements Trait.§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.§impl<T> DowncastSend for T
impl<T> DowncastSend for T
§impl<T> DowncastSync for T
impl<T> DowncastSync for T
§impl<T> DowncastSync for T
impl<T> DowncastSync for T
§impl<S> FromSample<S> for S
impl<S> FromSample<S> for S
fn from_sample_(s: S) -> S
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
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>
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>
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§impl<F, T> IntoSample<T> for Fwhere
T: FromSample<F>,
impl<F, T> IntoSample<T> for Fwhere
T: FromSample<F>,
fn into_sample(self) -> T
§impl<T> Pointable for T
impl<T> Pointable for T
§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read more§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.