World

Struct World 

Source
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

Source

pub fn spawn_without_component(&mut self) -> Entity

Creates an Entity without any components

Source

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

pub fn despawn(&mut self, entity: Entity)

Removes an Entity from the World

Source

pub fn add_component<T: ComponentBundle>( &mut self, entity: Entity, component_bundle: T, )

Adds a component or a tuple of components to an Entity

Source

pub fn remove_component<T: ComponentBundle>(&mut self, entity: Entity)

Removes a component or a tuple of components from an Entity

Source

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

pub fn get_resource<T: Resource + 'static>( &self, ) -> Option<RwLockReadGuard<'_, T>>

Gets a reference to the resource of the given type if it exists

Source

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

Source

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

Source

pub fn remove_resource<T: Resource + 'static>(&mut self) -> Option<T>

Removes a resource of a given type and returns it if it exists

Source

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

Source

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

Source

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

Source

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

Source

pub fn destroy_resource_with_type_id(&mut self, id: TypeId)

Destroy a resource using its type id

Source

pub fn destroy_unsendable_resource_with_type_id(&mut self, id: TypeId)

Destroy an unsendable resource using its type id

Source

pub fn get_event_handler<T: Any + Debug>( &self, ) -> Option<RwLockReadGuard<'_, EventHandler<T>>>

Gets a reference to an EventHandler of a given type

Source

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

Source

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

Trait Implementations§

Source§

impl Debug for World

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for World

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl !Freeze for World

§

impl !RefUnwindSafe for World

§

impl !Send for World

§

impl !Sync for World

§

impl Unpin for World

§

impl !UnwindSafe for World

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

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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, 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.