Crate shipyard[][src]

Expand description

Getting started

use shipyard::*;

struct Health(f32);
struct Position { x: f32, y: f32 };

struct InAcid;
impl<'a> System<'a> for InAcid {
    type Data = (&'a Position, &'a mut Health);
    fn run(&self, (pos, mut health): <Self::Data as SystemData>::View) {
        for (pos, health) in (&pos, &mut health).iter() {
            if is_in_acid(pos) {
                health.0 -= 1.0;
            }
        }
    }
}

fn is_in_acid(pos: &Position) -> bool {
    // well... it's wet season
      
    true
}

let world = World::default();

world.new_entity((Position { x: 0.0, y: 0.0 }, Health(1000.0)));

world.add_workload("In acid", InAcid);
world.run_default();

Let’s make some pigs!

use shipyard::*;

struct Health(f32);
struct Fat(f32);

struct Reproduction;
impl<'a> System<'a> for Reproduction {
    type Data = (&'a mut Fat, &'a mut Health, Entities);
    fn run(&self, (mut fat, mut health, mut entities): <Self::Data as SystemData>::View) {
        let count = (&health, &fat).iter().filter(|(health, fat)| health.0 > 40.0 && fat.0 > 20.0).count();
        (0..count).for_each(|_| {
            entities.add((&mut health, &mut fat), (Health(100.0), Fat(0.0)));
        });
    }
}

struct Meal;
impl<'a> System<'a> for Meal {
    type Data = &'a mut Fat;
    fn run(&self, mut fat: <Self::Data as SystemData>::View) {
        for slice in fat.iter().into_chunk(8) {
            for fat in slice {
                fat.0 += 3.0;
            }
        }
    }
}

struct Age;
impl<'a> System<'a> for Age {
    type Data = (&'a mut Health, ThreadPool);
    fn run(&self, (mut health, thread_pool): <Self::Data as SystemData>::View) {
        use rayon::prelude::ParallelIterator;

        thread_pool.install(|| {
            health.par_iter().for_each(|health| {
                health.0 -= 4.0;
            });
        });
    }
}

let world = World::new::<(Health, Fat)>();

world.run::<(Entities, &mut Health, &mut Fat), _>(|(mut entities, mut health, mut fat)| {
    (0..100).for_each(|_| {
        entities.add((&mut health, &mut fat), (Health(100.0), Fat(0.0)));
    })
});

world.add_workload("Life", (Meal, Age));
world.add_workload("Reproduction", Reproduction);

for day in 0..100 {
    if day % 6 == 0 {
        world.run_workload("Reproduction");
    }
    world.run_default();
}

// we've got some new pigs
assert_eq!(world.get_storage::<&Health>().len(), 900);

Re-exports

pub use iterators::IntoIter;

Modules

Structs

Contains all components present in the World.

Entities holds the Keys to all entities: living, removed and dead.

View into the entities, this allows to add and remove entities.

Used to filter out components. Get and iterators will skip entities that have this component.

Type used to borrow the rayon::ThreadPool inside World.

Holds all components and keeps track of entities and what they own.

Traits

Adds components to an existing entity without creating new storage.

Retrives components based on their type and entity key.

Allows to pack multiple storages.

Removes component from entities.

Trait to define systems.