Skip to content

Commit

Permalink
Add example with usual system (#142)
Browse files Browse the repository at this point in the history
* Add example with usual system

* Only build with parallel feature

* Reformatting and add actual entities

* Revert formatting in other examples
  • Loading branch information
torkleyy authored and kvark committed May 11, 2017
1 parent 78b1a10 commit 028c829
Showing 1 changed file with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions examples/sys.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
extern crate specs;

use specs::{Component, VecStorage, World};

struct Vel(f32);
struct Pos(f32);

impl Component for Vel {
type Storage = VecStorage<Vel>;
}

impl Component for Pos {
type Storage = VecStorage<Pos>;
}

#[cfg(not(feature="parallel"))]
fn main() {}

#[cfg(feature="parallel")]
fn main() {
use specs::{RunArg, Planner, System};

let mut planner = {
let mut world = World::new();
world.register::<Pos>();
world.register::<Vel>();

world
.create_now()
.with(Vel(2.0))
.with(Pos(0.0))
.build();
world
.create_now()
.with(Vel(4.0))
.with(Pos(1.6))
.build();
world
.create_now()
.with(Vel(1.5))
.with(Pos(5.4))
.build();

Planner::new(world)
};

struct SysA;

impl System<()> for SysA {
fn run(&mut self, arg: RunArg, _: ()) {
use specs::{Gate, Join};

let (pos, vel) = arg.fetch(|w| (w.write::<Pos>(), w.read::<Vel>()));

for (pos, vel) in (&mut pos.pass(), &vel.pass()).join() {
pos.0 += vel.0;
}
}
}

planner.add_system(SysA, "a", 1);
planner.dispatch(());
}

0 comments on commit 028c829

Please sign in to comment.