World

Struct World 

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

A container for managing entities and their components.

§Examples

use checs::{ComponentVec, IntoQuery, LendingIterator, Storage, World};

#[derive(Default, Storage)]
struct Storage {
    strs: ComponentVec<&'static str>,
    ints: ComponentVec<i32>,
}

let mut world = World::<Storage>::new();

let e0 = world.spawn_with(("ninety-nine", 99));
let e1 = world.spawn_with(("zero",));
let e2 = world.spawn_with(("forty-two", 42));
let e3 = world.spawn_with((1,));
let e4 = world.spawn_with(("seventeen", 17));

let storage = world.storage_mut();
let mut query = (&storage.strs, &mut storage.ints).into_query();

assert_eq!(query.next(), Some((e0, (&"ninety-nine", &mut 99))));
assert_eq!(query.next(), Some((e2, (&"forty-two", &mut 42))));
assert_eq!(query.next(), Some((e4, (&"seventeen", &mut 17))));
assert_eq!(query.next(), None);

Implementations§

Source§

impl<S> World<S>

Source

pub fn new() -> Self
where S: Default,

Constructs a new World.

Examples found in repository?
examples/basic.rs (line 23)
22fn main() {
23    let mut world = world::World::<Storage>::new();
24
25    // Create entities with initial components.
26    // Either manually...
27    let player = world.spawn();
28    world.insert(player, Position { x: 0, y: 0 });
29    world.insert(player, Visible);
30    world.insert(player, Health(80));
31
32    // ...or by using the `spawn` macro...
33    let _obstacle = checs::spawn!(world, Position { x: 1, y: 1 }, Visible);
34    let _trap = checs::spawn!(world, Position { x: 2, y: 1 });
35
36    // ...or by using `spawn_with`.
37    let _enemy = world.spawn_with((Position { x: 1, y: 4 }, Visible, Health(100)));
38
39    let storage = world.storage_mut();
40
41    // Create a query over all entities that have `Position`, `Health`, and
42    // `Visible` components.
43    let query = (&storage.positions, &mut storage.healths, &storage.visibles).into_query();
44
45    query.for_each(|(_, (_, h, _))| {
46        h.0 += 10;
47    });
48
49    println!();
50
51    let query = (&storage.positions, &storage.healths, &storage.visibles).into_query();
52
53    query.for_each(|(e, (p, h, v))| {
54        println!("{e:?} is {v:?} at ({}, {}) with {} HP", p.x, p.y, h.0);
55    });
56}
Source

pub fn storage(&self) -> &S

Returns a reference to the component storage.

Source

pub fn storage_mut(&mut self) -> &mut S

Returns a mutable reference to the component storage.

Examples found in repository?
examples/basic.rs (line 39)
22fn main() {
23    let mut world = world::World::<Storage>::new();
24
25    // Create entities with initial components.
26    // Either manually...
27    let player = world.spawn();
28    world.insert(player, Position { x: 0, y: 0 });
29    world.insert(player, Visible);
30    world.insert(player, Health(80));
31
32    // ...or by using the `spawn` macro...
33    let _obstacle = checs::spawn!(world, Position { x: 1, y: 1 }, Visible);
34    let _trap = checs::spawn!(world, Position { x: 2, y: 1 });
35
36    // ...or by using `spawn_with`.
37    let _enemy = world.spawn_with((Position { x: 1, y: 4 }, Visible, Health(100)));
38
39    let storage = world.storage_mut();
40
41    // Create a query over all entities that have `Position`, `Health`, and
42    // `Visible` components.
43    let query = (&storage.positions, &mut storage.healths, &storage.visibles).into_query();
44
45    query.for_each(|(_, (_, h, _))| {
46        h.0 += 10;
47    });
48
49    println!();
50
51    let query = (&storage.positions, &storage.healths, &storage.visibles).into_query();
52
53    query.for_each(|(e, (p, h, v))| {
54        println!("{e:?} is {v:?} at ({}, {}) with {} HP", p.x, p.y, h.0);
55    });
56}
Source

pub fn spawn(&mut self) -> Entity

Creates a new entity.

Examples found in repository?
examples/basic.rs (line 27)
22fn main() {
23    let mut world = world::World::<Storage>::new();
24
25    // Create entities with initial components.
26    // Either manually...
27    let player = world.spawn();
28    world.insert(player, Position { x: 0, y: 0 });
29    world.insert(player, Visible);
30    world.insert(player, Health(80));
31
32    // ...or by using the `spawn` macro...
33    let _obstacle = checs::spawn!(world, Position { x: 1, y: 1 }, Visible);
34    let _trap = checs::spawn!(world, Position { x: 2, y: 1 });
35
36    // ...or by using `spawn_with`.
37    let _enemy = world.spawn_with((Position { x: 1, y: 4 }, Visible, Health(100)));
38
39    let storage = world.storage_mut();
40
41    // Create a query over all entities that have `Position`, `Health`, and
42    // `Visible` components.
43    let query = (&storage.positions, &mut storage.healths, &storage.visibles).into_query();
44
45    query.for_each(|(_, (_, h, _))| {
46        h.0 += 10;
47    });
48
49    println!();
50
51    let query = (&storage.positions, &storage.healths, &storage.visibles).into_query();
52
53    query.for_each(|(e, (p, h, v))| {
54        println!("{e:?} is {v:?} at ({}, {}) with {} HP", p.x, p.y, h.0);
55    });
56}
Source

pub fn spawn_with<T>(&mut self, value: T) -> Entity
where Self: InsertMany<T>,

Creates a new entity with initial components.

Examples found in repository?
examples/basic.rs (line 37)
22fn main() {
23    let mut world = world::World::<Storage>::new();
24
25    // Create entities with initial components.
26    // Either manually...
27    let player = world.spawn();
28    world.insert(player, Position { x: 0, y: 0 });
29    world.insert(player, Visible);
30    world.insert(player, Health(80));
31
32    // ...or by using the `spawn` macro...
33    let _obstacle = checs::spawn!(world, Position { x: 1, y: 1 }, Visible);
34    let _trap = checs::spawn!(world, Position { x: 2, y: 1 });
35
36    // ...or by using `spawn_with`.
37    let _enemy = world.spawn_with((Position { x: 1, y: 4 }, Visible, Health(100)));
38
39    let storage = world.storage_mut();
40
41    // Create a query over all entities that have `Position`, `Health`, and
42    // `Visible` components.
43    let query = (&storage.positions, &mut storage.healths, &storage.visibles).into_query();
44
45    query.for_each(|(_, (_, h, _))| {
46        h.0 += 10;
47    });
48
49    println!();
50
51    let query = (&storage.positions, &storage.healths, &storage.visibles).into_query();
52
53    query.for_each(|(e, (p, h, v))| {
54        println!("{e:?} is {v:?} at ({}, {}) with {} HP", p.x, p.y, h.0);
55    });
56}
Source

pub fn despawn(&mut self, entity: Entity) -> Result<(), Error>
where S: RemoveAll,

Removes an entity and its components.

§Errors

The method internally calls entity::Allocator::free, so it might return any of its errors.

Source

pub fn despawn_all<T>(&mut self) -> Result<(), Error>
where S: RemoveAll + AsRef<ComponentVec<T>>,

Removes all entities that have the specified component T.

Also removes all of their other components.

§Errors

The method internally calls entity::Allocator::free, so it might return any of its errors.

Source

pub fn despawn_entities<'a, I>(&'a mut self, entities: I) -> Result<(), Error>
where S: RemoveAll, I: IntoIterator<Item = &'a Entity>,

Removes all entities in the iterator from the world.

Also removes all of their components.

§Errors

The method internally calls entity::Allocator::free, so it might return any of its errors.

Source

pub fn get<T>(&self, entity: Entity) -> Option<&T>
where S: AsRef<ComponentVec<T>>,

Returns a reference to the entity’s component, or None if the entity does not have that component.

Source

pub fn get_mut<T>(&mut self, entity: Entity) -> Option<&mut T>
where S: AsMut<ComponentVec<T>>,

Returns a mutable reference to an entity’s component, or None if the entity does not have that component.

Source

pub fn insert<T>(&mut self, entity: Entity, value: T) -> Option<T>
where S: AsMut<ComponentVec<T>>,

Adds a component to the entity.

If the entity already had that component its value is updated and the old value is returned.

Examples found in repository?
examples/basic.rs (line 28)
22fn main() {
23    let mut world = world::World::<Storage>::new();
24
25    // Create entities with initial components.
26    // Either manually...
27    let player = world.spawn();
28    world.insert(player, Position { x: 0, y: 0 });
29    world.insert(player, Visible);
30    world.insert(player, Health(80));
31
32    // ...or by using the `spawn` macro...
33    let _obstacle = checs::spawn!(world, Position { x: 1, y: 1 }, Visible);
34    let _trap = checs::spawn!(world, Position { x: 2, y: 1 });
35
36    // ...or by using `spawn_with`.
37    let _enemy = world.spawn_with((Position { x: 1, y: 4 }, Visible, Health(100)));
38
39    let storage = world.storage_mut();
40
41    // Create a query over all entities that have `Position`, `Health`, and
42    // `Visible` components.
43    let query = (&storage.positions, &mut storage.healths, &storage.visibles).into_query();
44
45    query.for_each(|(_, (_, h, _))| {
46        h.0 += 10;
47    });
48
49    println!();
50
51    let query = (&storage.positions, &storage.healths, &storage.visibles).into_query();
52
53    query.for_each(|(e, (p, h, v))| {
54        println!("{e:?} is {v:?} at ({}, {}) with {} HP", p.x, p.y, h.0);
55    });
56}
Source

pub fn remove<T>(&mut self, entity: Entity) -> Option<T>
where S: AsMut<ComponentVec<T>>,

Removes a component from the entity and returns the component.

Trait Implementations§

Source§

impl<S> Default for World<S>
where S: Default,

Source§

fn default() -> World<S>

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

impl<S> InsertMany<()> for World<S>

Source§

type Output = ()

The type of the (possibly) updated components.
Source§

fn insert_many(&mut self, _entity: Entity, _value: ()) -> Self::Output

Adds components to the entity. Read more
Source§

impl<S, A, B, C, D, E, F, G> InsertMany<(A, B, C, D, E, F, G)> for World<S>

Source§

type Output = (Option<A>, Option<B>, Option<C>, Option<D>, Option<E>, Option<F>, Option<G>)

The type of the (possibly) updated components.
Source§

fn insert_many( &mut self, entity: Entity, value: (A, B, C, D, E, F, G), ) -> Self::Output

Adds components to the entity. Read more
Source§

impl<S, B, C, D, E, F, G> InsertMany<(B, C, D, E, F, G)> for World<S>

Source§

type Output = (Option<B>, Option<C>, Option<D>, Option<E>, Option<F>, Option<G>)

The type of the (possibly) updated components.
Source§

fn insert_many( &mut self, entity: Entity, value: (B, C, D, E, F, G), ) -> Self::Output

Adds components to the entity. Read more
Source§

impl<S, C, D, E, F, G> InsertMany<(C, D, E, F, G)> for World<S>

Source§

type Output = (Option<C>, Option<D>, Option<E>, Option<F>, Option<G>)

The type of the (possibly) updated components.
Source§

fn insert_many( &mut self, entity: Entity, value: (C, D, E, F, G), ) -> Self::Output

Adds components to the entity. Read more
Source§

impl<S, D, E, F, G> InsertMany<(D, E, F, G)> for World<S>

Source§

type Output = (Option<D>, Option<E>, Option<F>, Option<G>)

The type of the (possibly) updated components.
Source§

fn insert_many(&mut self, entity: Entity, value: (D, E, F, G)) -> Self::Output

Adds components to the entity. Read more
Source§

impl<S, E, F, G> InsertMany<(E, F, G)> for World<S>

Source§

type Output = (Option<E>, Option<F>, Option<G>)

The type of the (possibly) updated components.
Source§

fn insert_many(&mut self, entity: Entity, value: (E, F, G)) -> Self::Output

Adds components to the entity. Read more
Source§

impl<S, F, G> InsertMany<(F, G)> for World<S>
where S: AsMut<ComponentVec<F>> + AsMut<ComponentVec<G>>,

Source§

type Output = (Option<F>, Option<G>)

The type of the (possibly) updated components.
Source§

fn insert_many(&mut self, entity: Entity, value: (F, G)) -> Self::Output

Adds components to the entity. Read more
Source§

impl<S, G> InsertMany<(G,)> for World<S>
where S: AsMut<ComponentVec<G>>,

Source§

type Output = (Option<G>,)

The type of the (possibly) updated components.
Source§

fn insert_many(&mut self, entity: Entity, value: (G,)) -> Self::Output

Adds components to the entity. Read more

Auto Trait Implementations§

§

impl<S> Freeze for World<S>
where S: Freeze,

§

impl<S> RefUnwindSafe for World<S>
where S: RefUnwindSafe,

§

impl<S> Send for World<S>
where S: Send,

§

impl<S> Sync for World<S>
where S: Sync,

§

impl<S> Unpin for World<S>
where S: Unpin,

§

impl<S> UnwindSafe for World<S>
where S: UnwindSafe,

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.