-
Notifications
You must be signed in to change notification settings - Fork 220
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add example with usual system (#142)
* Add example with usual system * Only build with parallel feature * Reformatting and add actual entities * Revert formatting in other examples
- Loading branch information
Showing
1 changed file
with
63 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(()); | ||
} |