pub struct World<R>where
    R: Registry,
{ /* private fields */ }
Expand description

A container of entities.

A World can contain entities made of any combination of components contained in the Registry R. These entities are not stored in any defined order, and thier internal location is subject to change. Therefore, entities stored inside a World are uniquely identified using an entity::Identifier.

use brood::{
    entity,
    registry,
    World,
};

// Define components.
struct Foo(u32);
struct Bar(bool);

// Create a world.
let mut world = World::<registry!(Foo, Bar)>::new();

// Insert a new entity. The returned identifier uniquely identifies the entity.
let entity_identifier = world.insert(entity!(Foo(42), Bar(true)));

Note that a World can only contain entities made of components defined in the World’s registry. Attempting to insert entities containing components not in the registry will result in a panic.

Components of entities can be queried using the query() method. Systems can also be run over components of entities using the various run methods.

Implementations

Creates an empty World.

Often, calls to new() are accompanied with a Registry to tell the compiler what components the World can contain.

Example
use brood::{
    registry,
    World,
};

struct Foo(u32);
struct Bar(bool);

type Registry = registry!(Foo, Bar);

let world = World::<Registry>::new();

Insert an entity, returning an entity::Identifier.

Example
use brood::{
    entity,
    registry,
    World,
};

struct Foo(u32);
struct Bar(bool);

type Registry = registry!(Foo, Bar);

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

let entity_identifier = world.insert(entity!(Foo(42), Bar(false)));

Insert multiple entities made from the same components, returning a Vec of entity::Identifiers.

Example
use brood::{
    entities,
    registry,
    World,
};

struct Foo(u32);
struct Bar(bool);

type Registry = registry!(Foo, Bar);

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

let entity_identiifers = world.extend(entities![(Foo(1), Bar(false)), (Foo(2), Bar(true))]);

Query for components contained within the World using the given Views V and Filter F, returning an Iterator over all components of entities matching the query.

Note that the order of the entities returned by a query is not specified.

Example
use brood::{
    entity,
    query::{
        filter,
        result,
        views,
    },
    registry,
    Query,
    World,
};

struct Foo(u32);
struct Bar(bool);
struct Baz(u32);

type Registry = registry!(Foo, Bar, Baz);

let mut world = World::<Registry>::new();
let inserted_entity_identifier = world.insert(entity!(Foo(42), Bar(true), Baz(100)));

// Note that the views provide implicit filters.
for result!(foo, baz, entity_identifier) in world.query(Query::<
    views!(&mut Foo, &Baz, entity::Identifier),
    filter::Has<Bar>,
>::new())
{
    // Allows immutable or mutable access to queried components.
    foo.0 = baz.0;
    // Also allows access to entity identifiers.
    assert_eq!(entity_identifier, inserted_entity_identifier);
}

For more information about Views and Filter, see the query module documentaion.

Available on crate feature rayon only.

Query for components contained within the World using the given ParViews V and Filter F, returning a ParallelIterator over all components of entities matching the query.

The difference between this method and query() is that this method allow results to be operated on in parallel rather than sequentially.

Example
use brood::{
    entity,
    query::{
        filter,
        result,
        views,
    },
    registry,
    Query,
    World,
};
use rayon::iter::ParallelIterator;

struct Foo(u32);
struct Bar(bool);
struct Baz(u32);

type Registry = registry!(Foo, Bar, Baz);

let mut world = World::<Registry>::new();
let inserted_entity_identifier = world.insert(entity!(Foo(42), Bar(true), Baz(100)));

// Note that the views provide implicit filters.
world
    .par_query(Query::<
        views!(&mut Foo, &Baz, entity::Identifier),
        filter::Has<Bar>,
    >::new())
    .for_each(|result!(foo, baz, entity_identifier)| {
        // Allows immutable or mutable access to queried components.
        foo.0 = baz.0;
        // Also allows access to entity identifiers.
        assert_eq!(entity_identifier, inserted_entity_identifier);
    });

For more information about ParViews and Filter, see the query module documentaion.

Run a System over the entities in this World.

Example
use brood::{
    entity,
    query::{
        filter,
        filter::Filter,
        result,
        views,
    },
    registry,
    registry::ContainsQuery,
    system::System,
    World,
};

// Define components.
struct Foo(usize);
struct Bar(usize);

type MyRegistry = registry!(Foo, Bar);

// Define system.
struct MySystem;

impl<'a> System<'a> for MySystem {
    type Views = views!(&'a mut Foo, &'a Bar);
    type Filter = filter::None;

    fn run<R, FI, VI, P, I, Q>(
        &mut self,
        query_results: result::Iter<'a, R, Self::Filter, FI, Self::Views, VI, P, I, Q>,
    ) where
        R: ContainsQuery<'a, Self::Filter, FI, Self::Views, VI, P, I, Q> + 'a,
    {
        for result!(foo, bar) in query_results {
            // Increment `Foo` by `Bar`.
            foo.0 += bar.0;
        }
    }
}

let mut world = World::<MyRegistry>::new();
world.insert(entity!(Foo(42), Bar(100)));

world.run_system(&mut MySystem);
Available on crate feature rayon only.

Run a ParSystem over the entities in this World.

Example
use brood::{
    entity,
    query::{
        filter,
        filter::Filter,
        result,
        views,
    },
    registry,
    registry::ContainsParQuery,
    system::ParSystem,
    World,
};
use rayon::iter::ParallelIterator;

// Define components.
struct Foo(usize);
struct Bar(usize);

type MyRegistry = registry!(Foo, Bar);

// Define system.
struct MySystem;

impl<'a> ParSystem<'a> for MySystem {
    type Views = views!(&'a mut Foo, &'a Bar);
    type Filter = filter::None;

    fn run<R, FI, VI, P, I, Q>(
        &mut self,
        query_results: result::ParIter<'a, R, Self::Filter, FI, Self::Views, VI, P, I, Q>,
    ) where
        R: ContainsParQuery<'a, Self::Filter, FI, Self::Views, VI, P, I, Q> + 'a,
    {
        query_results.for_each(|result!(foo, bar)| foo.0 += bar.0);
    }
}

let mut world = World::<MyRegistry>::new();
world.insert(entity!(Foo(42), Bar(100)));

world.run_par_system(&mut MySystem);
Available on crate feature rayon only.

Run a Schedule over the entities in this World.

Example
use brood::{
    entity,
    query::{
        filter,
        filter::Filter,
        result,
        views,
    },
    registry,
    registry::ContainsQuery,
    system::{
        Schedule,
        System,
    },
    World,
};

// Define components.
struct Foo(usize);
struct Bar(usize);

type MyRegistry = registry!(Foo, Bar);

// Define systems.
struct SystemA;
struct SystemB;

impl<'a> System<'a> for SystemA {
    type Views = views!(&'a mut Foo);
    type Filter = filter::None;

    fn run<R, FI, VI, P, I, Q>(
        &mut self,
        query_results: result::Iter<'a, R, Self::Filter, FI, Self::Views, VI, P, I, Q>,
    ) where
        R: ContainsQuery<'a, Self::Filter, FI, Self::Views, VI, P, I, Q> + 'a,
    {
        for result!(foo) in query_results {
            foo.0 += 1;
        }
    }
}

impl<'a> System<'a> for SystemB {
    type Views = views!(&'a mut Bar);
    type Filter = filter::None;

    fn run<R, FI, VI, P, I, Q>(
        &mut self,
        query_results: result::Iter<'a, R, Self::Filter, FI, Self::Views, VI, P, I, Q>,
    ) where
        R: ContainsQuery<'a, Self::Filter, FI, Self::Views, VI, P, I, Q> + 'a,
    {
        for result!(bar) in query_results {
            bar.0 += 1;
        }
    }
}

// Define schedule.
let mut schedule = Schedule::builder().system(SystemA).system(SystemB).build();

let mut world = World::<MyRegistry>::new();
world.insert(entity!(Foo(42), Bar(100)));

world.run_schedule(&mut schedule);

Returns true if the world contains an entity identified by entity_identifier.

Example
use brood::{
    entity,
    registry,
    World,
};

struct Foo(usize);
struct Bar(bool);

type Registry = registry!(Foo, Bar);

let mut world = World::<Registry>::new();
let entity_identifier = world.insert(entity!(Foo(42), Bar(true)));

assert!(world.contains(entity_identifier));
world.remove(entity_identifier);
assert!(!world.contains(entity_identifier));

Gets an Entry for the entity associated with an entity::Identifier for component-level manipulation.

If no such entity exists, None is returned.

Example
use brood::{
    entity,
    registry,
    World,
};

struct Foo(u32);
struct Bar(bool);

type Registry = registry!(Foo, Bar);

let mut world = World::<Registry>::new();
let entity_identifier = world.insert(entity!(Foo(42), Bar(true)));

let mut entry = world.entry(entity_identifier).unwrap();
// Remove the `Bar` component.
entry.remove::<Bar, _>();

Remove the entity associated with an entity::Identifier.

If the entity has already been removed, this method will do nothing.

Example
use brood::{
    entity,
    registry,
    World,
};

struct Foo(u32);
struct Bar(bool);

type Registry = registry!(Foo, Bar);

let mut world = World::<Registry>::new();
let entity_identifier = world.insert(entity!(Foo(42), Bar(true)));

world.remove(entity_identifier);

Removes all entities.

Keeps the allocated memory for reuse.

Example
use brood::{
    entity,
    registry,
    World,
};

struct Foo(usize);
struct Bar(bool);

type Registry = registry!(Foo, Bar);

let mut world = World::<Registry>::new();
world.insert(entity!(Foo(42), Bar(true)));

world.clear();

Returns the number of entities in the world.

Example
use brood::{entities, registry, World};

#[derive(Clone)]
struct Foo(usize);
#[derive(Clone)]
struct Bar(bool);

type Registry = registry!(Foo, Bar);

let mut world = World::<Registry>::new();
world.extend(entities!((Foo(42), Bar(false)); 100));

assert_eq!(world.len(), 100);

Returns true if the world contains no entities.

Example
use brood::{
    entity,
    registry,
    World,
};

struct Foo(usize);
struct Bar(bool);

type Registry = registry!(Foo, Bar);

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

assert!(world.is_empty());

world.insert(entity!(Foo(42), Bar(false)));

assert!(!world.is_empty());

Shrinks the allocated capacity of the internal storage as much as possible.

Example
use brood::{entities, registry, World};

#[derive(Clone)]
struct Foo(usize);
#[derive(Clone)]
struct Bar(bool);

type Registry = registry!(Foo, Bar);

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

world.extend(entities!((Foo(42), Bar(false)); 10));
world.clear();
world.extend(entities!((Foo(42), Bar(false)); 3));

// This will reduce the current allocation.
world.shrink_to_fit();

Trait Implementations

Formats the value using the given formatter. Read more
Returns the “default value” for a type. Read more
Deserialize this value from the given Serde deserializer. Read more
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more
Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The alignment of pointer.
The type for initializers.
Initializes a with the given initializer. Read more
Dereferences the given pointer. Read more
Mutably dereferences the given pointer. Read more
Drops the object pointed to by the given pointer. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.