Skip to content

Instantly share code, notes, and snippets.

@torkleyy
Created May 20, 2017 20:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save torkleyy/f1f9e4dd14b27e882b3aae9d1dd7766f to your computer and use it in GitHub Desktop.
Save torkleyy/f1f9e4dd14b27e882b3aae9d1dd7766f to your computer and use it in GitHub Desktop.
This is how the new specs API will look like
extern crate shred;
#[macro_use]
extern crate shred_derive;
extern crate specs;
use shred::{DispatcherBuilder, System};
use specs::{ReadStorage, WriteStorage, World};
use specs::entity::Component;
use specs::storages::VecStorage;
#[derive(Debug)]
struct Vel(f32);
#[derive(Debug)]
struct Pos(f32);
impl Component for Vel {
type Storage = VecStorage<Vel>;
}
impl Component for Pos {
type Storage = VecStorage<Pos>;
}
#[derive(SystemData)]
struct Data<'a> {
vel: ReadStorage<'a, Vel>,
pos: WriteStorage<'a, Pos>,
}
struct SysA;
impl<'a, C> System<'a, C> for SysA {
type SystemData = Data<'a>;
fn work(&mut self, mut data: Data, _: C) {
use specs::Join;
for (pos, vel) in (&mut data.pos, &data.vel).join() {
pos.0 += vel.0;
}
}
}
fn main() {
let mut world = World::new();
world.register::<Pos>();
world.register::<Vel>();
world.create_entity().with(Vel(2.0)).with(Pos(0.0)).build();
world.create_entity().with(Vel(4.0)).with(Pos(1.6)).build();
world.create_entity().with(Vel(1.5)).with(Pos(5.4)).build();
let mut dispatcher = DispatcherBuilder::new()
.add(SysA, "sys_a", &[])
.finish();
dispatcher.dispatch(&mut world.res, ());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment