GameWorld

Struct GameWorld 

Source
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

Source

pub fn spawn<B: ComponentBundle>(&mut self, bundle: B) -> EntityId

Spawns a new entity with the given component bundle.

Returns the EntityId of the newly created entity.

§Examples
let entity = world.spawn((Transform::identity(), Velocity::default()));
Source

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.

Source

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.

Source

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));
Source

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.

Source

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.

Source

pub fn query<'a, Q: WorldQuery>(&'a self) -> Query<'a, Q>

Creates a read-only query over the world.

§Examples
for (pos, vel) in world.query::<(&Transform, &Velocity)>() {
    // iterate matching entities
}
Source

pub fn query_mut<'a, Q: WorldQuery>(&'a mut self) -> QueryMut<'a, Q>

Creates a mutable query over the world.

§Examples
for (pos,) in world.query_mut::<(&mut Transform,)>() {
    pos.translate(Vec3::Y * delta);
}
Source

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.

Source

pub fn iter_entities(&self) -> impl Iterator<Item = EntityId> + '_

Returns an iterator over all entity IDs in the world.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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);
Source

pub fn update_transform<F>(&mut self, entity: EntityId, f: F)
where F: FnOnce(&mut Transform),

Updates an entity’s transform and immediately syncs it to GlobalTransform.

This is a convenience method that combines getting the transform, applying a modification function, and syncing to GlobalTransform.

§Example
world.update_transform(entity, |t| {
    t.translation += Vec3::Y * delta;
});
Source

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§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> AsAny for T
where T: Any,

Source§

fn as_any(&self) -> &(dyn Any + 'static)

Returns a reference to the inner value as &dyn Any.
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> Downcast<T> for T

§

fn downcast(&self) -> &T

§

impl<T> Downcast for T
where T: Any,

§

fn into_any(self: Box<T>) -> Box<dyn Any>

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

Convert 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)

Convert &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)

Convert &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 T
where T: Any,

§

fn into_any(self: Box<T>) -> Box<dyn Any>

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

Converts 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)

Converts &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)

Converts &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
where T: Any + Send,

§

fn into_any_send(self: Box<T>) -> Box<dyn Any + Send>

Converts Box<Trait> (where Trait: DowncastSend) to Box<dyn Any + Send>, which can then be downcast into Box<ConcreteType> where ConcreteType implements Trait.
§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Send + Sync>

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

§

fn into_any_sync(self: Box<T>) -> Box<dyn Any + Send + Sync>

Converts Box<Trait> (where Trait: DowncastSync) to Box<dyn Any + Send + Sync>, which can then be downcast into Box<ConcreteType> where ConcreteType implements Trait.
§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Send + Sync>

Converts Arc<Trait> (where Trait: DowncastSync) to Arc<Any>, which can then be downcast into Arc<ConcreteType> where ConcreteType implements Trait.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<S> FromSample<S> for S

§

fn from_sample_(s: S) -> S

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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 F
where T: FromSample<F>,

§

fn into_sample(self) -> T

§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
§

impl<T, U> ToSample<U> for T
where U: FromSample<T>,

§

fn to_sample_(self) -> U

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<T> Upcast<T> for T

§

fn upcast(&self) -> Option<&T>

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

impl<S, T> Duplex<S> for T
where T: FromSample<S> + ToSample<S>,

§

impl<T> WasmNotSend for T
where T: Send,

§

impl<T> WasmNotSendSync for T
where T: WasmNotSend + WasmNotSync,

§

impl<T> WasmNotSync for T
where T: Sync,