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
impl World
Sourcepub fn add_component<T: Component + 'static>(self, component: T) -> Self
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 });Sourcepub fn add_system<S: SystemOrdering + Copy>(
self,
system_ordering: S,
system: SystemType,
) -> Self
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();Sourcepub fn add_startup_system(self, system: SystemType) -> Self
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();Sourcepub fn add_resource<T: Resource + 'static>(self, resource: T) -> Self
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 });Sourcepub fn try_get_resource<T: Resource + 'static>(
&self,
) -> Result<ResourceReadGuard<'_, T>, StarryError>
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 });Sourcepub fn get_resource<T: Resource + 'static>(&self) -> ResourceReadGuard<'_, T>
pub fn get_resource<T: Resource + 'static>(&self) -> ResourceReadGuard<'_, T>
Same as try_get_resource but unwraps the value
Sourcepub fn try_get_resource_mut<T: Resource + 'static>(
&self,
) -> Result<ResourceWriteGuard<'_, T>, StarryError>
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 });Sourcepub fn get_resource_mut<T: Resource + 'static>(
&self,
) -> ResourceWriteGuard<'_, T>
pub fn get_resource_mut<T: Resource + 'static>( &self, ) -> ResourceWriteGuard<'_, T>
Same as try_get_resource_mut but unwraps the value
Sourcepub fn list_resources(&self)
pub fn list_resources(&self)
Prints out a list of all resources
Sourcepub fn try_get_components<T: Component + 'static>(
&self,
) -> Result<Vec<ComponentReadGuard<'_, T>>, StarryError>
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 });Sourcepub fn get_components<T: Component + 'static>(
&self,
) -> Vec<ComponentReadGuard<'_, T>> ⓘ
pub fn get_components<T: Component + 'static>( &self, ) -> Vec<ComponentReadGuard<'_, T>> ⓘ
Same as try_get_components but unwraps the value
Sourcepub fn try_get_components_mut<T: Component + 'static>(
&self,
) -> Result<Vec<ComponentWriteGuard<'_, T>>, StarryError>
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 });Sourcepub fn get_components_mut<T: Component + 'static>(
&self,
) -> Vec<ComponentWriteGuard<'_, T>> ⓘ
pub fn get_components_mut<T: Component + 'static>( &self, ) -> Vec<ComponentWriteGuard<'_, T>> ⓘ
Same as try_get_components_mut but unwraps the value
Sourcepub fn single_step(self) -> Self
pub fn single_step(self) -> Self
Runs a single step of the systems
use starry_ecs::World;
World::new().single_step();Trait Implementations§
Auto Trait Implementations§
impl Freeze for World
impl !RefUnwindSafe for World
impl Unpin for World
impl !UnwindSafe for World
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
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