World

Struct World 

Source
pub struct World { /* private fields */ }
Expand description

The main runtime of the ECS api.

use starry_ecs::World;
use starry_ecs::resources::Resource;
use starry_ecs::component::Component;
use starry_ecs::systems::DefaultOrdering;

#[derive(Clone, Debug)]
pub struct TestResource { x: i32 }
impl Resource for TestResource {}
 
#[derive(Clone, Debug)]
pub struct TestComponent { x: i32 }
impl Component for TestComponent {}
 
fn test_system(_: &World) {
    println!("Hello world!");
}

World::new().add_system(DefaultOrdering::Run, test_system).add_component(TestComponent { x: 0 }).add_resource(TestResource { x: 0 }).run();

Implementations§

Source§

impl World

Source

pub fn new() -> Self

Creates a new world instance

Source

pub fn add_component<T: Component + 'static>(self, component: T) -> Self

Adds a component to the world

use starry_ecs::component::Component;
use starry_ecs::World;

#[derive(Clone, Debug)]
pub struct TestComponent { x: i32 }
impl Component for TestComponent {}
 
World::new().add_component(TestComponent { x: 0 });
Source

pub fn add_system<S: SystemOrdering + Copy>( self, system_ordering: S, system: SystemType, ) -> Self

Adds a system with an ordering to the world

use starry_ecs::systems::DefaultOrdering;
use starry_ecs::World;

fn example_system(_: &World) {
    println!("Hello, world!");
}

World::new().add_system(DefaultOrdering::Run, example_system).single_step();
Source

pub fn add_startup_system(self, system: SystemType) -> Self

Adds a staring system

use starry_ecs::World;

fn only_ran_once(_: &World) {
    println!("Hello, World!");
}

World::new().add_startup_system(only_ran_once).start();
Source

pub fn add_resource<T: Resource + 'static>(self, resource: T) -> Self

Adds a resource to the world. There can only be once instance of each resource. If an existing resource exists, it will not be replaced.

use starry_ecs::resources::Resource;
use starry_ecs::World;

#[derive(Clone, Debug)]
pub struct TestResource { x: i32 }
impl Resource for TestResource {}
 
World::new().add_resource(TestResource { x: 0 });
Source

pub fn try_get_resource<T: Resource + 'static>( &self, ) -> Result<ResourceReadGuard<'_, T>, StarryError>

Gets a resource based on a given type T and returns a Read guard

§Errors

Will return a StarryError::ResourceNotFound if the resource is not found

§Example
use starry_ecs::World;
use starry_ecs::resources::Resource;
use starry_ecs::systems::DefaultOrdering;

#[derive(Clone, Debug)]
struct TestResource { x: i32 }
impl Resource for TestResource {}

fn test_system(world: &World) {
    let _resource = world.try_get_resource::<TestResource>().unwrap();
}

World::new().add_system(DefaultOrdering::Run, test_system).add_resource(TestResource { x: 0 });
Source

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

Same as try_get_resource but unwraps the value

Source

pub fn try_get_resource_mut<T: Resource + 'static>( &self, ) -> Result<ResourceWriteGuard<'_, T>, StarryError>

Gets a resource based on a given type T and returns a Write guard

§Errors

Will return a StarryError::ResourceNotFound if the resource is not found

§Example
use starry_ecs::World;
use starry_ecs::resources::Resource;
use starry_ecs::systems::DefaultOrdering;

#[derive(Clone, Debug)]
struct TestResource { x: i32 }
impl Resource for TestResource {}

fn test_system(world: &World) {
    let _resource = world.try_get_resource_mut::<TestResource>().unwrap();
}

World::new().add_system(DefaultOrdering::Run, test_system).add_resource(TestResource { x: 0 });
Source

pub fn get_resource_mut<T: Resource + 'static>( &self, ) -> ResourceWriteGuard<'_, T>

Same as try_get_resource_mut but unwraps the value

Source

pub fn list_resources(&self)

Prints out a list of all resources

Source

pub fn try_get_components<T: Component + 'static>( &self, ) -> Result<Vec<ComponentReadGuard<'_, T>>, StarryError>

Gets components based on a given type T and returns a Read guard

§Errors

Will return a StarryError::ComponentNotFound if components are not found

§Example
use starry_ecs::World;
use starry_ecs::component::Component;
use starry_ecs::systems::DefaultOrdering;

#[derive(Clone, Debug)]
struct TestComponent { x: i32 }
impl Component for TestComponent {}

fn test_system(world: &World) {
    let _resource = world.try_get_components::<TestResource>().unwrap();
}

World::new().add_system(DefaultOrdering::Run, test_system).add_component(TestResource { x: 0 }).add_component(TestResource { x: 1 });
Source

pub fn get_components<T: Component + 'static>( &self, ) -> Vec<ComponentReadGuard<'_, T>>

Same as try_get_components but unwraps the value

Source

pub fn try_get_components_mut<T: Component + 'static>( &self, ) -> Result<Vec<ComponentWriteGuard<'_, T>>, StarryError>

Gets components based on a given type T and returns a Write guard

§Errors

Will return a StarryError::ComponentNotFound if components are not found

§Example
use starry_ecs::World;
use starry_ecs::component::Component;
use starry_ecs::systems::DefaultOrdering;

#[derive(Clone, Debug)]
struct TestComponent { x: i32 }
impl Component for TestComponent {}

fn test_system(world: &World) {
    let _resource = world.try_get_components_mut::<TestResource>().unwrap();
}

World::new().add_system(DefaultOrdering::Run, test_system).add_component(TestResource { x: 0 }).add_component(TestResource { x: 1 });
Source

pub fn get_components_mut<T: Component + 'static>( &self, ) -> Vec<ComponentWriteGuard<'_, T>>

Same as try_get_components_mut but unwraps the value

Source

pub fn single_step(self) -> Self

Runs a single step of the systems

use starry_ecs::World;

World::new().single_step();
Source

pub fn start(self) -> Self

Runs startup systems

use starry_ecs::World;

World::new().start();
Source

pub fn run(self)

Runs systems

use starry_ecs::World;

World::new().run();

Trait Implementations§

Source§

impl Clone for World

Source§

fn clone(&self) -> World

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Send for World

Source§

impl Sync for World

Auto Trait Implementations§

§

impl Freeze for World

§

impl !RefUnwindSafe 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

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

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

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

Initializes a with the given initializer. Read more
Source§

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

Dereferences the given pointer. Read more
Source§

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

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

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

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.