Created
March 30, 2018 22:39
-
-
Save pakoito/6480b419440eb3fa007211b3e0e394b0 to your computer and use it in GitHub Desktop.
Specs example (https://github.com/slide-rs/specs)
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
extern crate specs; | |
use specs::Component; | |
use specs::VecStorage; | |
use specs::System; | |
use specs::WriteStorage; | |
use specs::ReadStorage; | |
use specs::World; | |
use specs::DispatcherBuilder; | |
use specs::RunNow; | |
use specs::Fetch; | |
use specs::Entities; | |
use specs::NullStorage; | |
#[derive(Debug)] | |
struct Vel(f32); | |
impl Component for Vel { | |
type Storage = VecStorage<Self>; | |
} | |
#[derive(Debug)] | |
struct Pos(f32); | |
impl Component for Pos { | |
type Storage = VecStorage<Self>; | |
} | |
#[derive(Debug, Default)] | |
struct Hero; | |
impl Component for Hero { | |
type Storage = NullStorage<Self>; | |
} | |
struct SysA; | |
impl<'a> System<'a> for SysA { | |
type SystemData = (Fetch<'a, DeltaTime>, Entities<'a>, WriteStorage<'a, Pos>, ReadStorage<'a, Vel>, ReadStorage<'a, Hero>); | |
fn run(&mut self, (delta, entities, mut pos, vel, tag): Self::SystemData) { | |
use specs::Join; | |
println!("{:?}", *delta); | |
for (entity, pos, vel, _) in (&*entities, &mut pos, &vel, !&tag).join() { | |
match tag.get(entity) { | |
Some(x) => panic!("{:?}", x), | |
None => println!("None") | |
} | |
pos.0 += vel.0; | |
println!("{:?}", pos); | |
println!("{:?}", vel); | |
} | |
} | |
} | |
#[derive(Debug)] | |
struct DeltaTime(i32); | |
fn main() { | |
let mut world = World::new(); | |
world.register::<Pos>(); | |
world.register::<Vel>(); | |
world.register::<Hero>(); | |
world.add_resource(DeltaTime(10)); | |
world.write_resource::<DeltaTime>().0 = 100; | |
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)).with(Hero).build(); | |
world.create_entity().with(Pos(2.0)).build(); | |
let sys = SysA; | |
let mut dispatcher = DispatcherBuilder::new().add(sys, "sys_a", &[]).build(); | |
dispatcher.dispatch(&world.res); | |
//sys.run_now(&world.res); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment